diff --git a/src/main.cpp b/src/main.cpp index 7560f7a..97d402b 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -11,8 +11,10 @@ int main() { SetTargetFPS(60); - world = create_world_with(1./10.); + create_world_with(1./10.); + World& world = get_world(); + loadmap(world); world.setup(); diff --git a/src/pacman.cpp b/src/pacman.cpp index f3e2acc..a8985da 100644 --- a/src/pacman.cpp +++ b/src/pacman.cpp @@ -1,6 +1,7 @@ #include #include #include "components.h" +#include "world.h" // Pacman definitions @@ -37,7 +38,11 @@ void Pacman::tick() { // Movement in direction double angle = PI/2.0*facing; Vector2 direction = {(float)(cos(angle)),(float)(sin(-angle))}; - this->position = Vector2Add(this->position,Vector2Scale(direction, (float)speed)); + Vector2 new_position = Vector2Add(this->position,Vector2Scale(direction, (float)speed)); + + if (get_world().grid[indexify_position(new_position)] == nullptr){ + this->position=new_position; + } // Bound check if (this->position.x < 0){ diff --git a/src/world.cpp b/src/world.cpp index c160a71..7c15c9f 100644 --- a/src/world.cpp +++ b/src/world.cpp @@ -1,5 +1,7 @@ #include "world.h" +#include "components.h" #include +#include World::World(){ this->entities = {}; @@ -50,8 +52,27 @@ void World::draw() const { DrawText(TextFormat("entities: %i",this->entities.size()), 0, 0, 14, WHITE); DrawText(TextFormat("seconds_per_tick: %f",this->seconds_per_tick), 0, 16, 14, WHITE); + if (IsKeyDown(KEY_F4)) + { + std::string str = ""; + for(int i = 0; i < GRID_CAPACITY; i++) + { + if (i!=0 && i%GRID_COLUMNS==0) + { + str += '\n'; + } + if (dynamic_cast(grid[i])) + str += '#'; + else if (dynamic_cast(grid[i])) + str += 'P'; + else + str += ' '; + } + DrawText(str.c_str(), 32, 0, 14, WHITE); + } } + int indexify_position(Vector2 vector){ return (int)(vector.x / 16.0) + GRID_COLUMNS * (int)(vector.y / 16.0); } @@ -71,10 +92,11 @@ void World::update_grid() { -World create_world_with(float seconds_per_tick){ - World result = World(); - - result.seconds_per_tick = seconds_per_tick; - - return result; +void create_world_with(float seconds_per_tick){ + get_world().seconds_per_tick = seconds_per_tick; +} + +World& get_world() { + static World world; + return world; } diff --git a/src/world.h b/src/world.h index bf6e754..0b80806 100644 --- a/src/world.h +++ b/src/world.h @@ -1,7 +1,9 @@ #ifndef WORLD_H + #define WORLD_H #include "components.h" +#include #include const int GRID_ROWS = 20; @@ -31,9 +33,8 @@ class World { void update_grid(); }; -static World world; /// World singleton - -World create_world_with(float seconds_per_tick); +void create_world_with(float seconds_per_tick); +World& get_world(); // Thanks, 2ndbeam, helps a lot int indexify_position(Vector2 vector); #endif