summaryrefslogtreecommitdiff
path: root/scenes/turn_manager.tscn
blob: 9e9a4f7ba24a2355f14f27ee10d69b8b1e771b38 (plain)
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
[gd_scene load_steps=2 format=3 uid="uid://ie3vpgatjvkw"]

[sub_resource type="CSharpScript" id="CSharpScript_4jeo4"]
script/source = "using Godot;
using System;
using System.Collections.Generic;
using System.Reflection.Metadata.Ecma335;

public partial class TurnManager : Node {
	[Signal]
	public delegate void turnBeginEventHandler();
	[Signal]
	public delegate void turnEndEventHandler();

	private Godot.Collections.Array<Node> actors = [];
	private int index = 0;

	public int TurnCount { get; private set; } = 1;

	public void Tick() {
		EmitSignal(SignalName.turnBegin);

		actors.Clear();
		actors = GetTree().GetNodesInGroup(\"TimeSlave\");

		index = -1;
		NextActor();
	}

	private void NextActor() {
		index++;
		if (index >= actors.Count) {
			EndTurn();
			return;
		}
		Actor currentActor = (Actor) actors[index];
		currentActor.Energy += currentActor.Speed;
		ActorPerformAction();
	}

	private void ActorPerformAction() {
		Actor currentActor = (Actor) actors[index];
		if (currentActor.Energy > 0) {
			currentActor.PerformAction();
		} else {
			NextActor();
		}
	}

	public void OnActionEnd() {
		ActorPerformAction();
	}

	private void EndTurn() {
		TurnCount++;
		EmitSignal(SignalName.turnEnd);
	}
}
"

[node name="TurnManager" type="Node"]
script = SubResource("CSharpScript_4jeo4")