modularity problem
This commit is contained in:
parent
57e9b91c4c
commit
b628beafb9
4 changed files with 74 additions and 57 deletions
5
makefile
5
makefile
|
|
@ -1,8 +1,9 @@
|
|||
CFLAGS=$(shell cat compiler_flags.txt)
|
||||
FILES=$(wildcard src/*.cpp)
|
||||
|
||||
build/raylib-test-linux.x86_64 : src/main.cpp
|
||||
build/raylib-test-linux.x86_64 : $(FILES)
|
||||
mkdir -p build
|
||||
clang src/main.cpp $(CFLAGS) -o build/raylib-test-linux.x86_64
|
||||
clang $(FILES) $(CFLAGS) -o build/raylib-test-linux.x86_64
|
||||
|
||||
|
||||
clean :
|
||||
|
|
|
|||
56
src/components.cpp
Normal file
56
src/components.cpp
Normal file
|
|
@ -0,0 +1,56 @@
|
|||
#include <raylib.h>
|
||||
#include <raymath.h>
|
||||
|
||||
class IComponent {
|
||||
public:
|
||||
virtual ~IComponent() {}
|
||||
virtual void process() = 0;
|
||||
};
|
||||
|
||||
class Pacman : IComponent {
|
||||
const int speed = 64;
|
||||
private:
|
||||
Texture2D texture;
|
||||
Vector2 position;
|
||||
int facing;
|
||||
|
||||
Rectangle getTextureRect(){
|
||||
Rectangle result;
|
||||
result.x = 0;
|
||||
result.y = facing*16;
|
||||
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
|
||||
if (IsKeyDown(KEY_W) || IsKeyDown(KEY_UP)) {this->facing=1;}
|
||||
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 @@
|
|||
#include <raylib.h>
|
||||
#include <raymath.h>
|
||||
|
||||
class IComponent {
|
||||
public:
|
||||
virtual ~IComponent() {}
|
||||
|
|
@ -5,8 +8,16 @@ class IComponent {
|
|||
};
|
||||
|
||||
class Pacman : IComponent {
|
||||
public:
|
||||
virtual void process() {
|
||||
const int speed = 64;
|
||||
private:
|
||||
Texture2D texture;
|
||||
Vector2 position;
|
||||
int facing;
|
||||
|
||||
}
|
||||
Rectangle getTextureRect();
|
||||
public:
|
||||
Pacman();
|
||||
Pacman(Vector2 position);
|
||||
~Pacman();
|
||||
virtual void process();
|
||||
};
|
||||
|
|
|
|||
53
src/main.cpp
53
src/main.cpp
|
|
@ -1,57 +1,6 @@
|
|||
#include <raylib.h>
|
||||
#include <cmath>
|
||||
#include <raymath.h>
|
||||
|
||||
// Player controlled entity
|
||||
class Pacman {
|
||||
const int speed = 64;
|
||||
private:
|
||||
Texture2D texture;
|
||||
Vector2 position;
|
||||
int facing;
|
||||
|
||||
Rectangle getTextureRect(){
|
||||
Rectangle result;
|
||||
result.x = 0;
|
||||
result.y = facing*16;
|
||||
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);
|
||||
}
|
||||
|
||||
void process() {
|
||||
float delta = GetFrameTime();
|
||||
|
||||
// Input handling
|
||||
if (IsKeyDown(KEY_W) || IsKeyDown(KEY_UP)) {this->facing=1;}
|
||||
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);
|
||||
}
|
||||
|
||||
};
|
||||
#include "components.h"
|
||||
|
||||
int main() {
|
||||
const int screen_height = 320;
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue