From bd63502601508cb1a57fd6df3726ecfa35e4343c Mon Sep 17 00:00:00 2001 From: Joeyrp Date: Wed, 26 Jan 2022 13:53:08 -0500 Subject: [PATCH] Added world size (the number of regions in a world) The gird's 0, 0 index is now the center of the grid (so a 10, 10 grid goes from 4, 4 to -4, -4) --- docs/core.todo | 6 ++-- src/internal_libs/assets/types/script.cpp | 33 +++++++++++++++++++ src/internal_libs/assets/types/script.h | 2 +- src/internal_libs/utils/grid.h | 12 +++++-- src/run_modes/game/world/world.cpp | 14 ++++++-- src/run_modes/game/world/world.h | 5 +-- .../tester/scenes/simpleRenderScene.cpp | 4 +-- test_data/engine_state.xml | 2 +- 8 files changed, 64 insertions(+), 14 deletions(-) create mode 100644 src/internal_libs/assets/types/script.cpp diff --git a/docs/core.todo b/docs/core.todo index 1a7c853..ffb802a 100644 --- a/docs/core.todo +++ b/docs/core.todo @@ -1,7 +1,7 @@ Build System: ✔ Add a build option to do a build without the editor @done (9/17/2021, 7:25:08 PM) - ☐ Modify .sh scripts to recognize the noeditor flag + ✔ Modify .sh scripts to recognize the noeditor flag @done (1/25/2022, 3:59:23 PM) Core: ☐ Add log settings to the state file @@ -72,7 +72,7 @@ Utils: Assets: - ☐ Internal Asset Manager @high + ✔ Internal Asset Manager @high @done (1/25/2022, 3:58:20 PM) Types: @@ -81,7 +81,7 @@ Assets: ✔ Decouple Image class from OGLRenderer @high @done (10/27/2021, 7:41:50 PM) ☐ Font class ☐ Sound class - ☐ Script class + ✔ Script class @done (1/25/2022, 3:58:28 PM) Loaders: - Need class (or classes?) to load resources from the packed format that the pipeline generates diff --git a/src/internal_libs/assets/types/script.cpp b/src/internal_libs/assets/types/script.cpp new file mode 100644 index 0000000..9c19dd6 --- /dev/null +++ b/src/internal_libs/assets/types/script.cpp @@ -0,0 +1,33 @@ +/****************************************************************************** +* File - script.h +* Author - Joey Pollack +* Date - 2022/01/18 (y/m/d) +* Mod Date - 2022/01/18 (y/m/d) +* Description - Holds data for a LUA script +******************************************************************************/ + +#include "script.h" + +namespace lunarium +{ + void Script::SetScript(const char* script) + { + mScript = script; + } + + const char* Script::GetScript() const + { + return mScript.c_str(); + } + + + void Script::AddError(std::string error) + { + mScriptErrors.push_back(error); + } + + const std::vector* Script::GetErrors() const + { + return &mScriptErrors; + } +} \ No newline at end of file diff --git a/src/internal_libs/assets/types/script.h b/src/internal_libs/assets/types/script.h index be71dac..a1aa428 100644 --- a/src/internal_libs/assets/types/script.h +++ b/src/internal_libs/assets/types/script.h @@ -26,7 +26,7 @@ namespace lunarium const std::vector* GetErrors() const; private: - char* mpScript; + std::string mScript; std::vector mScriptErrors; }; } diff --git a/src/internal_libs/utils/grid.h b/src/internal_libs/utils/grid.h index 48136ca..8326713 100644 --- a/src/internal_libs/utils/grid.h +++ b/src/internal_libs/utils/grid.h @@ -5,6 +5,8 @@ * Mod Date - 2022/01/25 (y/m/d) * Description - A 2D fixed-size array of data. Size must be set when * this container is created and can not be changed later. +* The 0, 0 position is the center of the grid (meaning negative +* indices are valid). ******************************************************************************/ #ifndef GRID_H_ @@ -33,6 +35,7 @@ namespace lunarium T Data; }; + Sizei mHalfSize; Sizei mSize; Node** mGrid; }; @@ -46,6 +49,9 @@ namespace lunarium Grid::Grid(Sizei size) : mSize(size) { + mHalfSize.Width = size.Width / 2; + mHalfSize.Height = size.Height / 2; + mGrid = new Node*[size.Width]; for (int i = 0; i < size.Width; i++) { @@ -56,19 +62,19 @@ namespace lunarium template T* Grid::operator[](Point2Di at) { - return &mGrid[at.X][at.Y].Data; + return &mGrid[at.X + mHalfSize.Width][at.Y + mHalfSize.Height].Data; } template T Grid::GetAt(Point2Di at) { - return mGrid[at.X][at.Y].Data; + return mGrid[at.X + mHalfSize.Width][at.Y + mHalfSize.Height].Data; } template void Grid::SetAt(Point2Di at, T value) { - mGrid[at.X][at.Y].Data = value; + mGrid[at.X + mHalfSize.Width][at.Y + mHalfSize.Height].Data = value; } template diff --git a/src/run_modes/game/world/world.cpp b/src/run_modes/game/world/world.cpp index c30a834..a85ab07 100644 --- a/src/run_modes/game/world/world.cpp +++ b/src/run_modes/game/world/world.cpp @@ -18,8 +18,8 @@ namespace lunarium { - World::World(Camera* pCam, Sizei region_size) - : mpCamera(pCam), mRegionSize(region_size), mpWorldScript(nullptr), mRegions(region_size) + World::World(Camera* pCam, Sizei region_size, Sizei world_size) + : mpCamera(pCam), mRegionSize(region_size), mpWorldScript(nullptr), mRegions(region_size), mWorldSize(world_size) { mActiveRegion = { 0, 0 }; mRegions.SetAll(nullptr); @@ -68,6 +68,16 @@ namespace lunarium return OpRes::Fail("World::SetRegion failed because a region already exists at (%n, %n)", at.X, at.Y); } + // Validate the layers - each image needs to match the region size + for (int i = 0; i < region->mLayers.size(); i++) + { + if (region->mLayers[i]->GetWidth() != mRegionSize.Width + || region->mLayers[i]->GetHeight() != mRegionSize.Height) + { + return OpRes::Fail("World::SetRegion failed because the region contains layers that do not match the region size"); + } + } + *mRegions[at] = region; return OpRes::OK(); } diff --git a/src/run_modes/game/world/world.h b/src/run_modes/game/world/world.h index 5d84fed..54183dc 100644 --- a/src/run_modes/game/world/world.h +++ b/src/run_modes/game/world/world.h @@ -39,7 +39,7 @@ namespace lunarium public: // INTERFACE - World(Camera* pCam, Sizei region_size); + World(Camera* pCam, Sizei region_size, Sizei world_size); void OnLoad(); void OnUnload(); @@ -65,7 +65,8 @@ namespace lunarium Camera* mpCamera; std::vector mWorldObjects; Script* mpWorldScript; - Sizei mRegionSize; + Sizei mRegionSize; // in pixels + Sizei mWorldSize; // num regions ex. 10x10 Grid mRegions; Point2Di mActiveRegion; diff --git a/src/run_modes/tester/scenes/simpleRenderScene.cpp b/src/run_modes/tester/scenes/simpleRenderScene.cpp index 8dc37ad..eeaec35 100644 --- a/src/run_modes/tester/scenes/simpleRenderScene.cpp +++ b/src/run_modes/tester/scenes/simpleRenderScene.cpp @@ -41,7 +41,7 @@ namespace lunarium box_angle = 0.0f; // Setup the grid container - *mGrid[{5, 5}] = new GridTestObj {5, "This is grid space 5, 5"}; + *mGrid[{-2, 0}] = new GridTestObj {5, "This is grid space -2, 0"}; } void SimpleRenderScene::OnTick(double delta) @@ -118,6 +118,6 @@ namespace lunarium g->DrawBox(Rectangle(400, 400, 128.0f, 128.0f), Color(0.0f, 1.0f, 0.0f, 1.0f), 2.0f, box_angle); - g->DrawString((*mGrid[{5, 5,}])->msg.c_str(), Rectangle::MakeFromTopLeft(200.0f, 500.0f, 500.0f, 200.0f), Color(0.5f, 0.0f, 0.75f, 1.0f)); + g->DrawString((*mGrid[{-2, 0,}])->msg.c_str(), Rectangle::MakeFromTopLeft(200.0f, 500.0f, 500.0f, 200.0f), Color(0.5f, 0.0f, 0.75f, 1.0f)); } } \ No newline at end of file diff --git a/test_data/engine_state.xml b/test_data/engine_state.xml index 277eb02..5cf7851 100644 --- a/test_data/engine_state.xml +++ b/test_data/engine_state.xml @@ -1,6 +1,6 @@ data/ - +