Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- public class PlayerManager : GenericSingleton<PlayerManager> {
- // Determine if can move selected unit
- private bool ableToMove;
- // Determine if can use selected unit top attack
- private bool ableToAttack;
- // Determine if unit is moving
- private bool isMoving;
- // Represents the selected Unit
- private Units selectedUnit;
- // Use this for initialization
- void Start ()
- {
- // Set to not be able to move unit
- ableToMove = false;
- // Set to not be able to attack
- ableToAttack = false;
- // Set stopped moving to false
- isMoving = false;
- // Set the selected unit to NULL first
- selectedUnit = null;
- }
- // Update Player Units during Player's turn
- public void UpdatePlayerUnits ()
- {
- // Exit function if it is not player's turn
- if (!TurnManager.Instance.IsPlayerTurn ())
- return;
- // Check if there is a unit selected
- if (selectedUnit)
- {
- // Only update menu when selected unit is not null
- UpdateMenu();
- // Update the unit's movement
- }
- }
- // If Player selects another unit
- public void ChangeUnit(Units newUnit)
- {
- if (selectedUnit)
- {
- // To reset the values for the unit
- selectedUnit.TurnEnd ();
- }
- // Set to the new selected unit
- selectedUnit = newUnit;
- if (selectedUnit)
- {
- // Init the new selected unit
- selectedUnit.TurnStart ();
- }
- // Set to not be able to move unit
- ableToMove = false;
- // Set to not be able to attack
- ableToAttack = false;
- // Set unit is moving to false
- isMoving = false;
- // Set for menu to be open
- if (selectedUnit)
- {
- selectedUnit.menuOpen = true;
- }
- }
- // Update the menu for each unit
- public void UpdateMenu()
- {
- // Only render the unit's menu if no action is selected
- // And if the unit is not moving
- if(selectedUnit.menuOpen && !ableToAttack && !isMoving)
- {
- // This makes the Canvas in the unit to be active
- selectedUnit.transform.GetChild (0).gameObject.SetActive (true);
- }
- else if(!selectedUnit.menuOpen || isMoving || ableToAttack)
- {
- Debug.Log ("False 1");
- // This makes the Canvas in the unit to be inactive
- selectedUnit.transform.GetChild (0).gameObject.SetActive (false);
- }
- // Check for deselection at the end
- DeselectActions ();
- }
- // For deselecting an action
- public void DeselectActions()
- {
- if (!ableToMove && !ableToAttack)
- { // To deselect the selected unit
- if (Input.GetMouseButtonDown(1))
- {
- selectedUnit.transform.GetChild (0).gameObject.SetActive (false);
- selectedUnit.menuOpen = false;
- selectedUnit.TurnEnd ();
- selectedUnit = null;
- }
- }
- if (ableToMove)
- { // If move is clicked, you can deselect move
- if (Input.GetMouseButtonDown(1))
- {
- selectedUnit.transform.GetChild (0).gameObject.SetActive (true);
- selectedUnit.menuOpen = true;
- isMoving = true;
- ableToMove = false;
- }
- }
- if (ableToAttack)
- { // If attack is clicked, you can deselect attack
- if (Input.GetMouseButtonDown(1))
- {
- selectedUnit.transform.GetChild (0).gameObject.SetActive (true);
- selectedUnit.menuOpen = true;
- ableToAttack = false;
- }
- }
- }
- // Set the selected unit to be able to move
- public void StartMoving()
- {
- // Check if unit is available and if unit can move
- if(selectedUnit && !ableToMove)
- {
- isMoving = true;
- ableToMove = true;
- }
- }
- // Set the selected unit to be able to attack
- public void StartAttacking()
- {
- // Check if unit is available and if unit can attack
- if(selectedUnit && !ableToAttack)
- {
- ableToAttack = true;
- }
- }
- // Skip current turn
- public void SkipTurn()
- {
- // Check if unit is available and if unit can move
- if(selectedUnit && ableToMove)
- {
- selectedUnit.TurnEnd ();
- TurnManager.Instance.ExitPlayerTurn ();
- }
- }
- // Set & Get Selected Unit
- public Units GetSelectedUnit() {return selectedUnit;}
- public void SetSelectedUnit(Units _nextUnit) {selectedUnit = _nextUnit;}
- // Set & Get Unit Can Move
- public bool GetAbleToMove() {return ableToMove;}
- public void SetAbleToMove(bool _canMove) {ableToMove = _canMove;}
- // Set & Get Unit Can Attack
- public bool GetAbleToAttack() {return ableToAttack;}
- public void SetAbleToAttack(bool _canAttack) {ableToAttack = _canAttack;}
- // Set & Get Unit Stop Moving
- public bool GetIsMoving() {return isMoving;}
- public void SetIsMoving(bool _stopMove) {isMoving = _stopMove;}
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement