started placement, its broken though

This commit is contained in:
nosch
2025-09-03 00:08:16 +02:00
parent 41d9622ff6
commit 9ed0561040
3 changed files with 70 additions and 43 deletions
+17 -8
View File
@@ -50,9 +50,11 @@ int main() {
float zoom = 1; float zoom = 1;
int chunkx = 0;
int chunky = 0;
while (!window.ShouldClose())
{ while (!window.ShouldClose()) {
float deltatime = GetFrameTime(); float deltatime = GetFrameTime();
player.Update(deltatime); player.Update(deltatime);
@@ -90,13 +92,19 @@ int main() {
if (IsMouseButtonReleased(MOUSE_BUTTON_LEFT)) { if (IsMouseButtonReleased(MOUSE_BUTTON_LEFT)) {
storedPosition = GetMousePosition(); storedPosition = GetMousePosition();
shouldDrawPlaceholder = true; storedPosition.x += camera.target.x;
} storedPosition.y += camera.target.y;
if (shouldDrawPlaceholder) { chunkx = storedPosition.x / 8 / 64;
storedPosition.x = camera.target.x - screenWidth / 2.0f + storedPosition.x; chunky = storedPosition.y / 8 / 64;
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;
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(); player.Draw();
EndMode2D(); EndMode2D();
EndDrawing(); EndDrawing();
} }
+20 -9
View File
@@ -1,6 +1,17 @@
#include "tilebase.hpp" #include "tilebase.hpp"
#include "iostream" #include "iostream"
#include <bits/types/cookie_io_functions_t.h>
Coordinate::Coordinate() {
x = 0;
y = 0;
}
Coordinate::Coordinate(int X, int Y) {
x = X;
y = Y;
}
// ----------------- Map ----------------- // ----------------- Map -----------------
Map::Map() Map::Map()
@@ -18,7 +29,7 @@ Layer::Layer()
: id(0), DEBUG(false), owner(nullptr) {} : id(0), DEBUG(false), owner(nullptr) {}
// ----------------- Chunk ----------------- // ----------------- Chunk -----------------
Chunk::Chunk(Vector2 pos, Layer* Owner) Chunk::Chunk(Coordinate pos, Layer* Owner)
: position(pos), owner(Owner) { : position(pos), owner(Owner) {
tiles.reserve(64); tiles.reserve(64);
@@ -49,13 +60,13 @@ Tile::Tile()
: id(0), spriteid(1), animationframe(0), owner(nullptr) {} : id(0), spriteid(1), animationframe(0), owner(nullptr) {}
// ----------------- Vector2Hash / Eq ----------------- // ----------------- Vector2Hash / Eq -----------------
std::size_t Vector2Hash::operator()(const Vector2& v) const noexcept { std::size_t CoordinateHash::operator()(const Coordinate& c) const noexcept {
std::size_t h1 = std::hash<int>{}((int)v.x); std::size_t h1 = std::hash<int>{}(c.x);
std::size_t h2 = std::hash<int>{}((int)v.y); std::size_t h2 = std::hash<int>{}(c.y);
return h1 ^ (h2 << 1); 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); 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++) { for (int i = 0; i < 64; i++) {
int x = i % 8; int x = i % 8;
int y = 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)) { if (box.CheckCollision(aera)) {
tiles[i].Draw(); tiles[i].Draw();
@@ -84,7 +95,7 @@ void Chunk::Draw(raylib::Rectangle aera) {
void Layer::Draw(raylib::Rectangle aera) { void Layer::Draw(raylib::Rectangle aera) {
for (auto it = chunks.begin(); it != chunks.end(); ++it) { 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)) { if (box.CheckCollision(aera)) {
it->second.Draw(aera); it->second.Draw(aera);
@@ -103,8 +114,8 @@ void Map::Init() {
for (int y = -1; y < 2; y++) { for (int y = -1; y < 2; y++) {
layers[0].chunks.emplace( layers[0].chunks.emplace(
std::piecewise_construct, std::piecewise_construct,
std::forward_as_tuple(Vector2{(float)x, (float)y}), // key std::forward_as_tuple(Coordinate{x, y}), // key
std::forward_as_tuple(Vector2{(float)x, (float)y}, &layers[0]) // value: construct Chunk here std::forward_as_tuple(Coordinate{x, y}, &layers[0]) // value: construct Chunk here
); );
} }
} }
+15 -8
View File
@@ -12,6 +12,13 @@ struct Chunk;
struct Layer; struct Layer;
struct Map; struct Map;
struct Coordinate {
int x, y;
Coordinate();
Coordinate(int X, int Y);
};
struct Tile { struct Tile {
ID id; ID id;
ID spriteid; ID spriteid;
@@ -26,26 +33,26 @@ struct Tile {
}; };
struct Chunk { struct Chunk {
raylib::Vector2 position; Coordinate position;
std::vector<Tile> tiles; // dynamic instead of fixed array std::vector<Tile> tiles; // dynamic instead of fixed array
Layer* owner; Layer* owner;
void Draw(raylib::Rectangle aera); void Draw(raylib::Rectangle aera);
Chunk(Vector2 pos, Layer* Owner); Chunk(Coordinate pos, Layer* Owner);
Chunk(); // default constructor Chunk(); // default constructor
}; };
struct Vector2Hash { struct CoordinateHash {
std::size_t operator()(const Vector2& v) const noexcept; std::size_t operator()(const Coordinate& c) const noexcept;
}; };
struct Vector2Eq { struct CoordinateEq {
bool operator()(const Vector2& a, const Vector2& b) const noexcept; bool operator()(const Coordinate& a, const Coordinate& b) const noexcept;
}; };
struct Layer { struct Layer {
std::unordered_map<Vector2, Chunk, Vector2Hash, Vector2Eq> chunks; std::unordered_map<Coordinate, Chunk, CoordinateHash, CoordinateEq> chunks;
ID id; ID id;
bool DEBUG; bool DEBUG;
Map* owner; Map* owner;
@@ -67,7 +74,7 @@ enum Actions{
struct packet { struct packet {
Actions action; Actions action;
Vector2 position; Coordinate position;
int data; int data;
}; };