OpenGothic
Open source reimplementation of Gothic I and II
Loading...
Searching...
No Matches
playercontrol.h
Go to the documentation of this file.
1#pragma once
2
3#include "world/focus.h"
4#include "utils/keycodec.h"
5#include "constants.h"
6
7#include <array>
8
9class DialogMenu;
10class InventoryMenu;
11class World;
12class Interactive;
13class Npc;
14class Item;
15class Camera;
16class Gothic;
17
18class PlayerControl final {
19 public:
22
23 void onKeyPressed (KeyCodec::Action a, Tempest::Event::KeyType key, KeyCodec::Mapping mapping = KeyCodec::Mapping::Primary);
25 bool isPressed(KeyCodec::Action a) const;
26 void onRotateMouse(float dAngle);
27 void onRotateMouseDy(float dAngle);
28
29 void changeZoom(int delta);
30 void tickFocus();
31 void clearFocus();
32
33 bool interact(Interactive& it);
34 bool interact(Npc& other);
35 bool interact(Item& item);
36
37 void clearInput();
38
39 void setTarget(Npc* other);
40 void actionFocus(Npc& other);
41 void emptyFocus();
42
43 Focus focus() const;
44 bool hasActionFocus() const;
45
46 bool tickMove(uint64_t dt);
47 bool tickCameraMove(uint64_t dt);
48
49 private:
50 enum WeaponAction : uint8_t {
51 WeaponClose,
52 WeaponMele,
53 WeaponBow,
54 Weapon3,
55 Weapon4,
56 Weapon5,
57 Weapon6,
58 Weapon7,
59 Weapon8,
60 Weapon9,
61 Weapon10,
62
63 Last,
64 };
65
66 enum FocusAction : uint8_t {
67 ActForward=0,
68 ActBack =1,
69 ActLeft =2,
70 ActRight =3,
71 ActGeneric=4,
72 ActMove =5,
73 ActKill =6,
74 };
75
77
78 struct AxisStatus {
80 std::array<bool, KeyCodec::NumMappings> main;
81
83 std::array<bool, KeyCodec::NumMappings> reverse;
84
86 auto value() const -> float {
87 return
88 (this->anyMain() ? 1.f : 0.f)
89 + (this->anyReverse() ? -1.f : 0.f);
90 }
91
94 auto any() const -> bool {
95 return this->value() != 0;
96 }
97
98 void reset() {
99 this->main.fill(false);
100 this->reverse.fill(false);
101 }
102
103 private:
106 auto anyMain() const -> bool {
107 for(auto elem : main) {
108 if(elem) return true;
109 }
110 return false;
111 }
112
115 auto anyReverse() const -> bool {
116 for(auto elem : reverse) {
117 if(elem) return true;
118 }
119 return false;
120 }
121 };
122
123 struct MovementStatus {
124 AxisStatus forwardBackward;
125 AxisStatus strafeRightLeft;
126 AxisStatus turnRightLeft;
127
129 void reset() {
130 this->forwardBackward.reset();
131 this->strafeRightLeft.reset();
132 this->turnRightLeft.reset();
133 }
134 } movement;
135
136 bool ctrl[Action::Last]={};
137 bool wctrl[WeaponAction::Last]={};
138 bool actrl[7]={};
139
140 WeaponAction wctrlLast = WeaponAction::WeaponMele;
141
142 bool cacheFocus=false;
143 Focus currentFocus;
144 float rotMouse=0;
145 float rotMouseY=0;
146 bool casting = false;
147 size_t pickLockProgress = 0;
148
149 float runAngleDest = 0.f;
150 uint64_t runAngleSmooth = 0;
151 uint64_t turnAniSmooth = 0;
152 int rotationAni = 0;
153 bool g2Ctrl = false;
154
155 DialogMenu& dlg;
156 InventoryMenu& inv;
157
158 void setupSettings();
159 bool canInteract() const;
160 void marvinF8(uint64_t dt);
161 void marvinK(uint64_t dt);
162 void marvinO();
163 void toggleWalkMode();
164 void toggleSneakMode();
165 void moveFocus(FocusAction act);
166 Focus findFocus(Focus *prev);
167
168 void clrDraw();
169 void implMove(uint64_t dt);
170 void implMoveMobsi(Npc& pl, uint64_t dt);
171 void processPickLock(Npc& pl, Interactive& inter, KeyCodec::Action key);
172 void processLadder(Npc& pl, Interactive& inter, KeyCodec::Action key);
173 void quitPicklock(Npc& pl);
174 void setPos(std::array<float,3> a, uint64_t dt, float speed);
175 void assignRunAngle(Npc& pl, float rotation, uint64_t dt);
176 void setAnimRotate (Npc& pl, float rotation, int anim, bool force, uint64_t dt);
177 void processAutoRotate(Npc& pl, float& rot, uint64_t dt);
178
179
181 // Helper functions for movement
183
184 auto wantsToMoveForward() const -> bool {
185 return movement.forwardBackward.value() > 0.f;
186 }
187 auto wantsToMoveBackward() const -> bool {
188 return movement.forwardBackward.value() < 0.f;
189 }
190
191 auto wantsToStrafeRight() const -> bool {
192 return movement.strafeRightLeft.value() > 0.f;
193 }
194 auto wantsToStrafeLeft() const -> bool {
195 return movement.strafeRightLeft.value() < 0.f;
196 }
197
198 auto wantsToTurnRight() const -> bool {
199 return movement.turnRightLeft.value() > 0.f;
200 }
201 auto wantsToTurnLeft() const -> bool {
202 return movement.turnRightLeft.value() < 0.f;
203 }
204
209 auto handleMovementAction(KeyCodec::ActionMapping actionMapping, bool pressed) -> void;
210 };
Definition focus.h:10
Definition item.h:14
Definition npc.h:25
void actionFocus(Npc &other)
bool isPressed(KeyCodec::Action a) const
bool interact(Interactive &it)
void setTarget(Npc *other)
void onRotateMouse(float dAngle)
void onKeyReleased(KeyCodec::Action a, KeyCodec::Mapping mapping=KeyCodec::Mapping::Primary)
bool tickMove(uint64_t dt)
void onRotateMouseDy(float dAngle)
void changeZoom(int delta)
bool tickCameraMove(uint64_t dt)
bool hasActionFocus() const
Focus focus() const
void onKeyPressed(KeyCodec::Action a, Tempest::Event::KeyType key, KeyCodec::Mapping mapping=KeyCodec::Mapping::Primary)
Definition world.h:31
Action
Definition constants.h:341
int main(int argc, const char **argv)
Definition main.cpp:70
Encapsulates an in-game action and a key mapping that caused it to be fired.
Definition keycodec.h:76