SebastianLawe

Untitled

Sep 30th, 2015
127
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 16.16 KB | None | 0 0
  1. using UnityEngine;
  2. using System.Collections;
  3. using System.Diagnostics;
  4. using Photon;
  5. using Assets.Scripts;
  6.  
  7. using System;
  8. using System.Collections.Generic;
  9.  
  10. public class AIController : PunBehaviour, IControlledObject {
  11.     public Transform target;
  12.     NavMeshAgent agent;
  13.     private bool targetSet;
  14.     private Vector3 lastFramePos;
  15.     public Stopwatch stunTimer;
  16.     public Animator animator;
  17.     public Rigidbody rigidbody;
  18.     public bool serverPrepDone;
  19.     public int maxStunTime;
  20.     public Color normalColor;
  21.     public FactionManager manager;
  22.     public ServerManager serverManager;
  23.     public TeamManager teamManager;
  24.     public bool stunned;
  25.     public int localPlayerNumber = 0;
  26.     //public PlayerIndex playerIndex;
  27.     public bool playerIndexSet;
  28.     IExternalGamePad GamePad;
  29.     public GroundCheck groundCheck;
  30.     public float x = 0;
  31.     public float y = 0;
  32.     public bool jump;
  33.     public string[] controllerPriority;
  34.     public GameObject[] objectsToDisableOnStun;
  35.     public GameObject[] objectsToEnableOnStun;
  36.     public Vector3 startingPos;
  37.     public float jumpForce;
  38.     public bool colorWasSet;
  39.     public Vector3 lastAgentPosition;
  40.     public DateTime lastAgentPositionCheck;
  41.     private bool agentHasBeenActivated;
  42.     private Stopwatch decisionTimer;
  43.  
  44.     public bool Stunned {
  45.         get {
  46.             return stunned;
  47.         }
  48.  
  49.         set {
  50.             stunned = value;
  51.         }
  52.     }
  53.  
  54.     public bool IsMine {
  55.         get {
  56.             return photonView.isMine;
  57.         }
  58.     }
  59.  
  60.     public FactionManager FactionManager {
  61.         get {
  62.             if (manager == null) {
  63.                 manager = GetComponent<FactionManager>();
  64.             }
  65.             return manager;
  66.         }
  67.  
  68.         set {
  69.             manager = value;
  70.         }
  71.     }
  72.  
  73.     public Color NormalColor {
  74.         get {
  75.             return normalColor;
  76.         }
  77.  
  78.         set {
  79.             normalColor = value;
  80.         }
  81.     }
  82.  
  83.     public bool ColorWasSet {
  84.         get {
  85.             return colorWasSet;
  86.         }
  87.  
  88.         set {
  89.             colorWasSet = value;
  90.         }
  91.     }
  92.  
  93.     public PhotonView PhotonViewReference {
  94.         get {
  95.             return photonView;
  96.         }
  97.     }
  98.  
  99.     public Vector3 StartingPosition {
  100.         get {
  101.             return startingPos;
  102.         }
  103.     }
  104.  
  105.     // Use this for initialization
  106.     void Start() {
  107.         controllerPriority = ControllerConfig.ControllerPriority();
  108.         Cursor.lockState = CursorLockMode.Confined;
  109.         Cursor.visible = false;
  110.         animator = GetComponent<Animator>();
  111.         rigidbody = GetComponent<Rigidbody>();
  112.         stunTimer = new Stopwatch();
  113.         manager = GetComponent<FactionManager>();
  114.         serverManager = FindObjectOfType<ServerManager>();
  115.         teamManager = FindObjectOfType<TeamManager>();
  116.         agent = GetComponent<NavMeshAgent>();
  117.         decisionTimer = Stopwatch.StartNew();
  118.         if (photonView.isMine) {
  119.             //localPlayerNumber = serverManager.localPlayers;
  120.         }
  121.         startingPos = transform.position;
  122.         if (teamManager.gameType == GameType.Elimination) {
  123.             GetComponent<TrailRenderer>().enabled = true;
  124.         }
  125.         if (!PhotonNetwork.connected) {
  126.             PhotonNetwork.offlineMode = true;
  127.             PhotonNetwork.JoinRoom("");
  128.         }
  129.     }
  130.  
  131.     void FixedUpdate() {
  132.     }
  133.  
  134.     // Update is called once per frame
  135.     void Update() {
  136.         if (localPlayerNumber == serverManager.botTurn++) {
  137.             if (decisionTimer.ElapsedMilliseconds >= 1000) {
  138.                 if (!stunned) {
  139.                     if (manager.color != Color.black) {
  140.                         if (normalColor != manager.color) {
  141.                             photonView.RPC("SetNormalColor", PhotonTargets.AllBuffered, manager.color.r, manager.color.g, manager.color.b);
  142.                         }
  143.                     }
  144.                 }
  145.                 if (serverPrepDone) {
  146.                     if (photonView.isMine) {
  147.                         if ((teamManager.teamAVictory || teamManager.teamBVictory || teamManager.eliminationGameComplete)) {
  148.                             transform.position = startingPos;
  149.                         }
  150.                     }
  151.                 }
  152.                 if (stunned) {
  153.                     foreach (GameObject item in objectsToDisableOnStun) {
  154.                         item.SetActive(false);
  155.                     }
  156.                     foreach (GameObject item in objectsToEnableOnStun) {
  157.                         item.SetActive(true);
  158.                     }
  159.                 } else {
  160.                     foreach (GameObject item in objectsToDisableOnStun) {
  161.                         item.SetActive(true);
  162.                     }
  163.                     foreach (GameObject item in objectsToEnableOnStun) {
  164.                         item.SetActive(false);
  165.                     }
  166.                 }
  167.                 if (serverPrepDone) {
  168.                     // If the game mode is CTF
  169.                     if (teamManager.gameType == GameType.CTF) {
  170.                         // Search for all flags
  171.                         bool iHaveTheFlag = false;
  172.                         foreach (EventReactor reactor in FindObjectsOfType<EventReactor>()) {
  173.                             // Is this flag ours?
  174.                             if (reactor.teamColor.r == manager.color.r && reactor.teamColor.g == manager.color.g && reactor.teamColor.b == manager.color.b) {
  175.                                 // Is this flag captured?
  176.                                 if (reactor.flagCaptured) {
  177.                                     // Check if we have the enemy flag. We arent worried about our own yet if we do.
  178.                                     if (!iHaveTheFlag) {
  179.                                         // The flag has been captured, stop whoever stole it!
  180.                                         if (reactor.currentPlayer != null) {
  181.                                             target = reactor.currentPlayer.FactionManager.transform;
  182.                                             agent.stoppingDistance = 5;
  183.                                         } else {
  184.                                             // This flag isn't tracking anybody now. Flag probably captured.
  185.                                             target = reactor.opposingFlag.transform;
  186.                                             agent.stoppingDistance = 0;
  187.                                         }
  188.                                     }
  189.                                 }
  190.                             }
  191.                             // Is this flag a different colour than us?
  192.                             if (reactor.teamColor.r != manager.color.r
  193.                                 || reactor.teamColor.g != manager.color.g
  194.                                 || reactor.teamColor.b != manager.color.b) {
  195.                                 // Is this flag captured?
  196.                                 if (reactor.flagCaptured) {
  197.                                     // Did we capture this flag?
  198.                                     if (reactor.currentPlayer == this) {
  199.                                         // Set our target to this flags enemy flag. Which is our flag.
  200.                                         target = reactor.opposingFlag.transform;
  201.                                         agent.stoppingDistance = 0;
  202.                                         iHaveTheFlag = true;
  203.                                     } else {
  204.                                         if (reactor.currentPlayer != null) {
  205.                                             // The flag has been captured, escort the person who captured it.
  206.                                             target = reactor.currentPlayer.FactionManager.transform;
  207.                                             agent.stoppingDistance = 5;
  208.                                         } else {
  209.                                             // This flag isn't tracking anybody now. Flag probably captured.
  210.                                             target = reactor.transform;
  211.                                             agent.stoppingDistance = 0;
  212.                                         }
  213.                                     }
  214.                                 } else {
  215.                                     // Lets capture the flag!
  216.                                     agent.stoppingDistance = 0;
  217.                                     target = reactor.transform;
  218.                                 }
  219.                             }
  220.                         }
  221.                     }
  222.                     decisionTimer.Reset();
  223.                     decisionTimer.Start();
  224.                 }
  225.             }
  226.             if (teamManager.gameType == GameType.Territores) {
  227.                 EventReactor closestReactor = null;
  228.                 EventReactor lastCheckedReactor = null;
  229.                 EventReactor lastCheckedInvalidReactor = null;
  230.                 foreach (EventReactor reactor in FindObjectsOfType<EventReactor>()) {
  231.                     if (reactor.factionManager.color.r != manager.color.r
  232.                         || reactor.factionManager.color.g != manager.color.g
  233.                         || reactor.factionManager.color.b != manager.color.b) {
  234.                         if (closestReactor == null) {
  235.                             closestReactor = reactor;
  236.                         }
  237.                         if (Vector3.Distance(transform.position, reactor.transform.position) < Vector3.Distance(transform.position, closestReactor.transform.position)) {
  238.                             targetSet = true;
  239.                             closestReactor = reactor;
  240.                         }
  241.                         lastCheckedReactor = reactor;
  242.                     }
  243.                     lastCheckedInvalidReactor = reactor;
  244.                 }
  245.                 if (closestReactor != null) {
  246.                     if (target != closestReactor) {
  247.                         target = closestReactor.transform;
  248.                     }
  249.                 } else {
  250.                     if (lastCheckedReactor != null) {
  251.                         if (target != lastCheckedReactor) {
  252.                             target = lastCheckedReactor.transform;
  253.                         }
  254.                     } else {
  255.                         if (lastCheckedInvalidReactor != null) {
  256.                             if (target != lastCheckedInvalidReactor) {
  257.                                 target = lastCheckedInvalidReactor.transform;
  258.                             }
  259.                         }
  260.                     }
  261.                 }
  262.                 Color color = target.GetComponent<FactionManager>().color;
  263.                 //if (color.r == manager.color.r && color.g == manager.color.g && color.b == manager.color.b) {
  264.                 //    foreach (EventReactor reactor in FindObjectsOfType<EventReactor>()) {
  265.                 //        if (reactor.factionManager.color.r != manager.color.r
  266.                 //            || reactor.factionManager.color.g != manager.color.g
  267.                 //            || reactor.factionManager.color.b != manager.color.b) {
  268.                 //            target = reactor.transform;
  269.                 //            targetSet = true;
  270.                 //            break;
  271.                 //        }
  272.                 //    }
  273.             }
  274.             if (serverManager.botTurn >= serverManager.botsSpawned) {
  275.                 serverManager.botTurn = 0;
  276.             }
  277.         }
  278.         if (teamManager.gameType == GameType.Elimination) {
  279.             PlayerController[] livePlayer = FindObjectsOfType<PlayerController>();
  280.             AIController[] aiPlayers = FindObjectsOfType<AIController>();
  281.             List<IControlledObject> controlledObjects = new List<IControlledObject>();
  282.             controlledObjects.AddRange(livePlayer);
  283.             controlledObjects.AddRange(aiPlayers);
  284.             IControlledObject nearestPlayer = null;
  285.             foreach (IControlledObject controlledObject in controlledObjects) {
  286.                 if (nearestPlayer == null) {
  287.                     nearestPlayer = controlledObject;
  288.                 }
  289.                 if (Vector3.Distance(transform.position, controlledObject.FactionManager.transform.position) < Vector3.Distance(transform.position, nearestPlayer.FactionManager.transform.position)) {
  290.                     if (!nearestPlayer.Stunned) {
  291.                         if (controlledObject != this) {
  292.                             nearestPlayer = controlledObject;
  293.                         }
  294.                     }
  295.                 }
  296.             }
  297.             if (nearestPlayer != null) {
  298.                 target = nearestPlayer.FactionManager.transform;
  299.                 agent.stoppingDistance = 5;
  300.             }
  301.         }
  302.         if (!stunTimer.IsRunning) {
  303.             //if (groundCheck.IsGrounded) {
  304.             if ((teamManager.teamAVictory || teamManager.teamBVictory)) {
  305.                 agent.enabled = false;
  306.             } else {
  307.                 agent.enabled = true;
  308.             }
  309.             if (lastAgentPosition == transform.position && (DateTime.Now - lastAgentPositionCheck).Seconds >= 5) {
  310.                 agent.enabled = false;
  311.                 agent.enabled = true;
  312.                 lastAgentPosition = transform.position;
  313.                 lastAgentPositionCheck = DateTime.Now;
  314.             }
  315.             //} else {
  316.             //    agent.enabled = false;
  317.             //}
  318.             agent.enabled = !stunned;
  319.             if (target != null) {
  320.                 if (photonView.isMine || !PhotonNetwork.offlineMode) {
  321.                     if (!stunned) {
  322.                         if (agent.pathEndPosition != target.position) {
  323.                             agent.SetDestination(target.position);
  324.                         }
  325.                     }
  326.                 }
  327.                 if (lastFramePos != transform.position && !stunned) {
  328.                     Animator(0, 1);
  329.                     lastFramePos = transform.position;
  330.                 } else {
  331.                     Animator(0, 0);
  332.                     targetSet = false;
  333.                 }
  334.             }
  335.         } else {
  336.             Animator(0, 0);
  337.             photonView.RPC("ResetStun", PhotonTargets.AllBuffered);
  338.         }
  339.     }
  340.  
  341.     [PunRPC]
  342.     void ResetStun() {
  343.         if (stunTimer.ElapsedMilliseconds >= maxStunTime && (teamManager.gameType != GameType.Elimination || teamManager.eliminationGameComplete)) {
  344.             stunTimer.Stop();
  345.             stunTimer.Reset();
  346.             manager.SetColor(normalColor.r, normalColor.g, normalColor.b);
  347.             agent.enabled = true;
  348.             stunned = false;
  349.         }
  350.     }
  351.  
  352.     [PunRPC]
  353.     public void SetNormalColor(float r, float g, float b) {
  354.         normalColor = new Color(r, g, b);
  355.     }
  356.  
  357.     [PunRPC]
  358.     public void Animator(float x, float y) {
  359.         if (animator != null) {
  360.             animator.SetFloat("MovementX", x);
  361.             animator.SetFloat("MovementY", y);
  362.         }
  363.     }
  364.     public virtual void Stun() {
  365.         CmdStun();
  366.     }
  367.     public virtual void CmdStun() {
  368.         photonView.RPC("RpcStun", PhotonTargets.AllBufferedViaServer);
  369.     }
  370.     [PunRPC]
  371.     public virtual void Teleport(float x, float y, float z) {
  372.         transform.position = new Vector3(x, y, z);
  373.     }
  374.     [PunRPC]
  375.     public virtual void RpcStun() {
  376.         if (!stunned) {
  377.             if (teamManager.gameType == GameType.CTF) {
  378.                 GetComponent<TrailRenderer>().enabled = false;
  379.             }
  380.             agent.enabled = false;
  381.             // Activate the stun.
  382.             stunTimer.Start();
  383.             // Track the players original color.
  384.             manager.SetColor(Color.black.r, Color.black.g, Color.black.b);
  385.             // Other code will check against this before trying to send another stun command.
  386.             stunned = true;
  387.             // Report that the stun was sent
  388.             //UnityEngine.Debug.Log("Stun Sent");
  389.         }
  390.     }
  391.     void OnCollisionEnter(Collision collision) {
  392.         if (!agentHasBeenActivated) {
  393.             agent.enabled = true;
  394.             agentHasBeenActivated = true;
  395.         }
  396.     }
  397.     void OnCollisionStay(Collision collision) {
  398.         if (!agentHasBeenActivated) {
  399.             agent.enabled = true;
  400.             agentHasBeenActivated = true;
  401.         }
  402.     }
  403. }
Advertisement
Add Comment
Please, Sign In to add comment