-
Notifications
You must be signed in to change notification settings - Fork 1
/
Player.cs
330 lines (282 loc) · 9.65 KB
/
Player.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
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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
using Godot;
using System;
public class Player : KinematicBody2D
{
public static Player instance;
private static String ANIM_RUN="RUN";
private static String ANIM_JUMP="HIT";
public static int LIVES;
static AudioStream sfxJump=ResourceLoader.Load<AudioStream>("res://sounds/ingame/12_Player_Movement_SFX/30_Jump_03.wav");
static AudioStream sfxDoubleJump=ResourceLoader.Load<AudioStream>("res://sounds/ingame/12_Player_Movement_SFX/42_Cling_climb_03.wav");
static AudioStream sfxLanding=ResourceLoader.Load<AudioStream>("res://sounds/ingame/12_Player_Movement_SFX/45_Landing_01.wav");
[Signal]
public delegate void damage(float amount=1f, Node2D attacker=null);
[Export] public float GRAVITY=700f, WALK_FORCE=1600f, WALK_MIN_SPEED=119f, WALK_MAX_SPEED=119f, STOP_FORCE=1600f, JUMP_SPEED=220f, JUMP_MAX_AIRBORNE_TIME=0.2f;
private Vector2 velocity=Vector2.Zero;
private float onAirTime=100f;
private bool jumping=false;
private bool doubleJump=false;
private bool justJumped=false;
private bool onSlope=false;
private int weaponCyle=0;
private float slopeAngle=0f;
private Vector2 lastVelocity=Vector2.Zero;
private Vector2 FORCE;
private Vector2 platformSpeed=Vector2.Zero;
private float smoothingSpeed;
public AnimatedSprite animationController;
public CollisionShape2D collisionShape;
private CPUParticles2D airParticles,jumpParticles;
private ShaderMaterial motionTrails;
private Weapon weapon;
public Player() : base()
{
instance=this;
}
public override void _Ready()
{
Position=World.instance.renderer.ToLocal(World.level.startingPoint);
if(GameSettings.current.usage!=Viewport.UsageEnum.Usage3d)
{
GetNode<Light2D>("Light2D").QueueFree();
}
collisionShape=GetNode<CollisionShape2D>(nameof(CollisionShape2D));
animationController=GetNode<AnimatedSprite>(nameof(AnimatedSprite));
animationController.Play(ANIM_RUN);
airParticles=GetNode<CPUParticles2D>(nameof(airParticles));
airParticles.Emitting=false;
jumpParticles=ResourceUtils.particles[(int)PARTICLES.JUMP].Instance<CPUParticles2D>();
AddChild(jumpParticles);
jumpParticles.Emitting=false;
equipWeapon((PackedScene)ResourceUtils.weapons[(int)WEAPONS.DAGGER]);
AddToGroup(GROUPS.PLAYERS.ToString());
ZIndex=2;
Connect(STATE.damage.ToString(),this,nameof(onDamaged));
FORCE=new Vector2(0f,GRAVITY);
smoothingSpeed=PlayerCamera.instance.SmoothingSpeed;
motionTrails=(ShaderMaterial)animationController.Material;
}
public override void _PhysicsProcess(float delta)
{
Gamestate gamestate=World.state;
if((int)gamestate<3)
{
return;
}
airParticles.InitialVelocity=jumpParticles.InitialVelocity=World.level.Speed;
float friction=1f;
if(World.level.Speed!=0f)
{
friction=36/World.level.Speed;
}
Vector2 force=FORCE;
bool left=World.instance.input.getLeft();
bool right=World.instance.input.getRight();
bool jump=World.instance.input.getJump();
bool attack=World.instance.input.getAttack();
bool changeWeapon=World.instance.input.getChange();
if(changeWeapon&&!attack)
{
weaponCyle++;
if(weaponCyle==ResourceUtils.weapons.Count)
{
weaponCyle=0;
}
unequipWeapon();
equipWeapon(ResourceUtils.weapons[weaponCyle]);
}
if(attack&&weapon!=null)
{
weapon.attack();
}
if(left)
{
PlayerCamera.instance.direction=1;
animationController.FlipH=friction==1f;
}
else if(right)
{
PlayerCamera.instance.direction=-1;
animationController.FlipH=false;
}
else
{
PlayerCamera.instance.direction=0;
if(friction!=1f)
{
animationController.FlipH=false;
}
}
Vector2 motVec=friction==1f?velocity*(PlayerCamera.instance.direction*-1):velocity;
motionTrails.SetShaderParam("velocity",motVec);
if(left&&velocity.x<WALK_MIN_SPEED&&velocity.x>-WALK_MAX_SPEED)
{
force.x-=WALK_FORCE;
}
else if(right&&velocity.x>=-(WALK_MIN_SPEED*friction)&&velocity.x<(WALK_MAX_SPEED*friction))
{
force.x+=WALK_FORCE;
}
else
{
float xlength=Mathf.Abs(velocity.x);
xlength-=STOP_FORCE*delta;
if(xlength<0f)
{
xlength=0f;
}
velocity.x=xlength*Mathf.Sign(velocity.x);
}
velocity+=force*delta;
velocity.x=Mathf.Min(Mathf.Abs(velocity.x),WALK_MAX_SPEED)*Mathf.Sign(velocity.x);
if(platformSpeed!=Vector2.Zero&&velocity.x==0.0f)
{
velocity.x=platformSpeed.x*1.65f*friction;
}
platformSpeed=Vector2.Zero;
if(justJumped)
{
MoveLocalX(velocity.x*delta);
MoveLocalY(velocity.y*delta);
justJumped=false;
}
else
{
Vector2 snap=jumping?Vector2.Zero:new Vector2(0f,4f);
velocity=MoveAndSlideWithSnap(velocity,snap,Vector2.Up,false,4,0.785398f,true);
}
int collides=GetSlideCount();
onSlope=false;
if(collides>0&&!jumping)
{
for(int i=0;i<collides;i++)
{
KinematicCollision2D collision=GetSlideCollision(i);
if(collision.ColliderId==World.level.GetInstanceId())
{
slopeAngle=collision.Normal.AngleTo(Vector2.Up);
onSlope=Mathf.Abs(slopeAngle)>=0.785298f;
}
else if(collision.Collider.HasSignal(STATE.passanger.ToString())&&collision.Normal.AngleTo(Vector2.Up)==0)
{
velocity.y=-JUMP_SPEED;
animationController.Play(ANIM_JUMP);
justJumped=jumping=true;
collision.Collider.EmitSignal(STATE.passanger.ToString(),this);
}
else if(collision.Collider is MovingPlatform)
{
MovingPlatform platform=(MovingPlatform)collision.Collider;
platformSpeed=platform.CurrentSpeed;
}
}
}
if(IsOnCeiling())
{
if(lastVelocity.y<-150f) World.instance.renderer.shake+=Mathf.Abs(lastVelocity.y*0.004f);
}
if(IsOnFloor())
{
if(airParticles.Emitting)
{
playSfx(sfxLanding);
airParticles.Emitting=false;
}
Vector2 floorVelocity=GetFloorVelocity();
if(floorVelocity!=Vector2.Zero)
{
MoveAndCollide(-floorVelocity*delta);
}
onAirTime=0.0f;
if(lastVelocity.y>300f)
{
World.instance.renderer.shake+=lastVelocity.y*0.004f;
}
}
else if(!airParticles.Emitting)
{
airParticles.Emitting=true;
}
if(jumping&&velocity.y>0f)
{
animationController.Play(ANIM_RUN);
doubleJump=jumping=false;
jumpParticles.Emitting=false;
}
if(jump&&jumping&&!doubleJump)
{
doubleJump=true;
velocity.y=-JUMP_SPEED;
animationController.Play(ANIM_JUMP);
jumpParticles.Emitting=true;
playSfx(sfxDoubleJump);
}
if(jump&&!jumping&&onAirTime<JUMP_MAX_AIRBORNE_TIME)
{
if(onSlope)
{
if(slopeAngle<0f)
{
velocity.x+=50f;
}
else if(slopeAngle<1f)
{
velocity.x-=50f;
}
}
velocity.y=-JUMP_SPEED;
animationController.Play(ANIM_JUMP);
justJumped=jumping=true;
playSfx(sfxJump);
}
onAirTime+=delta;
if(Position.x<-20f||Position.y<-20f||Position.x>World.RESOLUTION.x+20f||Position.y>World.RESOLUTION.y+20f)
{
onDamaged();
}
lastVelocity=velocity;
}
public void equipWeapon(PackedScene packed)
{
weapon=(Weapon)packed.Instance();
if(weapon!=null)
{
AddChild(weapon);
}
}
public void unequipWeapon()
{
RemoveChild(weapon);
weapon.QueueFree();
}
private void onDamaged(float amount=1f,Node2D damager=null)
{
PlayerCamera.instance.SmoothingSpeed=0f;
World.instance.setGamestate(Gamestate.DIEING);
PlayerDie effect=PlayerDie.create();
effect.Position=World.level.ToLocal(GlobalPosition);
Position=new Vector2(0,-100);
World.level.AddChild(effect);
LIVES--;
PlayerDieEffect.create();
}
public void die()
{
if(LIVES>0)
{
World.instance.CallDeferred(nameof(World.instance.restartLevel),true);
PlayerCamera.instance.SmoothingSpeed=smoothingSpeed;
}
else
{
World.instance.CallDeferred(nameof(World.changeScene),ResourceUtils.intro);
}
}
private void playSfx(AudioStream stream)
{
SfxPlayer sfx=new SfxPlayer();
sfx.Stream=stream;
sfx.Position=Position;
World.instance.renderer.AddChild(sfx);
}
}