diff --git a/2d-game.vcxproj.filters b/2d-game.vcxproj.filters index 1a82cd5d15b27774704da72a521d287dd8b69748..cbfe8f806026b8d69ae8842f53cd123c158ccd66 100644 --- a/2d-game.vcxproj.filters +++ b/2d-game.vcxproj.filters @@ -65,14 +65,14 @@ <ClCompile Include="TestGenerator.cpp"> <Filter>Source Files\gameObj</Filter> </ClCompile> - <ClCompile Include="CharacterTexture.cpp"> - <Filter>Source Files</Filter> - </ClCompile> <ClCompile Include="LivingEntity.cpp"> <Filter>Source Files\gameObj\entities</Filter> </ClCompile> + <ClCompile Include="CharacterTexture.cpp"> + <Filter>Source Files\gameObj</Filter> + </ClCompile> <ClCompile Include="PlayerEntity.cpp"> - <Filter>Source Files</Filter> + <Filter>Source Files\gameObj\entities</Filter> </ClCompile> </ItemGroup> <ItemGroup> @@ -127,14 +127,14 @@ <ClInclude Include="TestGenerator.h"> <Filter>Header Files\gameObj\management</Filter> </ClInclude> - <ClInclude Include="CharacterTexture.h"> - <Filter>Header Files</Filter> - </ClInclude> <ClInclude Include="LivingEntity.h"> <Filter>Header Files\gameObj\entities</Filter> </ClInclude> + <ClInclude Include="CharacterTexture.h"> + <Filter>Header Files\gameObj\render</Filter> + </ClInclude> <ClInclude Include="PlayerEntity.h"> - <Filter>Header Files</Filter> + <Filter>Header Files\gameObj\entities</Filter> </ClInclude> </ItemGroup> <ItemGroup> diff --git a/CharacterTexture.cpp b/CharacterTexture.cpp index ba037f4984a23ac3f5ec16fa6cdc868001ef82bb..6c724ba67dfc4854a64b7f4ae1db4f9d81b7f650 100644 --- a/CharacterTexture.cpp +++ b/CharacterTexture.cpp @@ -1 +1,26 @@ #include "CharacterTexture.h" + +#include "Entity.h" +#include "GameException.h" +#include "LivingEntity.h" + +using namespace olc; + +namespace render { + + CharacterTexture::CharacterTexture(const std::string& name, vf2d size) + : sprite(name), size(size) {} + + void CharacterTexture::render(TransformedView& scene, entities::Entity& entity) + { + if (entity.getAsLivingEntity() == nullptr) { + throw GameException("Can't render character texture for a not-living entity...", entity); + } + entities::LivingEntity& livingEntity = *entity.getAsLivingEntity(); + vf2d pos = this->uv + vf2d(livingEntity.getAnimPhase() * this->size.x, livingEntity.getDirection() * size.y); + this->sprite.renderCentered(scene, entity.getPos(), pos, size, entity.getSize()); + } + + CharacterTexture CharacterTexture::EngineerTexture("Commissions/Engineer.png"); + CharacterTexture CharacterTexture::MageTexture("Commissions/Mage.png"); +} \ No newline at end of file diff --git a/CharacterTexture.h b/CharacterTexture.h index f924fa4fdbfc7a2668a42af75fd718ff40b85538..9539b03fb25851a27800e2380482ea744842a5f1 100644 --- a/CharacterTexture.h +++ b/CharacterTexture.h @@ -5,6 +5,17 @@ namespace render { class CharacterTexture : public ITexture { + public: + static CharacterTexture EngineerTexture; + static CharacterTexture MageTexture; + //TODO add the others + private: + LazySprite sprite; + const olc::vf2d uv, size; + public: + + CharacterTexture(const std::string& name, olc::vf2d size = {16, 16}); + void render(olc::TransformedView& scene, entities::Entity& entity) override; }; } diff --git a/Entity.cpp b/Entity.cpp index dbf443ebb5926230c87df17012cfa04f7427d673..531a4484a62cd72844f2a70d836012c761d059fb 100644 --- a/Entity.cpp +++ b/Entity.cpp @@ -25,10 +25,10 @@ namespace entities { offset *= dir; hitSize -= vf2d(ignoreDistance, ignoreDistance); if (delta.x > hitSize.x) { - offset.x = 0; + offset.y = 0; } if (delta.y > hitSize.y) { - offset.y = 0; + offset.x = 0; } return offset; } diff --git a/LivingEntity.cpp b/LivingEntity.cpp index e9b16508881197abfccfd183d3fbf440a97b2826..f6e10caa0c6891b136f45c43d202aed0ba45405c 100644 --- a/LivingEntity.cpp +++ b/LivingEntity.cpp @@ -30,8 +30,12 @@ namespace entities { } } } + return offset; } + LivingEntity::LivingEntity(olc::vf2d pos) + : Entity(pos), timeUntilNextPhase(0), direction(0), anim_phase(0) {} + bool LivingEntity::damage(int damage, Entity& attacker) { if (isAlive()) { @@ -66,6 +70,11 @@ namespace entities { return direction; } + char LivingEntity::getAnimPhase() const + { + return this->anim_phase; + } + void LivingEntity::tick(GameClient& client, float deltaT, std::shared_ptr<Entity>& shared_this) { timeUntilNextPhase += deltaT; @@ -80,5 +89,10 @@ namespace entities { pos += offset; } + LivingEntity* LivingEntity::getAsLivingEntity() + { + return this; + } + const float LivingEntity::phaseLength = 0.5; } \ No newline at end of file diff --git a/LivingEntity.h b/LivingEntity.h index a1b358f238e9ea933173d43a7cc4454cb8ba8f4f..2e7999efa86643f0aaacd3ce343dfec4ea880728 100644 --- a/LivingEntity.h +++ b/LivingEntity.h @@ -23,5 +23,7 @@ namespace entities { void tick(GameClient& client, float deltaT, std::shared_ptr<Entity>& shared_this) override; //TODO + + LivingEntity* getAsLivingEntity() override; }; } diff --git a/PlayerEntity.cpp b/PlayerEntity.cpp index 0791d6dc1be943af8f163c1af9c9dca806015519..dca29ac49526d9300487ec8d1a9f1447fa53d6fc 100644 --- a/PlayerEntity.cpp +++ b/PlayerEntity.cpp @@ -1,7 +1,30 @@ #include "PlayerEntity.h" -entities::PlayerEntity::PlayerEntity(olc::vf2d pos, const std::string& name) - : LivingEntity(pos), name(name) -{ - -} +#include "mainGame.h" + +using namespace olc; + +namespace entities { + + render::ITexture& PlayerEntity::getTexture() + { + return this->texture; + } + + void PlayerEntity::tick(GameClient& client, float deltaT, std::shared_ptr<Entity>& shared_this) + { + vf2d newSpeed = { 0, 0 }; + if (client.GetKey(olc::Key::A).bHeld) newSpeed -= {-1, 0}; + if (client.GetKey(olc::Key::S).bHeld) newSpeed -= {0, 1}; + if (client.GetKey(olc::Key::D).bHeld) newSpeed -= {1, 0}; + if (client.GetKey(olc::Key::W).bHeld) newSpeed -= {0, -1}; + this->speed = newSpeed; + LivingEntity::tick(client, deltaT, shared_this); + } + + PlayerEntity::PlayerEntity(olc::vf2d pos, render::ITexture& skin, const std::string& name) + : LivingEntity(pos), name(name), texture(skin) + { + + } +} \ No newline at end of file diff --git a/PlayerEntity.h b/PlayerEntity.h index eb27dbae12ee8078a9c7b98ab68cb33f2036a646..5d84c713eb590100224d41cd1b0e97c6d2317083 100644 --- a/PlayerEntity.h +++ b/PlayerEntity.h @@ -8,10 +8,10 @@ namespace entities { { private: std::string name; + render::ITexture& texture; + render::ITexture& getTexture() override; public: void tick(GameClient& client, float deltaT, std::shared_ptr<Entity>& shared_this) override; - PlayerEntity(olc::vf2d pos, const std::string& name = "Player"); + PlayerEntity(olc::vf2d pos, render::ITexture& skin, const std::string& name = "Player"); }; } - -entities::PlayerEntity* = new entities::PlayerEntity(); \ No newline at end of file diff --git a/TestGenerator.cpp b/TestGenerator.cpp index ec45f2c8c9bc7b6698db29ace2fb4c18931df956..ee0244d040a6d5a1b71fde14f7372bbef578183b 100644 --- a/TestGenerator.cpp +++ b/TestGenerator.cpp @@ -2,6 +2,8 @@ #include "WallEntity.h" #include "mainGame.h" +#include "PlayerEntity.h" +#include "CharacterTexture.h" using namespace entities; using namespace std; @@ -16,5 +18,6 @@ void TestGenerator::generate(GameClient& client) client += make_shared<WallEntity>(*new WallEntity(olc::vf2d(-2, -1))); client += make_shared<WallEntity>(*new WallEntity(olc::vf2d(-1, -1))); client += make_shared<WallEntity>(*asd); - + + client += make_shared<PlayerEntity>(*new PlayerEntity({ 0, -4 }, render::CharacterTexture::EngineerTexture)); } diff --git a/mainGame.cpp b/mainGame.cpp index 2ccbcebfc993ab6403245409be846fcc9589695e..6e9b59a9c05040cd8e48df637afda08102bf9c70 100644 --- a/mainGame.cpp +++ b/mainGame.cpp @@ -54,7 +54,7 @@ bool GameClient::OnUserUpdate(float fElapsedTime) //return false if it want to exit. for(std::shared_ptr<Entity>& entity : this->getEntities()){ - entity->tick(*this, fElapsedTime); + entity->tick(*this, fElapsedTime, entity); } entities.finalizeAdd(); diff --git a/x64/Release/2d-game.log b/x64/Release/2d-game.log index 769525bdd7738c8a13ce2e5ae8e55acc8d908c9d..9db3760c7a02db531e7df631b328ae513fe56ecc 100644 --- a/x64/Release/2d-game.log +++ b/x64/Release/2d-game.log @@ -1,9 +1,17 @@ - TestGenerator.cpp - WallEntity.cpp - WallTexture.cpp + Entity.cpp +C:\Users\kosmx\Documents\GitHub\2d-game\Entity.cpp(20,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(20,20): warning C4244: 'argument': conversion from 'int' to 'T', possible loss of data + with + [ + T=float + ] Generating code - 313 of 2009 functions (15.6%) were compiled, the rest were copied from previous compilation. - 4 functions were new in current compilation + 1 of 2184 functions (<0.1%) were compiled, the rest were copied from previous compilation. + 0 functions were new in current compilation 1 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