-
Notifications
You must be signed in to change notification settings - Fork 1
/
Player.cs
86 lines (76 loc) · 2.64 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
using Godot;
using System;
public class Player : Node2D
{
public int vertVel {get; set;}
public int horVel {get; set;}
private PackedScene bulletClass;
private Node2D explosion;
private Timer shootCD;
private bool dying;
private const int HOR_SPEED = 150;
private const int VERT_SPEED_INCREMENT = 10;
private const int MAX_VERTICAL_SPEED = 200;
private const float MAX_RATE_OF_FIRE = 3f;//max bullets fired per second
// Called when the node enters the scene tree for the first time.
public override void _Ready()
{
bulletClass = ResourceLoader.Load<PackedScene>("res://Bullet.tscn");
explosion = (Node2D)ResourceLoader.Load<PackedScene>("res://Explosion.tscn").Instance();
shootCD = GetNode<Timer>("ShootCoolDown");
shootCD.WaitTime = 1/MAX_RATE_OF_FIRE;
vertVel = 0;
horVel = HOR_SPEED;
dying = false;
}
// Called every frame. 'delta' is the elapsed time since the previous frame.
public override void _Process(float delta)
{
MoveLocalX(horVel * delta);
float sizeY = GetViewportRect().Size.y;
if (this.Position.y > 1 && this.Position.y <= sizeY) {
MoveLocalY(vertVel * delta);
} else {
if (this.Position.y < 1) {
MoveLocalY(VERT_SPEED_INCREMENT);
vertVel = -1 * vertVel;
} else if (this.Position.y > sizeY) {
MoveLocalY(-1 * VERT_SPEED_INCREMENT);
vertVel = -1 * vertVel;
}
}
if (dying && shootCD.TimeLeft == 0) {
((GameScene)GetNode("/root/GameSceneRoot")).PlayerDied();
QueueFree();
}
}
//Handles user input events
public override void _Input(InputEvent @event) {
if (@event.IsAction("player_up") && vertVel >= -1 * MAX_VERTICAL_SPEED && !dying) {
vertVel -= VERT_SPEED_INCREMENT;
} else if (@event.IsAction("player_down") && vertVel <= MAX_VERTICAL_SPEED && !dying) {
vertVel += VERT_SPEED_INCREMENT;
} else if (@event.IsAction("player_shoot") && shootCD.TimeLeft == 0 && !dying) {
Bullet bullet = (Bullet)bulletClass.Instance();
bullet.Position = new Vector2(this.Position.x, this.Position.y + 20);
GetNode("/root/GameSceneRoot").AddChild(bullet);
shootCD.Start();
}
}
public void _on_Area2D_area_entered(Area2D area) {
if (area.GetCollisionLayerBit(2)) {
explode();
}
}
public void explode() {
explosion.Position = this.Position;
GetParent().AddChild(explosion);
((AnimationPlayer)explosion.GetNode("AnimationPlayer")).Play("Explode");
shootCD.WaitTime = 2.5f;
shootCD.Start();
((Sprite)GetNode("Sprite")).Visible = false;
horVel = 0;
vertVel = 0;
dying = true;
}
}