blob: 47102c11fea4c88a1dbe0281dd882fd6670e722f (
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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
|
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
{
private bool initialized = false;
/// <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;
}
public void NewGame()
{
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!");
initialized = true;
}
public bool LoadGame()
{
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.AddChild(camera);
if (!map.LoadGame(player))
{
return false;
}
player.HealthChanged += (int hp, int maxHp) => hud.OnHealthChanged(hp, maxHp);
player.ManaChanged += hud.OnManaChanged;
player.Died += () => inputHandler.SetInputHandler(InputHandlers.GameOver);
map.UpdateFOV(player.GridPosition);
turnManager = new(map);
MessageLogData.Instance.AddMessage("Boa sorte!");
initialized = true;
return true;
}
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);
if(initialized)
{
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();
}
}
}
|