summaryrefslogtreecommitdiff
path: root/scripts/TurnManager.cs
blob: ea9050e0d953ff7603e17f51be2520a97943d2ad (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
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);
	}
}