Skip to content

Commit

Permalink
refactor: simplify rigid body
Browse files Browse the repository at this point in the history
  • Loading branch information
developer239 committed Dec 3, 2023
1 parent 043d363 commit 7213ca9
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 31 deletions.
5 changes: 2 additions & 3 deletions src/apps/day3/src/components/RigidBodyComponent.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,7 @@ struct RigidBodyComponent {
SDL_Color color;

RigidBodyComponent(
int width = 0, int height = 0, Vec2 position = Vec2(0.0, 0.0),
Vec2 velocity = Vec2(0.0, 0.0), bool filled = true, SDL_Color color = {255, 255, 255, 255}
int width = 0, int height = 0, Vec2 position = Vec2(0.0, 0.0), bool filled = true, SDL_Color color = {255, 255, 255, 255}
)
: width(width), height(height), velocity(velocity), position(position), filled(filled), color(color) {}
: width(width), height(height), position(position), filled(filled), color(color) {}
};
2 changes: 0 additions & 2 deletions src/apps/day3/src/strategies/ECSStrategy.h
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,6 @@ class MinimalLoopStrategy : public Core::IStrategy {
scaledWidth,
scaledHeight,
Vec2(scaledX, scaledY),
Vec2(0, 0),
true
);

Expand Down Expand Up @@ -110,7 +109,6 @@ class MinimalLoopStrategy : public Core::IStrategy {
scaledSize,
scaledSize,
Vec2(scaledX, scaledY),
Vec2(0, 0),
true
);

Expand Down
35 changes: 9 additions & 26 deletions src/apps/day3/src/systems/KeyboardControlSystem.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,43 +3,26 @@
#include "ecs/System.h"
#include "events/Bus.h"

#include "../events/CollisionEvent.h"
#include "../components/RigidBodyComponent.h"
#include "../components/KeyboardControlledComponent.h"
#include "../components/RigidBodyComponent.h"
#include "../events/CollisionEvent.h"
#include "../events/KeyPressedEvent.h"

class KeyboardControlSystem : public ECS::System {
public:
KeyboardControlSystem() {
RequireComponent<KeyboardControlledComponent>();
}
KeyboardControlSystem() { RequireComponent<KeyboardControlledComponent>(); }

void SubscribeToEvents() {
Events::Bus::Instance().SubscribeToEvent<KeyPressedEvent>(this, &KeyboardControlSystem::OnKeyPressed);
Events::Bus::Instance().SubscribeToEvent<KeyPressedEvent>(
this,
&KeyboardControlSystem::OnKeyPressed
);
}

void OnKeyPressed(KeyPressedEvent& event) {
for (auto entity: GetSystemEntities()) {
const auto keyboardControl = ECS::Registry::Instance().GetComponent<KeyboardControlledComponent>(entity);
auto& rigidBody = ECS::Registry::Instance().GetComponent<RigidBodyComponent>(entity);

switch (event.symbol) {
case SDLK_UP:
rigidBody.velocity = keyboardControl.upVelocity;
break;
case SDLK_RIGHT:
rigidBody.velocity = keyboardControl.rightVelocity;
break;
case SDLK_DOWN:
rigidBody.velocity = keyboardControl.downVelocity;
break;
case SDLK_LEFT:
rigidBody.velocity = keyboardControl.leftVelocity;
break;
}
for (auto entity : GetSystemEntities()) {
}
}

void Update() {
}
void Update() {}
};

0 comments on commit 7213ca9

Please sign in to comment.