From 9b1dbdd1b5bfd8b47e2e421dbf25a3363b460fe1 Mon Sep 17 00:00:00 2001
From: KosmX <kosmx.mc@gmail.com>
Date: Thu, 6 May 2021 00:17:57 +0200
Subject: [PATCH] Entity init logic

---
 2d-game.vcxproj                               |  2 +
 2d-game.vcxproj.filters                       |  6 +++
 Entity.cpp                                    | 20 +++++++---
 Entity.h                                      |  7 +++-
 ITexture.h                                    |  1 +
 SimpleSprite.cpp                              |  2 +-
 WallEntity.cpp                                | 31 ++++++++++++++++
 WallEntity.h                                  | 10 ++++-
 WallTexture.cpp                               | 37 +++++++++++++++++++
 WallTexture.h                                 | 20 ++++++++++
 mainGame.cpp                                  |  8 +++-
 x64/Release/2d-game.log                       | 23 +++++++++++-
 .../2d-game.vcxproj.FileListAbsolute.txt      |  1 +
 13 files changed, 156 insertions(+), 12 deletions(-)
 create mode 100644 WallTexture.cpp
 create mode 100644 WallTexture.h

diff --git a/2d-game.vcxproj b/2d-game.vcxproj
index 6e89048..7cac94e 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 9cf0a13..5c77d05 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 7893c98..42e55b8 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 f823398..1ef293b 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 8cb504b..575963f 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 ef87941..6150478 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 4c161e9..53fb9a8 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 280b8fe..a863da4 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 0000000..e611ce6
--- /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 0000000..1c8b3f3
--- /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 d9946be..1af7bb5 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 5f28270..4ab68bc 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 e69de29..7a8382a 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
-- 
GitLab