Gameplay.Tome.cs - robblofield/TomeboundDocs GitHub Wiki

Gameplay.Tome.cs

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

Overview

The script for the GameObject Tome and has access to both of the player characters, wizard X and Y. It takes part in the "Gameplay".

Dependencies

  • GridBasedMovement.cs
  • CollisionChecker.cs
  • BaseObjectData.cs
  • GameManager.cs
  • GameObject Wizard X and Y

Contents

  • Use case
  • Breakdown of code
    • Using Statements
    • Variables
    • Start()
    • Update()
    • SetDistanceBetweenWizX
    • EndAbilityMode()
    • ActivateAbility()
  • Future Expansion
  • Full Code Reference

Use case

It's the main script for the Tome GameObject so it's used as long as the Tome is active in the Scene. It is also used for locking the movement of the player's characters or providing distance between them to the Gate.cs.

Breakdown of Code

Using Statements

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

Variables

    public Transform WizX, WizY;
    public Vector3 WizXDistance, WizYDistance;
    public bool isWizXHolding, isWizYHolding;
    private bool isHeld = false;
    public float floatHeight;
    private float origHeight;
    private GameManager gameManager;

Transform WizX, WizY: A transform value for the Wizard X and Y.

Vector3 WizXDistance, WizYDistance: They hold a vector value of the distance between the Tome and WizX and WizY

public bool isWizXHolding, isWizYHolding: These booleans indicate whether Wizard X or Y is holding the Tome.

bool isHeld: A boolean which is set to true while the Tome is being held by the player character.

float floatHeight: A value of y position of the Tome floating while being held.

float origHeight: An original value of y position of the Tome which was laid on the ground.

GameManager gameManager: A reference to the GameManager.cs.

Start()

    public void Start(){
        gameManager = GameObject.FindObjectOfType<GameManager>().GetComponent<GameManager>();
        origHeight = transform.position.y;
    }

The function runs once at the start of the program.

It is used for the initialization of GameManager and origHeight as its global y position.

Update()

    void Update(){
        if(WizX.GetComponent<BaseObjectData>().floorID == GetComponent<BaseObjectData>().floorID){
            WizX.GetComponent<GridBasedMovement1>().movementLocked = true;
        }else{
            WizX.GetComponent<GridBasedMovement1>().movementLocked = false;
        }

        if(WizY.GetComponent<BaseObjectData>().floorID == GetComponent<BaseObjectData>().floorID){
            WizY.GetComponent<GridBasedMovement1>().movementLocked = true;
        }else{
            WizY.GetComponent<GridBasedMovement1>().movementLocked = false;
        }

        if(isWizXHolding && isWizYHolding){
            WizX.GetComponent<GridBasedMovement1>().movementLocked = false;
            WizY.GetComponent<GridBasedMovement1>().movementLocked = false;
        }
    
        if(isWizXHolding || isWizYHolding){
            if(!isHeld){
                floatTome(floatHeight);
                isHeld = true;
            }
        }else{
            if(isHeld){
                floatTome(origHeight);
                isHeld = false;
            }
        }
    }

A method runs once every frame.

Update() Movement Lock

        if(WizX.GetComponent<BaseObjectData>().floorID == GetComponent<BaseObjectData>().floorID){
            WizX.GetComponent<GridBasedMovement1>().movementLocked = true;
        }else{
            WizX.GetComponent<GridBasedMovement1>().movementLocked = false;
        }

        if(WizY.GetComponent<BaseObjectData>().floorID == GetComponent<BaseObjectData>().floorID){
            WizY.GetComponent<GridBasedMovement1>().movementLocked = true;
        }else{
            WizY.GetComponent<GridBasedMovement1>().movementLocked = false;
        }

        if(isWizXHolding && isWizYHolding){
            WizX.GetComponent<GridBasedMovement1>().movementLocked = false;
            WizY.GetComponent<GridBasedMovement1>().movementLocked = false;
        }

This section of the function compares the floor ID of the Tome with that of Wiz X and Y and if the Wiz X/Y is not on the same floor as the Tome, the boolean movementLocked is set to false and Wiz X/Y can move in any direction.

Furthermore, if both player characters hold the Tome, movementLocked is set to false for them.

Under any other conditions, the movementLocked is set to true.

Update() Set Y Position of the Tome

       if(isWizXHolding || isWizYHolding){
            if(!isHeld){
                floatTome(floatHeight);
                isHeld = true;
            }
        }else{
            if(isHeld){
                floatTome(origHeight);
                isHeld = false;
            }
        }
    }

The rest of the function sets the Y Position of the Tome using the function floatTime().

If the Tome is held by WizX or WizY, the boolean isHeld is set to true and the Tome is launched up in the air by the amount of the variable floatHeight.

If the Tome is released by both of the player characters, the isHeld is set to false and the Tome is grounded regarding the variable origHeight.

SetDistanceBetweenX()

    public void SetDistanceBetweenWizX(Transform trans){
        WizX = trans;
        WizXDistance = transform.position - WizX.position;
        if(gameManager.invertTeleportation){
            WizXDistance = new Vector3(-WizXDistance.x, 0, -WizXDistance.z);
        }
        isWizXHolding = true;
        if(isWizYHolding){
            WizX.GetComponent<GridBasedMovement1>().allTransforms.Add(WizY);
            WizY.GetComponent<GridBasedMovement1>().allTransforms.Add(WizX);
        }
    }

This function mutates the WizXDistance variable into values by subtracting the position of the Tome from that of WizX provided in the parameter "trans".

It gets a reference of WizX from the parameter, and isWizXHolding is set to true.

When invertTeleportation in GameManager.cs is true, WizXDistance is inverted.

if WizY is also holding the Tome, WizX gets the transform of WizY and WizY gets the transform of WizX, in addition to their array allTransforms to move both player characters and the Tome in sync.

SetDistanceBetweenWizY()

    public void SetDistanceBetweenWizY(Transform trans){
        WizY = trans;
        WizYDistance = transform.position - WizY.position;
        if(gameManager.invertTeleportation){
            WizYDistance = new Vector3(-WizYDistance.x, 0, -WizYDistance.z);
        }
        isWizYHolding = true;
        if(isWizXHolding){
            WizY.GetComponent<GridBasedMovement1>().allTransforms.Add(WizX);
            WizX.GetComponent<GridBasedMovement1>().allTransforms.Add(WizY);
        }
    }

This function is a mutator for WizYDistance.

It works very similarly compared with the SetDistanceBetweenWizX(), so it will be merged in the future build.

Released()

    public void Released(Transform trans){
        Debug.Log("Released by tome");
        if(trans == WizX){
            isWizXHolding = false;
        }else if(trans == WizY){
            isWizYHolding = false;
        }
        
        if(isWizYHolding){
            WizY.GetComponent<GridBasedMovement1>().allTransforms.Remove(WizX);
        }else if(isWizXHolding){
            WizX.GetComponent<GridBasedMovement1>().allTransforms.Remove(WizY);
        }
    }

The function is called when WizX or WizY has released the Tome.

The variable isWizXHolding is set to false if WizX has released, and isWizYHolding for the WizY.

Also if another player character is still holding the Tome, it Removes the transform of the released player character from its allTransforms.

floatTome()

    public void floatTome(float yAxis){
        if(!GetComponent<GridBasedMovement1>().isMoving){
            float targetDistance = yAxis - transform.position.y;
            Vector3 heldDirection = new Vector3(0, targetDistance, 0);
            StartCoroutine(GetComponent<GridBasedMovement1>().MoveObject(heldDirection));
        }
    }

This function moves the Tome upwards or downwards by Starting the subroutine MoveObject() within its GridBasedMovement1.cs towards the direction which is predetermined by the parameter "yAxis".

Future Expansion

The functions SetDistanceBetweenWizX() and SetDistanceBetweenY() work very similarly with lots of repeated codes, thus it will be merged into a single function in the future build.

Furthermore, variables or functions to manage additional animation and effects of the Tome will be added in this script too.

Full Code Reference

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

public class Tome : MonoBehaviour
{
    public Transform WizX, WizY;
    public Vector3 WizXDistance, WizYDistance;
    public bool isWizXHolding, isWizYHolding;
    private bool isHeld = false;
    public float floatHeight;
    private float origHeight;
    
    private GameManager gameManager;

    public void Start(){
        gameManager = GameObject.FindObjectOfType<GameManager>().GetComponent<GameManager>();
        origHeight = transform.position.y;
    }

    void Update(){
        if(WizX.GetComponent<BaseObjectData>().floorID == GetComponent<BaseObjectData>().floorID){
            WizX.GetComponent<GridBasedMovement1>().movementLocked = true;
        }else{
            WizX.GetComponent<GridBasedMovement1>().movementLocked = false;
        }

        if(WizY.GetComponent<BaseObjectData>().floorID == GetComponent<BaseObjectData>().floorID){
            WizY.GetComponent<GridBasedMovement1>().movementLocked = true;
        }else{
            WizY.GetComponent<GridBasedMovement1>().movementLocked = false;
        }

        if(isWizXHolding && isWizYHolding){
            WizX.GetComponent<GridBasedMovement1>().movementLocked = false;
            WizY.GetComponent<GridBasedMovement1>().movementLocked = false;
        }
    
        if(isWizXHolding || isWizYHolding){
            if(!isHeld){
                floatTome(floatHeight);
                isHeld = true;
            }
        }else{
            if(isHeld){
                floatTome(origHeight);
                isHeld = false;
            }
        }
    }

    public void SetDistanceBetweenWizX(Transform trans){
        WizX = trans;
        WizXDistance = transform.position - WizX.position;
        if(gameManager.invertTeleportation){
            WizXDistance = new Vector3(-WizXDistance.x, 0, -WizXDistance.z);
        }
        isWizXHolding = true;
        if(isWizYHolding){
            WizX.GetComponent<GridBasedMovement1>().allTransforms.Add(WizY);
            WizY.GetComponent<GridBasedMovement1>().allTransforms.Add(WizX);
        }
    }

    public void SetDistanceBetweenWizY(Transform trans){
        WizY = trans;
        WizYDistance = transform.position - WizY.position;
        if(gameManager.invertTeleportation){
            WizYDistance = new Vector3(-WizYDistance.x, 0, -WizYDistance.z);
        }
        isWizYHolding = true;
        if(isWizXHolding){
            WizY.GetComponent<GridBasedMovement1>().allTransforms.Add(WizX);
            WizX.GetComponent<GridBasedMovement1>().allTransforms.Add(WizY);
        }
    }

    public void Released(Transform trans){
        Debug.Log("Released by tome");
        if(trans == WizX){
            isWizXHolding = false;
        }else if(trans == WizY){
            isWizYHolding = false;
        }
        
        if(isWizYHolding){
            WizY.GetComponent<GridBasedMovement1>().allTransforms.Remove(WizX);
        }else if(isWizXHolding){
            WizX.GetComponent<GridBasedMovement1>().allTransforms.Remove(WizY);
        }
    }

    public void floatTome(float yAxis){
        if(!GetComponent<GridBasedMovement1>().isMoving){
            float targetDistance = yAxis - transform.position.y;
            Vector3 heldDirection = new Vector3(0, targetDistance, 0);
            StartCoroutine(GetComponent<GridBasedMovement1>().MoveObject(heldDirection));
        }
    }
}