Permalink
Cannot retrieve contributors at this time
Name already in use
A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
strategic-game/Client/Assets/Scripts/World.cs
Go to fileThis commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
62 lines (55 sloc)
2.09 KB
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | |
} | |
} | |
} |