Action_Abilities - robblofield/TomeboundDocs GitHub Wiki

Action_Abilities

Author: Youngju Yun
Last Updated: 02/01/2024

Overview

Holds data and methods relating to the player character's ability systems including spawning portals and drones. It takes part in the "Action".

Dependencies

  • GameManager.cs
  • Player.cs
  • GridBasedMovement1.cs
  • BaseObjectData.cs
  • GameObject Wizard X
  • GameObject Wizard Y
  • Drones.prehab
  • PortalStart.Prehab
  • PortalExit.Prehab

Contents

  • Use case
  • Breakdown of code
    • Using Statements
    • Variables
    • Awake()
    • StartAbilityMode()
    • MoveCursor()
    • EndAbilityMode()
    • ActivateAbility()
  • Future Expansion
  • Full Code Reference

Use case

It's currently used for the 2 player characters Wizard X/Y in the scene. The code isn't standalone and its methods need to be called from other scripts such as Player.cs.

Breakdown of Code

Using Statements

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

Variables

    //Spawn Portal Ability
    public bool isUsingAbility;
    public GameObject PortalStart;
    public GameObject localPortalStart;
    public GameObject PortalExit;
    private GameObject localPortalExit;

    //Spawn Drone Ability
    public GameObject Drones;
    private GameObject localDrones;

    public GridBasedMovement1 movement; 
    private GameManager gameManager;

	public enum Abilites{
		SpawnPortals,
		SpawnDrones
	};
	public Abilites ability;

bool isUsingAbility: A boolean is mainly used to avoid overlap of ability usage. It's set to true when switched to the Ability Mode.

GameObject PortalStart, PortalExit: A GameObject references to the PortalStart.prehab and PortalExit.prehab. Used for the Instantiation.

GameObject localPortalStart, localPortalExit: A local reference to the instantiated PortalStart and PortalExit GameObjects.

GameObject Drones: A GameObject references to the Drones.prehab. Used for the Instantiation.

GameObject localDrones: A local reference to the instantiated Drones GameObjects.

GridBasedMovement1 movement: A reference to its GridBasedMovement1.cs component.

GameManager gameManager: A reference for the GameManager.cs in the scene.

enum Abilities: A database which stores the types of Abilities. So far there are two; SpawnPortals and SpawnDrones.

Abilities ability: Hold a type of ability that this player character can use. It can be selected from the Inspector Menu in Unity too.

Awake()

    public void Awake(){
        gameManager = GameObject.FindObjectOfType<GameManager>().GetComponent<GameManager>();
    }

It finds GameObject with the script GameManager.cs from the start of the application.

StartAbilityMode()

    public void StartAbilityMode(){
		if(ability == Abilites.SpawnPortals){
			if(GetComponent<BaseObjectData>().floorID < 2 && !movement.isMoving){
				isUsingAbility = true;
				if(GameObject.Find("PortalStart(Clone)")){
					localPortalStart = GameObject.Find("PortalStart(Clone)");
				}
				if(GameObject.Find("PortalExit(Clone)")){
					localPortalExit = GameObject.Find("PortalExit(Clone)");
				}
				if(localPortalStart == null){
					localPortalStart = Instantiate(PortalStart, new Vector3(transform.position.x, 1f, transform.position.z), Quaternion.identity);
				}else{
					localPortalStart.transform.position = new Vector3(transform.position.x, 1f, transform.position.z);
				}
				if(localPortalExit != null){
					Destroy(localPortalExit);
				}
			}else{
				if(movement.isMoving){
					gameManager.SetErrorMessage("You cannot use the ability while moving!");
				}else if (GetComponent<BaseObjectData>().floorID >= 2){
					gameManager.SetErrorMessage("You cannot spawn the portal when you are on the final floor!");
				}
			}
		}
		else if(ability == Abilites.SpawnDrones){
			if(movement.isMoving){
				gameManager.SetErrorMessage("You cannot use the ability while moving!");
			}else{
				isUsingAbility = true;
				if(GameObject.Find("Drones(Clone)")){
					localDrones = GameObject.Find("Drones(Clone)");
				}
				if(localDrones == null){
					localDrones = Instantiate(Drones, new Vector3(transform.position.x, 1f, transform.position.z), Quaternion.identity);
				}
				else{
					Destroy(localDrones);
				}
			}
		}
	}

A method is used when the player character has started using the ability. It handles mainly an initialization of variables.

StartAbilityMode() SpawnPortals

                   if(ability == Abilites.SpawnPortals){
			if(GetComponent<BaseObjectData>().floorID < 2 && !movement.isMoving){
				isUsingAbility = true;
				if(GameObject.Find("PortalStart(Clone)")){
					localPortalStart = GameObject.Find("PortalStart(Clone)");
				}
				if(GameObject.Find("PortalExit(Clone)")){
					localPortalExit = GameObject.Find("PortalExit(Clone)");
				}
				if(localPortalStart == null){
					localPortalStart = Instantiate(PortalStart, new Vector3(transform.position.x, 1f, transform.position.z), Quaternion.identity);
				}else{
					localPortalStart.transform.position = new Vector3(transform.position.x, 1f, transform.position.z);
				}
				if(localPortalExit != null){
					Destroy(localPortalExit);
				}
			}else{
				if(movement.isMoving){
					gameManager.SetErrorMessage("You cannot use the ability while moving!");
				}else if (GetComponent<BaseObjectData>().floorID >= 2){
					gameManager.SetErrorMessage("You cannot spawn the portal when you are on the final floor!");
				}
			}
		}

This section of the method is used when SpawnPortals ability is selected for the character.

This ability cannot be used while in motion, or when on the last floor since it spawns portals to connect to the next floor. It requests the error messages from GameManager.cs if those conditions were not cleared.

If conditions were cleared, a boolean isUsingAbility is set to true to avoid calling the same methods while in usage.

Then it searches for two GameObjects PortalStart(Clone) and PortalExit(Clone) to get a reference as its localPortalStart and localPortalExit.

If any PortalStart(Clone) doesn't exist, it instantiates the PortalStart.prehab at the same position as it and sets it as its local localPortalStart. If the GameObject does exist, it only transforms the GameObject.

Finally, PortalExit(Clone) GameObject will be destroyed.

StartAbilityMode() SpawnDrones

               else if(ability == Abilites.SpawnDrones){
			if(movement.isMoving){
				gameManager.SetErrorMessage("You cannot use the ability while moving!");
			}else{
				isUsingAbility = true;
				if(GameObject.Find("Drones(Clone)")){
					localDrones = GameObject.Find("Drones(Clone)");
				}
				if(localDrones == null){
					localDrones = Instantiate(Drones, new Vector3(transform.position.x, 1f, transform.position.z), Quaternion.identity);
				}
				else{
					Destroy(localDrones);
				}
			}
		}

The rest of the section of the method is used when SpawnDrones ability is selected for the character.

The Error Message will be displayed while the character is moving since this ability can only be used in the idle state too.

It primarily starts looking for Drones.prehab in the Scene already, and Instantiates Drones at the same coordinates only when Drones.prehab is not available.

It destroys any existing Drones.prehab in the last.

MoveCursor()

    public void MoveCursor(Vector3 direction){
		if(ability == Abilites.SpawnPortals){
			if(!localPortalStart.GetComponent<GridBasedMovement1>().isMoving){
				Debug.Log(direction);
				StartCoroutine(localPortalStart.GetComponent<GridBasedMovement1>().MoveObject(direction));
			}
		}else if(ability == Abilites.SpawnDrones){
			if(!localDrones.GetComponent<GridBasedMovement1>().isMoving){
				Debug.Log(direction);
				StartCoroutine(localDrones.GetComponent<GridBasedMovement1>().MoveObject(direction));
			}
		}
    }

This function uses the ability prehabs' GridBasedMovement1.cs component to move them in any specific direction.

As for the SpawnPortals ability types, it executes the subroutine MoveObject() of localPortalStart GameObject.

As for the SpawnDrones ability types, it executes the subroutine MoveObject() of localDrones GameObject.

EndAbilityMode()

    public void EndAbilityMode(){
		isUsingAbility = false;
		if(ability == Abilites.SpawnPortals){
			if(localPortalStart != null && localPortalExit == null){
				Destroy(localPortalStart);
			}
		}else if(ability == Abilites.SpawnDrones){
			if(localDrones != null){
				Destroy(localDrones);
			}
		}
	}

This function is used when the player wants to quit using the ability and revert back to control the character.

A boolean isUsingAbility is set to false so the player can now restart using abilities by executing StartAbilityMode().

Then it destroys every ability prehabs in usage.

ActivateAbility()

    public void ActivateAbility(){
		if(ability == Abilites.SpawnPortals){
			isUsingAbility = false;
			if(GetComponent<BaseObjectData>().floorID < 2 && !movement.isMoving){
				if(localPortalExit == null){
					localPortalExit = Instantiate(PortalExit, new Vector3(localPortalStart.transform.position.x + 8f, 2f, localPortalStart.transform.position.z), Quaternion.identity);
				}else{
					localPortalExit.transform.position = new Vector3(localPortalStart.transform.position.x + 8f, 2f, localPortalStart.transform.position.z);
				}
			}
		}else if(ability == Abilites.SpawnDrones){
			if(!movement.isMoving){
				Action_Grab grab = localDrones.GetComponent<Action_Grab>();
				CollisionChecker collision = localDrones.GetComponent<CollisionChecker>();
				if(!grab.isGrabbingObject){
					grab.GrabObject(null, Vector3.up, "Movable", localDrones.transform.Find("RaycastTransform"));
					Debug.Log(transform.Find("RaycastTransform"));
					grab.GrabObject(null, Vector3.up, "Block", localDrones.transform.Find("RaycastTransform"));
					if(grab.isGrabbingObject){
						collision.ignoreMovable = false;
						collision.ignorePlayer = false;
					}
				}else{
					grab.ReleaseObject();
					collision.ignoreMovable = true;
					collision.ignorePlayer = true;
				}
			}
		}
	}

The function handles the effect of each ability triggered by the player.

ActivateAbility() SpawnPortals

		if(ability == Abilites.SpawnPortals){
			isUsingAbility = false;
			if(GetComponent<BaseObjectData>().floorID < 2 && !movement.isMoving){
				if(localPortalExit == null){
					localPortalExit = Instantiate(PortalExit, new Vector3(localPortalStart.transform.position.x + 8f, 2f, localPortalStart.transform.position.z), Quaternion.identity);
				}else{
					localPortalExit.transform.position = new Vector3(localPortalStart.transform.position.x + 8f, 2f, localPortalStart.transform.position.z);
				}
			}

If the ability type is SpawnPortals, it creates the exit portal at the corresponding coordinate on the next floor.

ActivateAbility() SpawnDrones

		}else if(ability == Abilites.SpawnDrones){
			if(!movement.isMoving){
				Action_Grab grab = localDrones.GetComponent<Action_Grab>();
				CollisionChecker collision = localDrones.GetComponent<CollisionChecker>();
				if(!grab.isGrabbingObject){
					grab.GrabObject(null, Vector3.up, "Movable", localDrones.transform.Find("RaycastTransform"));
					Debug.Log(transform.Find("RaycastTransform"));
					grab.GrabObject(null, Vector3.up, "Block", localDrones.transform.Find("RaycastTransform"));
					if(grab.isGrabbingObject){
						collision.ignoreMovable = false;
						collision.ignorePlayer = false;
					}
				}else{
					grab.ReleaseObject();
					collision.ignoreMovable = true;
					collision.ignorePlayer = true;
				}
			}
		}

If the ability type is SpawnDrones, It uses Drones.prehab's Action_Grab.cs and CollisionChecker.cs components to grab and move along with the movable object below it.

The ability cannot be activated while Drones are moving.

While carrying the object, Drones cannot phase through the player or other movable objects.

It can release the object by activating the ability again.

Future Expansion

As further functionality, if there would be an idea for the additional abilities for player characters, any data and methods will be stored in the Action_Abilities.

Full Code Reference

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Action_Abilities : MonoBehaviour
{
	//Spawn Portal Ability
    public bool isUsingAbility;
	public GameObject PortalStart;
	public GameObject localPortalStart;
	public GameObject PortalExit;
	private GameObject localPortalExit;

	//Spawn Drone Ability
	public GameObject Drones;
	private GameObject localDrones;

    public GridBasedMovement1 movement; 
    private GameManager gameManager;

	public enum Abilites{
		SpawnPortals,
		SpawnDrones
	};
	public Abilites ability;

    public void Awake(){
        gameManager = GameObject.FindObjectOfType<GameManager>().GetComponent<GameManager>();
    }

    public void StartAbilityMode(){
		if(ability == Abilites.SpawnPortals){
			if(GetComponent<BaseObjectData>().floorID < 2 && !movement.isMoving){
				isUsingAbility = true;
				if(GameObject.Find("PortalStart(Clone)")){
					localPortalStart = GameObject.Find("PortalStart(Clone)");
				}
				if(GameObject.Find("PortalExit(Clone)")){
					localPortalExit = GameObject.Find("PortalExit(Clone)");
				}
				if(localPortalStart == null){
					localPortalStart = Instantiate(PortalStart, new Vector3(transform.position.x, 1f, transform.position.z), Quaternion.identity);
				}else{
					localPortalStart.transform.position = new Vector3(transform.position.x, 1f, transform.position.z);
				}
				if(localPortalExit != null){
					Destroy(localPortalExit);
				}
			}else{
				if(movement.isMoving){
					gameManager.SetErrorMessage("You cannot use the ability while moving!");
				}else if (GetComponent<BaseObjectData>().floorID >= 2){
					gameManager.SetErrorMessage("You cannot spawn the portal when you are on the final floor!");
				}
			}
		}
		else if(ability == Abilites.SpawnDrones){
			if(movement.isMoving){
				gameManager.SetErrorMessage("You cannot use the ability while moving!");
			}else{
				isUsingAbility = true;
				if(GameObject.Find("Drones(Clone)")){
					localDrones = GameObject.Find("Drones(Clone)");
				}
				if(localDrones == null){
					localDrones = Instantiate(Drones, new Vector3(transform.position.x, 1f, transform.position.z), Quaternion.identity);
				}
				else{
					Destroy(localDrones);
				}
			}
		}
	}

    public void MoveCursor(Vector3 direction){
		if(ability == Abilites.SpawnPortals){
			if(!localPortalStart.GetComponent<GridBasedMovement1>().isMoving){
				Debug.Log(direction);
				StartCoroutine(localPortalStart.GetComponent<GridBasedMovement1>().MoveObject(direction));
			}
		}else if(ability == Abilites.SpawnDrones){
			if(!localDrones.GetComponent<GridBasedMovement1>().isMoving){
				Debug.Log(direction);
				StartCoroutine(localDrones.GetComponent<GridBasedMovement1>().MoveObject(direction));
			}
		}
    }

    public void EndAbilityMode(){
		isUsingAbility = false;
		if(ability == Abilites.SpawnPortals){
			if(localPortalStart != null && localPortalExit == null){
				Destroy(localPortalStart);
			}
		}else if(ability == Abilites.SpawnDrones){
			if(localDrones != null){
				Destroy(localDrones);
			}
		}
	}

	public void ActivateAbility(){
		if(ability == Abilites.SpawnPortals){
			isUsingAbility = false;
			if(GetComponent<BaseObjectData>().floorID < 2 && !movement.isMoving){
				if(localPortalExit == null){
					localPortalExit = Instantiate(PortalExit, new Vector3(localPortalStart.transform.position.x + 8f, 2f, localPortalStart.transform.position.z), Quaternion.identity);
				}else{
					localPortalExit.transform.position = new Vector3(localPortalStart.transform.position.x + 8f, 2f, localPortalStart.transform.position.z);
				}
			}
		}else if(ability == Abilites.SpawnDrones){
			if(!movement.isMoving){
				Action_Grab grab = localDrones.GetComponent<Action_Grab>();
				CollisionChecker collision = localDrones.GetComponent<CollisionChecker>();
				if(!grab.isGrabbingObject){
					grab.GrabObject(null, Vector3.up, "Movable", localDrones.transform.Find("RaycastTransform"));
					Debug.Log(transform.Find("RaycastTransform"));
					grab.GrabObject(null, Vector3.up, "Block", localDrones.transform.Find("RaycastTransform"));
					if(grab.isGrabbingObject){
						collision.ignoreMovable = false;
						collision.ignorePlayer = false;
					}
				}else{
					grab.ReleaseObject();
					collision.ignoreMovable = true;
					collision.ignorePlayer = true;
				}
			}
		}
	}
}