Potato Engine
Loading...
Searching...
No Matches
PlayerController.cpp
Go to the documentation of this file.
1
2
6#include "GameInstance.hpp"
7#include "Game/World.hpp"
10#include "Debug/Debug.hpp"
11
12#include "PlayerController.hpp"
13
15
16PlayerController::PlayerController() {
17
18}
19
20void PlayerController::Initialize() {
21
22 World* world = GameInstance::get()->GetWorld();
23
24 // Create camera
25 ActiveCamera = world->SpawnActor<Camera>();
26
27 SetTicking(true);
28}
29
30void PlayerController::Tick(float dt) {
32
33 if (ActivePlayer == nullptr) { return; }
34
35 Vector2 newCamPos = ActivePlayer->GetPosition();
36
37 // camera x is median of player
38 // camera y fills screen (top)
39
40 newCamPos.x -= ActiveCamera->GetSize().x/2;
41 newCamPos.y = ActiveCamera->GetSize().y-1;
42
43 ActiveCamera->SetPosition(newCamPos);
44
45 ActivePlayer->AddLocalOffset(playerMoveVec * 0.5);
46
47}
48
49void PlayerController::sMvL() { playerMoveVec.x = -1; }
50void PlayerController::sMvR() { playerMoveVec.x = 1; }
51void PlayerController::sMvU() { playerMoveVec.y = 1; }
52void PlayerController::sMvD() { playerMoveVec.y = -1; }
53
54void PlayerController::eMvL() { playerMoveVec.x = 0; }
55void PlayerController::eMvR() { playerMoveVec.x = 0; }
56void PlayerController::eMvU() { playerMoveVec.y = 0; }
57void PlayerController::eMvD() { playerMoveVec.y = 0; }
58
59
60void PlayerController::AssignPlayer(Player* player) {
61 ActivePlayer = player;
62 if (ActivePlayer == nullptr) {
63 LOG_DEFAULT(LogType::WARNING, "Player set to nullptr during controller assignment");
64 }
65}
66
67Player* PlayerController::GetPlayer() const { return ActivePlayer; }
68Camera* PlayerController::GetCamera() const { return ActiveCamera; }
69
71
72 // setup input bindings
74
75 controller->RegisterInputBinding({
76 InputBinding(Keycode::A, InputType::Triggered, "StartMoveLeft", this, &PlayerController::sMvL),
77 InputBinding(Keycode::D, InputType::Triggered, "StartMoveRight", this, &PlayerController::sMvR),
78 InputBinding(Keycode::W, InputType::Triggered, "StartMoveUp", this, &PlayerController::sMvU),
79 InputBinding(Keycode::S, InputType::Triggered, "StartMoveDown", this, &PlayerController::sMvD),
80
81 InputBinding(Keycode::A, InputType::Completed, "CompleteMoveLeft", this, &PlayerController::eMvL),
82 InputBinding(Keycode::D, InputType::Completed, "CompleteMoveRight", this, &PlayerController::eMvR),
83 InputBinding(Keycode::W, InputType::Completed, "CompleteMoveUp", this, &PlayerController::eMvU),
84 InputBinding(Keycode::S, InputType::Completed, "CompleteMoveDown", this, &PlayerController::eMvD)
85 });
86}
87
88PlayerController::~PlayerController() {
90}
Logger LOG_DEFAULT
Default log object.
Definition Debug.cpp:9
#define SET_DEFAULT_SUBCLASS(def, set)
Registers classes as the default subobject for its class.
Camera wrapper for rendering.
Definition Camera.hpp:13
static GameInstance * get()
Gets singleton instance.
World * GetWorld() const
Gets world object.
Interface to manage input binding operations.
virtual void UnregisterAllInputBindings(void *object)=0
Unregisters all bindings tied to an object.
virtual void RegisterInputBinding(InputBinding binding)=0
Registers a single binding.
Main managing class for the player, controlling interactions and non-local behavior.
virtual void SetupInputBindings()
Called automatically to bind player inputs to InputController.
virtual void Tick(float dt) override
Update tick event.
Camera * GetCamera() const
Gets assigned Camera.
Player * GetPlayer() const
Gets assigned Player.
User controllable character.
Definition Player.hpp:12
IInputController * GetInputController() const
Gets input controller.
static PotatoEngine & Get()
Global access to engine object.
void SetTicking(bool isEnabled)
Sets tick state for object.
Definition Tickable.cpp:12
virtual void Tick(float dt)
Update tick event.
Definition Tickable.cpp:26
ActorClass * SpawnActor()
Spawns Actor into world.
Definition World.hpp:74
Wrapper for input bindings.
Standard 2-dimensional vector.
Definition Vector2.hpp:11
float x
X Component.
Definition Vector2.hpp:13
float y
Y Component.
Definition Vector2.hpp:15