From cc54c6bc802f6dcf80cc192a44dad286099f5ee2 Mon Sep 17 00:00:00 2001 From: UnknownShadow200 Date: Fri, 20 Jan 2017 09:43:44 +1100 Subject: [PATCH] Initial work on survival test --- ClassicalSharp/Blocks/Block.cs | 1 + ClassicalSharp/Blocks/DefaultSet.cs | 2 +- ClassicalSharp/ClassicalSharp.csproj | 4 ++ .../Commands/SinglePlayerCommands.cs | 17 +++++---- ClassicalSharp/Game/Game.Init.cs | 9 ++++- ClassicalSharp/Game/Game.Properties.cs | 4 ++ ClassicalSharp/Game/InputHandler.cs | 4 +- ClassicalSharp/Game/Inventory.cs | 10 ++--- ClassicalSharp/Mode/Creative.cs | 38 +++++++++++++++++++ ClassicalSharp/Mode/IGameMode.cs | 11 ++++++ ClassicalSharp/Mode/Survival.cs | 32 ++++++++++++++++ ClassicalSharp/Singleplayer/Server.cs | 1 - ClassicalSharp/Utils/Options.cs | 1 + 13 files changed, 113 insertions(+), 21 deletions(-) create mode 100644 ClassicalSharp/Mode/Creative.cs create mode 100644 ClassicalSharp/Mode/IGameMode.cs create mode 100644 ClassicalSharp/Mode/Survival.cs diff --git a/ClassicalSharp/Blocks/Block.cs b/ClassicalSharp/Blocks/Block.cs index cffd8ebfa..4b204f386 100644 --- a/ClassicalSharp/Blocks/Block.cs +++ b/ClassicalSharp/Blocks/Block.cs @@ -73,6 +73,7 @@ namespace ClassicalSharp { public const byte Pillar = 63; public const byte Crate = 64; public const byte StoneBrick = 65; + public const byte Invalid = 0xFF; public const string Names = "Air Stone Grass Dirt Cobblestone Wood Sapling Bedrock Water StillWater Lava" + " StillLava Sand Gravel GoldOre IronOre CoalOre Log Leaves Sponge Glass Red Orange Yellow Lime Green" + diff --git a/ClassicalSharp/Blocks/DefaultSet.cs b/ClassicalSharp/Blocks/DefaultSet.cs index 39b63e5c9..11f14fba3 100644 --- a/ClassicalSharp/Blocks/DefaultSet.cs +++ b/ClassicalSharp/Blocks/DefaultSet.cs @@ -57,7 +57,7 @@ namespace ClassicalSharp.Blocks { public static byte Draw(byte b) { - if (b == Block.Air) return DrawType.Gas; + if (b == Block.Air || b == Block.Invalid) return DrawType.Gas; if (b == Block.Leaves) return DrawType.TransparentThick; if (b == Block.Ice || b == Block.Water || b == Block.StillWater) diff --git a/ClassicalSharp/ClassicalSharp.csproj b/ClassicalSharp/ClassicalSharp.csproj index c929b365e..ea19983c9 100644 --- a/ClassicalSharp/ClassicalSharp.csproj +++ b/ClassicalSharp/ClassicalSharp.csproj @@ -204,6 +204,9 @@ + + + @@ -346,6 +349,7 @@ + diff --git a/ClassicalSharp/Commands/SinglePlayerCommands.cs b/ClassicalSharp/Commands/SinglePlayerCommands.cs index d1bbba439..9675dc051 100644 --- a/ClassicalSharp/Commands/SinglePlayerCommands.cs +++ b/ClassicalSharp/Commands/SinglePlayerCommands.cs @@ -64,17 +64,17 @@ namespace ClassicalSharp.Commands { if (Utils.CaselessEquals(args[1], "yes")) { persist = true; return true; } int temp = -1; - byte blockID = 0; + byte block = 0; if ((temp = game.BlockInfo.FindID(args[1])) != -1) { - blockID = (byte)temp; - } else if (!byte.TryParse(args[1], out blockID)) { + block = (byte)temp; + } else if (!byte.TryParse(args[1], out block)) { game.Chat.Add("&eCuboid: &c\"" + args[1] + "\" is not a valid block name or id."); return false; } - if (blockID >= Block.CpeCount && game.BlockInfo.Name[blockID] == "Invalid") { + if (block >= Block.CpeCount && game.BlockInfo.Name[block] == "Invalid") { game.Chat.Add("&eCuboid: &cThere is no block with id \"" + args[1] + "\"."); return false; } - block = blockID; + block = block; return true; } @@ -102,14 +102,15 @@ namespace ClassicalSharp.Commands { Vector3I min = Vector3I.Min(mark1, mark2); Vector3I max = Vector3I.Max(mark1, mark2); if (!game.World.IsValidPos(min) || !game.World.IsValidPos(max)) return; - byte id = block; - if (id == 0xFF) id = (byte)game.Inventory.HeldBlock; + byte toPlace = block; + if (toPlace == Block.Invalid) toPlace = game.Inventory.HeldBlock; + for (int y = min.Y; y <= max.Y; y++) for (int z = min.Z; z <= max.Z; z++) for (int x = min.X; x <= max.X; x++) { - game.UpdateBlock(x, y, z, id); + game.UpdateBlock(x, y, z, toPlace); } } } diff --git a/ClassicalSharp/Game/Game.Init.cs b/ClassicalSharp/Game/Game.Init.cs index 47ae41803..729ccee34 100644 --- a/ClassicalSharp/Game/Game.Init.cs +++ b/ClassicalSharp/Game/Game.Init.cs @@ -9,6 +9,7 @@ using ClassicalSharp.Entities; using ClassicalSharp.GraphicsAPI; using ClassicalSharp.Gui.Screens; using ClassicalSharp.Map; +using ClassicalSharp.Mode; using ClassicalSharp.Model; using ClassicalSharp.Network; using ClassicalSharp.Particles; @@ -44,8 +45,14 @@ namespace ClassicalSharp { ETags.Load(); LastModified.Load(); + if (Options.GetBool(OptionsKey.SurvivalMode, false)) { + Mode = AddComponent(new SurvivalGameMode()); + } else { + Mode = AddComponent(new CreativeGameMode()); + } + Input = new InputHandler(this); - defaultIb = Graphics.MakeDefaultIb(); + defaultIb = Graphics.MakeDefaultIb(); ParticleManager = AddComponent(new ParticleManager()); TabList = AddComponent(new TabList()); LoadOptions(); diff --git a/ClassicalSharp/Game/Game.Properties.cs b/ClassicalSharp/Game/Game.Properties.cs index 48cdab47f..38a844918 100644 --- a/ClassicalSharp/Game/Game.Properties.cs +++ b/ClassicalSharp/Game/Game.Properties.cs @@ -11,6 +11,7 @@ using ClassicalSharp.Events; using ClassicalSharp.GraphicsAPI; using ClassicalSharp.Gui; using ClassicalSharp.Map; +using ClassicalSharp.Mode; using ClassicalSharp.Model; using ClassicalSharp.Network; using ClassicalSharp.Particles; @@ -52,6 +53,9 @@ namespace ClassicalSharp { /// Abstracts the underlying 3D graphics rendering API. public IGraphicsApi Graphics; + /// Handles game mode specific functionality. + public IGameMode Mode; + /// Contains the block data and metadata/properties for the player's current world. public World World; diff --git a/ClassicalSharp/Game/InputHandler.cs b/ClassicalSharp/Game/InputHandler.cs index a3410f70a..8328249cb 100644 --- a/ClassicalSharp/Game/InputHandler.cs +++ b/ClassicalSharp/Game/InputHandler.cs @@ -243,9 +243,7 @@ namespace ClassicalSharp { } } else if (key == Keys[KeyBind.PauseOrExit] && !game.World.IsNotLoaded) { game.Gui.SetNewScreen(new PauseScreen(game)); - } else if (key == Keys[KeyBind.Inventory]) { - game.Gui.SetNewScreen(new InventoryScreen(game)); - } else { + } else if (!game.Mode.HandlesKeyDown(key)) { return false; } return true; diff --git a/ClassicalSharp/Game/Inventory.cs b/ClassicalSharp/Game/Inventory.cs index 9dc4c1f88..57d9e1e50 100644 --- a/ClassicalSharp/Game/Inventory.cs +++ b/ClassicalSharp/Game/Inventory.cs @@ -21,16 +21,12 @@ namespace ClassicalSharp { Game game; public bool CanChangeHeldBlock = true; - public byte[] Hotbar = new byte[] { Block.Stone, - Block.Cobblestone, Block.Brick, Block.Dirt, Block.Wood, - Block.Log, Block.Leaves, Block.Grass, Block.Slab }; - public byte[] Count = new byte[] { 1, 1, 1, 1, 1, 1, 1, 1, 1 }; - + public byte[] Hotbar = new byte[9]; public InventoryPermissions CanPlace = new InventoryPermissions(); public InventoryPermissions CanDelete = new InventoryPermissions(); /// Gets or sets the index of the held block. - /// Fails if the server has forbidden up from changing the held block. + /// Fails if the server has forbidden user from changing the held block. public int HeldBlockIndex { get { return hotbarIndex; } set { @@ -44,7 +40,7 @@ namespace ClassicalSharp { } /// Gets or sets the block currently held by the player. - /// Fails if the server has forbidden up from changing the held block. + /// Fails if the server has forbidden user from changing the held block. public byte HeldBlock { get { return Hotbar[hotbarIndex]; } set { diff --git a/ClassicalSharp/Mode/Creative.cs b/ClassicalSharp/Mode/Creative.cs new file mode 100644 index 000000000..2f86d0d97 --- /dev/null +++ b/ClassicalSharp/Mode/Creative.cs @@ -0,0 +1,38 @@ +// Copyright 2014-2017 ClassicalSharp | Licensed under BSD-3 +using System; +using ClassicalSharp.Gui.Screens; +using OpenTK.Input; + +namespace ClassicalSharp.Mode { + + public sealed class CreativeGameMode : IGameMode { + + Game game; + + public bool HandlesKeyDown(Key key) { + if (key == game.Input.Keys[KeyBind.Inventory]) { + game.Gui.SetNewScreen(new InventoryScreen(game)); + return true; + } + return false; + } + + public void OnNewMapLoaded(Game game) { + if (game.Server.IsSinglePlayer) + game.Chat.Add("&ePlaying single player", MessageType.Status1); + } + + public void Init(Game game) { + this.game = game; + game.Inventory.Hotbar = new byte[] { Block.Stone, + Block.Cobblestone, Block.Brick, Block.Dirt, Block.Wood, + Block.Log, Block.Leaves, Block.Grass, Block.Slab }; + } + + + public void Ready(Game game) { } + public void Reset(Game game) { } + public void OnNewMap(Game game) { } + public void Dispose() { } + } +} diff --git a/ClassicalSharp/Mode/IGameMode.cs b/ClassicalSharp/Mode/IGameMode.cs new file mode 100644 index 000000000..571bf7f5a --- /dev/null +++ b/ClassicalSharp/Mode/IGameMode.cs @@ -0,0 +1,11 @@ +// Copyright 2014-2017 ClassicalSharp | Licensed under BSD-3 +using System; +using OpenTK.Input; + +namespace ClassicalSharp.Mode { + + public interface IGameMode : IGameComponent { + + bool HandlesKeyDown(Key key); + } +} diff --git a/ClassicalSharp/Mode/Survival.cs b/ClassicalSharp/Mode/Survival.cs new file mode 100644 index 000000000..4d0236376 --- /dev/null +++ b/ClassicalSharp/Mode/Survival.cs @@ -0,0 +1,32 @@ +// Copyright 2014-2017 ClassicalSharp | Licensed under BSD-3 +using System; +using OpenTK.Input; + +namespace ClassicalSharp.Mode { + + public sealed class SurvivalGameMode : IGameMode { + + Game game; + int score = 0; + byte[] invCount = new byte[] { 0, 0, 0, 0, 0, 0, 0, 0, 10 }; + + public bool HandlesKeyDown(Key key) { return false; } + + public void OnNewMapLoaded(Game game) { + game.Chat.Add("&fScore: &e" + score, MessageType.Status1); + } + + public void Init(Game game) { + this.game = game; + for (int i = 0; i < game.Inventory.Hotbar.Length; i++) + game.Inventory.Hotbar[i] = Block.Invalid; + game.Inventory.Hotbar[8] = Block.TNT; + } + + + public void Ready(Game game) { } + public void Reset(Game game) { } + public void OnNewMap(Game game) { } + public void Dispose() { } + } +} diff --git a/ClassicalSharp/Singleplayer/Server.cs b/ClassicalSharp/Singleplayer/Server.cs index 490e99148..0eda0e645 100644 --- a/ClassicalSharp/Singleplayer/Server.cs +++ b/ClassicalSharp/Singleplayer/Server.cs @@ -101,7 +101,6 @@ namespace ClassicalSharp.Singleplayer { } generator = null; - game.Chat.Add("&ePlaying single player", MessageType.Status1); GC.Collect(); } diff --git a/ClassicalSharp/Utils/Options.cs b/ClassicalSharp/Utils/Options.cs index e4501b3a8..fd6423ceb 100644 --- a/ClassicalSharp/Utils/Options.cs +++ b/ClassicalSharp/Utils/Options.cs @@ -22,6 +22,7 @@ namespace ClassicalSharp { public const string EntityShadow = "entityshadow"; public const string RenderType = "normal"; public const string SmoothLighting = "gfx-smoothlighting"; + public const string SurvivalMode = "game-survivalmode"; public const string HacksEnabled = "hacks-hacksenabled"; public const string FieldOfView = "hacks-fov";