From 0432d6932c169d902358ec80fcd89dc965b3df98 Mon Sep 17 00:00:00 2001 From: Rendo Date: Wed, 11 Mar 2026 18:48:29 +0500 Subject: [PATCH] Grid base --- assets/map | 20 ++++++++++++++++++++ assets/sprites/wall.png | Bin 0 -> 134 bytes compiler_flags.txt | 1 - makefile | 5 +++-- src/components.cpp | 6 +++--- src/components.h | 11 ++++++----- src/main.cpp | 4 ++++ src/maploader.cpp | 8 ++++++++ src/maploader.h | 6 ++++++ src/world.cpp | 23 ++++++++++++++++++++++- src/world.h | 11 +++++++++-- 11 files changed, 81 insertions(+), 14 deletions(-) create mode 100644 assets/map create mode 100644 assets/sprites/wall.png create mode 100644 src/maploader.cpp create mode 100644 src/maploader.h diff --git a/assets/map b/assets/map new file mode 100644 index 0000000..1ece295 --- /dev/null +++ b/assets/map @@ -0,0 +1,20 @@ +######### ######### +# # +# # +# # +# P # +# # +# # +# # + + +# # +# # +# # +# # +# # +# # +# # +# # +# # +######### ######### diff --git a/assets/sprites/wall.png b/assets/sprites/wall.png new file mode 100644 index 0000000000000000000000000000000000000000..b9849846b12740ac66738f1abc7eba12e9af8730 GIT binary patch literal 134 zcmeAS@N?(olHy`uVBq!ia0vp^0wB!61|;P_|4#%`jKx9jP7LeL$-D$|Sc;uILpXq- zh9ji|$T#qGaSXBOO-_06=l}dhrUNdWX>33sX7SouS=rNSq2BTvtc%%tqzo*05@bHy a;b2%5&#u2K_;M0ZFN3G6pUXO@geCwqnj|m) literal 0 HcmV?d00001 diff --git a/compiler_flags.txt b/compiler_flags.txt index 63dd840..c518829 100644 --- a/compiler_flags.txt +++ b/compiler_flags.txt @@ -2,4 +2,3 @@ -lraylib -lstdc++ -lm --v diff --git a/makefile b/makefile index 5a71e4e..cffd6c4 100644 --- a/makefile +++ b/makefile @@ -1,9 +1,10 @@ -CFLAGS=$(shell cat compiler_flags.txt) +CP=clang +CFLAGS=$(shell cat compiler_flags.txt | tr '\n' ' ') FILES=$(wildcard src/*.cpp) build/raylib-test-linux.x86_64 : $(FILES) mkdir -p build - clang $(FILES) $(CFLAGS) -o build/raylib-test-linux.x86_64 + $(CP) $(FILES) $(CFLAGS) -o build/raylib-test-linux.x86_64 clean : diff --git a/src/components.cpp b/src/components.cpp index b3e44e7..84addd9 100644 --- a/src/components.cpp +++ b/src/components.cpp @@ -5,7 +5,7 @@ // Pacman definitions -Rectangle Pacman::getTextureRect(){ +Rectangle Pacman::getTextureRect() const{ Rectangle result; result.x = 0; result.y = facing*16; @@ -53,7 +53,7 @@ void Pacman::tick() { } } -void Pacman::draw() { +void Pacman::draw() const { // Drawing DrawTextureRec(this->texture, this->getTextureRect(), this->position, WHITE); } @@ -72,7 +72,7 @@ void Wall::ready() { // Set neigbors depending on state } -void Wall::draw() { +void Wall::draw() const { // Draw based on neighbors } diff --git a/src/components.h b/src/components.h index b1f4dfe..2b31aca 100644 --- a/src/components.h +++ b/src/components.h @@ -8,9 +8,11 @@ class Entity{ public: virtual ~Entity(){} + Vector2 position; + virtual void ready() {} virtual void process() {} - virtual void draw() {} + virtual void draw() const {} virtual void tick() {} }; @@ -19,16 +21,15 @@ class Pacman : public Entity{ const int speed = 16; private: Texture2D texture; - Vector2 position; int facing; - Rectangle getTextureRect(); + Rectangle getTextureRect() const; public: Pacman(); Pacman(Vector2 position); ~Pacman(); void tick() override; - void draw() override; + void draw() const override; }; class Wall : public Entity { @@ -38,7 +39,7 @@ class Wall : public Entity { Wall(); ~Wall(); void ready() override; - void draw() override; + void draw() const override; }; #endif diff --git a/src/main.cpp b/src/main.cpp index 9616590..fb2b3f9 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -35,3 +35,7 @@ int main() { return 0; } + +void load_map(const char * path){ + +} diff --git a/src/maploader.cpp b/src/maploader.cpp new file mode 100644 index 0000000..a646687 --- /dev/null +++ b/src/maploader.cpp @@ -0,0 +1,8 @@ +#include "maploader.h" +#include +#include "world.h" + +void loadmap() { + +} + diff --git a/src/maploader.h b/src/maploader.h new file mode 100644 index 0000000..f27ac8b --- /dev/null +++ b/src/maploader.h @@ -0,0 +1,6 @@ +#ifndef MAPLOADER_H +#define MAPLOADER_H + +void loadmap(); + +#endif diff --git a/src/world.cpp b/src/world.cpp index 641330d..73fc1a9 100644 --- a/src/world.cpp +++ b/src/world.cpp @@ -21,6 +21,8 @@ void World::setup(){ void World::process(){ this->clock += GetFrameTime(); + this->update_grid(); + for(int i = 0; i < this->entities.size(); i++) { this->entities[i]->process(); if (this->clock>=this->seconds_per_tick) { @@ -33,12 +35,31 @@ void World::process(){ } } -void World::draw() { +void World::draw() const { for(int i = 0; i < this->entities.size(); i++) { this->entities[i]->draw(); } } +int indexify_position(Vector2 vector){ + return (int)(vector.x / 16.0) + GRID_COLUMNS * (int)(vector.y / 16.0); +} + +void World::update_grid() { + for(int i = 0; i < GRID_CAPACITY; i++) + this->grid[i] = nullptr; + + for(int i = 0; i < this->entities.size(); i++) + { + int indexified_position = indexify_position(this->entities[i]->position); + if (indexified_position < 0 || indexified_position >= GRID_CAPACITY) + continue; + grid[indexified_position] = this->entities[i]; + } +} + + + World create_world_with(float seconds_per_tick){ World result = World(); diff --git a/src/world.h b/src/world.h index 5c2fbe9..1a7ed51 100644 --- a/src/world.h +++ b/src/world.h @@ -4,6 +4,10 @@ #include "components.h" #include +const int GRID_ROWS = 20; +const int GRID_COLUMNS = 20; +const int GRID_CAPACITY = GRID_COLUMNS*GRID_ROWS; + /// Class that holds information about game world class World { public: @@ -11,7 +15,8 @@ class World { ~World(); std::vector entities; /// Main subjects of game world. - float seconds_per_tick; /// Internal clock speed + Entity* grid[GRID_CAPACITY]; /// Grid representation + float seconds_per_tick; /// Internal clock speed void setup(); /// Sets up game world. /// Should be called once when entites are set. @@ -19,14 +24,16 @@ class World { void process(); /// Should be called every frame. Calls /// process() on every entity - void draw(); /// Should be called at the end of frame. + void draw() const; /// Should be called at the end of frame. /// Calls draw() on every entity private: float clock; + void update_grid(); }; static World world; /// World singleton World create_world_with(float seconds_per_tick); +int indexify_position(Vector2 vector); #endif