using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.Assertions; public enum CurrentCommand { defend, moveForward, retreat } // Left is Player, Enemy is right. public enum Side { left = 1, right = -1 } public class Unit : MonoBehaviour { public string unitName; [SerializeField] private int basicAttack; private int finalAttack; [SerializeField] private int currentDefense; [SerializeField] private int basicMaxDefense; private int finalMaxDefense; [SerializeField] private CardTag[] cardTags; [SerializeField] private int attackRange = 1; [SerializeField] private int movement = 1; [SerializeField] private CurrentCommand currentCommand; [SerializeField] private Cell standingCell; private Cell destinationCell; private CellMap cellMap; [SerializeField] private Side side; public bool walkingToDestination = false; // Start is called before the first frame update void Start() { cellMap = FindObjectOfType(); transform.position = standingCell.transform.position; transform.position = new Vector3(transform.position.x, transform.position.y, -1); standingCell.standingUnit = this; } // Update is called once per frame void Update() { if (walkingToDestination && destinationCell && transform.position != destinationCell.transform.position) { Vector3 moveVec = destinationCell.transform.position - transform.position; transform.position = new Vector3(transform.position.x + moveVec.x * 5.0f * Time.deltaTime, transform.position.y + moveVec.y * 5.0f * Time.deltaTime, -1); Debug.Log("Manitude: " + moveVec.magnitude); if ((Mathf.Approximately(transform.position.x, destinationCell.transform.position.x) && Mathf.Approximately(transform.position.y, destinationCell.transform.position.y)) || moveVec.magnitude <= 1) { walkingToDestination = false; transform.position = new Vector3(destinationCell.transform.position.x, destinationCell.transform.position.y, -1); destinationCell = null; Debug.Log("Arrived."); } } } public void MoveAction() { if (walkingToDestination) { Debug.Log("Can start a new move action. Unit is still moving."); return; } Debug.Log("Call MoveAction()."); switch (currentCommand) { case CurrentCommand.defend: break; case CurrentCommand.moveForward: Debug.Log("Try Move Forward."); if (checkDestinationCellEmpty()) { // Attack() Debug.Log("Start Moving."); MoveToCell(destinationCell); walkingToDestination = true; } else { Debug.Log("Can't Move."); // Check the unit is facing the camp or unit. if (destinationCell) { // Check the unit is Allie or Enemy. if (destinationCell.standingUnit.side == side) { // Check the Allie is swappable or not. } else { // Can't move to cell this is standed by enemy. } } else { // Facing the camp. } } break; case CurrentCommand.retreat: break; default: break; } } bool checkDestinationCellEmpty(bool retreat = false) { int currentXPos = standingCell.xPos; int movePath = 0; if (retreat) { movePath = movement * -(int)side; } else { movePath = movement * (int)side; } int destinationXPos = standingCell.xPos + movePath; // Mean the destination cell is the Camp. if (destinationXPos < 0 || destinationXPos >= cellMap.column) { return false; } destinationCell = cellMap.GetCell(destinationXPos, standingCell.yPos); if (destinationCell.standingUnit == null) { return true; } else { return false; } } void MoveToCell(Cell cell) { Assert.IsTrue(destinationCell.standingUnit == null, "Trying to move a cell that is already be standing by another unit."); standingCell.standingUnit = null; standingCell = cell; cell.standingUnit = this; } }