Gameplay.Player.cs - robblofield/TomeboundDocs GitHub Wiki

Gameplay.Player.cs

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

Overview

A script that stores reference of all the player character's action scripts and movements and runs their functions in regards to the player's input. It takes forms of the "Gameplay".

Dependencies

  • GridBasedMovement1.cs
  • Action_Grab.cs
  • Action_EnterGate.cs
  • Action_Abilities.cs
  • GameManager.cs
  • Tome.cs
  • The Input System Package from the Package Manager
  • PlayerInputActions() from the New Input System

Contents

  • Use case
  • Breakdown of code
    • Using Statements
    • Variables
    • Awake()
    • Update()
    • GrabAction()
    • SwapAction()
    • UseGateAction()
    • UseAbilityAction()
    • UsePlayerAction()
    • SpawnAction()
    • OnControlsChanged()
  • Future Expansion
  • Full Code Reference

Use case

It is used whenever there is a player input mapped in the PlayerInputActions.

Breakdown of Code

Using Statements

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

It import UnityEngine.InputSystem on top of the other 3 typical packages. It is essential to get a reference of parameters or methods relating to the InputSystem.

Variables

    //Variables for movement of the player
    public GridBasedMovement1 movement;
    public Action_Grab grab;
    public Action_EnterGate gate;
    public Action_Abilities abilities;
    public bool isControlling = false;

    //Reference for the new input system
    private PlayerInput playerInput;
	private PlayerInputActions playerInputActions;
    private GameManager gameManager;

    //Reference for the book
    private GameObject book;

GridBasedMovement1 movement: A reference to the character's GridBasedMovement1.cs.

Action_Grab grab: A reference to the character's Action_Grab.cs.

Action_EnterGate gate: A reference to the character's Action_EnterGate.cs.

Action_Abilities: A reference to the character's Action_Abilities.cs.

bool isControlling: The boolean is set to true if the player controls the character. It is not used in the current build.

PlayerInput playerInput: A reference to the character's PlayerInput.

PlayerInputActions playerInputActions: A local reference to the Action Asset.

GameManager gameManager: A local reference to the GameManager.cs.

GameObject book: A data of the GameObject Tome.

Awake()

        void Awake()
	{
		playerInput = GetComponent<PlayerInput>();
		playerInputActions = new PlayerInputActions();
		playerInputActions.Player.Grab.performed += GrabAction;
		playerInputActions.Player.Swap.performed += SwapAction;
		playerInputActions.Player.UseGate.performed += UseGateAction;
		playerInputActions.Player.UseAbility.started += UseAbilityAction;

        gameManager = GameObject.FindObjectOfType<GameManager>().GetComponent<GameManager>();
        book = GameObject.FindWithTag("Book");
        // playerInput.onControlsChanged += OnControlsChanged;
		// playerInputActions.Player.Movement.performed += MovementAction;
	}

The function runs once at the start of the program, to find the PlayerInput component, instantiate a new PlayerInputActions, and add callback methods for each action.

Callback methods GrabAction, SwapAction, UseGateAction are added each time an action is performed, and UseAbilityAction are added every time it is started, to count hold presses as a single input.

After that, it Finds GameObjects one of them with GameManager.cs and others with the tag "Tome".

Update()

    void Update(){
        if (gameManager.controllingObject == gameObject && !abilities.isUsingAbility)
		{
			playerInputActions.Player.Enable();
		}
		else
		{
			playerInputActions.Player.Disable();
		}
		if (gameManager.controllingObject == gameObject)
		{
			//Input to move player
			float inputX = Mathf.Round(playerInputActions.Player.Movement.ReadValue<Vector2>().x);
			if (movement.xLocked && movement.movementLocked) inputX = 0;
			float inputY = Mathf.Round(playerInputActions.Player.Movement.ReadValue<Vector2>().y);
			if (movement.yLocked && movement.movementLocked) inputY = 0;
			if (!movement.isMoving)
			{
				if (new Vector2(inputX, inputY) != Vector2.zero)
				{
					Debug.Log(new Vector2(inputX, inputY));
					StartCoroutine(movement.MoveObject(new Vector3(inputX, 0, inputY)));
				}
			}

			//Input to move portal
			inputX = Mathf.Round(playerInputActions.AbilityPortal.MoveCursor.ReadValue<Vector2>().x);
			inputY = Mathf.Round(playerInputActions.AbilityPortal.MoveCursor.ReadValue<Vector2>().y);
			if (!movement.isMoving)
			{
				if (new Vector2(inputX, inputY) != Vector2.zero)
				{
					abilities.MoveCursor(new Vector3(inputX, 0, inputY));
				}
			}
		}
    }

The function runs every frame while the script is active. It manages the direction of movement of the character and the ability prehabs while in the Ability Mode.

Update() Enable and Disable PlayerInput Under Conditions

                if (gameManager.controllingObject == gameObject && !abilities.isUsingAbility)
		{
			playerInputActions.Player.Enable();
		}
		else
		{
			playerInputActions.Player.Disable();
		}

The PlayerInputAction affects the character while it is the controllingObject in GameManager.cs and is not in Ability Mode.

Update() Move Player Character

                if (gameManager.controllingObject == gameObject)
		{
			//Input to move player
			float inputX = Mathf.Round(playerInputActions.Player.Movement.ReadValue<Vector2>().x);
			if (movement.xLocked && movement.movementLocked) inputX = 0;
			float inputY = Mathf.Round(playerInputActions.Player.Movement.ReadValue<Vector2>().y);
			if (movement.yLocked && movement.movementLocked) inputY = 0;
			if (!movement.isMoving)
			{
				if (new Vector2(inputX, inputY) != Vector2.zero)
				{
					Debug.Log(new Vector2(inputX, inputY));
					StartCoroutine(movement.MoveObject(new Vector3(inputX, 0, inputY)));
				}
			}

The rest of the functions run if the character is the controllingObject.

float inputX gets value from the x component of composite 2D vector type action called Movement, and inputY gets from the y component of it.

Either inputX or inputY value resets to 0 if the character can only move in certain directions.

If the character is not in motion, it starts subroutine MoveObject to move the character. The value from inputX affects the character's horizontal movement, and inputY affects their vertical movement.

Update() Move Portal

			inputX = Mathf.Round(playerInputActions.AbilityPortal.MoveCursor.ReadValue<Vector2>().x);
			inputY = Mathf.Round(playerInputActions.AbilityPortal.MoveCursor.ReadValue<Vector2>().y);
			if (!movement.isMoving)
			{
				if (new Vector2(inputX, inputY) != Vector2.zero)
				{
					abilities.MoveCursor(new Vector3(inputX, 0, inputY));
				}
			}
		}
    }

This section handles the movement of the PortalStart asset which takes a part of one of the player's ability to spawn portals in Action_Abilities.cs.

If the asset is not moving, it executes the method MoveCursor in Action_Abilities.cs to move the portals in regards to values of inputX and inputY.

GrabAction()

        public void GrabAction(InputAction.CallbackContext context)
	{
		Debug.Log(context);
		if(!grab.isGrabbingObject){
			grab.GrabObject("Tome");
		}else{
			grab.ReleaseObject();
		}
	}

A callback method for the Grab action.

If the character is not grabbing any object, it executes GrabObject() in Action_Grab.cs to grab the object with the tag "Tome", and it executes ReleaseObject() to the object if the character is grabbing it instead.

SwapAction()

	public void SwapAction(InputAction.CallbackContext context)
	{
		Debug.Log(context);
		gameManager.SwapPlayer();
	}

A callback method for the Swap action.

It executes SwapPlayer() in GameManager.cs to change the character in control.

UseGateAction()

	public void UseGateAction(InputAction.CallbackContext context)
	{
		Debug.Log(context);
		gate.EnterGate();
	}

A callback method for the UseGate action.

It executes the EnterGate() function in Action_EnterGate.cs.

UseAbilityAction()

	public void UseAbilityAction(InputAction.CallbackContext context){
		Debug.Log(context);
		playerInput.SwitchCurrentActionMap("AbilityPortal");
		playerInputActions.Player.Disable();
		playerInputActions.AbilityPortal.Enable();
		playerInputActions.AbilityPortal.Spawn.started += SpawnAction;
		playerInputActions.AbilityPortal.Escape.started += UsePlayerAction;
		abilities.StartAbilityMode();
		Debug.Log("Use Ability");
	}

A callback method for the UseAbility action.

It toggles the current action map to AbilityPortal, then it enables the AbilityPortal action map and disables the Player action map. That's because this action map uses the same keybinds for some actions.

Afterwards, it creates two callback methods SpawnAction and UsePlayerAction every time Spawn or Escape action has started.

Finally, the StartAbilityMode() in Action_Abilities.cs is executed.

UsePlayerAction()

	public void UsePlayerAction(InputAction.CallbackContext context){
		Debug.Log(context);
		playerInput.SwitchCurrentActionMap("Player");
		playerInputActions.AbilityPortal.Disable();
		playerInputActions.Player.Enable();
		abilities.EndAbilityMode();
		Debug.Log("Back to Player");
	}

A callback method for the Escape action in the AbilityPortal action map.

It is used to switch back to the Player action map and stop using Abilities.

The method EndAbilityMode() in Action_Abilities is executed.

SpawnAction()

	public void SpawnAction(InputAction.CallbackContext context){
		Debug.Log(context);
		abilities.ActivateAbility();
		if(!abilities.isUsingAbility){
			playerInput.SwitchCurrentActionMap("Player");
			playerInputActions.AbilityPortal.Disable();
			playerInputActions.Player.Enable();
		}
	}

A callback method for the Spawn action in the AbilityPortal action map.

It executes ActivateAbility() in Action_Abilities to use toggleable abilities like spawn portals or drones carrying movable objects.

If the player no longer uses the ability in Action_Abilities, it switches back to the Player action map.

void OnControlsChanged()

    public void OnControlsChanged(PlayerInput input)
    {
        Debug.Log("OnControlsChanged");
    }

A function to change the current keybind of the action in the PlayerInputActions. It is not used in the current build.

Future Expansion

The function OnControlsChanged() is yet to be implemented. When there are additional actions for players to use, additional callback methods are required as well.

Full Code Reference

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

public class Player : MonoBehaviour
{
    //Variables for movement of the player
    public GridBasedMovement1 movement;
    public Action_Grab grab;
    public Action_EnterGate gate;
    public Action_Abilities abilities;
	public bool isControlling = false;

    //Reference for the new input system
    private PlayerInput playerInput;
	private PlayerInputActions playerInputActions;
    private GameManager gameManager;
    //Reference for the book
    private GameObject book;

    void Awake()
	{
		playerInput = GetComponent<PlayerInput>();
		playerInputActions = new PlayerInputActions();
		playerInputActions.Player.Grab.performed += GrabAction;
		playerInputActions.Player.Swap.performed += SwapAction;
		playerInputActions.Player.UseGate.performed += UseGateAction;
		playerInputActions.Player.UseAbility.started += UseAbilityAction;

        gameManager = GameObject.FindObjectOfType<GameManager>().GetComponent<GameManager>();
        book = GameObject.FindWithTag("Book");
        // playerInput.onControlsChanged += OnControlsChanged;
		// playerInputActions.Player.Movement.performed += MovementAction;
	}

    void Update(){
        if (gameManager.controllingObject == gameObject && !abilities.isUsingAbility)
		{
			playerInputActions.Player.Enable();
		}
		else
		{
			playerInputActions.Player.Disable();
		}
		if (gameManager.controllingObject == gameObject)
		{
			//Input to move player
			float inputX = Mathf.Round(playerInputActions.Player.Movement.ReadValue<Vector2>().x);
			if (movement.xLocked && movement.movementLocked) inputX = 0;
			float inputY = Mathf.Round(playerInputActions.Player.Movement.ReadValue<Vector2>().y);
			if (movement.yLocked && movement.movementLocked) inputY = 0;
			if (!movement.isMoving)
			{
				if (new Vector2(inputX, inputY) != Vector2.zero)
				{
					Debug.Log(new Vector2(inputX, inputY));
					StartCoroutine(movement.MoveObject(new Vector3(inputX, 0, inputY)));
				}
			}

			//Input to move portal
			inputX = Mathf.Round(playerInputActions.AbilityPortal.MoveCursor.ReadValue<Vector2>().x);
			inputY = Mathf.Round(playerInputActions.AbilityPortal.MoveCursor.ReadValue<Vector2>().y);
			if (!movement.isMoving)
			{
				if (new Vector2(inputX, inputY) != Vector2.zero)
				{
					abilities.MoveCursor(new Vector3(inputX, 0, inputY));
				}
			}
		}
    }

    public void GrabAction(InputAction.CallbackContext context)
	{
		Debug.Log(context);
		if(!grab.isGrabbingObject){
			grab.GrabObject("Tome");
		}else{
			grab.ReleaseObject();
		}
	}

	public void SwapAction(InputAction.CallbackContext context)
	{
		Debug.Log(context);
		gameManager.SwapPlayer();
	}

	public void UseGateAction(InputAction.CallbackContext context)
	{
		Debug.Log(context);
		gate.EnterGate();
	}

	public void UseAbilityAction(InputAction.CallbackContext context){
		Debug.Log(context);
		playerInput.SwitchCurrentActionMap("AbilityPortal");
		playerInputActions.Player.Disable();
		playerInputActions.AbilityPortal.Enable();
		playerInputActions.AbilityPortal.Spawn.started += SpawnAction;
		playerInputActions.AbilityPortal.Escape.started += UsePlayerAction;
		abilities.StartAbilityMode();
		Debug.Log("Use Ability");
	}

	public void UsePlayerAction(InputAction.CallbackContext context){
		Debug.Log(context);
		playerInput.SwitchCurrentActionMap("Player");
		playerInputActions.AbilityPortal.Disable();
		playerInputActions.Player.Enable();
		abilities.EndAbilityMode();
		Debug.Log("Back to Player");
	}

	public void SpawnAction(InputAction.CallbackContext context){
		Debug.Log(context);
		abilities.ActivateAbility();
		if(!abilities.isUsingAbility){
			playerInput.SwitchCurrentActionMap("Player");
			playerInputActions.AbilityPortal.Disable();
			playerInputActions.Player.Enable();
		}
	}

	public void OnControlsChanged(PlayerInput input)
    {
        Debug.Log("OnControlsChanged");
    }
}
⚠️ **GitHub.com Fallback** ⚠️