-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBuildingRegistry.cs
More file actions
55 lines (49 loc) · 1.24 KB
/
BuildingRegistry.cs
File metadata and controls
55 lines (49 loc) · 1.24 KB
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
// SPDX-License-Identifier: MIT
using System.Collections.Generic;
using Godot;
/// <summary>
/// Registry haelt schnelle Indizes auf Gebaeude (per Zelle, per Id optional).
/// </summary>
public class BuildingRegistry
{
private readonly Dictionary<Vector2I, Building> byCell = new();
public void Add(Building b)
{
var size = b.Size;
for (int x = 0; x < size.X; x++)
{
for (int y = 0; y < size.Y; y++)
{
var c = new Vector2I(b.GridPos.X + x, b.GridPos.Y + y);
this.byCell[c] = b;
}
}
}
public void Remove(Building b)
{
var size = b.Size;
for (int x = 0; x < size.X; x++)
{
for (int y = 0; y < size.Y; y++)
{
var c = new Vector2I(b.GridPos.X + x, b.GridPos.Y + y);
if (this.byCell.ContainsKey(c) && this.byCell[c] == b)
{
this.byCell.Remove(c);
}
}
}
}
public Building? GetAt(Vector2I cell)
{
if (this.byCell.TryGetValue(cell, out var b))
{
return b;
}
return null;
}
public void Clear()
{
this.byCell.Clear();
}
}