瀏覽代碼

working at iso game

meemknight 1 年之前
父節點
當前提交
275d2b07f7

+ 8 - 3
Pika/gameplay/containers.h

@@ -33,6 +33,10 @@ Container *getContainer(const char* name, pika::memory::MemoryArena *memoryArena
 #include "containers/threeDGameExample/threeDGameExample.h"
 #include "containers/threeDGameExample/threeDGameMenu.h"
 
+
+#include "containers/isometricGame/isometricGameEditor.h"
+
+
 #if PIKA_PRODUCTION == 1
 
 #define PIKA_ALL_CONTAINERS() \
@@ -52,9 +56,10 @@ Container *getContainer(const char* name, pika::memory::MemoryArena *memoryArena
 	PIKA_DECLARE_CONTAINER(PikaTextEditor) \
 	PIKA_DECLARE_CONTAINER(ThreeDGameExample) \
 	PIKA_DECLARE_CONTAINER(ThreeDGameMenu) \
-	PIKA_DECLARE_CONTAINER(McDungeonsGameplay) \
-	PIKA_DECLARE_CONTAINER(McDungeonsEditor) \
-	PIKA_DECLARE_CONTAINER(McDungeonsMenu)
+	PIKA_DECLARE_CONTAINER(IsometricGameEditor)
+	//PIKA_DECLARE_CONTAINER(McDungeonsGameplay) \
+	//PIKA_DECLARE_CONTAINER(McDungeonsEditor)
+	//PIKA_DECLARE_CONTAINER(McDungeonsMenu)
 
 	//PIKA_DECLARE_CONTAINER(Holloknight)\
 	//PIKA_DECLARE_CONTAINER(Mario) \

+ 258 - 0
Pika/gameplay/containers/isometricGame/isometricGameEditor.cpp

@@ -0,0 +1,258 @@
+#include <containers/isometricGame/isometricGameEditor.h>
+#include <glui/glui.h>
+
+static int blocksCount = 8;
+
+static bool pointInBox(glm::vec2 p, glm::vec4 box)
+{
+	if
+		(
+		(p.x >= box.x && p.x <= (box.x + box.z)) &&
+		(p.y >= box.y && p.y <= (box.y + box.w))
+		)
+	{
+		return true;
+	}
+	else
+	{
+		return false;
+	}
+}
+
+
+bool IsometricGameEditor::create(RequestedContainerInfo &requestedInfo, pika::StaticString<256> commandLineArgument)
+{
+	renderer.create(requestedInfo.requestedFBO.fbo);
+
+	tiles = pika::gl2d::loadTextureWithPixelPadding(PIKA_RESOURCES_PATH "isoTiles/Isometric-Tiles.png", requestedInfo, 32, true);
+
+	tilesAtlas = gl2d::TextureAtlasPadding(8, 2, tiles.GetSize().x, tiles.GetSize().y);
+
+	//fileChanged.setFile(mapFile.c_str());
+
+	loadedLevel.setInfo("Level", PIKA_RESOURCES_PATH, {".mcDungeons"});
+
+	bool created = 0;
+	if (commandLineArgument.size() > 0)
+	{
+		//editor.loadFromFile(renderer, commandLineArgument.to_string(), requestedInfo);
+		size_t s = 0;
+		pika::strlcpy(loadedLevel.file, commandLineArgument.to_string(), sizeof(loadedLevel.file));
+
+		glm::ivec3 mapSize = {};
+		if (requestedInfo.readEntireFileBinary(commandLineArgument.to_string(), &mapSize, sizeof(mapSize), 0))
+		{
+			if (requestedInfo.getFileSizeBinary(commandLineArgument.to_string().c_str(), s))
+			{
+
+				if (s == mapSize.x * mapSize.y * mapSize.z * sizeof(Block) + sizeof(mapSize))
+				{
+					created = 1;
+
+					map.init(mapSize);
+
+					requestedInfo.readEntireFileBinary(commandLineArgument.to_string().c_str(), 
+						map.mapData.data(), mapSize.x * mapSize.y * mapSize.z * sizeof(Block), sizeof(mapSize));
+
+				}
+			}
+
+
+		}
+
+		
+	}
+
+	if (!created)
+	{
+		map.init({10,10,10});
+
+		for (int i = 0; i < 10; i++)
+			for (int j = 0; j < 10; j++)
+			{
+				map.setSafe({i,0,j}, 1, 0);
+			}
+
+		map.setSafe({5,1,5}, 6, 0);
+		map.setSafe({5,2,5}, 6, 0);
+	}
+
+	
+
+
+	return true;
+}
+
+bool IsometricGameEditor::update(pika::Input input, pika::WindowState windowState, RequestedContainerInfo &requestedInfo)
+{
+
+#pragma region clear stuff
+	glClear(GL_COLOR_BUFFER_BIT);
+	renderer.updateWindowMetrics(windowState.windowW, windowState.windowH);
+#pragma endregion
+
+	float size = 100;
+
+	auto calculateBlockPos = [size](glm::ivec3 in)
+	{
+		glm::vec2 position = {};
+
+		position += glm::vec2(-size / 2.f, size / 4.f) * float(in.x);
+		position += glm::vec2(size / 2.f, size / 4.f) * float(in.z);
+
+		position += glm::vec2(0, -size / 2.f) * float(in.y);
+
+		return position;
+	};
+
+	for (int y = 0; y < map.size.y; y++)
+		for (int z = 0; z < map.size.z; z++)
+			for (int x = 0; x < map.size.x; x++)
+			{
+				auto b = map.getSafe({x,y,z});
+
+				if (b->get().x != 0)
+				{
+					glm::vec2 position = calculateBlockPos({x,y,z});
+
+					requestedInfo.consoleWrite("yes");
+					renderer.renderRectangle({position,size,size}, tiles, Colors_White, {}, 0,
+						tilesAtlas.get(b->get().x, b->get().y));
+				}
+			}
+
+
+	auto viewRect = renderer.getViewRect();
+
+	auto lerp = [](auto a, auto b, auto c)
+	{
+		return a * (1.f - c) + b * c;
+	};
+
+	glm::vec2 blockPositionScreen = lerp(glm::vec2(viewRect.x, viewRect.y),
+		glm::vec2(viewRect.x + viewRect.z, viewRect.y + viewRect.w),
+		glm::vec2(input.mouseX, input.mouseY) / glm::vec2(windowState.windowW, windowState.windowH));
+	
+
+	glm::ivec3 currentSelectedBlock{-1};
+	
+	if(input.rMouse.pressed())
+	for (int y = 0; y < map.size.y; y++)
+		for (int z = 0; z < map.size.z; z++)
+			for (int x = 0; x < map.size.x; x++)
+			{
+
+				auto b = map.getSafe({x,y,z});
+
+				if (b->get().x != 0)
+				{
+					glm::vec2 position = calculateBlockPos({x,y,z});
+					glm::vec4 box = renderer.toScreen({position, size, size});
+					//todo new functon
+
+					box.x += 1;
+					box.x /= 2.f;
+					box.x *= renderer.windowW;
+
+					box.y *= -1;
+					box.y += 1;
+					box.y /= 2.f;
+					box.y *= renderer.windowH;
+
+					box.z += 1;
+					box.z /= 2.f;
+					box.z *= renderer.windowW;
+
+					box.w *= -1;
+					box.w += 1;
+					box.w /= 2.f;
+					box.w *= renderer.windowH;
+
+					box.z = box.z - box.x;
+					box.w = box.w - box.y;
+
+					if(pointInBox(glm::vec2(input.mouseX, input.mouseY), box))
+					{
+						currentSelectedBlock = {x,y,z};
+					}
+
+				}
+
+			}
+
+	if (currentSelectedBlock.x > -1)
+	{
+		auto b = map.getSafe(currentSelectedBlock);
+		if (b)
+		{
+			b->set(0, 0);
+		}
+	}
+
+	renderer.flush();
+
+	ImGui::Begin("camera editor");
+
+	ImGui::DragFloat2("camera", &renderer.currentCamera.position[0], 2, -3200, 3200);
+
+
+	{
+		auto uv1 = tilesAtlas.get(currentBlock, 0);
+		ImGui::Image((void *)(intptr_t)tiles.id,
+			{35,35}, {uv1.x, uv1.y}, {uv1.z, uv1.w});
+	}
+
+	ImGui::NewLine();
+	{
+		for (int mCount = 0; mCount < blocksCount; mCount++)
+		{
+			auto uv1 = tilesAtlas.get(mCount, 0);
+
+			ImGui::PushID(mCount);
+			if (ImGui::ImageButton((void *)(intptr_t)tiles.id,
+				{35,35}, {uv1.x, uv1.y}, {uv1.z, uv1.w}))
+			{
+				currentBlock = mCount;
+			}
+
+			ImGui::PopID();
+
+			if ((mCount + 1) % 5 != 0)
+			{
+				ImGui::SameLine();
+			}
+
+		}
+	}
+
+	currentBlock = glm::clamp(currentBlock, 0, blocksCount-1);
+
+	ImGui::NewLine();
+
+	loadedLevel.run(requestedInfo.requestedImguiIds);
+
+	if (ImGui::Button("save map"))
+	{
+		if (requestedInfo.writeEntireFileBinary(loadedLevel.file, &map.size, sizeof(map.size)))
+		{
+			requestedInfo.appendFileBinary(loadedLevel.file, map.mapData.data(),
+				sizeof(Block) * map.mapData.size());
+
+			//toto log errors
+		}
+	}
+
+
+
+	ImGui::End();
+
+
+
+	return true;
+}
+
+void IsometricGameEditor::destruct(RequestedContainerInfo &requestedInfo)
+{
+	renderer.cleanup();
+
+}

+ 113 - 0
Pika/gameplay/containers/isometricGame/isometricGameEditor.h

@@ -0,0 +1,113 @@
+#pragma once
+
+#include <gl2d/gl2d.h>
+#include <imgui.h>
+#include <baseContainer.h>
+#include <shortcutApi/shortcutApi.h>
+#include <pikaSizes.h>
+#include <fileChanged.h>
+#include <engineLibraresSupport/engineGL2DSupport.h>
+
+
+struct IsometricGameEditor: public Container
+{
+
+	pika::FileChanged fileChanged;
+
+	gl2d::Renderer2D renderer;
+	pika::pikaImgui::FileSelector loadedLevel;
+
+	gl2d::Texture tiles;
+	gl2d::TextureAtlasPadding tilesAtlas;
+
+	static ContainerStaticInfo containerInfo()
+	{
+		ContainerStaticInfo info = {};
+		info.defaultHeapMemorySize = pika::MB(10);
+
+		info.extensionsSuported = {".isomap"};
+
+		info.requestImguiFbo = true;
+
+		return info;
+	}
+
+	bool create(RequestedContainerInfo &requestedInfo, pika::StaticString<256> commandLineArgument);
+	int currentBlock = 0;
+
+	struct Block
+	{
+		unsigned char type;
+
+		void set(unsigned char count, bool down)
+		{
+			type = count;
+			if (down)
+			{
+				type |= 0b1000'0000;
+			}
+		}
+
+		glm::ivec2 get()
+		{
+			if (type & 0b1000'0000)
+			{
+				return {type & 0b0111'1111, 1};
+			}
+			else
+			{
+				return {type, 0};
+			}
+		}
+	};
+
+	struct Map
+	{
+
+		std::vector<Block> mapData;
+
+		glm::ivec3 size = {};
+
+		void init(glm::ivec3 size)
+		{
+			mapData.clear();
+			mapData.resize(size.x * size.y * size.z);
+			this->size = size;
+		}
+
+		void setSafe(glm::ivec3 pos, unsigned char count, bool down)
+		{
+			auto get = getSafe(pos);
+			if (get) { get->set(count, down); }
+		}
+
+		void setSafe(glm::ivec3 pos, Block b)
+		{
+			auto get = getSafe(pos);
+			if (get) { *get = b; }
+		}
+
+		Block *getSafe(glm::ivec3 pos)
+		{
+			if (pos.x >= 0 && pos.y >= 0 && pos.z >= 0
+				&& pos.x < size.x && pos.y < size.y && pos.z < size.z
+				)
+			{
+				return &mapData[pos.x * size.z * size.y + pos.y * size.z + pos.z];
+			}
+			else
+			{
+				return 0;
+			}
+		}
+
+	};
+
+	Map map;
+
+	bool update(pika::Input input, pika::WindowState windowState,
+		RequestedContainerInfo &requestedInfo) override;
+
+	void destruct(RequestedContainerInfo &requestedInfo) override;
+
+};

+ 48 - 11
Pika/gameplay/containers/threeDGameExample/threeDGameExample.h

@@ -81,7 +81,7 @@ struct ThreeDGameExample: public Container
 
 	//todo add this in the renderer
 	//todo add a default material if material left blank or something
-	gl3d::Model createCubeModel(gl3d::Renderer3D &renderer, glm::vec3 color)
+	gl3d::Model createCubeModel(gl3d::Renderer3D &renderer, glm::vec3 color, float metallic = 1)
 	{
 		static float uv = 1;
 		static float cubePositionsNormals[] = {
@@ -244,7 +244,7 @@ struct ThreeDGameExample: public Container
 		20, 22, 21, 20, 23, 22, // Bottom
 		};
 
-		auto material = renderer.createMaterial(0, {color,1}, 0, 1);
+		auto material = renderer.createMaterial(0, {color,1}, 0, metallic);
 
 		return renderer.createModelFromData(material, "cube",
 			sizeof(cubePositionsNormals)/sizeof(cubePositionsNormals[0]), cubePositionsNormals,
@@ -290,7 +290,7 @@ struct ThreeDGameExample: public Container
 				0, 1 * uv,			 //uv
 		};
 
-		auto material =  renderer.createMaterial(0, {0.4,1,0.4,1}, 0, 1);
+		auto material =  renderer.createMaterial(0, {1,1,1,1}, 0, 1);
 
 		return renderer.createModelFromData(material, "plane",
 			topVer.size(), topVer.data(), ind.size(),
@@ -298,6 +298,47 @@ struct ThreeDGameExample: public Container
 	}
 
 
+	void createStalpi()
+	{
+		auto cube = createCubeModel(renderer, {0.2,0.4,0.8}, 0);
+
+		bool flip = 0;
+		for (int x = -100; x <= 100; x += 25)
+		{
+			for (int z = -100; z <= 100; z+=50)
+			{
+
+				addStalp({x, z + 25 * flip}, cube);
+
+			}
+
+			flip = !flip;
+		}
+
+	}
+
+	std::vector<gl3d::SpotLight> spotLights;
+
+	void addStalp(glm::vec2 position, gl3d::Model cube)
+	{
+		renderer.createEntity(cube, {glm::vec3{position.x, 3.5, position.y}
+			,{},{0.25,5,0.25}
+			});
+
+		renderer.createEntity(cube, {glm::vec3{position.x, 8.5, position.y}
+			,{},{3,0.25,0.25}
+			});
+
+		spotLights.push_back(renderer.createSpotLight({position.x - 1.5, 8.1, position.y},
+			glm::radians(50.f), glm::vec3(0, -1, 0), 20, 2, glm::vec3(20.f), 1.f, 0));
+
+		spotLights.push_back
+		(renderer.createSpotLight({position.x + 1.5, 8.1, position.y},
+			glm::radians(50.f), glm::vec3(0, -1, 0), 20, 2, glm::vec3(20.f), 1.f, 0));
+
+
+	}
+
 	void addEnemy(glm::vec2 position, gl3d::Model playerM, gl3d::Material mat)
 	{
 		Enemy i = {};
@@ -349,6 +390,8 @@ struct ThreeDGameExample: public Container
 
 		//pika::gl3d::loadSettingsFromFileName(renderer, PIKA_RESOURCES_PATH "/threedgame/settings.gl3d", requestedInfo);
 		editor.loadFromFile(renderer, PIKA_RESOURCES_PATH "/threedgame/settings.gl3d", requestedInfo);
+		//renderer.skyBox.color = glm::vec3(0.8f);
+
 
 		zombieMat = renderer.loadMaterial(PIKA_RESOURCES_PATH "threedgame/zombie.mtl", 0);
 		if (zombieMat.size() != 1) { return false; }
@@ -367,6 +410,8 @@ struct ThreeDGameExample: public Container
 
 		createPlayerEntity(renderer, {1,1,0});
 
+		createStalpi();
+
 		groundModel = createPlane(renderer);
 
 	
@@ -390,18 +435,10 @@ struct ThreeDGameExample: public Container
 
 		//if (0)
 		{
-			enemies.push_back(Enemy(18, 16));
-			enemies.push_back(Enemy(16, 16));
-
 			addEnemy({18,16}, playerM, zombieMat[0]);
 			addEnemy({16,16}, playerM, zombieMat[0]);
 		}
 
-		for (auto &i : enemies)
-		{
-			
-		}
-
 		return true;
 	}
 

二進制
Pika/resources/isoSaves/test1.isomap


二進制
Pika/resources/isoTiles/IsoCharacter.png


二進制
Pika/resources/isoTiles/IsoTreeLeaves.png


二進制
Pika/resources/isoTiles/Isometric-Tiles.png


二進制
Pika/resources/isoTiles/Tileaddons 2.png


二進制
Pika/resources/isoTiles/TreeOrange.png


+ 1 - 0
Pika/resources/isoTiles/credits.txt

@@ -0,0 +1 @@
+https://je1ly.itch.io/isometrictilemap

二進制
Pika/resources/isoTiles/isoTreeStump.png


二進制
Pika/resources/isoTiles/isoTreeStumpTall.png


+ 4 - 1
Pika/resources/logs.txt

@@ -1 +1,4 @@
-#2024-01-03 10:17:37: Created container: ThreeDGameExample
+#2024-01-03 15:37:45: Created container: IsometricGameEditor
+#2024-01-03 15:37:52: Destroyed continer: IsometricGameEditor #1
+#2024-01-03 15:37:53: Created container: IsometricGameEditor
+#2024-01-03 15:37:59: Destroyed continer: IsometricGameEditor #2

+ 1 - 1
Pika/resources/threedgame/settings.gl3d

@@ -1 +1 @@
-{"SSR":true,"SSRdata":{"maxRayDelta":0.7940000295639038,"maxRayStep":1.75,"maxSteps":33,"minRayStep":0.35199999809265137,"numBinarySearchSteps":7},"adaptiveResolution":false,"ambientb":1.7647058963775635,"ambientg":1.7647058963775635,"ambientr":1.7647058963775635,"chromaticAberation":false,"chromaticAberationData":{"strength":20.0,"unfocusDistance":5.0},"colorCorrection":false,"exposure":2.8289999961853027,"frustumCulling":false,"fxaa":true,"fxaaData":{"edgeDarkTreshold":0.24500000476837158,"edgeMinTreshold":0.004000000189989805,"iterations":30,"qualityMultiplyer":0.8240000009536743,"subPixelQuality":0.9259999990463257},"light subscatter":true,"normal mapping":true,"ssao":true,"ssaoData":{"bias":0.02500000037252903,"exponent":5.0,"radius":0.20000000298023224,"sampleCount":16}}
+{"SSR":true,"SSRdata":{"maxRayDelta":0.7940000295639038,"maxRayStep":1.75,"maxSteps":33,"minRayStep":0.35199999809265137,"numBinarySearchSteps":7},"adaptiveResolution":false,"ambientb":1.0,"ambientg":1.0,"ambientr":1.0,"chromaticAberation":false,"chromaticAberationData":{"strength":20.0,"unfocusDistance":5.0},"colorCorrection":false,"exposure":2.8289999961853027,"frustumCulling":false,"fxaa":true,"fxaaData":{"edgeDarkTreshold":0.24500000476837158,"edgeMinTreshold":0.004000000189989805,"iterations":30,"qualityMultiplyer":0.8240000009536743,"subPixelQuality":0.9259999990463257},"light subscatter":true,"normal mapping":true,"ssao":true,"ssaoData":{"bias":0.02500000037252903,"exponent":5.0,"radius":0.20000000298023224,"sampleCount":16}}