diff --git a/src/main.cpp b/src/main.cpp index 97d402b..e95e39a 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -1,7 +1,6 @@ #include #include #include "world.h" -#include "maploader.h" const int SCREEN_HEIGHT = 320; const int SCREEN_WIDTH = 320; @@ -15,7 +14,7 @@ int main() { World& world = get_world(); - loadmap(world); + load_world(world,"assets/map"); world.setup(); diff --git a/src/maploader.cpp b/src/maploader.cpp index 15e7249..582a901 100644 --- a/src/maploader.cpp +++ b/src/maploader.cpp @@ -1,22 +1,31 @@ -#include "maploader.h" #include #include #include "components.h" #include "world.h" -void loadmap(World& world) { +void load_world(World& world,const char* path) { std::ifstream in; - in.open("assets/map"); + in.open(path); int x = 0; int y = 0; + int max_x = 0; + int max_y = 0; + while(in.eof() == false) { char c; in.get(c); + + if (max_x < x) + max_x = x; + if (max_y < y) + max_y = y; + switch (c) { + case '#': { Wall* wall = new Wall; @@ -35,6 +44,7 @@ void loadmap(World& world) { break; case '\n': + x = 0; y++; break; @@ -42,6 +52,12 @@ void loadmap(World& world) { x++; break; } + + + } + + world.height = CELL_SIZE * (max_y-1); + world.width = CELL_SIZE * (max_x-1); } diff --git a/src/maploader.h b/src/maploader.h deleted file mode 100644 index 61abcd0..0000000 --- a/src/maploader.h +++ /dev/null @@ -1,8 +0,0 @@ -#ifndef MAPLOADER_H -#define MAPLOADER_H - -#include "world.h" - -void loadmap(World& world); - -#endif diff --git a/src/pacman.cpp b/src/pacman.cpp index 9ade9d2..2f3a41f 100644 --- a/src/pacman.cpp +++ b/src/pacman.cpp @@ -1,3 +1,4 @@ +#include #include #include #include "components.h" @@ -19,6 +20,7 @@ Pacman::Pacman() { this->facing = 0; this->position = {0.,0.}; this->frame = 0; + this->time = 0; } Pacman::Pacman(Vector2 position) { this->texture = LoadTexture("assets/sprites/pacman.png"); @@ -40,22 +42,24 @@ void Pacman::tick() { Vector2 direction = {(float)(cos(angle)),(float)(sin(-angle))}; Vector2 new_position = Vector2Add(this->position,Vector2Scale(direction, (float)speed)); - if (get_world().grid[indexify_position(new_position)] == nullptr){ + World& world = get_world(); + + if (world.grid[indexify_position(new_position)] == nullptr){ this->position=new_position; } - + // Bound check - if (this->position.x < 16){ - this->position.x += 288; + if (this->position.x < world.bound_offset*CELL_SIZE){ + this->position.x += world.width-2*world.bound_offset*CELL_SIZE; } - if (this->position.y < 16){ - this->position.y += 288; + if (this->position.y < world.bound_offset*CELL_SIZE){ + this->position.y += world.height-2*world.bound_offset*CELL_SIZE; } - if (this->position.x >= 304){ - this->position.x -= 288; + if (this->position.x >= world.width-(world.bound_offset-1)*CELL_SIZE){ + this->position.x -= world.width-2*world.bound_offset*CELL_SIZE; } - if (this->position.y >= 304){ - this->position.y -= 288; + if (this->position.y >= world.height-(world.bound_offset-1)*CELL_SIZE){ + this->position.y -= world.height-2*world.bound_offset*CELL_SIZE; } } diff --git a/src/world.cpp b/src/world.cpp index 7c15c9f..22e6eaf 100644 --- a/src/world.cpp +++ b/src/world.cpp @@ -8,6 +8,7 @@ World::World(){ this->seconds_per_tick = 0; this->clock = 0; this->debug = false; + this->bound_offset = 1; } World::~World(){ diff --git a/src/world.h b/src/world.h index 0b80806..da7529f 100644 --- a/src/world.h +++ b/src/world.h @@ -9,6 +9,7 @@ const int GRID_ROWS = 20; const int GRID_COLUMNS = 20; const int GRID_CAPACITY = GRID_COLUMNS*GRID_ROWS; +const int CELL_SIZE = 16; /// Class that holds information about game world class World { @@ -19,7 +20,10 @@ class World { std::vector entities; /// Main subjects of game world. Entity* grid[GRID_CAPACITY]; /// Grid representation float seconds_per_tick; /// Internal clock speed - + int width; + int height; + int bound_offset; + void setup(); /// Sets up game world. /// Should be called once when entites are set. @@ -36,5 +40,6 @@ class World { void create_world_with(float seconds_per_tick); World& get_world(); // Thanks, 2ndbeam, helps a lot int indexify_position(Vector2 vector); +void load_world(World& world, const char* path); #endif