-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathGame.cs
274 lines (226 loc) · 7.06 KB
/
Game.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
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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
using System;
using System.Linq;
using CityBuilder.Components;
using CityBuilder.Core.Components;
using CityBuilder.Core.Components.AI;
using CityBuilder.Core.Components.Flags;
using CityBuilder.Core.Components.Inventory;
using CityBuilder.Core.Components.Needs;
using CityBuilder.Core.Components.Production;
using CityBuilder.Core.Systems;
using CityBuilder.Core.Systems.AI;
using CityBuilder.GUI;
using CityBuilder.Messages;
using CityBuilder.ModSupport;
using CityBuilder.Systems;
using CityBuilder.Systems.UI;
using DefaultEcs;
using DefaultEcs.System;
using Godot;
using Input = CityBuilder.Core.Components.Production.Input;
using World = DefaultEcs.World;
namespace CityBuilder;
public class Game : Control
{
private const int MapSize = 64 * 1000;
public static World World { get; } = new();
public static IPublisher Publisher => World;
private Map? _map;
private Map Map => _map ??= GetNode<Map>("Map");
private CanvasItem? _mainMenu;
public CanvasItem MainMenu => _mainMenu ??= FindNode<CanvasItem>("MainMenu");
private CanvasItem? _inGameMenu;
public CanvasItem InGameMenu => _inGameMenu ??= FindNode<CanvasItem>("InGameMenu");
private Node? inGameUI;
private ISystem<float> _system = null!;
public Game()
{
World.Subscribe(this);
}
public override void _Ready()
{
Map.Initialize(new Vector2(MapSize, MapSize));
var collisionSystem = new CollisionSystem(World, 0, 0, MapSize, MapSize);
World.SetMaxCapacity<ICollisionSystem>(1);
World.Set<ICollisionSystem>(collisionSystem);
var inventorySystem = new InventorySystem(World);
World.SetMaxCapacity<IInventorySystem>(1);
World.Set<IInventorySystem>(inventorySystem);
World.SubscribeComponentAdded((in Entity entity, in Market _) =>
{
if (!entity.Has<Good>())
{
inventorySystem.EnsureCreated(entity, Goods.Food);
}
});
World.SubscribeComponentAdded((in Entity entity, in Agent _) => entity.Set(BehaviorState.Deciding));
World.SubscribeComponentAdded((in Entity entity, in HungerRate _) => entity.Set<Hunger>());
World.SubscribeComponentAdded((in Entity entity, in TirednessRate _) => entity.Set<Tiredness>());
World.SubscribeComponentAdded((in Entity entity, in Input input) =>
{
foreach (var good in input.Value.Keys)
{
inventorySystem.EnsureCreated(entity, good);
}
entity.AddFlag(CanNotWorkReason.NoInput);
});
World.SubscribeComponentAdded((in Entity entity, in Output output)
=> inventorySystem.EnsureCreated(entity, output.Good));
_system = new SequentialSystem<float>(
new RemoveSystem(World, collisionSystem),
new CollectResourceSystem(World, collisionSystem),
new SpriteCreationSystem(World, Map.EntityRoot),
new SpritePositionSystem(World),
new SpriteRotationSystem(World),
new SizeFromSpriteSystem(World),
new NavigationInitSystem(World),
new NavigationMeshSystem(World, Map),
new NavigationDestinationSystem(World),
new NavigationSystem(World),
new MovementSystem(World),
collisionSystem,
new LocationSensorSystem(World),
new AISystem(World, System.Numerics.Vector2.One * MapSize, inventorySystem),
new TransportReservationSystem(World),
new WaitingSystem(World),
new HungerSystem(World),
new TirednessSystem(World),
new HousingSystem(World),
new SleepSystem(World, 10f),
new WorkSystem(World),
new WorkingSystem(World),
new ProductionWithInputSystem(World, inventorySystem),
new ProductionSystem(World, inventorySystem),
new InventoryFullSystem(World),
new CheckInputSystem(World, inventorySystem),
new ConstructionSystem(World),
new ConstructionProgressVisualisationInitSystem(World),
new ConstructionProgressVisualisationSystem(World),
new MovementRotationSystem(World),
new ResourcePileVisualisationSystem(World),
new ResourcePileRemovalSystem(World),
new GrowthInitSystem(World),
new GrowthSystem(World),
new GrowthVisualisationSystem(World));
var textureManager = new TextureManager();
textureManager.Manage(World);
var modLoader = new ModLoader(textureManager, ProjectSettings.GlobalizePath("res://mods"));
modLoader.LoadMods();
}
public override void _Process(float delta)
{
_system.Update(delta);
}
public override void _UnhandledInput(InputEvent @event)
{
if (@event.IsActionPressed(InputAction.MouseclickLeft))
{
var mousePosition = GetGlobalMousePosition();
var selected = World.Get<ICollisionSystem>().GetEntities(mousePosition.ToNumericsVector())
.FirstOrDefault();
if (selected.IsAlive)
{
World.Publish(new EntitySelected(selected));
}
}
}
[Subscribe]
private void On(in BlueprintPlacedMessage message)
{
if (message.Position.Value is not { X: > 0 and < MapSize, Y: > 0 and < MapSize })
{
return;
}
if (false)
{
var random = new Random();
for (var i = 0; i < 1000; i++)
{
var entity = World.CreateEntity();
var position = new System.Numerics.Vector2(100000 * (float)random.NextDouble(),
100000 * (float)random.NextDouble());
entity.Set(new Position(position));
}
}
else
{
var position = message.Position;
var blueprint = message.Blueprint;
var rotation = message.Rotation;
if (message.Blueprint.Construction is not null)
{
var entity = World.CreateEntity();
entity.Set(position);
entity.Set(rotation);
blueprint.PopulateConstruction(entity);
}
else
{
World.Publish(new FinishedBuilding(blueprint, position, rotation));
}
}
}
[Subscribe]
private void On(in FinishedBuilding message)
{
var entity = World.CreateEntity();
entity.Set(message.Position);
entity.Set(message.Rotation);
message.Blueprint.Populate(entity);
}
[Subscribe]
private void On(in StartNewGameMessage message)
{
MainMenu.Visible = false;
var mapBackground = GD.Load<PackedScene>("user://map.scn").Instance<Node2D>();
mapBackground.Name = "Background";
mapBackground.ZIndex = -128;
Map.AddChild(mapBackground);
inGameUI = GD.Load<PackedScene>("res://GUI/InGameUI.tscn").Instance();
GetNode("GUI").AddChild(inGameUI);
for (var i = 0; i <= message.Population; i++)
{
World.Publish(new BlueprintPlacedMessage(Global.Blueprints["Human"], new Position(100 * i, 100), 0));
}
World.Get<IInventorySystem>().CreatePile(new Position(200, 100), "Wood", 10);
}
private void NavigateToMainMenu(string subMenu)
{
var overview = FindNode<CanvasItem>("Overview");
var newGameMenu = FindNode<CanvasItem>("NewGameMenu");
if (subMenu == "NewGameMenu")
{
overview.Visible = false;
newGameMenu.Visible = true;
}
else
{
newGameMenu.Visible = false;
overview.Visible = true;
}
}
public void ExitGame()
{
GetTree().Quit();
}
[Subscribe]
private void On(in CloseInGameMenuMessage message)
{
InGameMenu.Visible = false;
}
[Subscribe]
private void On(in OpenMainMenuMessage message)
{
foreach (var entity in World)
{
entity.Dispose();
}
Map.RemoveChild(Map.GetNode("Background"));
Map.Reset();
GetNode("GUI").RemoveChild(inGameUI);
inGameUI!.QueueFree();
MainMenu.Visible = true;
NavigateToMainMenu("Overview");
}
private T FindNode<T>(NodePath nodePath) where T : Node => (T)FindNode(nodePath, owned: false);
}