From 904983d6338316dbae885395ab5d549d8c821a94 Mon Sep 17 00:00:00 2001 From: nosch Date: Tue, 2 Sep 2025 01:02:54 +0200 Subject: [PATCH] started implementing the tilesystem, rendering is broken, all chunks are at 0,0 --- src/assets/conveyor1.png | Bin 0 -> 296 bytes src/assets/rock1.png | Bin 0 -> 682 bytes src/main.cpp | 39 ++++++++++++++++--------- src/tiles/tilebase.cpp | 61 ++++++++++++++++++++++++++++++--------- src/tiles/tilebase.hpp | 26 ++++++++++++++--- 5 files changed, 95 insertions(+), 31 deletions(-) create mode 100644 src/assets/conveyor1.png create mode 100644 src/assets/rock1.png diff --git a/src/assets/conveyor1.png b/src/assets/conveyor1.png new file mode 100644 index 0000000000000000000000000000000000000000..2e9e389bc6a962a55f6836bce69c574692ab1607 GIT binary patch literal 296 zcmeAS@N?(olHy`uVBq!ia0vp^4j|0I1|(Ny7TyC=jKx9jP7LeL$-D$|9(lSrhE&XX zdvhc2Ap;4Qhxa=SZ!|U;N;J*dDAA#N<8X-Mi3c9ry3w| zDLC<^(2~br>y}wB;45LA#e9J^gU>;Hfh9w1!!rh=1y28$@tyj-dhwNeGhKtfe{$^W z*kK!gwdMHJ5Z>m}(*JfY_o{aanVsgidv`mt3F{wjCf-v2y^N^=Px%WJyFpRCt{2m_KXVKp2M~mwuSyhAcMC*p{?oI&>&>zoq#G2GhY5`VEprdmuxG zX7G@-NoXlJ*kFun`fb#qIrVhbpVR3ineskhuw?1(p5MLJNdiF-1VIo4K@bE%5ClOG z1VIo4K@bE%5ClOG{%3kE^bzmC7+UQ-CH&>`vhd(HM@ROqRs#vySq$aE0MqF-_qWrN z6aBT7AVPFfC|)F6L|Z}BZ9L8r_m$4#A&AC8sMGM4d`NMVCPVOTv^9B6Qz1MG(MZ$s zi^Za`wyOjX8=uyBc5q;iA;?%BI~)@O+@s37cn*d z8f7QOwm86#yPwJT@npn@<740cOEJs;em2_*0O0KW0z5+~Iw>V@O52PlBVD2Z3STAo~%bO^rc>nHgvh7>`Q2sR7Gq3cTF6ALz30mG4r^jJ)_PdrhsVff^ zUx@6Iyp2!!L!dW8UE?bc6>f!harPg?p~edlmkQ_&heO}?8^O9&uJxky4|jnTRnRRB Q`~Uy|07*qoM6N<$f&$V!>i_@% literal 0 HcmV?d00001 diff --git a/src/main.cpp b/src/main.cpp index 17f04b5..0384b79 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -1,6 +1,7 @@ #include "../include/raylib-cpp.hpp" #include "entities/entitybase.hpp" #include "render/textures.hpp" +#include "tiles/tilebase.hpp" void PlayerController(Entity &e, float deltatime) { raylib::Vector2 acceleration = {0,0}; @@ -25,7 +26,10 @@ int main() { SetTargetFPS(60); - Texture Texturemissing = GenDefaultTexture(); + GenDefaultTexture(); + + Map gamemap; + gamemap.Init(); raylib::Rectangle playerrect{screenWidth / 2.0f, screenHeight / 2.0f,64,64}; Entity player{1, playerrect, "MainPlayer", 100, 100}; @@ -34,7 +38,7 @@ int main() { entityTextures.LoadImage("./src/assets/playersprite.png"); tileTextures.LoadImage("./src/assets/BasicTile.png"); - raylib::Texture placeholder("./src/assets/Dirt_01.png"); + raylib::Texture placeholder("./src/assets/rock1.png"); Camera2D camera = { 0 }; camera.offset = { screenWidth/2.0f, screenHeight/2.0f }; @@ -67,18 +71,27 @@ int main() { int offsetX = ((int)camera.target.x % bg.width); int offsetY = ((int)camera.target.y % bg.height); - for (int i = -1; i <= GetScreenWidth() / bw + 1; i++) { - for (int j = -1; j <= GetScreenHeight() / bh + 1; j++) { - DrawTexture( - bg, - camera.target.x - offsetX - screenWidth / 2.0f + i * bw, - camera.target.y - offsetY - screenHeight / 2.0f + j * bh, - WHITE - ); - } - } + //for (int i = -1; i <= GetScreenWidth() / bw + 1; i++) { + // for (int j = -1; j <= GetScreenHeight() / bh + 1; j++) { + // DrawTexture( + // bg, + // camera.target.x - offsetX - screenWidth / 2.0f + i * bw, + // camera.target.y - offsetY - screenHeight / 2.0f + j * bh, + // WHITE + // ); + // } + //} - if (IsMouseButtonDown(MOUSE_BUTTON_LEFT)) placeholder.Draw(GetMousePosition().x, GetMousePosition().y, WHITE); + //if (IsMouseButtonDown(MOUSE_BUTTON_LEFT)) placeholder.Draw(GetMousePosition().x, GetMousePosition().y, WHITE); + + raylib::Rectangle fov{ + camera.target.x - screenWidth / 2 * camera.zoom, + camera.target.y - screenHeight / 2 * camera.zoom, + screenWidth * camera.zoom, + screenHeight * camera.zoom, + }; + + gamemap.Draw(fov); player.Draw(); diff --git a/src/tiles/tilebase.cpp b/src/tiles/tilebase.cpp index 947cb9a..6f542da 100644 --- a/src/tiles/tilebase.cpp +++ b/src/tiles/tilebase.cpp @@ -1,5 +1,6 @@ #include "tilebase.hpp" +#include "iostream" // ----------------- Map ----------------- Map::Map() @@ -17,12 +18,23 @@ Layer::Layer() : id(0), DEBUG(false), owner(nullptr) {} // ----------------- Chunk ----------------- -Chunk::Chunk(Layer* Owner) - : position(0, 0), owner(Owner) { +Chunk::Chunk(Vector2 pos, Layer* Owner) + : position(pos), owner(Owner) { + for (int i = 0; i < 64; i++) { + tiles[i].owner = this; + tiles[i].id = i; + tiles[i].spriteid = 0; + } } Chunk::Chunk() - : position(0, 0), owner(nullptr) {} + : position(0, 0), owner(nullptr) { + for (int i = 0; i < 64; i++) { + tiles[i].owner = this; + tiles[i].id = i; + tiles[i].spriteid = 0; + } +} // ----------------- Tile ----------------- Tile::Tile(ID Id, ID Spriteid, Chunk* Owner) @@ -33,37 +45,58 @@ Tile::Tile() // ----------------- Vector2Hash / Eq ----------------- std::size_t Vector2Hash::operator()(const Vector2& v) const noexcept { - std::size_t h1 = std::hash{}(v.x); - std::size_t h2 = std::hash{}(v.y); + std::size_t h1 = std::hash{}((int)v.x); + std::size_t h2 = std::hash{}((int)v.y); return h1 ^ (h2 << 1); } bool Vector2Eq::operator()(const Vector2& a, const Vector2& b) const noexcept { - return (a.x == b.x && a.y == b.y); + return ((int)a.x == (int)b.x && (int)a.y == (int)b.y); } void Tile::Draw() { - int posx = owner->position.x * 64 * 8 + id % 8; - int posy = owner->position.y * 64 * 8 + id / 8; + int posx = owner->position.x * 64 * 8 + id % 8 * 64; + int posy = owner->position.y * 64 * 8 + id / 8 * 64; + + std::cout << "Tile at "<< posx << ", " << posy << "\n"; + std::cout << "Owner at "<< owner->position.x << ", " << owner->position.y << "\n"; DrawTextureEx(tileTextures.GetTexture(spriteid), Vector2({(float)posx,(float)posy}), 0, 1, WHITE); } -void Chunk::Draw() { +void Chunk::Draw(raylib::Rectangle aera) { for (int i = 0; i < 64; i++) { - tiles[i].Draw(); + int x = i % 8; + int y = i / 8; + raylib::Rectangle box{x * 64 + position.x * 8 * 64, y * 64 + position.y * 8 * 64, 64, 64}; + + //if (box.CheckCollision(aera)) { + tiles[i].Draw(); + //} } } -void Layer::Draw() { +void Layer::Draw(raylib::Rectangle aera) { for (auto it = chunks.begin(); it != chunks.end(); ++it) { - it->second.Draw(); + raylib::Rectangle box{it->first.x * 64 * 8, it->first.y * 64 * 8, 64 * 8, 64 * 8}; + + //if (box.CheckCollision(aera)) { + it->second.Draw(aera); + //} } } -void Map::Draw() { +void Map::Draw(raylib::Rectangle aera) { for (int i = 0; i < 4; i++) { - layers[i].Draw(); + layers[i].Draw(aera); + } +} + +void Map::Init() { + for (int x = -1; x < 2; x++) { + for (int y = -1; y < 2; y++) { + layers[0].chunks[Vector2{(float)x,(float)y}] = Chunk(Vector2{ (float)x, (float)y }, &layers[0]); + } } } diff --git a/src/tiles/tilebase.hpp b/src/tiles/tilebase.hpp index ea5dec0..57adec5 100644 --- a/src/tiles/tilebase.hpp +++ b/src/tiles/tilebase.hpp @@ -5,6 +5,7 @@ #include #include #include +#include "iostream" // Forward declarations struct Chunk; @@ -29,9 +30,9 @@ struct Chunk { Tile tiles[64]; // dynamic instead of fixed array Layer* owner; - void Draw(); + void Draw(raylib::Rectangle aera); - Chunk(Layer* Owner); + Chunk(Vector2 pos, Layer* Owner); Chunk(); // default constructor }; @@ -49,19 +50,36 @@ struct Layer { bool DEBUG; Map* owner; - void Draw(); + void Draw(raylib::Rectangle aera); Layer(ID Id, Map* Owner); Layer(); // default constructor }; +enum Actions{ + place, + destroy, + rotate, + open, + close, + drop, +}; + +struct packet { + Actions action; + Vector2 position; + int data; +}; + struct Map { std::array layers; // ground/water/void/decoration int worldtime; bool DEBUG; Map(); - void Draw(); + void Draw(raylib::Rectangle aera); + void Init(); + void Update(float deltatime, packet datapacket); }; #endif // !TILEBASE_HPP