diff --git a/assets/map b/assets/map index 1fab00d..f6e4ade 100644 --- a/assets/map +++ b/assets/map @@ -1,20 +1,31 @@ -######### ######### -# # -# ####### ####### # -# # -# # P # # -# # # # # # -# #### #### # -# # # # # # - # # - # # -# # #### # # -# # # # -# # # # -# #### # # ##### # -# # # # # # -# # ### ### # # -# # # # -# # #### ##### # # -# # -######### ######### +#############--############## +# # +# # +# # +# # +# # +# # +# # +# # +# # +# # +# # +# # +# # +- - +- - +# # +# # +# # +# # +# # +# # +# # +# # +# # +# # +# # +# # +# # +# P # +############--############### diff --git a/makefile b/makefile index b095237..b45c76b 100644 --- a/makefile +++ b/makefile @@ -10,7 +10,7 @@ build/linux/raylib-test-linux-debug: $(FILES) build/linux/raylib-test-linux-release: $(FILES) mkdir -p build/linux - $(CP) $(FILES) $(CFLAGS) -o build/linux/raylib-test-linux-release + $(CP) $(FILES) $(CFLAGS) -O3 -o build/linux/raylib-test-linux-release release: build/linux/raylib-test-linux-release diff --git a/src/components.h b/src/components.h index 23f3eeb..89cd62b 100644 --- a/src/components.h +++ b/src/components.h @@ -17,6 +17,7 @@ class Entity{ virtual void tick() {} virtual void collision(Entity* with) {} void queue_free(); + bool get_free_flag(); private: bool free_queued; }; diff --git a/src/entity.cpp b/src/entity.cpp index 7df294f..7a80291 100644 --- a/src/entity.cpp +++ b/src/entity.cpp @@ -1,17 +1,12 @@ #include "components.h" -#include "world.h" void Entity::queue_free() { if (this->free_queued) return; this->free_queued = true; - World& world = get_world(); - - for(int i = 0; i < world.entities.size(); i++) - if(world.entities[i] == this) { - delete world.entities[i]; - world.entities.erase(world.entities.begin()+i); - break; - } +} + +bool Entity::get_free_flag() { + return this->free_queued; } diff --git a/src/maploader.cpp b/src/maploader.cpp index 44533d3..86a3ad2 100644 --- a/src/maploader.cpp +++ b/src/maploader.cpp @@ -38,6 +38,9 @@ void load_world(World& world,const char* path) { x = 0; y++; break; + case '-': + x++; + break; default: { Scorepoint* point = new Scorepoint; @@ -45,6 +48,7 @@ void load_world(World& world,const char* path) { world.entities.push_back(point); } x++; + break; } diff --git a/src/pacman.cpp b/src/pacman.cpp index c1b4755..38cd89c 100644 --- a/src/pacman.cpp +++ b/src/pacman.cpp @@ -37,7 +37,7 @@ void Pacman::tick() { World& world = get_world(); - if (dynamic_cast(world.grid[indexify_position(new_position)]) == nullptr){ + if (dynamic_cast(world.grid[world.indexify_position(new_position)]) == nullptr){ this->position=new_position; } @@ -77,6 +77,7 @@ void Pacman::collision(Entity* with){ Scorepoint* score = dynamic_cast(with); if (score != nullptr) { score->queue_free(); + get_world().points+=10; } } diff --git a/src/world.cpp b/src/world.cpp index e03690b..d72de2f 100644 --- a/src/world.cpp +++ b/src/world.cpp @@ -1,5 +1,6 @@ #include "world.h" #include "components.h" +#include #include #include @@ -42,11 +43,31 @@ void World::process(){ if (this->clock>=this->seconds_per_tick) { this->entities[i]->tick(); } + + // Collision check + for(int j = i+1; j < this->entities.size(); j++) + if (abs(this->entities[j]->position.x-this->entities[i]->position.x) < CELL_SIZE/2 && abs(this->entities[j]->position.y-this->entities[i]->position.y) < CELL_SIZE/2){ + this->entities[j]->collision(this->entities[i]); + this->entities[i]->collision(this->entities[j]); + } } if (this->clock>=this->seconds_per_tick) { this->clock = 0; } + + // Check for freed. Very very bad loop + bool freed_continue = true; + while (freed_continue) { + freed_continue = false; + + for(int i = 0; i < this->entities.size(); i++) + if (this->entities[i]->get_free_flag()) { + this->entities.erase(this->entities.begin()+i); + freed_continue = true; + break; + } + } } void World::draw() const { @@ -61,9 +82,9 @@ void World::draw() const { if (IsKeyDown(KEY_F4)) { std::string str = ""; - for(int i = 0; i < GRID_CAPACITY; i++) + for(int i = 0; i < this->get_capacity(); i++) { - if (i!=0 && i%GRID_COLUMNS==0) + if (i!=0 && i%this->get_columns()==0) { str += '\n'; } @@ -79,26 +100,19 @@ void World::draw() const { } -int indexify_position(Vector2 vector){ - return (int)(vector.x / 16.0) + GRID_COLUMNS * (int)(vector.y / 16.0); +int World::indexify_position(Vector2 vector){ + return (int)(vector.x / 16.0) + this->get_columns() * (int)(vector.y / 16.0); } void World::update_grid() { - for(int i = 0; i < GRID_CAPACITY; i++) + for(int i = 0; i < this->get_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) + if (indexified_position < 0 || indexified_position >= this->get_capacity() || grid[indexified_position] != nullptr) continue; - if(grid[indexified_position] != nullptr){ - Entity* entity = this->entities[i]; - grid[indexified_position]->collision(entity); - if(entity != nullptr && grid[indexified_position] != nullptr && grid[indexified_position] != entity){ - entity->collision(grid[indexified_position]); - } - } grid[indexified_position] = this->entities[i]; } } @@ -106,6 +120,21 @@ void World::update_grid() { void World::set_size(Vector2i size){ this->width = size.x; this->height = size.y; + if(this->grid != nullptr) + delete [] this->grid; + this->grid = new Entity*[this->get_capacity()]; +} + +int World::get_capacity() const{ + return this->get_columns()*this->get_rows(); +} + +int World::get_rows() const{ + return this->height/CELL_SIZE; +} + +int World::get_columns() const{ + return this->width/CELL_SIZE; } Texture2D* World::get_atlas() { diff --git a/src/world.h b/src/world.h index c4c592e..6422667 100644 --- a/src/world.h +++ b/src/world.h @@ -7,9 +7,6 @@ #include #include -const int GRID_ROWS = 20; -const int GRID_COLUMNS = 20; -const int GRID_CAPACITY = GRID_COLUMNS*GRID_ROWS; const int CELL_SIZE = 16; struct Vector2i{ @@ -24,7 +21,7 @@ class World { ~World(); bool debug; std::vector entities; /// Main subjects of game world. - Entity* grid[GRID_CAPACITY]; /// Grid representation + Entity** grid; /// Grid representation float seconds_per_tick; /// Internal clock speed int width; int height; @@ -39,8 +36,13 @@ class World { void draw() const; /// Should be called at the end of frame. /// Calls draw() on every entity void set_size(Vector2i); + int get_capacity() const; + int get_columns() const; + int get_rows() const; void load_atlas(); Texture2D* get_atlas(); + int indexify_position(Vector2 vector); + int points; private: float clock; void update_grid(); @@ -49,7 +51,6 @@ class World { void create_world_with(float seconds_per_tick); World& get_world(); // Thanks, 2ndbeam, helps a lot -int indexify_position(Vector2 vector); Vector2i get_map_size(const char* path); void load_world(World& world, const char* path);