Fixed modularity issues
This commit is contained in:
parent
b628beafb9
commit
7fe086a477
2 changed files with 43 additions and 52 deletions
|
|
@ -1,56 +1,42 @@
|
||||||
#include <raylib.h>
|
#include <raylib.h>
|
||||||
#include <raymath.h>
|
#include <raymath.h>
|
||||||
|
#include "components.h"
|
||||||
|
|
||||||
class IComponent {
|
Rectangle Pacman::getTextureRect(){
|
||||||
public:
|
Rectangle result;
|
||||||
virtual ~IComponent() {}
|
result.x = 0;
|
||||||
virtual void process() = 0;
|
result.y = facing*16;
|
||||||
};
|
result.height = 16;
|
||||||
|
result.width = 16;
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
Pacman::Pacman() {
|
||||||
|
this->texture = LoadTexture("assets/sprites/pacman.png");
|
||||||
|
this->facing = 0;
|
||||||
|
this->position = {0.,0.};
|
||||||
|
}
|
||||||
|
Pacman::Pacman(Vector2 position) {
|
||||||
|
this->texture = LoadTexture("assets/sprites/pacman.png");
|
||||||
|
this->facing = 0;
|
||||||
|
this->position = position;
|
||||||
|
}
|
||||||
|
Pacman::~Pacman() {
|
||||||
|
UnloadTexture(this->texture);
|
||||||
|
}
|
||||||
|
void Pacman::process() {
|
||||||
|
float delta = GetFrameTime();
|
||||||
|
|
||||||
class Pacman : IComponent {
|
// Input handling
|
||||||
const int speed = 64;
|
if (IsKeyDown(KEY_W) || IsKeyDown(KEY_UP)) {this->facing=1;}
|
||||||
private:
|
if (IsKeyDown(KEY_A) || IsKeyDown(KEY_LEFT)) {this->facing=2;}
|
||||||
Texture2D texture;
|
if (IsKeyDown(KEY_S) || IsKeyDown(KEY_DOWN)) {this->facing=3;}
|
||||||
Vector2 position;
|
if (IsKeyDown(KEY_D) || IsKeyDown(KEY_RIGHT)) {this->facing=0;}
|
||||||
int facing;
|
|
||||||
|
|
||||||
Rectangle getTextureRect(){
|
// Movement in direction
|
||||||
Rectangle result;
|
double angle = PI/2.0*facing;
|
||||||
result.x = 0;
|
Vector2 direction = {(float)(cos(angle)),(float)(sin(-angle))};
|
||||||
result.y = facing*16;
|
this->position = Vector2Add(this->position,Vector2Scale(direction, delta * (float)speed));
|
||||||
result.height = 16;
|
|
||||||
result.width = 16;
|
|
||||||
return result;
|
|
||||||
}
|
|
||||||
public:
|
|
||||||
Pacman() {
|
|
||||||
this->texture = LoadTexture("assets/sprites/pacman.png");
|
|
||||||
this->facing = 0;
|
|
||||||
this->position = {0.,0.};
|
|
||||||
}
|
|
||||||
Pacman(Vector2 position) {
|
|
||||||
this->texture = LoadTexture("assets/sprites/pacman.png");
|
|
||||||
this->facing = 0;
|
|
||||||
this->position = position;
|
|
||||||
}
|
|
||||||
~Pacman() {
|
|
||||||
UnloadTexture(this->texture);
|
|
||||||
}
|
|
||||||
virtual void process() {
|
|
||||||
float delta = GetFrameTime();
|
|
||||||
|
|
||||||
// Input handling
|
// Drawing
|
||||||
if (IsKeyDown(KEY_W) || IsKeyDown(KEY_UP)) {this->facing=1;}
|
DrawTextureRec(this->texture, this->getTextureRect(), this->position, WHITE);
|
||||||
if (IsKeyDown(KEY_A) || IsKeyDown(KEY_LEFT)) {this->facing=2;}
|
}
|
||||||
if (IsKeyDown(KEY_S) || IsKeyDown(KEY_DOWN)) {this->facing=3;}
|
|
||||||
if (IsKeyDown(KEY_D) || IsKeyDown(KEY_RIGHT)) {this->facing=0;}
|
|
||||||
|
|
||||||
// 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));
|
|
||||||
|
|
||||||
// Drawing
|
|
||||||
DrawTextureRec(this->texture, this->getTextureRect(), this->position, WHITE);
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
|
||||||
|
|
@ -1,3 +1,6 @@
|
||||||
|
#ifndef COMPONENTS_H
|
||||||
|
#define COMPONENTS_H
|
||||||
|
|
||||||
#include <raylib.h>
|
#include <raylib.h>
|
||||||
#include <raymath.h>
|
#include <raymath.h>
|
||||||
|
|
||||||
|
|
@ -21,3 +24,5 @@ class Pacman : IComponent {
|
||||||
~Pacman();
|
~Pacman();
|
||||||
virtual void process();
|
virtual void process();
|
||||||
};
|
};
|
||||||
|
|
||||||
|
#endif
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue