Guest User

CombatMode_SCRIPT

a guest
Nov 19th, 2013
145
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 8.17 KB | None | 0 0
  1. using UnityEngine;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4.  
  5. public class CombatMode_SCRIPT : Character_CLASS {
  6.    
  7.     // Variable declaration
  8.     public Vector3 playerPosition;               // Dynamic variable that stores player's current location.
  9.     public string activeTriggerVolume;           // Dynamic variable that hooks into whatever trigger volume player encountered
  10.     public GameObject[] enemySocketArray = new GameObject[0];
  11.     public List<string> combatList;             // List that tracks active combatants in a fight sequence
  12.     public bool isCombatOver;                  // Boolean trigger for ending/starting combat
  13.     private string playerAction = "Pass";       // Variable to store the player's action. Defaulted to 'Pass' to protect it from pre-assigned access.
  14.    
  15.     public GameObject enemyScript;                  // Public variable for enemy AI scripts to hook into and tell what the name of their script is.
  16.     public Character_CLASS activeCombatantScript;       // private dynamic variable to change to the script name of whatever the current combatant is
  17.     public GameObject activeCombatant;          // Dynamic GameObject variable to store current combat participant
  18.     public GameObject activeDefender;           // Dynamic GameObject variable to store current defender participant
  19.     public Character_CLASS activeDefenderScript;         // Dynamic Object variable to store current defender's script access
  20.    
  21.     private int totalDamage;                     // Dynamic integer used for calculating total damage to a Defender.
  22.    
  23.     // GUI variables
  24.     private bool playerAction_isActive = false;
  25.    
  26.    
  27.    
  28.    
  29.    
  30.    
  31.  
  32.     // Use this for initialization
  33.     void Start () {
  34.    
  35.     }
  36.    
  37.     // Update is called once per frame
  38.     void Update () {
  39.    
  40.     }
  41.    
  42.    
  43.    
  44.    
  45.    
  46.    
  47.    
  48.         // Prepares everything for Combat Mode.
  49.     public void setUpCombat(){
  50.        
  51.         // Saves the player's location, and relocates them to the player character's socket in the combat area.
  52.         var playerCombatLocation = GameObject.FindWithTag ("PlayerCombat_SOCKET");
  53.         var player = GameObject.FindWithTag("Player");
  54.         playerPosition = player.transform.position;
  55.         player.transform.position = playerCombatLocation.transform.position;
  56.        
  57.         // Talks to the trigger volume to obtain access to script
  58.         var triggerVolume = GameObject.FindWithTag(activeTriggerVolume);
  59.         var otherscript = triggerVolume.GetComponent<TestEncounterTriggerVolume_SCRIPT>();
  60.    
  61.         // Talks to the player script and sets boolean flag for 'isMovable'
  62.         var playerscript = player.GetComponent<Player_SCRIPT>();
  63.         playerscript.isMovable = false;
  64.        
  65.         // Adds player to the combatList list
  66.         combatList.Add (player.transform.tag);
  67.        
  68.        
  69.         // For loop running through each enemy. For each enemy, it generates a prefab, and instantiates it in the corresponding socket.
  70.         // Solution assistance by Ryan Miller
  71.         for (int i = 0; i < otherscript.enemyArray.Length; i++) {
  72.             GameObject newEnemy = Instantiate (otherscript.enemyPrefab, Vector3.zero, Quaternion.identity) as GameObject;
  73.             newEnemy.transform.position = GameObject.FindWithTag ("EnemyCombat0" + (i+1) + "_SOCKET").transform.position;
  74.            
  75.             // Assigns the new enemy a unique Tag and pushes it to the combatList list
  76.             newEnemy.transform.tag = "Enemy0"+(i+1);
  77.             combatList.Add(newEnemy.transform.tag);
  78.            
  79.             // Makes enemy face player
  80.             var lookAt = GameObject.FindWithTag ("Player");
  81.             var rot = Quaternion.LookRotation (lookAt.transform.position).eulerAngles;
  82.             rot.x = 0;
  83.             rot.z = 0;
  84.             //rot.y +=180;
  85.             newEnemy.transform.Find("Goblin").rotation = Quaternion.Euler(rot);
  86.         }
  87.        
  88.         string result = string.Join(", ", combatList.ToArray());
  89.         Debug.Log(result); // test debug
  90.        
  91.         // Sets isCombatOver to false and runs the combatRoutine void
  92.         isCombatOver = false;
  93.         combatRoutine();
  94.     }
  95.    
  96.    
  97.    
  98.    
  99.     public void combatRoutine(){
  100.        
  101.         if (isCombatOver == false){
  102.            
  103.             // runs the getPlayerAction routine
  104.             getPlayerAction();
  105.            
  106.             // just a test to ensure the while loop is successful
  107.             Debug.Log ("combat loop successful.");
  108.             // isCombatOver = true;
  109.         }
  110.        
  111.     }
  112.    
  113.    
  114.     public void getPlayerAction(){
  115.        
  116.         // sets the visibility for the player action GUI in the OnGUI method
  117.         playerAction_isActive = true;
  118.        
  119.     }
  120.    
  121.    
  122.    
  123.    
  124.    
  125.    
  126.    
  127.    
  128.    
  129.     // the actual run-once method that does the following:
  130.     // Organizes combatants based on SPEED attributes
  131.     // Performs an action for each Combatant
  132.     // Checks to see if any one combatant is dead
  133.     // Checks to see if all combatants on a team are dead
  134.     public void combatFightSequence(){
  135.        
  136.        
  137.         // Wraps up the sequence in a For loop to iterate through each combatant in the combatList
  138.         foreach (string value in combatList) {
  139.            
  140.             // Makes player face active combatant; or the defender if they're the active combatant
  141.             var targetLookPoint = GameObject.FindWithTag(value);
  142.             if (activeCombatant.Equals("Player")){
  143.                 targetLookPoint = GameObject.FindWithTag(activeDefender.ToString());
  144.             }
  145.            
  146.            
  147.            
  148.            
  149.             else {
  150.                 targetLookPoint = GameObject.FindWithTag(activeCombatant.ToString());
  151.             }
  152.             var player = GameObject.FindWithTag ("Player");
  153.             var newRotation = Quaternion.LookRotation (targetLookPoint.transform.position).eulerAngles;
  154.             newRotation.x = 0;
  155.             newRotation.z = 0;
  156.             newRotation.y +=180;
  157.             player.transform.Find("TestPlayer").rotation = Quaternion.Euler(newRotation);
  158.        
  159.             // Wraps playerAction in an if statement so it only happens if the current value of the string is "Player"
  160.             if (value == "Player"){
  161.                 // Runs the Attack action if the player opted to attack
  162.                 if (playerAction == "Attack"){
  163.                     Debug.Log ("An attack was launched.");
  164.                     playerAction_isActive = false;
  165.                     activeCombatant = GameObject.FindWithTag(value);
  166.                     activeCombatantScript = activeCombatant.GetComponent<Player_SCRIPT>() as Player_SCRIPT;
  167.                 }
  168.             }
  169.            
  170.             // if the current value is Enemy01, he does his action.
  171.             if (value == "Enemy01"){
  172.                 Debug.Log (value + " attacked you.");
  173.                 // Sets up dynamic variables for action sequence
  174.                 activeCombatant = GameObject.FindWithTag (value);
  175.                 activeCombatantScript = activeCombatant.GetComponent<Goblin_SCRIPT>() as Goblin_SCRIPT;
  176.                 activeDefender = GameObject.FindWithTag("Player");
  177.                 activeDefenderScript = activeDefender.GetComponent<Player_SCRIPT>() as Player_SCRIPT;
  178.                
  179.                 // Performs Test attack sequence against the player
  180.                 attackSequence();
  181.             }
  182.            
  183.             if (value == "Enemy02"){
  184.                 Debug.Log (value + " did nothing.");
  185.                 activeCombatant = GameObject.FindWithTag (value);
  186.                 activeCombatantScript = activeCombatant.GetComponent<Goblin_SCRIPT>() as Goblin_SCRIPT;
  187.                 activeDefender = GameObject.FindWithTag("Player");
  188.                 activeDefenderScript = activeDefender.GetComponent<Player_SCRIPT>() as Player_SCRIPT;
  189.             }
  190.            
  191.             if (value == "Enemy03"){
  192.                 Debug.Log (value + " did nothing.");
  193.                 activeCombatant = GameObject.FindWithTag (value);
  194.                 activeCombatantScript = activeCombatant.GetComponent<Goblin_SCRIPT>() as Goblin_SCRIPT;
  195.                 activeDefender = GameObject.FindWithTag("Player");
  196.                 activeDefenderScript = activeDefender.GetComponent<Player_SCRIPT>() as Player_SCRIPT;
  197.                
  198.                 // makes the enemy attack the player
  199.                 attackSequence();
  200.             }
  201.         }
  202.        
  203.        
  204.        
  205.         // Runs through the combat sequence if isCombatOver is still false.
  206.         if (isCombatOver == false){
  207.             combatRoutine();
  208.         }
  209.        
  210.     } // end of combatFightSequence
  211.    
  212.    
  213.    
  214.    
  215.    
  216.    
  217.     private void attackSequence(){
  218.        
  219.         totalDamage = (activeCombatantScript.ATK + 10) - activeDefenderScript.DEF;
  220.         activeDefenderScript.HP_current = activeDefenderScript.HP_current - totalDamage;
  221.         Debug.Log (activeCombatant + " dealt " + totalDamage + " damage to " + activeDefender);
  222.        
  223.     }
  224.    
  225.    
  226.    
  227.    
  228.    
  229.    
  230.    
  231.    
  232.    
  233.    
  234.    
  235.     public void OnGUI(){
  236.        
  237.         // If playerAction_isActive is true, displays the player action GUI. Then launches the combatFightSequence method when it's clicked.
  238.         if (playerAction_isActive == true){
  239.             if (GUI.Button (new Rect (10,Screen.height-100,300,100), "Click to select an Attack action")) {
  240.                 // sets the playerAction to Attack.
  241.                 playerAction = "Attack";
  242.                 // calls on the combatFightSequence method.
  243.                 combatFightSequence();
  244.             }
  245.         }
  246.        
  247.     }
  248. }
Advertisement
Add Comment
Please, Sign In to add comment