From f84c96ad28f97b309709a4663df6d9d1988ace2f Mon Sep 17 00:00:00 2001 From: UnknownShadow200 Date: Wed, 25 Oct 2017 09:56:15 +1100 Subject: [PATCH] Port GameMode to C. --- ClassicalSharp/Mode/Creative.cs | 2 +- src/Client/Client.vcxproj | 2 + src/Client/Client.vcxproj.filters | 6 ++ src/Client/GameMode.c | 94 +++++++++++++++++++++++++++---- 4 files changed, 93 insertions(+), 11 deletions(-) diff --git a/ClassicalSharp/Mode/Creative.cs b/ClassicalSharp/Mode/Creative.cs index 97e7b98e4..cb32c763f 100644 --- a/ClassicalSharp/Mode/Creative.cs +++ b/ClassicalSharp/Mode/Creative.cs @@ -54,7 +54,7 @@ namespace ClassicalSharp.Mode { if (!inv.CanChangeSelected() || inv.Selected == old) return; // Is the currently selected block an empty slot - if (inv.Hotbar[inv.SelectedIndex] == Block.Air) { + if (inv[inv.SelectedIndex] == Block.Air) { inv.Selected = old; return; } diff --git a/src/Client/Client.vcxproj b/src/Client/Client.vcxproj index 8d4c67d09..5f1491122 100644 --- a/src/Client/Client.vcxproj +++ b/src/Client/Client.vcxproj @@ -201,6 +201,7 @@ + @@ -272,6 +273,7 @@ + diff --git a/src/Client/Client.vcxproj.filters b/src/Client/Client.vcxproj.filters index 6a6b1f1e9..e6c2777d5 100644 --- a/src/Client/Client.vcxproj.filters +++ b/src/Client/Client.vcxproj.filters @@ -372,6 +372,9 @@ Header Files\2D + + Header Files\Game + @@ -575,5 +578,8 @@ Source Files\2D + + Source Files\Game + \ No newline at end of file diff --git a/src/Client/GameMode.c b/src/Client/GameMode.c index b5c6976f3..8fc28454f 100644 --- a/src/Client/GameMode.c +++ b/src/Client/GameMode.c @@ -1,13 +1,87 @@ #include "GameMode.h" +#include "Inventory.h" +#include "Widgets.h" +#include "Game.h" +#include "Screens.h" +#include "Block.h" +#include "Events.h" -IGameComponent GameMode_MakeComponent(void); +void GameMode_Init(void) { + BlockID* inv = Inventory_Table; + inv[0] = BlockID_Stone; inv[1] = BlockID_Cobblestone; inv[2] = BlockID_Brick; + inv[3] = BlockID_Dirt; inv[4] = BlockID_Wood; inv[5] = BlockID_Log; + inv[6] = BlockID_Leaves; inv[7] = BlockID_Grass; inv[8] = BlockID_Slab; +} -bool GameMode_HandlesKeyDown(Key key); -bool GameMode_PickingLeft(void); -bool GameMode_PickingRight(void); -void GameMode_PickLeft(BlockID old); -void GameMode_PickMiddle(BlockID old); -void GameMode_PickRight(BlockID old, BlockID block); -HotbarWidget* GameMode_MakeHotbar(void); -void GameMode_BeginFrame(Real64 delta); -void GameMode_EndFrame(Real64 delta); \ No newline at end of file +IGameComponent GameMode_MakeComponent(void) { + IGameComponent comp = IGameComponent_MakeEmpty(); + comp.Init = GameMode_Init; + return comp; +} + +bool GameMode_HandlesKeyDown(Key key) { + Screen* activeScreen = Gui_GetActiveScreen(); + if (key == KeyBind_Get(KeyBind_Inventory) && activeScreen == Gui_HUD) { + Screen* screen = InventoryScreen_MakeInstance(); + Gui_SetNewScreen(screen); + return true; + } else if (key == KeyBind_Get(KeyBind_DropBlock) && !Game_ClassicMode) { + if (Inventory_CanChangeSelected()) { + /* Don't assign SelectedIndex directly, because we don't want held block + switching positions if they already have air in their inventory hotbar. */ + Inventory_Set(Inventory_SelectedIndex, BlockID_Air); + Event_RaiseVoid(&UserEvents_HeldBlockChanged); + } + return true; + } + return false; +} + +bool GameMode_PickingLeft(void) { + /* always play delete animations, even if we aren't picking a block */ + game.HeldBlockRenderer.ClickAnim(true); + return false; +} +bool GameMode_PickingRight(void) { return false; } + +void GameMode_PickLeft(BlockID old) { + Vector3I pos = Game_SelectedPos.BlockPos; + Game_UpdateBlock(pos.X, pos.Y, pos.Z, BlockID_Air); + Event_RaiseBlock(&UserEvents_BlockChanged, pos, old, BlockID_Air); +} + +void GameMode_PickMiddle(BlockID old) { + if (Block_Draw[old] == DrawType_Gas) return; + if (!(Block_CanPlace[old] || Block_CanDelete[old])) return; + if (!Inventory_CanChangeSelected() || Inventory_SelectedBlock == old) return; + + // Is the currently selected block an empty slot + if (Inventory_Get(Inventory_SelectedIndex) == BlockID_Air) { + Inventory_SetSelectedBlock(old); + return; + } + + /* Try to replace same block or empty slots first */ + UInt32 i; + for (i = 0; i < INVENTORY_BLOCKS_PER_HOTBAR; i++) { + if (Inventory_Get(i) != old && Inventory_Get(i) != BlockID_Air) continue; + + Inventory_Set(i, old); + Inventory_SetSelectedIndex(i); + return; + } + + /* Finally, replace the currently selected block */ + Inventory_SetSelectedBlock(old); +} + +void GameMode_PickRight(BlockID old, BlockID block) { + Vector3I pos = Game_SelectedPos.TranslatedPos; + Game_UpdateBlock(pos.X, pos.Y, pos.Z, block); + Event_RaiseBlock(&UserEvents_BlockChanged, pos, old, block); +} + +HotbarWidget GameMode_Hotbar; +HotbarWidget* GameMode_MakeHotbar(void) { return &GameMode_Hotbar; } +void GameMode_BeginFrame(Real64 delta) { } +void GameMode_EndFrame(Real64 delta) { } \ No newline at end of file