Skip to content
Snippets Groups Projects
Commit 9992c549 authored by KosmX's avatar KosmX
Browse files

some more code

parent 52fb6372
No related branches found
No related tags found
No related merge requests found
......@@ -143,12 +143,15 @@
</Link>
</ItemDefinitionGroup>
<ItemGroup>
<ClCompile Include="CharacterTexture.cpp" />
<ClCompile Include="DungeonGenerator.cpp" />
<ClCompile Include="Entity.cpp" />
<ClCompile Include="game.cpp" />
<ClCompile Include="GameException.cpp" />
<ClCompile Include="LazySprite.cpp" />
<ClCompile Include="LivingEntity.cpp" />
<ClCompile Include="mainGame.cpp" />
<ClCompile Include="PlayerEntity.cpp" />
<ClCompile Include="ResourceManager.cpp" />
<ClCompile Include="SimpleSprite.cpp" />
<ClCompile Include="TestGenerator.cpp" />
......@@ -156,9 +159,11 @@
<ClCompile Include="WallTexture.cpp" />
</ItemGroup>
<ItemGroup>
<ClInclude Include="CharacterTexture.h" />
<ClInclude Include="DungeonGenerator.h" />
<ClInclude Include="DynamicArray.hpp" />
<ClInclude Include="GameException.h" />
<ClInclude Include="LivingEntity.h" />
<ClInclude Include="mainGame.h" />
<ClInclude Include="olc.h" />
<ClInclude Include="ITexture.h" />
......@@ -167,6 +172,7 @@
<ClInclude Include="LazySprite.h" />
<ClInclude Include="olcPGEX_TransformedView.h" />
<ClInclude Include="olcPixelGameEngine.h" />
<ClInclude Include="PlayerEntity.h" />
<ClInclude Include="resource.h" />
<ClInclude Include="ResourceManager.h" />
<ClInclude Include="SimpleSprite.h" />
......
......@@ -65,6 +65,15 @@
<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="PlayerEntity.cpp">
<Filter>Source Files</Filter>
</ClCompile>
</ItemGroup>
<ItemGroup>
<ClInclude Include="interfaces.h">
......@@ -118,6 +127,15 @@
<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="PlayerEntity.h">
<Filter>Header Files</Filter>
</ClInclude>
</ItemGroup>
<ItemGroup>
<None Include="README.md">
......
#include "CharacterTexture.h"
#pragma once
#include "ITexture.h"
namespace render {
class CharacterTexture :
public ITexture
{
};
}
......@@ -3,14 +3,14 @@
<Class Name="DynamicArray&lt;T&gt;" Collapsed="true">
<Position X="8.5" Y="10" Width="1.5" />
<TypeIdentifier>
<HashCode>AAAAAAACAAAAAAAEAAAggQBQAAAAAAAAAAAAAAIAAAA=</HashCode>
<HashCode>AAAAAAACAIAAAAAEAAAggQBQAAAAAAAAAAAAAAIAAAA=</HashCode>
<FileName>DynamicArray.hpp</FileName>
</TypeIdentifier>
</Class>
<Class Name="GameClient" Collapsed="true">
<Position X="1" Y="10" Width="1.5" />
<TypeIdentifier>
<HashCode>AgAAAAAAABAAAAAQAAEABAAAAABAAQQACAAAAAgAAAA=</HashCode>
<HashCode>AgAAAAAAABAAAAAAAAGABcAAAABAAQQACAAAAAiAAAA=</HashCode>
<FileName>mainGame.h</FileName>
</TypeIdentifier>
</Class>
......@@ -63,7 +63,7 @@
<FileName>WallTexture.h</FileName>
</TypeIdentifier>
</Class>
<Class Name="entities::Entity" Collapsed="true">
<Class Name="entities::Entity">
<Position X="0.5" Y="11.25" Width="1.5" />
<TypeIdentifier>
<HashCode>EABCAAAAQIAAAgAAAACAAIAAAAAIAAIEAAAAAIEQAAA=</HashCode>
......@@ -71,9 +71,9 @@
</TypeIdentifier>
</Class>
<Class Name="entities::WallEntity" Collapsed="true">
<Position X="0.5" Y="12.25" Width="1.5" />
<Position X="2.5" Y="14.25" Width="1.5" />
<TypeIdentifier>
<HashCode>AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAKAgAAAAIAAAAA=</HashCode>
<HashCode>AAAAAAAAAAgAAgAAAACAAAgAAAAAAAKAgQAgAIAAAAA=</HashCode>
<FileName>WallEntity.h</FileName>
</TypeIdentifier>
</Class>
......
#include "Entity.h"
#include <sstream>
using namespace std;
using namespace olc;
......@@ -51,6 +52,14 @@ namespace entities {
getTexture().render(scene, *this);
}
std::string Entity::getName() const
{
stringstream s;
s << "Entity#" << this;
return s.str();
}
bool Entity::isInitialized()
{
return is_initialized;
......
......@@ -8,6 +8,7 @@ class GameClient;
namespace entities {
class WallEntity;
class LivingEntity;
class Entity
{
......@@ -31,11 +32,19 @@ namespace entities {
[[nodiscard]] virtual olc::vf2d getSize() const;
[[nodiscard]] virtual bool isAlive() const;
virtual void tick(GameClient& client, float deltaT){}
virtual void tick(GameClient& client, float deltaT, std::shared_ptr<Entity>& shared_this){}
/**
* @param damage how much damage should it take
* @param attacker who deal the damage. not the projectile, the entity
* @return did the entity take the damage or no (like a wall)
*/
virtual bool damage(int damage, Entity& attacker) = 0;
//this shouldn't change it's state
virtual void render(olc::TransformedView& scene);
virtual std::string getName() const;
virtual ~Entity() = default;
//TODO getAs*** stuff, all virtual
......@@ -45,6 +54,10 @@ namespace entities {
return nullptr;
}
virtual LivingEntity* getAsLivingEntity()
{
return nullptr;
}
};
......
#include "LivingEntity.h"
#include "mainGame.h"
using namespace olc;
namespace entities {
vf2d LivingEntity::getHitBoxSize() const
{
return olc::vf2d(0.8, 0.8);
}
vf2d LivingEntity::collisionOffset(GameClient& client)
{
vf2d offset = { 0, 0 };
for (auto& entity : client.getEntities()) {
vf2d current = this->getCollision(*entity);
if (offset == vf2d(0, 0)) {
offset = current;
}
else if (offset != current) {
if (std::abs(offset.dot(current)) < 0.01) {
offset += current;
}
else {
if (offset.mag2() < current.mag2()) {
offset = current;
}
}
}
}
}
bool LivingEntity::damage(int damage, Entity& attacker)
{
if (isAlive()) {
this->health = std::max(0, this->health - damage);
if (health == 0) {
this->is_alive = false;
std::cout << attacker.getName() << " killed " << this->getName() << "." << std::endl;
}
return true;//basically you can do that
}
//you can't hurt dead creatures.
return false;
}
char LivingEntity::getDirection()
{
if(speed.mag2() < 0.1 ){
return direction;
}
else if(speed.x > std::abs(speed.y)){
direction = 2;
}
else if(speed.x < -std::abs(speed.y)){
direction = 1;
}
else if(speed.y > std::abs(speed.x)){
direction = 3;
}
else{
direction = 0;
}
return direction;
}
void LivingEntity::tick(GameClient& client, float deltaT, std::shared_ptr<Entity>& shared_this)
{
timeUntilNextPhase += deltaT;
if(timeUntilNextPhase > phaseLength){
anim_phase = (anim_phase + 1) % 4;
timeUntilNextPhase -= phaseLength;
}
Entity::tick(client, deltaT, shared_this);
pos += speed * deltaT;
vf2d offset = collisionOffset(client);
pos += offset;
}
const float LivingEntity::phaseLength = 0.5;
}
\ No newline at end of file
#pragma once
#include "Entity.h"
namespace entities {
class LivingEntity :
public Entity
{
private:
int health;
char direction;
char anim_phase;
float timeUntilNextPhase;
const static float phaseLength;
protected:
olc::vf2d getHitBoxSize() const override;
olc::vf2d speed;
virtual olc::vf2d collisionOffset(GameClient& client);
public:
LivingEntity(olc::vf2d pos);
bool damage(int damage, Entity& attacker) override;
char getDirection();
char getAnimPhase() const;
void tick(GameClient& client, float deltaT, std::shared_ptr<Entity>& shared_this) override; //TODO
};
}
#include "PlayerEntity.h"
entities::PlayerEntity::PlayerEntity(olc::vf2d pos, const std::string& name)
: LivingEntity(pos), name(name)
{
}
#pragma once
#include "LivingEntity.h"
namespace entities {
class PlayerEntity :
public LivingEntity
{
private:
std::string name;
public:
void tick(GameClient& client, float deltaT, std::shared_ptr<Entity>& shared_this) override;
PlayerEntity(olc::vf2d pos, const std::string& name = "Player");
};
}
entities::PlayerEntity* = new entities::PlayerEntity();
\ No newline at end of file
......@@ -40,10 +40,16 @@ namespace entities {
WallEntity::WallEntity(const olc::vf2d& pos, render::WallTexture& texture)
: Entity(pos), usedTexture(texture), neighbourID(0) {}
bool WallEntity::damage(int damage, Entity& attacker)
{
return false;
}
byte WallEntity::getNeighbourID() const
{
return neighbourID;
}
render::WallTexture WallEntity::simpleWallTexture("Objects/Wall.png", { 0, 3 });
render::WallTexture WallEntity::simpleGreenWallTexture("Objects/Wall.png", { 14, 30 });
}
\ No newline at end of file
......@@ -7,8 +7,9 @@
namespace entities {
class WallEntity : public Entity
{
protected:
public:
static render::WallTexture simpleWallTexture;
static render::WallTexture simpleGreenWallTexture;
private:
byte neighbourID;
......@@ -24,6 +25,8 @@ namespace entities {
void init(GameClient& client) override;
bool damage(int damage, Entity& attacker) override;
virtual byte getNeighbourID() const;
//This is a wall entity after all.
......
......@@ -84,7 +84,7 @@ bool GameClient::OnUserUpdate(float fElapsedTime)
//debug section
if (debug) {
scene.DrawCircle({ 0, 0 }, 1);
//scene.DrawCircle({ 0, 0 }, 1);
cout << entities.getSize() << " was ticked" << endl;
}
return true;
......
 TestGenerator.cpp
WallEntity.cpp
WallTexture.cpp
Generating code
2 of 2012 functions (<0.1%) were compiled, the rest were copied from previous compilation.
0 functions were new in current compilation
313 of 2009 functions (15.6%) were compiled, the rest were copied from previous compilation.
4 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
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment