blob: f03aa20cf4a462bb340266485898e70c6e411cea (
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
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
|
using Godot;
using TheLegendOfGustav.Entities.Actors;
using TheLegendOfGustav.Entities.Actions;
using TheLegendOfGustav.InputHandling;
using TheLegendOfGustav.GUI;
using TheLegendOfGustav.Time;
using TheLegendOfGustav.Utils;
namespace TheLegendOfGustav;
/// <summary>
/// Classe principal do jogo.
/// Lar da lógica central do jogo.
/// </summary>
public partial class Game : Node
{
/// <summary>
/// Definição de um jogador.
/// </summary>
private static readonly PlayerDefinition playerDefinition = GD.Load<PlayerDefinition>("res://assets/definitions/actor/Player.tres");
/// <summary>
/// O jogo possui o mapa.
/// </summary>
private Map.Map map;
/// <summary>
/// Objeto para obter input do usuário.
/// </summary>
private InputHandler inputHandler;
/// <summary>
/// Gerenciador de turnos
/// </summary>
private TurnManager turnManager;
private Hud hud;
private SignalBus.EscapeRequestedEventHandler escapeLambda;
[Signal]
public delegate void MainMenuRequestedEventHandler();
public override void _Ready()
{
base._Ready();
escapeLambda = () => EmitSignal(SignalName.MainMenuRequested);
SignalBus.Instance.EscapeRequested += escapeLambda;
map = GetNode<Map.Map>("Map");
inputHandler = GetNode<InputHandler>("InputHandler");
hud = GetNode<Hud>("HUD");
// O jogador é criado pelo jogo.
Player player = new Player(Vector2I.Zero, null, playerDefinition);
Camera2D camera = GetNode<Camera2D>("Camera2D");
RemoveChild(camera);
player.HealthChanged += (int hp, int maxHp) => hud.OnHealthChanged(hp, maxHp);
player.ManaChanged += hud.OnManaChanged;
player.Died += () => inputHandler.SetInputHandler(InputHandlers.GameOver);
player.AddChild(camera);
map.Generate(player);
map.UpdateFOV(player.GridPosition);
turnManager = new(map);
MessageLogData.Instance.AddMessage("Boa sorte!");
}
public override void _Notification(int what)
{
if (what == NotificationPredelete)
{
if (escapeLambda != null)
{
SignalBus.Instance.EscapeRequested -= escapeLambda;
}
}
base._Notification(what);
}
/// <summary>
/// Método executa aproximadamente 60 vezes por segundo.
/// </summary>
/// <param name="delta"></param>
public override void _PhysicsProcess(double delta)
{
base._PhysicsProcess(delta);
Player player = map.MapData.Player;
// Pegamos uma ação do usuário
Action action = inputHandler.GetAction(player);
if (action != null)
{
turnManager.InsertPlayerAction(action);
}
// Computamos um turno.
turnManager.Tick();
}
}
|