Skip to content
Snippets Groups Projects
Commit 9b1dbdd1 authored by KosmX's avatar KosmX
Browse files

Entity init logic

parent 01122425
No related branches found
No related tags found
No related merge requests found
......@@ -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">
......
......@@ -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
#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) {
......
......@@ -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){}
......
#pragma once
#include "olcPGEX_TransformedView.h"
#include "LazySprite.h"
namespace entities {
class Entity;
......
......@@ -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);
}
#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;
}
......@@ -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
......
#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
#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;
};
}
......@@ -47,6 +47,12 @@ bool GameClient::OnUserUpdate(float fElapsedTime)
entity->tick(*this, fElapsedTime);
}
entities.finalizeAdd();
for(auto &entity : entities){
if(!entity->isInitialized()){
entity->init(*this);
}
}
return true;
}
......

\ 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
C:\Users\kosmx\Documents\GitHub\2d-game\x64\Release\2d-game.exe
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment