-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFlappyBody.cs
More file actions
71 lines (56 loc) · 1.31 KB
/
FlappyBody.cs
File metadata and controls
71 lines (56 loc) · 1.31 KB
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
using Godot;
using System;
using Godot.Collections;
public partial class FlappyBody : RigidBody2D
{
[Export] float speed = 200f;
[Export] float jumpPower = 550f;
//time before game reloads
[Export] float resetTimer = 1f;
private Vector2 movementThisFrame = new Vector2();
private Vector2 jumpImpulse = new Vector2();
private bool hasCrashed = false;
// Called when the node enters the scene tree for the first time.
public override void _Ready()
{
movementThisFrame.X = speed;
jumpImpulse.Y = -jumpPower;
}
// Called every frame. 'delta' is the elapsed time since the previous frame.
public override void _Process(double delta)
{
}
public override void _PhysicsProcess(double delta)
{
if (hasCrashed)
{
return;
}
movementThisFrame.Y = LinearVelocity.Y;
LinearVelocity = movementThisFrame;
}
public override void _Input(InputEvent @event)
{
if (hasCrashed)
{
return;
}
if (@event is InputEventKey eventKey)
{
if (eventKey.Pressed && eventKey.Keycode == Key.Space)
{
//canceling current down/up velocity
movementThisFrame.Y = 0f;
LinearVelocity = movementThisFrame;
ApplyImpulse(jumpImpulse);
}
}
}
public void StopPlayer()
{
hasCrashed = true;
LinearVelocity = Vector2.Zero;
GravityScale = 0f;
LockRotation = false;
}
}