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
+35 -26
View File
@@ -26,33 +26,35 @@ int main() {
SetTargetFPS(60); SetTargetFPS(60);
GenDefaultTexture(); GenDefaultTexture();
Map gamemap; Map gamemap;
gamemap.Init(); gamemap.Init();
raylib::Rectangle playerrect{screenWidth / 2.0f, screenHeight / 2.0f,64,64}; raylib::Rectangle playerrect{screenWidth / 2.0f, screenHeight / 2.0f,64,64};
Entity player{1, playerrect, "MainPlayer", 100, 100}; Entity player{1, playerrect, "MainPlayer", 100, 100};
player.controller = PlayerController; player.controller = PlayerController;
player.spriteid = 0; player.spriteid = 0;
entityTextures.LoadImage("./src/assets/playersprite.png"); entityTextures.LoadImage("./src/assets/playersprite.png");
tileTextures.LoadImage("./src/assets/BasicTile.png"); tileTextures.LoadImage("./src/assets/BasicTile.png");
tileTextures.LoadImage("./src/assets/rock1.png"); tileTextures.LoadImage("./src/assets/rock1.png");
Camera2D camera = { 0 }; Camera2D camera = { 0 };
camera.offset = { screenWidth/2.0f, screenHeight/2.0f }; camera.offset = { screenWidth/2.0f, screenHeight/2.0f };
camera.rotation = 0.0f; camera.rotation = 0.0f;
camera.zoom = 1.0f; camera.zoom = 1.0f;
Vector2 storedPosition = { 0, 0 }; Vector2 storedPosition = { 0, 0 };
bool shouldDrawPlaceholder = false; bool shouldDrawPlaceholder = false;
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);
@@ -89,14 +91,20 @@ 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;
}; };