mirror of
https://github.com/ClassiCube/ClassiCube.git
synced 2025-09-18 12:05:14 -04:00
Add screen for loading maps in singleplayer.
This commit is contained in:
parent
85eef80e21
commit
448ec55e6e
@ -1,8 +1,6 @@
|
||||
using System;
|
||||
using System.Drawing;
|
||||
using OpenTK.Input;
|
||||
using System.IO;
|
||||
using ClassicalSharp.TexturePack;
|
||||
|
||||
namespace ClassicalSharp {
|
||||
|
||||
@ -64,7 +62,7 @@ namespace ClassicalSharp {
|
||||
|
||||
protected abstract void TextButtonClick( Game game, ButtonWidget widget );
|
||||
|
||||
void PageClick( bool forward ) {
|
||||
protected void PageClick( bool forward ) {
|
||||
currentIndex += forward ? 5 : -5;
|
||||
if( currentIndex >= files.Length )
|
||||
currentIndex -= 5;
|
||||
@ -76,6 +74,19 @@ namespace ClassicalSharp {
|
||||
}
|
||||
}
|
||||
|
||||
public override bool HandlesKeyDown( Key key ) {
|
||||
if( key == Key.Escape ) {
|
||||
game.SetNewScreen( new NormalScreen( game ) );
|
||||
} else if( key == Key.Left ) {
|
||||
PageClick( false );
|
||||
} else if( key == Key.Right ) {
|
||||
PageClick( true );
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
public override bool HandlesMouseMove( int mouseX, int mouseY ) {
|
||||
for( int i = 0; i < buttons.Length; i++ )
|
||||
buttons[i].Active = false;
|
||||
@ -121,45 +132,4 @@ namespace ClassicalSharp {
|
||||
graphicsApi.Texturing = false;
|
||||
}
|
||||
}
|
||||
|
||||
public sealed class TexturePackScreen : FilesScreen {
|
||||
|
||||
public TexturePackScreen( Game game ) : base( game ) {
|
||||
titleText = "Select a texture pack zip";
|
||||
string directory = Environment.CurrentDirectory;
|
||||
files = Directory.GetFiles( directory, "*.zip", SearchOption.AllDirectories );
|
||||
|
||||
for( int i = 0; i < files.Length; i++ ) {
|
||||
string absolutePath = files[i];
|
||||
files[i] = absolutePath.Substring( directory.Length + 1 );
|
||||
}
|
||||
}
|
||||
|
||||
public override bool HandlesKeyDown( Key key ) {
|
||||
if( key == Key.Escape ) {
|
||||
game.SetNewScreen( new NormalScreen( game ) );
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
public override void Init() {
|
||||
base.Init();
|
||||
buttons[buttons.Length - 1] =
|
||||
Make( 0, 5, "Back to menu", (g, w) => g.SetNewScreen( new PauseScreen( g ) ) );
|
||||
}
|
||||
|
||||
ButtonWidget Make( int x, int y, string text, Action<Game, ButtonWidget> onClick ) {
|
||||
return ButtonWidget.Create( game, x, y, 240, 35, text,
|
||||
Anchor.Centre, Anchor.BottomOrRight, titleFont, onClick );
|
||||
}
|
||||
|
||||
protected override void TextButtonClick( Game game, ButtonWidget widget ) {
|
||||
string path = widget.Text;
|
||||
if( File.Exists( path ) ) {
|
||||
TexturePackExtractor extractor = new TexturePackExtractor();
|
||||
extractor.Extract( path, game );
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
70
ClassicalSharp/2D/Screens/Menu/LoadLevelScreen.cs
Normal file
70
ClassicalSharp/2D/Screens/Menu/LoadLevelScreen.cs
Normal file
@ -0,0 +1,70 @@
|
||||
using System;
|
||||
using System.IO;
|
||||
|
||||
namespace ClassicalSharp {
|
||||
|
||||
public sealed class LoadLevelScreen : FilesScreen {
|
||||
|
||||
public LoadLevelScreen( Game game ) : base( game ) {
|
||||
titleText = "Select a level";
|
||||
string directory = Environment.CurrentDirectory;
|
||||
string[] cwFiles = Directory.GetFiles( directory, "*.cw", SearchOption.AllDirectories );
|
||||
string[] datFiles = Directory.GetFiles( directory, "*.dat", SearchOption.AllDirectories );
|
||||
files = new string[cwFiles.Length + datFiles.Length];
|
||||
Array.Copy( cwFiles, 0, files, 0, cwFiles.Length );
|
||||
Array.Copy( datFiles, 0, files, cwFiles.Length, datFiles.Length );
|
||||
|
||||
for( int i = 0; i < files.Length; i++ ) {
|
||||
string absolutePath = files[i];
|
||||
files[i] = absolutePath.Substring( directory.Length + 1 );
|
||||
}
|
||||
Array.Sort( files );
|
||||
}
|
||||
|
||||
public override void Init() {
|
||||
base.Init();
|
||||
buttons[buttons.Length - 1] =
|
||||
Make( 0, 5, "Back to menu", (g, w) => g.SetNewScreen( new PauseScreen( g ) ) );
|
||||
}
|
||||
|
||||
ButtonWidget Make( int x, int y, string text, Action<Game, ButtonWidget> onClick ) {
|
||||
return ButtonWidget.Create( game, x, y, 240, 35, text,
|
||||
Anchor.Centre, Anchor.BottomOrRight, titleFont, onClick );
|
||||
}
|
||||
|
||||
protected override void TextButtonClick( Game game, ButtonWidget widget ) {
|
||||
string path = widget.Text;
|
||||
if( File.Exists( path ) )
|
||||
LoadMap( path );
|
||||
}
|
||||
|
||||
void LoadMap( string path ) {
|
||||
IMapFile mapFile = null;
|
||||
if( path.EndsWith( ".dat" ) ) {
|
||||
mapFile = new MapDat();
|
||||
} else if( path.EndsWith( ".fcm" ) ) {
|
||||
mapFile = new MapFcm3();
|
||||
} else if( path.EndsWith( ".cw" ) ) {
|
||||
mapFile = new MapCw();
|
||||
}
|
||||
|
||||
try {
|
||||
using( FileStream fs = new FileStream( path, FileMode.Open, FileAccess.Read, FileShare.Read ) ) {
|
||||
int width, height, length;
|
||||
game.Map.Reset();
|
||||
|
||||
byte[] blocks = mapFile.Load( fs, game, out width, out height, out length );
|
||||
game.Map.UseRawMap( blocks, width, height, length );
|
||||
game.Events.RaiseOnNewMapLoaded();
|
||||
|
||||
LocalPlayer p = game.LocalPlayer;
|
||||
LocationUpdate update = LocationUpdate.MakePos( p.SpawnPoint, false );
|
||||
p.SetLocation( update, false );
|
||||
}
|
||||
} catch( Exception ex ) {
|
||||
Utils.LogError( "Error while trying to load map: {0}{1}", Environment.NewLine, ex );
|
||||
game.Chat.Add( "&e/client loadmap: Failed to load map \"" + path + "\"" );
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
@ -18,16 +18,28 @@ namespace ClassicalSharp {
|
||||
|
||||
public override void Init() {
|
||||
titleFont = new Font( "Arial", 16, FontStyle.Bold );
|
||||
if( game.Network.IsSinglePlayer ) {
|
||||
buttons = new ButtonWidget[] {
|
||||
Make( -140, -50, "Options", Anchor.Centre, (g, w) => g.SetNewScreen( new OptionsScreen( g ) ) ),
|
||||
Make( -140, 0, "Environment settings", Anchor.Centre, (g, w) => g.SetNewScreen( new EnvSettingsScreen( g ) ) ),
|
||||
Make( -140, 50, "Select texture pack", Anchor.Centre, (g, w) => g.SetNewScreen( new TexturePackScreen( g ) ) ),
|
||||
Make( 140, -50, "Save level", Anchor.Centre, (g, w) => g.SetNewScreen( new SaveLevelScreen( g ) ) ),
|
||||
Make( 140, 0, "Load level", Anchor.Centre, (g, w) => g.SetNewScreen( new LoadLevelScreen( g ) ) ),
|
||||
// TODO: singleplayer Make( 0, 50, "Load/Save/Gen level", Docking.Centre, (g, w) => g.SetNewScreen( new SaveLevelScreen( g ) ) ),
|
||||
Make( 0, 55, "Back to game", Anchor.BottomOrRight, (g, w) => g.SetNewScreen( new NormalScreen( g ) ) ),
|
||||
Make( 0, 5, "Quit game", Anchor.BottomOrRight, (g, w) => g.Exit() ),
|
||||
};
|
||||
} else {
|
||||
buttons = new ButtonWidget[] {
|
||||
Make( 0, -100, "Options", Anchor.Centre, (g, w) => g.SetNewScreen( new OptionsScreen( g ) ) ),
|
||||
Make( 0, -50, "Environment settings", Anchor.Centre, (g, w) => g.SetNewScreen( new EnvSettingsScreen( g ) ) ),
|
||||
Make( 0, 0, "Select texture pack", Anchor.Centre, (g, w) => g.SetNewScreen( new TexturePackScreen( g ) ) ),
|
||||
Make( 0, 50, "Save level", Anchor.Centre, (g, w) => g.SetNewScreen( new SaveLevelScreen( g ) ) ),
|
||||
// TODO: singleplayer Make( 0, 50, "Load/Save/Gen level", Docking.Centre, (g, w) => g.SetNewScreen( new SaveLevelScreen( g ) ) ),
|
||||
Make( 0, 55, "Back to game", Anchor.BottomOrRight, (g, w) => g.SetNewScreen( new NormalScreen( g ) ) ),
|
||||
Make( 0, 5, "Quit game", Anchor.BottomOrRight, (g, w) => g.Exit() ),
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
ButtonWidget Make( int x, int y, string text, Anchor vDocking, Action<Game, ButtonWidget> onClick ) {
|
||||
return ButtonWidget.Create( game, x, y, 240, 35, text, Anchor.Centre, vDocking, titleFont, onClick );
|
||||
|
41
ClassicalSharp/2D/Screens/Menu/TexturePackScreen.cs
Normal file
41
ClassicalSharp/2D/Screens/Menu/TexturePackScreen.cs
Normal file
@ -0,0 +1,41 @@
|
||||
using System;
|
||||
using System.IO;
|
||||
using ClassicalSharp.TexturePack;
|
||||
using OpenTK.Input;
|
||||
|
||||
namespace ClassicalSharp {
|
||||
|
||||
public sealed class TexturePackScreen : FilesScreen {
|
||||
|
||||
public TexturePackScreen( Game game ) : base( game ) {
|
||||
titleText = "Select a texture pack zip";
|
||||
string directory = Environment.CurrentDirectory;
|
||||
files = Directory.GetFiles( directory, "*.zip", SearchOption.AllDirectories );
|
||||
|
||||
for( int i = 0; i < files.Length; i++ ) {
|
||||
string absolutePath = files[i];
|
||||
files[i] = absolutePath.Substring( directory.Length + 1 );
|
||||
}
|
||||
Array.Sort( files );
|
||||
}
|
||||
|
||||
public override void Init() {
|
||||
base.Init();
|
||||
buttons[buttons.Length - 1] =
|
||||
Make( 0, 5, "Back to menu", (g, w) => g.SetNewScreen( new PauseScreen( g ) ) );
|
||||
}
|
||||
|
||||
ButtonWidget Make( int x, int y, string text, Action<Game, ButtonWidget> onClick ) {
|
||||
return ButtonWidget.Create( game, x, y, 240, 35, text,
|
||||
Anchor.Centre, Anchor.BottomOrRight, titleFont, onClick );
|
||||
}
|
||||
|
||||
protected override void TextButtonClick( Game game, ButtonWidget widget ) {
|
||||
string path = widget.Text;
|
||||
if( File.Exists( path ) ) {
|
||||
TexturePackExtractor extractor = new TexturePackExtractor();
|
||||
extractor.Extract( path, game );
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
@ -18,6 +18,7 @@ namespace ClassicalSharp {
|
||||
public override void Render( double delta ) {
|
||||
if( game.HideGui ) return;
|
||||
|
||||
if( chat.HandlesAllInput )
|
||||
chat.RenderBackground();
|
||||
graphicsApi.Texturing = true;
|
||||
chat.Render( delta );
|
||||
|
@ -78,11 +78,13 @@
|
||||
<Compile Include="2D\Screens\LoadingMapScreen.cs" />
|
||||
<Compile Include="2D\Screens\Menu\EnvSettingsScreen.cs" />
|
||||
<Compile Include="2D\Screens\Menu\KeyMappingsScreen.cs" />
|
||||
<Compile Include="2D\Screens\Menu\LoadLevelScreen.cs" />
|
||||
<Compile Include="2D\Screens\Menu\MenuInputScreen.cs" />
|
||||
<Compile Include="2D\Screens\Menu\MenuScreen.cs" />
|
||||
<Compile Include="2D\Screens\Menu\PauseScreen.cs" />
|
||||
<Compile Include="2D\Screens\Menu\OptionsScreen.cs" />
|
||||
<Compile Include="2D\Screens\Menu\SaveLevelScreen.cs" />
|
||||
<Compile Include="2D\Screens\Menu\TexturePackScreen.cs" />
|
||||
<Compile Include="2D\Screens\NormalScreen.cs" />
|
||||
<Compile Include="2D\Screens\Screen.cs" />
|
||||
<Compile Include="2D\Texture.cs" />
|
||||
|
@ -41,54 +41,4 @@ namespace ClassicalSharp.Singleplayer {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public sealed class LoadMapCommand : Command {
|
||||
|
||||
public LoadMapCommand() {
|
||||
Name = "LoadMap";
|
||||
Help = new [] {
|
||||
"&a/client loadmap [filename]",
|
||||
"&bfilename: &eLoads a map from the specified filename.",
|
||||
"&eSupported formats are .fcm (fCraft map) and ",
|
||||
"&e.dat (Original classic map or WoM saved map)",
|
||||
};
|
||||
}
|
||||
|
||||
public override void Execute( CommandReader reader ) {
|
||||
string path = reader.NextAll();
|
||||
if( String.IsNullOrEmpty( path ) ) return;
|
||||
|
||||
IMapFile mapFile;
|
||||
if( path.EndsWith( ".dat" ) ) {
|
||||
mapFile = new MapDat();
|
||||
} else if( path.EndsWith( ".fcm" ) ) {
|
||||
mapFile = new MapFcm3();
|
||||
} else if( path.EndsWith( ".cw" ) ) {
|
||||
mapFile = new MapCw();
|
||||
} else {
|
||||
game.Chat.Add( "&e/client loadmap: Map format of file \"" + path + "\" not supported" );
|
||||
return;
|
||||
}
|
||||
|
||||
try {
|
||||
using( FileStream fs = new FileStream( path, FileMode.Open, FileAccess.Read, FileShare.Read ) ) {
|
||||
int width, height, length;
|
||||
game.Map.Reset();
|
||||
|
||||
byte[] blocks = mapFile.Load( fs, game, out width, out height, out length );
|
||||
game.Map.UseRawMap( blocks, width, height, length );
|
||||
game.Events.RaiseOnNewMapLoaded();
|
||||
|
||||
LocalPlayer p = game.LocalPlayer;
|
||||
LocationUpdate update = LocationUpdate.MakePos( p.SpawnPoint, false );
|
||||
p.SetLocation( update, false );
|
||||
}
|
||||
} catch( FileNotFoundException ) {
|
||||
game.Chat.Add( "&e/client loadmap: Couldn't find file \"" + path + "\"" );
|
||||
} catch( Exception ex ) {
|
||||
Utils.LogError( "Error while trying to load map: {0}{1}", Environment.NewLine, ex );
|
||||
game.Chat.Add( "&e/client loadmap: Failed to load map \"" + path + "\"" );
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -27,7 +27,6 @@ namespace ClassicalSharp.Singleplayer {
|
||||
NewMap();
|
||||
MakeMap( 128, 128, 128 );
|
||||
game.CommandManager.RegisterCommand( new GenerateCommand() );
|
||||
game.CommandManager.RegisterCommand( new LoadMapCommand() );
|
||||
}
|
||||
|
||||
public override void SendChat( string text ) {
|
||||
|
Loading…
x
Reference in New Issue
Block a user