-
Notifications
You must be signed in to change notification settings - Fork 6
/
SpaceMissile.cs
110 lines (82 loc) · 2.97 KB
/
SpaceMissile.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
using Godot;
using System;
using redhatgamedev.srt.v1;
public partial class SpaceMissile : Area2D
{
// for the server we're interfaced with
Server MyServer;
public Serilog.Core.Logger _serilogger;
public float MissileLife;
public int MissileSpeed;
public int MissileDamage;
public PlayerShip MyPlayer;
public String uuid;
[Signal]
public delegate void HitEventHandler(PlayerShip HitPlayer);
public GameEvent.GameObject CreateMissileGameObjectBuffer(String OwnerUUID)
{
GameEvent.GameObject gameObject = new GameEvent.GameObject();
gameObject.GameObjectType = GameEvent.GameObjectType.GameObjectTypeMissile;
gameObject.Uuid = uuid;
gameObject.PositionX = (int)GlobalPosition.X;
gameObject.PositionY = (int)GlobalPosition.Y;
gameObject.Angle = RotationDegrees;
gameObject.AbsoluteVelocity = MissileSpeed;
gameObject.OwnerUuid = OwnerUUID;
return gameObject;
}
// Called when the node enters the scene tree for the first time.
public override void _Ready()
{
// initialize the logging configuration
MyServer = GetNode<Server>("/root/Server");
_serilogger = MyServer._serilogger;
// connect the hit signal to handling the hit
//Connect(nameof(Hit), this, "_HandleHit");
// add the missile to the missiles group so that we can iterate over
// the entire group and send updates later
AddToGroup("missiles");
}
public override void _PhysicsProcess(double delta)
{
// TODO disable the collision shape until the missile is "away" from the ship
// create a new vector and rotate it by the current heading of the missile
// then move the missile in the direction of that vector
Vector2 velocity = new Vector2(0, -1);
velocity = velocity.Rotated(Rotation);
velocity = velocity * (float)MissileSpeed * (float)delta;
Position += velocity;
// once the life reaches zero, remove the missile and don't forget
// to expire it from the parent's perspective
MissileLife -= (float)delta;
if (MissileLife <= 0) {
QueueFree();
// there's got to be a better way
MyPlayer.ExpireMissile();
}
}
void _onSpaceMissileBodyEntered(Node body)
{
_serilogger.Debug("SpaceMissile.cs: Body entered!");
if (body.GetType().Name != "PlayerShip")
{
// We didn't hit another player, so remove ourselves, expire the missile, and return
// TODO: may want to decide to do something fancy here
QueueFree();
MyPlayer.ExpireMissile();
return;
}
// We hit another Player, so proceed
// EmitSignal("Hit", (PlayerShip)body);
_HandleHit((PlayerShip)body);
// Must be deferred as we can't change physics properties on a physics callback.
GetNode<CollisionShape2D>("CollisionShape2D").SetDeferred("disabled", true);
}
void _HandleHit(PlayerShip HitPlayer)
{
_serilogger.Debug("SpaceMissile.cs: Evaluating hit!");
QueueFree();
MyPlayer.ExpireMissile();
HitPlayer.TakeDamage(MissileDamage);
}
}