Advertisement
Guest User

Before

a guest
Jul 21st, 2019
176
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 11.67 KB | None | 0 0
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using System.Linq;
  5. using UnityEngine;
  6.  
  7. public class ChampionController : UnitController
  8. {
  9.     //Capturepoint data.
  10.     public CapturePoint[] capturesPoints;
  11.     private int targetCapturePoint;
  12.  
  13.     //Variables for threat logic
  14.     public float threatLvl = 0;
  15.     private float dangerLevel = 5;
  16.     private float minThreatLvl = -5;
  17.     private float maxThreatLvl = 10;
  18.  
  19.     //Initial position
  20.     Vector3 initialPosition;
  21.     Vector3 basePosition;
  22.  
  23.     //Range distances
  24.     private float aggressionRange = 8;
  25.     private float threatRange = 10;
  26.     private const float capturePointRange = 5;
  27.     private float collabDistance = 30;
  28.  
  29.     //Ally gameobject reference
  30.     public ChampionController ally;
  31.     public bool isFighting = false;
  32.  
  33.     //On start, get a reference to all capture points and set a target.
  34.     protected override void Start()
  35.     {
  36.         base.Start();
  37.         capturesPoints = FindObjectsOfType<CapturePoint>();
  38.         targetCapturePoint = getClosestCapturePointOfInterest();
  39.         initialPosition = transform.position;
  40.         basePosition = GameObject.FindWithTag("redBase").transform.position;
  41.     }
  42.  
  43.     void Update()
  44.     {
  45.         calculateThreatLevel();
  46.  
  47.         //As long as we are not in danger...
  48.         if (!inDanger())
  49.         {
  50.             var enemy = FindClosestEnemy();
  51.             if (enemy != null && enemy != target)
  52.             {
  53.                 Fight(enemy);
  54.             }
  55.             //else if im in range and the target point is captured, get a new capture point.
  56.             else if (InRangeOfPoint() && TargetPointIsCaptured())
  57.             {
  58.                 targetCapturePoint = getClosestCapturePointOfInterest();
  59.                 SetTargetCapturePoint(targetCapturePoint);
  60.                 isFighting = false;
  61.             }
  62.             //else if im not moving and either im not in range of my target or the target point is captured, go to the capture point
  63.             else if (isStill && !InRangeOfPoint())
  64.             {
  65.                 SetTargetCapturePoint(targetCapturePoint);
  66.                 isFighting = false;
  67.             }
  68.         }
  69.         else
  70.         {
  71.             NeedHelp();
  72.         }
  73.     }
  74.  
  75.     void FindEnemy()
  76.     {
  77.         //Get the closest enemy to us if we see one.
  78.         var enemy = FindClosestEnemy();
  79.         if (enemy != null && enemy != target)
  80.         {
  81.             if (Vector3.Distance(enemy.transform.position, transform.position) <= (aggressionRange / 2.0f))
  82.                 Attack(enemy, false);
  83.             else
  84.                 Fire(enemy.transform.position);
  85.         }
  86.     }
  87.  
  88.     void Fight(UnitController enemy)
  89.     {
  90.         isFighting = true;
  91.  
  92.         if(enemy != null)
  93.         {
  94.             if (threatened())
  95.             {
  96.                 if (PathfindingGraph.instance.HasClearPath(transform.position, enemy.transform.position, 0.5f))
  97.                 {
  98.                     var distance = Vector3.Distance(transform.position, enemy.transform.position);
  99.                     var timeToTarget = distance / projectile.GetComponent<Projectile>().speed;
  100.                     Fire(enemy.transform.position + (enemy.velocity * timeToTarget));
  101.                 }
  102.                 else if (AllyInRange() && ally.isFighting)
  103.                 {
  104.                     Vector3 newDirection = enemy.transform.position + (enemy.transform.position - ally.transform.position);
  105.                     Arrive(transform.position + (newDirection - transform.position).normalized * 3, false);
  106.                 }
  107.                 else
  108.                 {
  109.                     Arrive(transform.position + (transform.position - enemy.transform.position).normalized * 3, false);
  110.                 }
  111.             }
  112.             else
  113.             {
  114.                 if (PathfindingGraph.instance.HasClearPath(transform.position, enemy.transform.position, 0.5f))
  115.                 {
  116.                     var distance = Vector3.Distance(transform.position, enemy.transform.position);
  117.                     var timeToTarget = distance / projectile.GetComponent<Projectile>().speed;
  118.                     Fire(enemy.transform.position + (enemy.velocity * timeToTarget));
  119.                 }
  120.                 else
  121.                 {
  122.                     Attack(enemy, false);
  123.                 }
  124.             }
  125.         }
  126.     }
  127.  
  128.     //Returns if its in danger.
  129.     bool inDanger()
  130.     {
  131.         return threatLvl >= maxThreatLvl || IsCritical();
  132.     }
  133.  
  134.     //Returns if its in critical condition.
  135.     bool IsCritical()
  136.     {
  137.         return hitPoints <= (totalHitPoints / 3.0f);
  138.     }
  139.  
  140.     //Returns if it's threatened.
  141.     bool threatened()
  142.     {
  143.         //print(gameObject.name + " feels threatened");
  144.         return threatLvl >= dangerLevel || hitPoints <= (totalHitPoints / 2.0f);
  145.     }
  146.  
  147.     //Returns whether the ally is in range to collaborate.
  148.     bool AllyInRange()
  149.     {
  150.         return ally != null && !ally.inDanger() && Vector3.Distance(transform.position, ally.transform.position) <= collabDistance;
  151.     }
  152.  
  153.     void OnDrawGizmos()
  154.     {
  155.         Gizmos.color = Color.red;
  156.         Gizmos.DrawWireSphere(transform.position, aggressionRange);
  157.         Gizmos.color = Color.yellow;
  158.         Gizmos.DrawWireSphere(transform.position, threatRange);
  159.         Gizmos.color = Color.green;
  160.         Gizmos.DrawWireSphere(transform.position, collabDistance);
  161.     }
  162.  
  163.     //Gets the closest point of interest based on neutrality or enemy capture.
  164.     int getClosestCapturePointOfInterest()
  165.     {
  166.         float shortestDistance = Mathf.Infinity;
  167.         int bestPoint = 0;
  168.         for (int i = 0; i < capturesPoints.Length; i++)
  169.         {
  170.             if (!capturesPoints[i].IsOwnedByTeam(team) && i != ally.targetCapturePoint)
  171.             {
  172.                 float distanceToPoint = Vector3.Distance(transform.position, capturesPoints[i].transform.position);
  173.                 if (distanceToPoint < shortestDistance)
  174.                 {
  175.                     shortestDistance = distanceToPoint;
  176.                     bestPoint = i;
  177.                 }
  178.             }
  179.         }
  180.         //print(gameObject.name +  " thinks that the Shortest distance is: " + shortestDistance + " to point " + bestPoint);
  181.         return bestPoint;
  182.     }
  183.     //Calculates the closest and safest point based on distance and number of ally units on that point.
  184.     int getClosestSafetyPoint()
  185.     {
  186.         float shortestDistance = Mathf.Infinity;
  187.         int bestPoint = targetCapturePoint;
  188.         for (int i = 0; i < capturesPoints.Length; i++)
  189.         {
  190.             if (capturesPoints[i].IsOwnedByTeam(team) && capturesPoints[i].redTeam > capturesPoints[i].blueTeam)
  191.             {
  192.                 float distanceToPoint = Vector3.Distance(transform.position, capturesPoints[i].transform.position);
  193.                 if (distanceToPoint < shortestDistance)
  194.                 {
  195.                     shortestDistance = distanceToPoint;
  196.                     bestPoint = i;
  197.                 }
  198.             }
  199.         }
  200.         //print(gameObject.name + " thinks that the safest point is: " + bestPoint + " with red: " + capturesPoints[bestPoint].redTeam + " vs blue: " + capturesPoints[bestPoint].blueTeam);
  201.         return bestPoint;
  202.     }
  203.  
  204.     // Ask ally to help on the point
  205.     public void NeedHelp()
  206.     {
  207.         // If not to far, make ally come to the point and fight with him
  208.         var enemy = FindClosestEnemy();
  209.         if (enemy != null && AllyInRange() && hitPoints > 1)
  210.         {
  211.             ally.SetTargetCapturePoint(targetCapturePoint);
  212.             if (enemy != null && enemy != target)
  213.             {
  214.                 Fight(enemy);
  215.             }
  216.             else
  217.             {
  218.                 Arrive(basePosition, true, capturePointRange, Heal);
  219.                 isFighting = false;
  220.             }
  221.         }
  222.         // Go to the base to heal.
  223.         else if(IsCritical())
  224.         {
  225.             Arrive(basePosition, true, capturePointRange, Heal);
  226.             isFighting = false;
  227.         }
  228.     }
  229.  
  230.     // Check if the champion is in range of its target capture point.
  231.     private bool InRangeOfPoint()
  232.     {
  233.         var capturePoint = capturesPoints[targetCapturePoint];
  234.         var distance = Vector3.Distance(transform.position, capturePoint.transform.position);
  235.         return distance <= capturePointRange;
  236.     }
  237.  
  238.     // Set the target capture point of the champion.
  239.     // The champion will move toward it.
  240.     public void SetTargetCapturePoint(int capturePointIndex)
  241.     {
  242.         targetCapturePoint = capturePointIndex;
  243.         var capturePoint = capturesPoints[targetCapturePoint];
  244.         Arrive(capturePoint.transform.position, true, capturePointRange, OnEnterCapturePoint);
  245.     }
  246.  
  247.     // Callback called when the champion enters its target capture point.
  248.     private void OnEnterCapturePoint()
  249.     {
  250.         if (TargetPointIsCaptured())
  251.         {
  252.             SetTargetCapturePoint(getClosestCapturePointOfInterest());
  253.         }
  254.     }
  255.     // Check if the target capture point is captured.
  256.     private bool TargetPointIsCaptured()
  257.     {
  258.         var capturePoint = capturesPoints[targetCapturePoint].GetComponent<CapturePoint>();
  259.         return capturePoint != null && capturePoint.IsOwnedByTeam(team);
  260.     }
  261.  
  262.     //Calculates the threat level by counting ally units vs enemy units, when there is no threat, lvl is -5
  263.     //Can be put in with closest enemy method but for now we can leave it like this.
  264.     void calculateThreatLevel()
  265.     {
  266.         Collider[] colliders = Physics.OverlapSphere(
  267.         transform.position,
  268.         threatRange,
  269.         LayerMask.GetMask("Units"),
  270.         QueryTriggerInteraction.Ignore);
  271.         if (colliders.Length <= 1)
  272.         {
  273.             threatLvl = minThreatLvl;
  274.         }
  275.         var orderedColliders = colliders.OrderBy(
  276.         collider => Vector3.Distance(collider.transform.position, transform.position));
  277.         foreach(Collider col in orderedColliders)
  278.         {
  279.             if(threatLvl < maxThreatLvl)
  280.             {
  281.                 if (col.tag == "bluePlayer")
  282.                 {
  283.                     threatLvl += 5;
  284.                 }
  285.                 else if (col.tag == "blueNPC")
  286.                 {
  287.                     threatLvl++;
  288.                 }
  289.             }
  290.             if(threatLvl > minThreatLvl)
  291.             {
  292.                 if (col.tag == "redPlayer")
  293.                 {
  294.                     threatLvl -= 5;
  295.                 }
  296.                 else if (col.tag == "redNPC")
  297.                 {
  298.                     threatLvl--;
  299.                 }
  300.             }
  301.         }
  302.     }
  303.  
  304.     private UnitController FindClosestEnemy()
  305.     {
  306.         Collider[] colliders = Physics.OverlapSphere(
  307.             transform.position,
  308.             aggressionRange,
  309.             LayerMask.GetMask("Units"),
  310.             QueryTriggerInteraction.Ignore);
  311.  
  312.         //Check to see if there is a player around first.
  313.         foreach (Collider collider in colliders)
  314.         {
  315.             if (collider.tag == "bluePlayer")
  316.             {
  317.                 var enemy = collider.GetComponent<UnitController>();
  318.                 if (enemy != null)
  319.                 {
  320.                     return enemy;
  321.                 }
  322.             }
  323.         }
  324.  
  325.         var orderedColliders = colliders.OrderBy(
  326.             collider => Vector3.Distance(collider.transform.position, transform.position));
  327.         foreach (Collider collider in orderedColliders)
  328.         {
  329.             if (collider.tag.StartsWith(enemyTeam))
  330.             {
  331.                 var enemy = collider.GetComponent<UnitController>();
  332.                 if (enemy != null)
  333.                 {
  334.                     return enemy;
  335.                 }
  336.             }
  337.         }
  338.         return null;
  339.     }
  340. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement