added a tilesystem, please look at it lol and make it more efficient in drawing so it draws whats needed.

This commit is contained in:
nosch
2025-09-01 20:52:19 +02:00
parent 8ac12f71cc
commit 29c8801ada
2 changed files with 137 additions and 0 deletions
+69
View File
@@ -0,0 +1,69 @@
#include "tilebase.hpp"
// ----------------- Map -----------------
Map::Map()
: worldtime(0), DEBUG(false) {
for (int i = 0; i < 4; i++) {
layers[i] = Layer(i, this);
}
}
// ----------------- Layer -----------------
Layer::Layer(ID Id, Map* Owner)
: id(Id), DEBUG(false), owner(Owner) {}
Layer::Layer()
: id(0), DEBUG(false), owner(nullptr) {}
// ----------------- Chunk -----------------
Chunk::Chunk(Layer* Owner)
: position(0, 0), owner(Owner) {
}
Chunk::Chunk()
: position(0, 0), owner(nullptr) {}
// ----------------- Tile -----------------
Tile::Tile(ID Id, ID Spriteid, Chunk* Owner)
: id(Id), spriteid(Spriteid), animationframe(0), owner(Owner) {}
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<float>{}(v.x);
std::size_t h2 = std::hash<float>{}(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);
}
void Tile::Draw() {
int posx = owner->position.x * 64 * 8 + id % 8;
int posy = owner->position.y * 64 * 8 + id / 8;
DrawTextureEx(tileTextures.GetTexture(spriteid), Vector2({(float)posx,(float)posy}), 0, 1, WHITE);
}
void Chunk::Draw() {
for (int i = 0; i < 64; i++) {
tiles[i].Draw();
}
}
void Layer::Draw() {
for (auto it = chunks.begin(); it != chunks.end(); ++it) {
it->second.Draw();
}
}
void Map::Draw() {
for (int i = 0; i < 4; i++) {
layers[i].Draw();
}
}
+68
View File
@@ -0,0 +1,68 @@
#ifndef TILEBASE_HPP
#define TILEBASE_HPP
#include "../render/textures.hpp"
#include <unordered_map>
#include <vector>
#include <array>
// Forward declarations
struct Chunk;
struct Layer;
struct Map;
struct Tile {
ID id;
ID spriteid;
int animationframe;
std::vector<int> inventory;
Chunk* owner; // pointer instead of reference
void Draw();
void Update(float deltatime);
Tile(ID Id, ID spriteId, Chunk* owner);
Tile(); // default constructor (needed for containers)
};
struct Chunk {
raylib::Vector2 position;
Tile tiles[64]; // dynamic instead of fixed array
Layer* owner;
void Draw();
Chunk(Layer* Owner);
Chunk(); // default constructor
};
struct Vector2Hash {
std::size_t operator()(const Vector2& v) const noexcept;
};
struct Vector2Eq {
bool operator()(const Vector2& a, const Vector2& b) const noexcept;
};
struct Layer {
std::unordered_map<Vector2, Chunk, Vector2Hash, Vector2Eq> chunks;
ID id;
bool DEBUG;
Map* owner;
void Draw();
Layer(ID Id, Map* Owner);
Layer(); // default constructor
};
struct Map {
std::array<Layer, 4> layers; // ground/water/void/decoration
int worldtime;
bool DEBUG;
Map();
void Draw();
};
#endif // !TILEBASE_HPP