diff --git a/src/main.cpp b/src/main.cpp index 883f076..43d5ed0 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -26,33 +26,35 @@ int main() { SetTargetFPS(60); - GenDefaultTexture(); + GenDefaultTexture(); - Map gamemap; - gamemap.Init(); + Map gamemap; + gamemap.Init(); - raylib::Rectangle playerrect{screenWidth / 2.0f, screenHeight / 2.0f,64,64}; - Entity player{1, playerrect, "MainPlayer", 100, 100}; - player.controller = PlayerController; - player.spriteid = 0; + raylib::Rectangle playerrect{screenWidth / 2.0f, screenHeight / 2.0f,64,64}; + Entity player{1, playerrect, "MainPlayer", 100, 100}; + player.controller = PlayerController; + player.spriteid = 0; - entityTextures.LoadImage("./src/assets/playersprite.png"); - tileTextures.LoadImage("./src/assets/BasicTile.png"); - tileTextures.LoadImage("./src/assets/rock1.png"); + entityTextures.LoadImage("./src/assets/playersprite.png"); + tileTextures.LoadImage("./src/assets/BasicTile.png"); + tileTextures.LoadImage("./src/assets/rock1.png"); - Camera2D camera = { 0 }; - camera.offset = { screenWidth/2.0f, screenHeight/2.0f }; - camera.rotation = 0.0f; - camera.zoom = 1.0f; + Camera2D camera = { 0 }; + camera.offset = { screenWidth/2.0f, screenHeight/2.0f }; + camera.rotation = 0.0f; + camera.zoom = 1.0f; - Vector2 storedPosition = { 0, 0 }; - bool shouldDrawPlaceholder = false; + Vector2 storedPosition = { 0, 0 }; + bool shouldDrawPlaceholder = false; - float zoom = 1; + float zoom = 1; + + int chunkx = 0; + int chunky = 0; - while (!window.ShouldClose()) - { + while (!window.ShouldClose()) { float deltatime = GetFrameTime(); player.Update(deltatime); @@ -89,14 +91,20 @@ int main() { //} if (IsMouseButtonReleased(MOUSE_BUTTON_LEFT)) { - storedPosition = GetMousePosition(); - shouldDrawPlaceholder = true; - } + storedPosition = GetMousePosition(); + storedPosition.x += camera.target.x; + storedPosition.y += camera.target.y; - if (shouldDrawPlaceholder) { - storedPosition.x = camera.target.x - screenWidth / 2.0f + storedPosition.x; - storedPosition.y = camera.target.y - screenHeight / 2.0f + storedPosition.y; - gamemap.layers[1].chunks[Vector2{storedPosition.x / 512, storedPosition.y / 512}].tiles[0].spriteid = 1; + chunkx = storedPosition.x / 8 / 64; + chunky = storedPosition.y / 8 / 64; + + + int tileindex = (int)storedPosition.x % (8*64) / 64 + 8 * ((int)storedPosition.y % (8*64) / 64); + + std::cout << "placed at: " << chunkx << ", " << chunky << ". index: " << tileindex << "\n"; + + + gamemap.layers[0].chunks[Coordinate{chunkx, chunky}].tiles[tileindex].spriteid = 1; } @@ -111,6 +119,7 @@ int main() { player.Draw(); + EndMode2D(); EndDrawing(); } diff --git a/src/tiles/tilebase.cpp b/src/tiles/tilebase.cpp index 919b24e..a8f358d 100644 --- a/src/tiles/tilebase.cpp +++ b/src/tiles/tilebase.cpp @@ -1,6 +1,17 @@ #include "tilebase.hpp" #include "iostream" +#include + +Coordinate::Coordinate() { + x = 0; + y = 0; +} + +Coordinate::Coordinate(int X, int Y) { + x = X; + y = Y; +} // ----------------- Map ----------------- Map::Map() @@ -18,7 +29,7 @@ Layer::Layer() : id(0), DEBUG(false), owner(nullptr) {} // ----------------- Chunk ----------------- -Chunk::Chunk(Vector2 pos, Layer* Owner) +Chunk::Chunk(Coordinate pos, Layer* Owner) : position(pos), owner(Owner) { tiles.reserve(64); @@ -49,13 +60,13 @@ Tile::Tile() : id(0), spriteid(1), animationframe(0), owner(nullptr) {} // ----------------- Vector2Hash / Eq ----------------- -std::size_t Vector2Hash::operator()(const Vector2& v) const noexcept { - std::size_t h1 = std::hash{}((int)v.x); - std::size_t h2 = std::hash{}((int)v.y); +std::size_t CoordinateHash::operator()(const Coordinate& c) const noexcept { + std::size_t h1 = std::hash{}(c.x); + std::size_t h2 = std::hash{}(c.y); return h1 ^ (h2 << 1); } -bool Vector2Eq::operator()(const Vector2& a, const Vector2& b) const noexcept { +bool CoordinateEq::operator()(const Coordinate& a, const Coordinate& b) const noexcept { return ((int)a.x == (int)b.x && (int)a.y == (int)b.y); } @@ -74,7 +85,7 @@ void Chunk::Draw(raylib::Rectangle aera) { for (int i = 0; i < 64; i++) { 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}; + raylib::Rectangle box{x * 64 + (float)position.x * 8 * 64, y * 64 + (float)position.y * 8 * 64, 64, 64}; if (box.CheckCollision(aera)) { tiles[i].Draw(); @@ -84,7 +95,7 @@ void Chunk::Draw(raylib::Rectangle aera) { void Layer::Draw(raylib::Rectangle aera) { for (auto it = chunks.begin(); it != chunks.end(); ++it) { - raylib::Rectangle box{it->first.x * 64 * 8, it->first.y * 64 * 8, 64 * 8, 64 * 8}; + raylib::Rectangle box{(float)(it->first.x * 64 * 8), (float)(it->first.y * 64 * 8), 64 * 8, 64 * 8}; if (box.CheckCollision(aera)) { it->second.Draw(aera); @@ -103,8 +114,8 @@ void Map::Init() { for (int y = -1; y < 2; y++) { layers[0].chunks.emplace( std::piecewise_construct, - std::forward_as_tuple(Vector2{(float)x, (float)y}), // key - std::forward_as_tuple(Vector2{(float)x, (float)y}, &layers[0]) // value: construct Chunk here + std::forward_as_tuple(Coordinate{x, y}), // key + std::forward_as_tuple(Coordinate{x, y}, &layers[0]) // value: construct Chunk here ); } } diff --git a/src/tiles/tilebase.hpp b/src/tiles/tilebase.hpp index e68e597..24a1bf1 100644 --- a/src/tiles/tilebase.hpp +++ b/src/tiles/tilebase.hpp @@ -12,6 +12,13 @@ struct Chunk; struct Layer; struct Map; +struct Coordinate { + int x, y; + + Coordinate(); + Coordinate(int X, int Y); +}; + struct Tile { ID id; ID spriteid; @@ -26,26 +33,26 @@ struct Tile { }; struct Chunk { - raylib::Vector2 position; + Coordinate position; std::vector tiles; // dynamic instead of fixed array Layer* owner; void Draw(raylib::Rectangle aera); - Chunk(Vector2 pos, Layer* Owner); + Chunk(Coordinate pos, Layer* Owner); Chunk(); // default constructor }; -struct Vector2Hash { - std::size_t operator()(const Vector2& v) const noexcept; +struct CoordinateHash { + std::size_t operator()(const Coordinate& c) const noexcept; }; -struct Vector2Eq { - bool operator()(const Vector2& a, const Vector2& b) const noexcept; +struct CoordinateEq { + bool operator()(const Coordinate& a, const Coordinate& b) const noexcept; }; struct Layer { - std::unordered_map chunks; + std::unordered_map chunks; ID id; bool DEBUG; Map* owner; @@ -67,7 +74,7 @@ enum Actions{ struct packet { Actions action; - Vector2 position; + Coordinate position; int data; };