Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using Maporino;
- using System.Collections.Generic;
- using UnityEngine;
- using UnityEngine.AI;
- public class PlayerAI : Player
- {
- // Units to operate
- public List<UnitController> units = new List<UnitController>();
- // Trade posts
- public List<TradePostController> tradePosts = new List<TradePostController>();
- // AI's buildings
- public List<BuildingController> buildings = new List<BuildingController>();
- // Enemy buildings
- public List<BuildingController> enemyBuildings = new List<BuildingController>();
- // Time between issuing new commands
- public float timeout = 1.0f;
- public float current = 1.0f;
- // Is AI awake?
- public bool awake = false;
- // Start is called once before the first execution of Update after the MonoBehaviour is created
- void Start()
- {
- }
- // Update is called once per frame
- void Update()
- {
- if (awake == false)
- {
- return;
- }
- current -= Time.deltaTime;
- if (current < 0.0f)
- {
- current = timeout;
- // Check if AI buildings are producing
- foreach (BuildingController building in buildings)
- {
- if (building != null && building.spawning == false && gold >= 200)
- {
- try
- {
- gold -= 200;
- building.spawning = true;
- }
- catch
- {
- buildings.Remove(building);
- }
- }
- }
- List<Transform> targets = new List<Transform>();
- // Check for trade posts not owned by AI
- foreach (TradePostController tradePost in tradePosts)
- {
- if (tradePost.owner != this)
- {
- targets.Add(tradePost.transform);
- }
- }
- // Add each enemy building to targets (if there are no trade posts anymore)
- if (targets.Count == 0)
- {
- foreach (BuildingController building in enemyBuildings)
- {
- if (building != null)
- {
- try
- {
- targets.Add(building.transform);
- }
- catch
- {
- enemyBuildings.Remove(building);
- }
- }
- }
- }
- // Gather all idle untis
- List<UnitController> idleUnits = new List<UnitController>();
- foreach (UnitController unit in units)
- {
- try
- {
- if (unit != null &&
- unit.commanded == false &&
- unit.GetComponent<AttackController>().target == null &&
- unit.GetComponent<NavMeshAgent>().remainingDistance < 1.0f)
- {
- idleUnits.Add(unit);
- }
- }
- catch
- {
- units.Remove(unit);
- }
- }
- // Pick a random group of idle units
- int groupSize = Random.Range(1, idleUnits.Count);
- // Select a single random target
- if (targets.Count > 0 && idleUnits.Count > 0)
- {
- Transform target = targets[Random.Range(0, targets.Count)];
- for (int i = 0; i < groupSize; i++)
- {
- if (idleUnits.Count == 0)
- {
- break;
- }
- int unitIndex = Random.Range(0, idleUnits.Count);
- UnitController unit = idleUnits[unitIndex];
- idleUnits.RemoveAt(unitIndex);
- if (unit != null)
- {
- unit.SetTarget(target.position);
- }
- }
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment