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 <raymath.h>
|
||||
#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
|
||||
|
|
|
|||
|
|
@ -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
|
||||
|
|
|
|||
17
src/main.cpp
17
src/main.cpp
|
|
@ -1,6 +1,7 @@
|
|||
#include <raylib.h>
|
||||
#include <raymath.h>
|
||||
#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;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,55 +1,42 @@
|
|||
#include "world.h"
|
||||
#include "components.h"
|
||||
#include <raylib.h>
|
||||
|
||||
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<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();
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
|
|
|
|||
19
src/world.h
19
src/world.h
|
|
@ -2,31 +2,30 @@
|
|||
#define WORLD_H
|
||||
|
||||
#include "components.h"
|
||||
#include <vector>
|
||||
|
||||
/// 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<Entity> 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
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue