Architecture shift and not working render
This commit is contained in:
parent
a54f49d188
commit
e88327799b
5 changed files with 65 additions and 84 deletions
|
|
@ -1,3 +1,4 @@
|
||||||
|
#include <iostream>
|
||||||
#include <raylib.h>
|
#include <raylib.h>
|
||||||
#include <raymath.h>
|
#include <raymath.h>
|
||||||
#include "components.h"
|
#include "components.h"
|
||||||
|
|
@ -25,9 +26,7 @@ Pacman::Pacman(Vector2 position) {
|
||||||
Pacman::~Pacman() {
|
Pacman::~Pacman() {
|
||||||
UnloadTexture(this->texture);
|
UnloadTexture(this->texture);
|
||||||
}
|
}
|
||||||
void Pacman::process() {
|
void Pacman::tick() {
|
||||||
float delta = GetFrameTime();
|
|
||||||
|
|
||||||
// Input handling
|
// Input handling
|
||||||
if (IsKeyDown(KEY_W) || IsKeyDown(KEY_UP)) {this->facing=1;}
|
if (IsKeyDown(KEY_W) || IsKeyDown(KEY_UP)) {this->facing=1;}
|
||||||
if (IsKeyDown(KEY_A) || IsKeyDown(KEY_LEFT)) {this->facing=2;}
|
if (IsKeyDown(KEY_A) || IsKeyDown(KEY_LEFT)) {this->facing=2;}
|
||||||
|
|
@ -37,12 +36,14 @@ void Pacman::process() {
|
||||||
// Movement in direction
|
// Movement in direction
|
||||||
double angle = PI/2.0*facing;
|
double angle = PI/2.0*facing;
|
||||||
Vector2 direction = {(float)(cos(angle)),(float)(sin(-angle))};
|
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() {
|
void Pacman::draw() {
|
||||||
// Drawing
|
// Drawing
|
||||||
DrawTextureRec(this->texture, this->getTextureRect(), this->position, WHITE);
|
DrawTextureRec(this->texture, this->getTextureRect(), this->position, WHITE);
|
||||||
|
DrawText(TextFormat("Position: %"), int posX, int posY, int fontSize, Color color)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Wall definitions
|
// Wall definitions
|
||||||
|
|
|
||||||
|
|
@ -7,29 +7,16 @@
|
||||||
class Entity{
|
class Entity{
|
||||||
public:
|
public:
|
||||||
virtual ~Entity(){}
|
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 {
|
class Pacman : public Entity{
|
||||||
public:
|
const int speed = 16;
|
||||||
virtual ~IDrawable() {}
|
|
||||||
virtual void draw() = 0;
|
|
||||||
};
|
|
||||||
|
|
||||||
class Pacman : public Entity, public IProcessable, public IDrawable {
|
|
||||||
const int speed = 64;
|
|
||||||
private:
|
private:
|
||||||
Texture2D texture;
|
Texture2D texture;
|
||||||
Vector2 position;
|
Vector2 position;
|
||||||
|
|
@ -40,18 +27,18 @@ class Pacman : public Entity, public IProcessable, public IDrawable {
|
||||||
Pacman();
|
Pacman();
|
||||||
Pacman(Vector2 position);
|
Pacman(Vector2 position);
|
||||||
~Pacman();
|
~Pacman();
|
||||||
virtual void process();
|
void tick() override;
|
||||||
virtual void draw();
|
void draw() override;
|
||||||
};
|
};
|
||||||
|
|
||||||
class Wall :public Entity,public IReadible, public IDrawable {
|
class Wall : public Entity {
|
||||||
private:
|
private:
|
||||||
Texture2D texture;
|
Texture2D texture;
|
||||||
public:
|
public:
|
||||||
Wall();
|
Wall();
|
||||||
~Wall();
|
~Wall();
|
||||||
virtual void ready();
|
void ready() override;
|
||||||
virtual void draw();
|
void draw() override;
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|
|
||||||
15
src/main.cpp
15
src/main.cpp
|
|
@ -1,6 +1,7 @@
|
||||||
#include <raylib.h>
|
#include <raylib.h>
|
||||||
#include <raymath.h>
|
#include <raymath.h>
|
||||||
#include "components.h"
|
#include "components.h"
|
||||||
|
#include "world.h"
|
||||||
|
|
||||||
int main() {
|
int main() {
|
||||||
const int screen_height = 320;
|
const int screen_height = 320;
|
||||||
|
|
@ -10,21 +11,27 @@ int main() {
|
||||||
|
|
||||||
SetTargetFPS(60);
|
SetTargetFPS(60);
|
||||||
|
|
||||||
Pacman pacman = Pacman(Vector2{screen_width/2.0,screen_height/2.0});
|
world = create_world_with(1.0);
|
||||||
|
|
||||||
|
world.entities.push_back(Pacman({screen_width/2,screen_height/2}));
|
||||||
|
|
||||||
|
world.setup();
|
||||||
|
|
||||||
while (WindowShouldClose() == false) {
|
while (WindowShouldClose() == false) {
|
||||||
|
|
||||||
|
world.process();
|
||||||
pacman.process();
|
|
||||||
|
|
||||||
BeginDrawing();
|
BeginDrawing();
|
||||||
|
|
||||||
ClearBackground(BLACK);
|
ClearBackground(BLACK);
|
||||||
|
|
||||||
pacman.draw();
|
DrawText(TextFormat("Entities: %i",world.entities.size()),0,0, 14,WHITE);
|
||||||
|
world.draw();
|
||||||
|
|
||||||
EndDrawing();
|
EndDrawing();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
CloseWindow();
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -1,55 +1,42 @@
|
||||||
#include "world.h"
|
#include "world.h"
|
||||||
#include "components.h"
|
#include <raylib.h>
|
||||||
|
|
||||||
World::World(){
|
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(){
|
void World::setup(){
|
||||||
this->processables_count = 0;
|
for(int i = 0; i < this->entities.size(); i++)
|
||||||
this->drawables_count = 0;
|
this->entities[i].ready();
|
||||||
|
|
||||||
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<IProcessable*>(&this->entities[i]);
|
|
||||||
IDrawable* drawable = dynamic_cast<IDrawable*>(&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<IReadible*>(&this->entities[i]);
|
|
||||||
if (readible != nullptr)
|
|
||||||
readible->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;
|
||||||
|
}
|
||||||
|
|
|
||||||
19
src/world.h
19
src/world.h
|
|
@ -2,31 +2,30 @@
|
||||||
#define WORLD_H
|
#define WORLD_H
|
||||||
|
|
||||||
#include "components.h"
|
#include "components.h"
|
||||||
|
#include <vector>
|
||||||
|
|
||||||
/// Class that holds information about game world
|
/// Class that holds information about game world
|
||||||
class World {
|
class World {
|
||||||
public:
|
public:
|
||||||
const int GRID_SIZE = 20*20;
|
|
||||||
World();
|
World();
|
||||||
~World();
|
|
||||||
Entity* entities; /// Main subjects of game world.
|
std::vector<Entity> entities; /// Main subjects of game world.
|
||||||
|
float seconds_per_tick; /// Internal clock speed
|
||||||
|
|
||||||
void setup(); /// Sets up game world.
|
void setup(); /// Sets up game world.
|
||||||
/// Should be called once when entites are set.
|
/// Should be called once when entites are set.
|
||||||
|
|
||||||
void process(); /// Should be called every frame. Calls
|
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.
|
void draw(); /// Should be called at the end of frame.
|
||||||
/// Calls draw() on every drawable
|
/// Calls draw() on every entity
|
||||||
|
|
||||||
private:
|
private:
|
||||||
int processables_count;
|
float clock;
|
||||||
IProcessable** processables;
|
|
||||||
int drawables_count;
|
|
||||||
IDrawable** drawables;
|
|
||||||
};
|
};
|
||||||
|
|
||||||
static World world; /// World singleton
|
static World world; /// World singleton
|
||||||
|
|
||||||
|
World create_world_with(float seconds_per_tick);
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue