-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathPlayerMovement.cs
181 lines (156 loc) · 3.54 KB
/
PlayerMovement.cs
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
using Godot;
using System;
public partial class PlayerMovement : CharacterBody3D
{
public const float Speed = 5.0f;
public const float JumpVelocity = 7.0f;
private double timer = 0;
private double ptimer = 0;
private Machine machine;
private Action<double> currentAct;
private Action<double> currentPAct;
double p1, p2; // parameters
Vector3 velocity;
Vector3 movdir = Vector3.Zero;
private bool gravityEnabled = true;
// Get the gravity from the project settings to be synced with RigidBody nodes.
public float gravity = ProjectSettings.GetSetting("physics/3d/default_gravity").AsSingle();
//[Signal]
//public delegate void SignalName(int param1, float param2);
public override void _Ready()
{
machine = this.GetParent<Machine>();
machine.Connect("doROT", new Callable(this, "doROT"));
machine.Connect("doMOV_", new Callable(this, "doMov_"));
machine.Connect("doJUMP_UP", new Callable(this, "doJump_up"));
machine.Connect("doFLY_", new Callable(this, "doFly_"));
machine.Connect("doFALL", new Callable(this, "doFall"));
}
private void rot(double delta)
{
double secondsToFinish = 0.05;
if (timer < secondsToFinish)
{
RotateY((float)(p1 * delta / secondsToFinish));
}
if(timer >= secondsToFinish)
{
machine.bbox.Set(true);
}
}
// signal
public void doROT(int amount)
{
timer = 0;
currentAct = rot;
currentPAct = null;
p1 = Mathf.DegToRad((float)amount);
}
private void mov(double delta)
{
if (ptimer >= p1)
{
machine.bbox.Set(true);
movdir = Vector3.Zero;
}
}
// signal
public void doMov_(int amount, Vector2 dir)
{
movdir = (Transform.basis * new Vector3(dir.x, 0, dir.y)).Normalized();
movdir.x = -movdir.x;
if (amount == 0)
return;
p1 = ((double)amount) / 10.0;
currentPAct = mov;
currentAct = null;
ptimer = 0;
}
// signal
private bool willJump = false;
private double lastJump = 0.0f;
private double jumpStrength = 0;
public void doJump_up(int amount)
{
jumpStrength = JumpVelocity * ((double)amount) / 255.0;
if (lastJump > 0.3)
{
willJump = true;
lastJump = 0;
}
}
private void fly(double delta)
{
if (ptimer >= Math.Abs((float)p1))
{
machine.bbox.Set(true);
movdir = Vector3.Zero;
}
}
// signal
public void doFly_(int amount)
{
movdir = (Transform.basis * new Vector3(0, (float)amount, 0)).Normalized();
p1 = ((double)amount) / 10.0;
currentPAct = fly;
currentAct = null;
ptimer = 0;
gravityEnabled = false;
MotionMode = MotionModeEnum.Floating;
}
// signal
public void doFall()
{
gravityEnabled = true;
MotionMode = MotionModeEnum.Grounded;
}
public override void _Process(double delta)
{
if (currentAct is not null)
{
currentAct(delta);
}
timer += delta;
lastJump += delta;
}
public override void _PhysicsProcess(double delta)
{
velocity = this.GetVelocity();
// Add the gravity.
if (!IsOnFloor() && gravityEnabled)
{
velocity.y -= gravity * (float)delta;
}
// Handle Jump.
if (willJump && IsOnFloor())
{
velocity.y = JumpVelocity;
willJump = false;
}
if (currentPAct is not null)
{
currentPAct(delta);
}
ptimer += delta;
if (movdir != Vector3.Zero)
{
velocity.x = movdir.x * Speed;
if (!gravityEnabled)
{
velocity.y = movdir.y * Speed;
}
velocity.z = -movdir.z * Speed;
}
else
{
velocity.x = Mathf.MoveToward(Velocity.x, 0, Speed);
if (!gravityEnabled)
{
velocity.y = Mathf.MoveToward(Velocity.y, 0, Speed);
}
velocity.z = Mathf.MoveToward(Velocity.z, 0, Speed);
}
this.SetVelocity(velocity);
MoveAndSlide();
}
}