diff --git a/2d-game.vcxproj b/2d-game.vcxproj index 6e89048060844b151b9f7025156e331e6f1fd164..7cac94e0b00b558c1d1ed98044c4ea04045d2be4 100644 --- a/2d-game.vcxproj +++ b/2d-game.vcxproj @@ -150,6 +150,7 @@ <ClCompile Include="ResourceManager.cpp" /> <ClCompile Include="SimpleSprite.cpp" /> <ClCompile Include="WallEntity.cpp" /> + <ClCompile Include="WallTexture.cpp" /> </ItemGroup> <ItemGroup> <ClInclude Include="DynamicArray.hpp" /> @@ -165,6 +166,7 @@ <ClInclude Include="ResourceManager.h" /> <ClInclude Include="SimpleSprite.h" /> <ClInclude Include="WallEntity.h" /> + <ClInclude Include="WallTexture.h" /> </ItemGroup> <Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" /> <ImportGroup Label="ExtensionTargets"> diff --git a/2d-game.vcxproj.filters b/2d-game.vcxproj.filters index 9cf0a13ba51445fe962c57faee6126995cb26eca..5c77d055d245233c0a2189b9c0951238a71b5d71 100644 --- a/2d-game.vcxproj.filters +++ b/2d-game.vcxproj.filters @@ -53,6 +53,9 @@ <ClCompile Include="WallEntity.cpp"> <Filter>Source Files\gameObj\entities</Filter> </ClCompile> + <ClCompile Include="WallTexture.cpp"> + <Filter>Source Files\gameObj</Filter> + </ClCompile> </ItemGroup> <ItemGroup> <ClInclude Include="interfaces.h"> @@ -94,5 +97,8 @@ <ClInclude Include="WallEntity.h"> <Filter>Header Files\gameObj\entities</Filter> </ClInclude> + <ClInclude Include="WallTexture.h"> + <Filter>Header Files\gameObj\render</Filter> + </ClInclude> </ItemGroup> </Project> \ No newline at end of file diff --git a/Entity.cpp b/Entity.cpp index 7893c98b2ea12d3cc655d36a751a1da34c3fd886..42e55b88243fcba908b67d7ef6804c85b73af9b3 100644 --- a/Entity.cpp +++ b/Entity.cpp @@ -1,7 +1,5 @@ #include "Entity.h" -#include <utility> - using namespace std; using namespace olc; @@ -36,19 +34,29 @@ namespace entities { Entity::Entity(const olc::vf2d& pos) - : pos(pos) - { - - } + : pos(pos), is_initialized(false){} + vf2d Entity::getPos() const { return this->pos; } + + olc::vf2d Entity::getSize() const + { + return olc::vf2d(1, 1); + } + void Entity::render(olc::TransformedView& scene) { getTexture().render(scene, *this); } + boolean Entity::isInitialized() + { + return is_initialized; + } + + } TransformedView& operator+=(olc::TransformedView& scene, entities::Entity& entity) { diff --git a/Entity.h b/Entity.h index f823398aba02b52ba9f8af0fc1e483949ff69161..1ef293bc32fd70cabc22ae48084caf7a32c7d6c1 100644 --- a/Entity.h +++ b/Entity.h @@ -17,12 +17,17 @@ namespace entities { virtual olc::vf2d getHitBoxSize() const = 0; virtual olc::vf2d getCollision(const Entity& other); + bool is_initialized; public: - + Entity(const olc::vf2d& pos); + virtual void init(GameClient& client){} + virtual boolean isInitialized(); + //nodiscard??? [[nodiscard]] virtual olc::vf2d getPos() const; + [[nodiscard]] virtual olc::vf2d getSize() const; virtual void tick(GameClient& client, float deltaT){} diff --git a/ITexture.h b/ITexture.h index 8cb504bed35e0547ec9c21d00c2d539c1dc053a5..575963f6cc549cd427a7d5f66b98310dfda75745 100644 --- a/ITexture.h +++ b/ITexture.h @@ -1,6 +1,7 @@ #pragma once #include "olcPGEX_TransformedView.h" +#include "LazySprite.h" namespace entities { class Entity; diff --git a/SimpleSprite.cpp b/SimpleSprite.cpp index ef87941efbc9aadc439d170d309a9cb2fb728ad3..6150478df1f1fb74b3708db5faab03ba05796bdf 100644 --- a/SimpleSprite.cpp +++ b/SimpleSprite.cpp @@ -8,5 +8,5 @@ render::SimpleSprite::SimpleSprite(const std::string& name, const vi2d& pos, con void render::SimpleSprite::render(olc::TransformedView& scene, entities::Entity& entity) { - this->sprite.render(scene, entity.getPos(), uv, size); + this->sprite.renderCentered(scene, entity.getPos(), uv, size); } diff --git a/WallEntity.cpp b/WallEntity.cpp index 4c161e92377a3dd031c8c43d149eb224855ff902..53fb9a801ac8e74c0f514ea6c0217eae9e5ceef1 100644 --- a/WallEntity.cpp +++ b/WallEntity.cpp @@ -1 +1,32 @@ #include "WallEntity.h" + +#include "mainGame.h" + +using namespace olc; + +void entities::WallEntity::updateNeighbours() +{ + this->neighbourID = 0; + for (auto& entity : client.getEntities()) { + if (entity->getAsWallEntity() != nullptr) { + WallEntity& wallEntity = *entity->getAsWallEntity(); + vi2d distance = entity->getPos() - this->getPos(); + if (abs(distance.x) == 1 && abs(distance.y) == 0) { + neighbourID |= distance.x == 1 ? 0b0100 : 0b1000; + } + else if (abs(distance.x) == 0 && abs(distance.y) == 1) { + neighbourID |= distance.x == 1 ? 0b01 : 0b10; + } + } + } +} + +void entities::WallEntity::init(GameClient& client) +{ + this->updateNeighbours(); +} + +byte entities::WallEntity::getNeighbourID() const +{ + return neighbourID; +} diff --git a/WallEntity.h b/WallEntity.h index 280b8fe49493c4b0b01c3c824bfd21aaeb8ae993..a863da4cc5a043817dd68f9cc3c6c462ee6cd6d6 100644 --- a/WallEntity.h +++ b/WallEntity.h @@ -5,10 +5,16 @@ namespace entities { class WallEntity : public Entity { - public: - + private: + byte neighbourID; + protected: + virtual void updateNeighbours(); + public: + void init(GameClient& client) override; + + virtual byte getNeighbourID() const; //This is a wall entity after all. WallEntity* getAsWallEntity() override diff --git a/WallTexture.cpp b/WallTexture.cpp new file mode 100644 index 0000000000000000000000000000000000000000..e611ce6b3d313c1d376c94bb7a19299db0414088 --- /dev/null +++ b/WallTexture.cpp @@ -0,0 +1,37 @@ +#include "WallTexture.h" +#include "WallEntity.h" + +using namespace olc; +using namespace entities; + +namespace render +{ + + + WallTexture::WallTexture(const std::string& resName, const vf2d& baseOffset, const vf2d& size) + : sprite(resName), baseOffset(baseOffset), size(size) {} + void WallTexture::render(olc::TransformedView& scene, Entity& entity) + { + if(entity.getAsWallEntity() == nullptr){ + throw std::invalid_argument("Wall texture needs a wall entity"); + } + WallEntity& wallEntity = *entity.getAsWallEntity(); + vf2d offset = this->baseOffset; + + //I can't use index[] because that is not const function... WHY??? + offset += (offsetMap.find(wallEntity.getNeighbourID())->second * this->size); + this->sprite.render(scene, wallEntity.getPos(), offset, size, wallEntity.getSize()); + } + + + + + + //Wall neighbour texture offset mapping + const std::map<byte, vi2d> WallTexture::offsetMap = { + {0b0000, vi2d(1, 1) }, {0b0001, vi2d(1, 1)}, {0b0010, vi2d(0, 1)}, {0b0011, vi2d(0, 1)}, + {0b0100, vi2d(1, 0) }, {0b0101, vi2d(2, 2)}, {0b0110, vi2d(2, 0)}, {0b0111, vi2d(5, 1)}, + {0b1000, vi2d(1, 0) }, {0b1001, vi2d(0, 2)}, {0b1010, vi2d(0, 0)}, {0b1011, vi2d(3, 1)}, + {0b1100, vi2d(1, 0) }, {0b1101, vi2d(4, 2)}, {0b1110, vi2d(4, 0)}, {0b1111, vi2d(4, 1)} + }; +} \ No newline at end of file diff --git a/WallTexture.h b/WallTexture.h new file mode 100644 index 0000000000000000000000000000000000000000..1c8b3f3136ef81b0d29d152d18dd2aca97d873c5 --- /dev/null +++ b/WallTexture.h @@ -0,0 +1,20 @@ +#pragma once +#include "ITexture.h" +#include <map> + + +namespace render +{ + class WallTexture : + public ITexture + { + private: + LazySprite sprite; + const olc::vf2d baseOffset, size; + protected: + static const std::map<byte, olc::vi2d> offsetMap; + public: + WallTexture(const std::string& resName, const olc::vf2d& baseOffset, const olc::vf2d& size = olc::vf2d(16, 16)); + void render(olc::TransformedView& scene, entities::Entity& entity) override; + }; +} diff --git a/mainGame.cpp b/mainGame.cpp index d9946be58a656b7dcfa8f9976b55c49a3816d7e4..1af7bb5544365e0fd57912942007ecaa3940f2dc 100644 --- a/mainGame.cpp +++ b/mainGame.cpp @@ -46,7 +46,13 @@ bool GameClient::OnUserUpdate(float fElapsedTime) for(std::shared_ptr<Entity> entity : this->getEntities()){ entity->tick(*this, fElapsedTime); } - + + entities.finalizeAdd(); + for(auto &entity : entities){ + if(!entity->isInitialized()){ + entity->init(*this); + } + } return true; } diff --git a/x64/Release/2d-game.log b/x64/Release/2d-game.log index 5f282702bb03ef11d7184d19c80927b47f919764..4ab68bc82922b2ad0b273adf8eb6f414cee26339 100644 --- a/x64/Release/2d-game.log +++ b/x64/Release/2d-game.log @@ -1 +1,22 @@ - \ No newline at end of file + Entity.cpp +C:\Users\kosmx\Documents\GitHub\2d-game\Entity.cpp(21,42): warning C4244: 'argument': conversion from 'int' to 'T', possible loss of data + with + [ + T=float + ] +C:\Users\kosmx\Documents\GitHub\2d-game\Entity.cpp(21,20): warning C4244: 'argument': conversion from 'int' to 'T', possible loss of data + with + [ + T=float + ] + game.cpp + mainGame.cpp + SimpleSprite.cpp + WallEntity.cpp + WallTexture.cpp + Generating code + 327 of 1619 functions (20.2%) were compiled, the rest were copied from previous compilation. + 112 functions were new in current compilation + 103 functions had inline decision re-evaluated but remain unchanged + Finished generating code + 2d-game.vcxproj -> C:\Users\kosmx\Documents\GitHub\2d-game\x64\Release\2d-game.exe diff --git a/x64/Release/2d-game.vcxproj.FileListAbsolute.txt b/x64/Release/2d-game.vcxproj.FileListAbsolute.txt index e69de29bb2d1d6434b8b29ae775ad8c2e48c5391..7a8382a9924cb4235ce4cc1102344aab08bad82f 100644 --- a/x64/Release/2d-game.vcxproj.FileListAbsolute.txt +++ b/x64/Release/2d-game.vcxproj.FileListAbsolute.txt @@ -0,0 +1 @@ +C:\Users\kosmx\Documents\GitHub\2d-game\x64\Release\2d-game.exe