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?
RTS/Assets/Buildings/Building.cs
Go to fileThis commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
113 lines (96 sloc)
2.87 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 UnityEngine; | |
using System.Collections; | |
using System.Collections.Generic; | |
using RTS; | |
public class Building : RTSObject { | |
public Unit[] buildableUnits; | |
private Dictionary<string, Unit> units; | |
public float maxBuildProgress; | |
protected Queue< Unit > buildQueue; | |
private float currentBuildProgress = 0.0f; | |
private Vector3 spawnPoint; | |
private bool needsBuilding; | |
public int startBelow; | |
private Vector3 startSpawnPoint; | |
public Vector3 endSpawnPoint; | |
private float currentTime = 0.0f; | |
public bool constructing; | |
protected override void Awake() { | |
base.Awake(); | |
Debug.Log (objectName); | |
buildQueue = new Queue< Unit >(); | |
float spawnX = selectionBounds.center.x + transform.forward.x * selectionBounds.extents.x + transform.forward.x * 10; | |
float spawnZ = selectionBounds.center.z + transform.forward.z + selectionBounds.extents.z + transform.forward.z * 10; | |
spawnPoint = new Vector3 (spawnX, 0.0f, spawnZ); | |
units = new Dictionary<string, Unit> (); | |
for (int i = 0; i < buildableUnits.Length; i++) { | |
Unit buildableUnit = buildableUnits [i]; | |
units.Add (buildableUnit.objectName, buildableUnit); | |
} | |
endSpawnPoint = transform.position; | |
transform.position = new Vector3(transform.position.x, startBelow, transform.position.z); | |
constructing = true; | |
startSpawnPoint = transform.position; | |
} | |
protected override void Start () { | |
base.Start(); | |
} | |
protected override void Update () { | |
base.Update(); | |
if (!constructing) { | |
ProcessBuildQueue (); | |
} else { | |
Construct (); | |
} | |
} | |
protected override void OnGUI() { | |
base.OnGUI(); | |
} | |
protected void CreateUnit(string unitName) { | |
if (units.ContainsKey(unitName)) { | |
buildQueue.Enqueue(units[unitName]); | |
Debug.Log (unitName); | |
} | |
} | |
protected void ProcessBuildQueue() { | |
if(buildQueue.Count > 0) { | |
maxBuildProgress = buildQueue.Peek ().buildTime; | |
currentBuildProgress += Time.deltaTime * ResourceManager.BuildSpeed; | |
if(currentBuildProgress >= maxBuildProgress) { | |
InstantiateUnit (); | |
currentBuildProgress = 0.0f; | |
} | |
} | |
} | |
protected virtual void InstantiateUnit() { | |
if(player) player.AddUnit(buildQueue.Dequeue(), spawnPoint, transform.rotation); | |
} | |
public string[] getBuildQueueValues() { | |
string[] values = new string[buildQueue.Count]; | |
int pos=0; | |
foreach(Unit unit in buildQueue) values[pos++] = unit.objectName; | |
return values; | |
} | |
public float getBuildPercentage() { | |
return currentBuildProgress / maxBuildProgress; | |
} | |
public void StartConstruction() { | |
CalculateBounds(); | |
needsBuilding = true; | |
hitPoints = 0; | |
} | |
public bool UnderConstruction() { | |
return needsBuilding; | |
} | |
protected void Construct () { | |
if (constructing) { | |
if (transform.position == endSpawnPoint) { | |
constructing = false; | |
} else { | |
currentTime += Time.deltaTime; | |
float timeRatio = currentTime / buildTime; | |
transform.position = Vector3.Lerp (startSpawnPoint, endSpawnPoint, timeRatio); | |
} | |
} | |
} | |
} |