Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using UnityEngine;
- using System.Collections;
- using System.Collections.Generic;
- public class CombatMode_SCRIPT : Character_CLASS {
- // Variable declaration
- public Vector3 playerPosition; // Dynamic variable that stores player's current location.
- public string activeTriggerVolume; // Dynamic variable that hooks into whatever trigger volume player encountered
- public GameObject[] enemySocketArray = new GameObject[0];
- public List<string> combatList; // List that tracks active combatants in a fight sequence
- public bool isCombatOver; // Boolean trigger for ending/starting combat
- private string playerAction = "Pass"; // Variable to store the player's action. Defaulted to 'Pass' to protect it from pre-assigned access.
- public GameObject enemyScript; // Public variable for enemy AI scripts to hook into and tell what the name of their script is.
- public Character_CLASS activeCombatantScript; // private dynamic variable to change to the script name of whatever the current combatant is
- public GameObject activeCombatant; // Dynamic GameObject variable to store current combat participant
- public GameObject activeDefender; // Dynamic GameObject variable to store current defender participant
- public Character_CLASS activeDefenderScript; // Dynamic Object variable to store current defender's script access
- private int totalDamage; // Dynamic integer used for calculating total damage to a Defender.
- // GUI variables
- private bool playerAction_isActive = false;
- // Use this for initialization
- void Start () {
- }
- // Update is called once per frame
- void Update () {
- }
- // Prepares everything for Combat Mode.
- public void setUpCombat(){
- // Saves the player's location, and relocates them to the player character's socket in the combat area.
- var playerCombatLocation = GameObject.FindWithTag ("PlayerCombat_SOCKET");
- var player = GameObject.FindWithTag("Player");
- playerPosition = player.transform.position;
- player.transform.position = playerCombatLocation.transform.position;
- // Talks to the trigger volume to obtain access to script
- var triggerVolume = GameObject.FindWithTag(activeTriggerVolume);
- var otherscript = triggerVolume.GetComponent<TestEncounterTriggerVolume_SCRIPT>();
- // Talks to the player script and sets boolean flag for 'isMovable'
- var playerscript = player.GetComponent<Player_SCRIPT>();
- playerscript.isMovable = false;
- // Adds player to the combatList list
- combatList.Add (player.transform.tag);
- // For loop running through each enemy. For each enemy, it generates a prefab, and instantiates it in the corresponding socket.
- // Solution assistance by Ryan Miller
- for (int i = 0; i < otherscript.enemyArray.Length; i++) {
- GameObject newEnemy = Instantiate (otherscript.enemyPrefab, Vector3.zero, Quaternion.identity) as GameObject;
- newEnemy.transform.position = GameObject.FindWithTag ("EnemyCombat0" + (i+1) + "_SOCKET").transform.position;
- // Assigns the new enemy a unique Tag and pushes it to the combatList list
- newEnemy.transform.tag = "Enemy0"+(i+1);
- combatList.Add(newEnemy.transform.tag);
- // Makes enemy face player
- var lookAt = GameObject.FindWithTag ("Player");
- var rot = Quaternion.LookRotation (lookAt.transform.position).eulerAngles;
- rot.x = 0;
- rot.z = 0;
- //rot.y +=180;
- newEnemy.transform.Find("Goblin").rotation = Quaternion.Euler(rot);
- }
- string result = string.Join(", ", combatList.ToArray());
- Debug.Log(result); // test debug
- // Sets isCombatOver to false and runs the combatRoutine void
- isCombatOver = false;
- combatRoutine();
- }
- public void combatRoutine(){
- if (isCombatOver == false){
- // runs the getPlayerAction routine
- getPlayerAction();
- // just a test to ensure the while loop is successful
- Debug.Log ("combat loop successful.");
- // isCombatOver = true;
- }
- }
- public void getPlayerAction(){
- // sets the visibility for the player action GUI in the OnGUI method
- playerAction_isActive = true;
- }
- // the actual run-once method that does the following:
- // Organizes combatants based on SPEED attributes
- // Performs an action for each Combatant
- // Checks to see if any one combatant is dead
- // Checks to see if all combatants on a team are dead
- public void combatFightSequence(){
- // Wraps up the sequence in a For loop to iterate through each combatant in the combatList
- foreach (string value in combatList) {
- // Makes player face active combatant; or the defender if they're the active combatant
- var targetLookPoint = GameObject.FindWithTag(value);
- if (activeCombatant.Equals("Player")){
- targetLookPoint = GameObject.FindWithTag(activeDefender.ToString());
- }
- else {
- targetLookPoint = GameObject.FindWithTag(activeCombatant.ToString());
- }
- var player = GameObject.FindWithTag ("Player");
- var newRotation = Quaternion.LookRotation (targetLookPoint.transform.position).eulerAngles;
- newRotation.x = 0;
- newRotation.z = 0;
- newRotation.y +=180;
- player.transform.Find("TestPlayer").rotation = Quaternion.Euler(newRotation);
- // Wraps playerAction in an if statement so it only happens if the current value of the string is "Player"
- if (value == "Player"){
- // Runs the Attack action if the player opted to attack
- if (playerAction == "Attack"){
- Debug.Log ("An attack was launched.");
- playerAction_isActive = false;
- activeCombatant = GameObject.FindWithTag(value);
- activeCombatantScript = activeCombatant.GetComponent<Player_SCRIPT>() as Player_SCRIPT;
- }
- }
- // if the current value is Enemy01, he does his action.
- if (value == "Enemy01"){
- Debug.Log (value + " attacked you.");
- // Sets up dynamic variables for action sequence
- activeCombatant = GameObject.FindWithTag (value);
- activeCombatantScript = activeCombatant.GetComponent<Goblin_SCRIPT>() as Goblin_SCRIPT;
- activeDefender = GameObject.FindWithTag("Player");
- activeDefenderScript = activeDefender.GetComponent<Player_SCRIPT>() as Player_SCRIPT;
- // Performs Test attack sequence against the player
- attackSequence();
- }
- if (value == "Enemy02"){
- Debug.Log (value + " did nothing.");
- activeCombatant = GameObject.FindWithTag (value);
- activeCombatantScript = activeCombatant.GetComponent<Goblin_SCRIPT>() as Goblin_SCRIPT;
- activeDefender = GameObject.FindWithTag("Player");
- activeDefenderScript = activeDefender.GetComponent<Player_SCRIPT>() as Player_SCRIPT;
- }
- if (value == "Enemy03"){
- Debug.Log (value + " did nothing.");
- activeCombatant = GameObject.FindWithTag (value);
- activeCombatantScript = activeCombatant.GetComponent<Goblin_SCRIPT>() as Goblin_SCRIPT;
- activeDefender = GameObject.FindWithTag("Player");
- activeDefenderScript = activeDefender.GetComponent<Player_SCRIPT>() as Player_SCRIPT;
- // makes the enemy attack the player
- attackSequence();
- }
- }
- // Runs through the combat sequence if isCombatOver is still false.
- if (isCombatOver == false){
- combatRoutine();
- }
- } // end of combatFightSequence
- private void attackSequence(){
- totalDamage = (activeCombatantScript.ATK + 10) - activeDefenderScript.DEF;
- activeDefenderScript.HP_current = activeDefenderScript.HP_current - totalDamage;
- Debug.Log (activeCombatant + " dealt " + totalDamage + " damage to " + activeDefender);
- }
- public void OnGUI(){
- // If playerAction_isActive is true, displays the player action GUI. Then launches the combatFightSequence method when it's clicked.
- if (playerAction_isActive == true){
- if (GUI.Button (new Rect (10,Screen.height-100,300,100), "Click to select an Attack action")) {
- // sets the playerAction to Attack.
- playerAction = "Attack";
- // calls on the combatFightSequence method.
- combatFightSequence();
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment