diff --git a/src/components.cpp b/src/components.cpp index a06d782..9ef9ba5 100644 --- a/src/components.cpp +++ b/src/components.cpp @@ -1,3 +1,4 @@ +#include #include #include #include "components.h" @@ -25,9 +26,7 @@ Pacman::Pacman(Vector2 position) { Pacman::~Pacman() { UnloadTexture(this->texture); } -void Pacman::process() { - float delta = GetFrameTime(); - +void Pacman::tick() { // Input handling if (IsKeyDown(KEY_W) || IsKeyDown(KEY_UP)) {this->facing=1;} if (IsKeyDown(KEY_A) || IsKeyDown(KEY_LEFT)) {this->facing=2;} @@ -37,12 +36,14 @@ void Pacman::process() { // Movement in direction double angle = PI/2.0*facing; Vector2 direction = {(float)(cos(angle)),(float)(sin(-angle))}; - this->position = Vector2Add(this->position,Vector2Scale(direction, delta * (float)speed)); + this->position = Vector2Add(this->position,Vector2Scale(direction, (float)speed)); + } void Pacman::draw() { // Drawing DrawTextureRec(this->texture, this->getTextureRect(), this->position, WHITE); + DrawText(TextFormat("Position: %"), int posX, int posY, int fontSize, Color color) } // Wall definitions diff --git a/src/components.h b/src/components.h index 167b6ea..b1f4dfe 100644 --- a/src/components.h +++ b/src/components.h @@ -7,29 +7,16 @@ class Entity{ public: virtual ~Entity(){} + + virtual void ready() {} + virtual void process() {} + virtual void draw() {} + virtual void tick() {} }; -class IProcessable { - public: - virtual ~IProcessable() {} - // Called every frame - virtual void process() = 0; -}; -class IReadible { - public: - virtual ~IReadible() {} - // Called when the level is set up - virtual void ready() = 0; -}; -class IDrawable { - public: - virtual ~IDrawable() {} - virtual void draw() = 0; -}; - -class Pacman : public Entity, public IProcessable, public IDrawable { - const int speed = 64; +class Pacman : public Entity{ + const int speed = 16; private: Texture2D texture; Vector2 position; @@ -40,18 +27,18 @@ class Pacman : public Entity, public IProcessable, public IDrawable { Pacman(); Pacman(Vector2 position); ~Pacman(); - virtual void process(); - virtual void draw(); + void tick() override; + void draw() override; }; -class Wall :public Entity,public IReadible, public IDrawable { +class Wall : public Entity { private: Texture2D texture; public: Wall(); ~Wall(); - virtual void ready(); - virtual void draw(); + void ready() override; + void draw() override; }; #endif diff --git a/src/main.cpp b/src/main.cpp index 5356416..983897d 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -1,6 +1,7 @@ #include #include #include "components.h" +#include "world.h" int main() { const int screen_height = 320; @@ -9,22 +10,28 @@ int main() { InitWindow(screen_width, screen_height, "Test game"); SetTargetFPS(60); + + world = create_world_with(1.0); + + world.entities.push_back(Pacman({screen_width/2,screen_height/2})); - Pacman pacman = Pacman(Vector2{screen_width/2.0,screen_height/2.0}); + world.setup(); while (WindowShouldClose() == false) { - - pacman.process(); + world.process(); BeginDrawing(); ClearBackground(BLACK); - - pacman.draw(); + + DrawText(TextFormat("Entities: %i",world.entities.size()),0,0, 14,WHITE); + world.draw(); EndDrawing(); } + CloseWindow(); + return 0; } diff --git a/src/world.cpp b/src/world.cpp index 325ff9a..16f4228 100644 --- a/src/world.cpp +++ b/src/world.cpp @@ -1,55 +1,42 @@ #include "world.h" -#include "components.h" +#include World::World(){ - this->entities = new Entity[World::GRID_SIZE]; + this->entities = {}; + this->seconds_per_tick = 0; + this->clock = 0; } -World::~World(){ - delete [] this->entities; - delete [] this->processables; - delete [] this->drawables; - } - void World::setup(){ - this->processables_count = 0; - this->drawables_count = 0; - - IProcessable* temp_processables[World::GRID_SIZE]; - IDrawable* temp_drawables[World::GRID_SIZE]; - - for(int i = 0; i < World::GRID_SIZE; i++) { - // Get all processables and drawables as pointers to store later. - IProcessable* processable = dynamic_cast(&this->entities[i]); - IDrawable* drawable = dynamic_cast(&this->entities[i]); - - if (processable != nullptr) { - temp_processables[this->processables_count] = processable; - this->processables_count++; - } - if (drawable != nullptr) { - temp_drawables[this->drawables_count] = drawable; - this->drawables_count++; - } - } - - // Store processables and drawables - this->processables = new IProcessable*[this->processables_count]; - this->drawables = new IDrawable*[this->drawables_count]; - - for(int i = 0; i < this->processables_count; i++) - this->processables[i] = temp_processables[i]; - for(int i = 0; i < this->drawables_count; i++) - this->drawables[i] = temp_drawables[i]; - - // Call ready on all readibles - for(int i = 0; i < World::GRID_SIZE; i++) - { - IReadible* readible = dynamic_cast(&this->entities[i]); - if (readible != nullptr) - readible->ready(); - } - + for(int i = 0; i < this->entities.size(); i++) + this->entities[i].ready(); } +void World::process(){ + this->clock += GetFrameTime(); + for(int i = 0; i < this->entities.size(); i++) { + this->entities[i].process(); + if (this->clock>=this->seconds_per_tick) { + this->entities[i].tick(); + } + } + + if (this->clock>=this->seconds_per_tick) { + this->clock = 0; + } +} + +void World::draw() { + for(int i = 0; i< this->entities.size(); i++) { + this->entities[i].draw(); + } +} + +World create_world_with(float seconds_per_tick){ + World result = World(); + + result.seconds_per_tick = seconds_per_tick; + + return result; +} diff --git a/src/world.h b/src/world.h index dc0633d..582a04c 100644 --- a/src/world.h +++ b/src/world.h @@ -2,31 +2,30 @@ #define WORLD_H #include "components.h" +#include /// Class that holds information about game world class World { public: - const int GRID_SIZE = 20*20; World(); - ~World(); - Entity* entities; /// Main subjects of game world. + + std::vector entities; /// Main subjects of game world. + float seconds_per_tick; /// Internal clock speed void setup(); /// Sets up game world. /// Should be called once when entites are set. void process(); /// Should be called every frame. Calls - /// process() on every processable + /// process() on every entity void draw(); /// Should be called at the end of frame. - /// Calls draw() on every drawable - + /// Calls draw() on every entity private: - int processables_count; - IProcessable** processables; - int drawables_count; - IDrawable** drawables; + float clock; }; static World world; /// World singleton +World create_world_with(float seconds_per_tick); + #endif