more component stuff
This commit is contained in:
@@ -2,12 +2,13 @@
|
||||
|
||||
#include "Graphics.h"
|
||||
|
||||
class GameObject;
|
||||
|
||||
class GraphicsComponent {
|
||||
|
||||
public:
|
||||
virtual ~GraphicsComponent() {}
|
||||
virtual void update(Graphics &graphics) = 0;
|
||||
|
||||
private:
|
||||
};
|
||||
|
||||
|
||||
17
include/components/InputComponent.h
Normal file
17
include/components/InputComponent.h
Normal file
@@ -0,0 +1,17 @@
|
||||
#pragma once
|
||||
|
||||
#include <SDL3/SDL_events.h>
|
||||
#include "GameState.h"
|
||||
|
||||
struct InputComponent {
|
||||
|
||||
public:
|
||||
virtual ~InputComponent() {};
|
||||
virtual GameState update(SDL_Event const* event) = 0;
|
||||
|
||||
bool moveUp;
|
||||
bool moveDown;
|
||||
bool moveLeft;
|
||||
bool moveRight;
|
||||
};
|
||||
|
||||
@@ -1,11 +1,31 @@
|
||||
#pragma once
|
||||
|
||||
#include "GameObject.h"
|
||||
#include "Graphics.h"
|
||||
#include "GraphicsComponent.h"
|
||||
#include <print>
|
||||
#include <SDL3/SDL_pixels.h>
|
||||
#include <SDL3/SDL_rect.h>
|
||||
#include <SDL3/SDL_stdinc.h>
|
||||
|
||||
class PlayerGraphics : public GraphicsComponent {
|
||||
void update(Graphics &graphics) {
|
||||
std::print("Updating player graphics");
|
||||
public:
|
||||
void setPos(int x, int y) {
|
||||
hitbox.x = x;
|
||||
hitbox.y = y;
|
||||
}
|
||||
|
||||
void update(GameObject const& object, Graphics &graphics) {
|
||||
SDL_Color color = { .r = 0, .g = 255, .b = 0, .a = 255};
|
||||
|
||||
graphics.drawRect(hitbox, color);
|
||||
}
|
||||
|
||||
private:
|
||||
SDL_FRect hitbox = {
|
||||
.x = 40,
|
||||
.y = 40,
|
||||
.w = 300,
|
||||
.h = 300,
|
||||
};
|
||||
|
||||
};
|
||||
|
||||
30
include/components/PlayerInput.h
Normal file
30
include/components/PlayerInput.h
Normal file
@@ -0,0 +1,30 @@
|
||||
#include "components/InputComponent.h"
|
||||
#include "GameState.h"
|
||||
#include <SDL3/SDL_keycode.h>
|
||||
|
||||
class PlayerInput : public InputComponent {
|
||||
GameState update(SDL_Event const* event) {
|
||||
if (SDL_EVENT_QUIT == event->type || SDLK_ESCAPE == event->key.key) {
|
||||
return GameState::STOPPING;
|
||||
}
|
||||
// currently dont care about anything other than down
|
||||
if (SDL_EVENT_KEY_DOWN != event->type) {
|
||||
return GameState::RUNNING;
|
||||
}
|
||||
|
||||
if (SDLK_W == event->key.key) {
|
||||
moveUp = event->key.type == SDL_EVENT_KEY_DOWN;
|
||||
}
|
||||
if (SDLK_S == event->key.key) {
|
||||
moveDown = event->key.type == SDL_EVENT_KEY_DOWN;
|
||||
}
|
||||
if (SDLK_A == event->key.key) {
|
||||
moveLeft = event->key.type == SDL_EVENT_KEY_DOWN;
|
||||
}
|
||||
if (SDLK_D == event->key.key) {
|
||||
moveRight = event->key.type == SDL_EVENT_KEY_DOWN;
|
||||
}
|
||||
|
||||
return GameState::RUNNING;
|
||||
}
|
||||
};
|
||||
Reference in New Issue
Block a user