-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathManualLeaning.reds
209 lines (167 loc) · 6 KB
/
ManualLeaning.reds
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
// -----------------------------------------------------------------------------
// Manual Leaning Mode
// -----------------------------------------------------------------------------
//
// When aiming press the corresponding keys (default `Q` and `E`) to lean.
// Other actions bound to the same keys will be discarded when leaning.
//
// Additional installation steps:
//
// 1) Edit `r6/config/inputContexts.xml` to add new actions to the Exploration context:
// <context name="Exploration">
// ...
// <action name="LeanLeft" map="LeanLeft_Button"/>
// <action name="LeanRight" map="LeanRight_Button"/>
// </context>
//
// 2) Optionally edit `r6/config/inputUserMappings.xml` to change the bindings:
// <mapping name="LeanLeft_Button" type="Button">
// <button id="IK_Q"/>
// </mapping>
// <mapping name="LeanRight_Button" type="Button">
// <button id="IK_E"/>
// </mapping>
//
module ManualLeaning
public class LeaningHandler {
private let m_player: wref<PlayerPuppet>;
private let m_obstacleSystem: ref<PlayerObstacleSystem>;
private let m_currentLeaning: coverLeanDirection;
private let m_lastLeaning: coverLeanDirection;
private let m_isAiming: Bool;
private let m_isInCover: Bool;
private let m_attachedCallback: Uint32;
private let m_aimingListener: ref<CallbackHandle>;
private let m_coverListener: ref<CallbackHandle>;
public func Register(player: wref<PlayerPuppet>) -> Void {
this.m_player = player;
this.m_player.RegisterInputListener(this, n"LeanLeft");
this.m_player.RegisterInputListener(this, n"LeanRight");
this.m_attachedCallback = GameInstance.GetPlayerSystem(this.m_player.GetGame()).RegisterPlayerPuppetAttachedCallback(this, n"OnPlayerAttached");
this.ResetState();
}
public func Unregister() -> Void {
GameInstance.GetPlayerSystem(this.m_player.GetGame()).UnregisterPlayerPuppetAttachedCallback(this.m_attachedCallback);
this.m_player.UnregisterInputListener(this);
this.m_player = null;
this.m_aimingListener = null;
this.m_coverListener = null;
this.m_obstacleSystem = null;
this.ResetState();
}
public func ResetState() -> Bool {
this.m_currentLeaning = coverLeanDirection.Top;
this.m_lastLeaning = coverLeanDirection.Top;
}
public func IsLeaning() -> Bool {
return !Equals(this.m_currentLeaning, coverLeanDirection.Top);
}
public func WasLeaning() -> Bool {
return !Equals(this.m_lastLeaning, coverLeanDirection.Top);
}
public func LeanTo(leanDirection: coverLeanDirection) -> Bool {
if !this.m_isAiming {
return false;
}
if Equals(this.m_currentLeaning, leanDirection) {
return false;
}
if Equals(leanDirection, coverLeanDirection.Left) {
this.m_obstacleSystem.ManualLeanRight(this.m_player);
if this.m_isInCover {
this.m_obstacleSystem.ManualLeanRight(this.m_player);
}
} else {
this.m_obstacleSystem.ManualLeanLeft(this.m_player);
if this.m_isInCover {
this.m_obstacleSystem.ManualLeanLeft(this.m_player);
}
}
this.m_currentLeaning = this.IsLeaning() ? coverLeanDirection.Top : leanDirection;
this.m_lastLeaning = coverLeanDirection.Top;
return true;
}
public func LeanLeft() -> Bool {
return this.LeanTo(coverLeanDirection.Left);
}
public func LeanRight() -> Bool {
return this.LeanTo(coverLeanDirection.Right);
}
protected cb func OnPlayerAttached(playerPuppet: ref<GameObject>) -> Void {
let pmsBlackboard = this.m_player.GetPlayerStateMachineBlackboard();
this.m_aimingListener = pmsBlackboard.RegisterListenerInt(GetAllBlackboardDefs().PlayerStateMachine.UpperBody, this, n"OnUpperBodyStateChanged");
this.m_coverListener = pmsBlackboard.RegisterListenerBool(GetAllBlackboardDefs().PlayerStateMachine.UsingCover, this, n"OnUsingCoverStateChanged");
this.m_obstacleSystem = GameInstance.GetSpatialQueriesSystem(this.m_player.GetGame()).GetPlayerObstacleSystem();
}
protected cb func OnUpperBodyStateChanged(state: Int32) -> Bool {
let isAiming = (state == EnumInt(gamePSMUpperBodyStates.Aim));
if !Equals(isAiming, this.m_isAiming) {
if isAiming {
if this.WasLeaning() {
let leanCallback = new DelayedLeanToCallback();
leanCallback.direction = this.m_lastLeaning;
leanCallback.handler = this;
GameInstance.GetDelaySystem(this.m_player.GetGame()).DelayCallback(leanCallback, 0);
}
} else {
if this.IsLeaning() {
this.m_lastLeaning = this.m_currentLeaning;
this.m_currentLeaning = coverLeanDirection.Top;
}
}
}
this.m_isAiming = isAiming;
}
protected cb func OnUsingCoverStateChanged(state: Bool) -> Bool {
this.m_isInCover = state;
}
protected cb func OnAction(action: ListenerAction, consumer: ListenerActionConsumer) -> Void {
if ListenerAction.IsButtonJustPressed(action) {
let leanDirection = ListenerAction.IsAction(action, n"LeanLeft")
? coverLeanDirection.Left
: coverLeanDirection.Right;
if this.LeanTo(leanDirection) {
ListenerActionConsumer.Consume(consumer);
}
return;
}
if ListenerAction.IsButtonJustReleased(action) {
if this.WasLeaning() {
ListenerActionConsumer.Consume(consumer);
this.ResetState();
} else {
let leanDirection = ListenerAction.IsAction(action, n"LeanLeft")
? coverLeanDirection.Right
: coverLeanDirection.Left;
if this.LeanTo(leanDirection) {
ListenerActionConsumer.Consume(consumer);
}
}
}
}
}
public class DelayedLeanToCallback extends DelayCallback {
public let handler: ref<LeaningHandler>;
public let direction: coverLeanDirection;
public func Call() -> Void {
this.handler.LeanTo(this.direction);
}
}
@addField(PlayerPuppet)
private let m_leaningHandler: ref<LeaningHandler>;
@wrapMethod(PlayerPuppet)
protected cb func OnGameAttached() -> Bool {
if this.IsControlledByLocalPeer() || IsHost() {
this.m_leaningHandler = new LeaningHandler();
this.m_leaningHandler.Register(this);
}
wrappedMethod();
}
@wrapMethod(PlayerPuppet)
protected cb func OnDetach() -> Bool {
wrappedMethod();
if this.IsControlledByLocalPeer() || IsHost() {
this.m_leaningHandler.Unregister();
this.m_leaningHandler = null;
}
}