-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathWorld.cs
62 lines (55 loc) · 2.09 KB
/
World.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
using System.Collections.Generic;
using UnityEngine;
using StrategicGame.Common;
namespace StrategicGame.Client {
public class World : MonoBehaviour {
public GameObject CellPrefab;
public Unit UnitPrefab;
Transform _cellsRoot;
Transform _unitsRoot;
WorldParameters _params;
Dictionary<UnitId, Unit> _units = new Dictionary<UnitId, Unit>();
public Vector3 Center {
get { return new Vector3(_params.Width / 2f, 0, _params.Height / 2f); }
}
public void Initialize(WorldParameters worldParams) {
Debug.Assert(_params == null);
_params = worldParams;
_cellsRoot = CreateGroup("Cells");
_unitsRoot = CreateGroup("Units");
for (int x = 0; x < worldParams.Width; ++x)
for (int z = 0; z < worldParams.Height; ++z)
GameObject.Instantiate(
CellPrefab,
new Vector3(x, 0, z),
Quaternion.identity,
_cellsRoot);
}
public void UpdateUnitPosition(UnitPosition unitPosition) {
bool created;
var unit = GetOrInstantiate(unitPosition.Id, out created);
if (unit.Movement) {
unit.Movement.Moving = unitPosition.Moving;
unit.Movement.SetPosition(
new Vector3(unitPosition.X, 0, unitPosition.Y),
interpolate: !created);
}
}
Unit GetOrInstantiate(UnitId id, out bool created) {
created = false;
Unit unit;
if (!_units.TryGetValue(id, out unit)) {
unit = GameObject.Instantiate(UnitPrefab, _unitsRoot);
unit.Initialize(id);
_units.Add(id, unit);
created = true;
}
return unit;
}
Transform CreateGroup(string name) {
var transform = new GameObject(name).transform;
transform.SetParent(this.transform, false);
return transform;
}
}
}