Last active
March 15, 2018 17:00
-
-
Save BenTristem/1bdc2d781b39bf293ec8be489622ea0b to your computer and use it in GitHub Desktop.
A circular or ring buffer system for placing RTS towers. Part of https://www.udemy.com/unitycourse2
This file contains hidden or 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
public class TowerFactory : MonoBehaviour { | |
[SerializeField] Tower towerPrefab; | |
[SerializeField] int towerLimit = 5; | |
Queue<Tower> towerQueue = new Queue<Tower>(); | |
public void AddTower(Waypoint waypoint) | |
{ | |
Vector3 newPos = waypoint.transform.position; | |
if (towerQueue.Count < towerLimit) | |
{ | |
InstantiateNewTower(waypoint, newPos); | |
} | |
else | |
{ | |
MoveExistingTower(waypoint, newPos); | |
} | |
} | |
private void InstantiateNewTower(Waypoint waypoint, Vector3 newPos) | |
{ | |
var newTower = Instantiate(towerPrefab, newPos, Quaternion.identity); | |
newTower.waypoint = waypoint; | |
waypoint.isPlaceable = false; | |
towerQueue.Enqueue(newTower); | |
} | |
private void MoveExistingTower(Waypoint newWayPoint, Vector3 newPos) | |
{ | |
var oldTower = towerQueue.Dequeue(); | |
oldTower.waypoint.isPlaceable = true; | |
newWayPoint.isPlaceable = false; | |
oldTower.waypoint = newWayPoint; | |
oldTower.transform.position = newPos; | |
towerQueue.Enqueue(oldTower); // stick it back on top of the pile | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment