generated from developer239/cpp-starter
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
7213ca9
commit ceaf19c
Showing
5 changed files
with
115 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
#pragma once | ||
|
||
#include "../common/Vec2.h" | ||
|
||
struct CameraComponent { | ||
Vec2 position; | ||
int width; | ||
int height; | ||
|
||
CameraComponent(Vec2 position = Vec2(0, 0), int width = 0, int height = 0) | ||
: position(position), width(width), height(height){}; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
#pragma once | ||
|
||
#include "../components/CameraComponent.h" | ||
#include "../events/KeyPressedEvent.h" | ||
#include "ecs/System.h" | ||
#include "events/Bus.h" | ||
|
||
class CameraSystem : public ECS::System { | ||
public: | ||
CameraSystem() { | ||
RequireComponent<CameraComponent>(); | ||
SubscribeToEvents(); | ||
} | ||
|
||
void SubscribeToEvents() { | ||
Events::Bus::Instance().SubscribeToEvent<KeyPressedEvent>( | ||
this, | ||
&CameraSystem::OnKeyPressed | ||
); | ||
} | ||
|
||
void OnKeyPressed(KeyPressedEvent& event) { | ||
for (auto entity : GetSystemEntities()) { | ||
auto& camera = | ||
ECS::Registry::Instance().GetComponent<CameraComponent>(entity); | ||
|
||
int cameraSpeed = 10; | ||
|
||
switch (event.symbol) { | ||
case SDLK_UP: | ||
camera.position.y -= cameraSpeed; | ||
break; | ||
case SDLK_DOWN: | ||
camera.position.y += cameraSpeed; | ||
break; | ||
case SDLK_LEFT: | ||
camera.position.x -= cameraSpeed; | ||
break; | ||
case SDLK_RIGHT: | ||
camera.position.x += cameraSpeed; | ||
break; | ||
} | ||
} | ||
} | ||
|
||
void Update() { | ||
// Additional logic for updating camera (if needed) | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters