-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathEnemy.cs
More file actions
100 lines (81 loc) · 2.46 KB
/
Enemy.cs
File metadata and controls
100 lines (81 loc) · 2.46 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
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
using Godot;
using System;
public class EnemyType {
public static EnemyType KNIGHT = new EnemyType(1);
public static EnemyType REACH_KNIGHT = new EnemyType(2);
public int coinReward;
public EnemyType(int coinReward) {
this.coinReward = coinReward;
}
}
public class Enemy : Node2D
{
[Signal]
public delegate void EnemyDied(Enemy enemy);
[Export]
public int coinReward = 1;
[Export]
public PackedScene deadEnemy;
public static String GROUP_NAME = "Enemy";
private bool died = false;
private bool exploded = false;
private int deathSoundCount;
private Skeleton2D skeleton;
private GameObject body;
// Called when the node enters the scene tree for the first time.
public override void _Ready()
{
AddToGroup(GROUP_NAME);
deathSoundCount = GetNode<Node>("Sounds/DeathSounds").GetChildCount();
body = GetNode<GameObject>("RidigBody");
//skeleton = GetNode<Skeleton2D>("Skeleton2D");
var damageController = new DamageController();
damageController.Init(body);
damageController.Connect("OnDestroyed", this, "OnDestroyed");
body.DamageController = damageController;
}
public void OnDestroyed()
{
Die();
}
private void Die() {
if (died) return;
died = true;
var soundIndex = ((int)GD.RandRange(0, deathSoundCount));
var sound = GetNode<AudioStreamPlayer2D>("Sounds/DeathSounds/Death" + (soundIndex + 1));
sound.Play();
EmitSignal("EnemyDied", this);
var deadBody = deadEnemy.Instance() as Node2D;
if (exploded)
{
RemoveAllExplodable(deadBody);
}
var deadPosition = body.Position;
deadPosition += new Vector2(0, 50);
var deadRotation = body.GlobalRotation;
body.QueueFree();
GD.Print("deadPosition: " + deadPosition);
AddChild(deadBody);
deadBody.Position = deadPosition;
deadBody.GlobalRotation = deadRotation;
}
private void RemoveAllExplodable(Node2D deadBody)
{
foreach (Node node in deadBody.GetChildren())
{
if (node.IsInGroup("Explodable"))
{
node.QueueFree();
}
}
}
public void Explode(float force)
{
if (force < 10)
{
GD.Print("Not enough force to explode");
return;
}
exploded = true;
}
}