2022-03-14 15:34:06 -07:00
|
|
|
using System.Collections;
|
|
|
|
using System.Collections.Generic;
|
2022-03-15 03:20:00 -07:00
|
|
|
using System.Linq;
|
2022-03-14 15:34:06 -07:00
|
|
|
using UnityEngine;
|
|
|
|
using UnityEngine.Assertions;
|
2022-03-15 03:20:00 -07:00
|
|
|
using TMPro;
|
2022-03-14 15:34:06 -07:00
|
|
|
|
|
|
|
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;
|
2022-03-15 03:20:00 -07:00
|
|
|
public CardData cardData;
|
2022-03-14 15:34:06 -07:00
|
|
|
|
|
|
|
[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;
|
|
|
|
|
2022-03-15 03:20:00 -07:00
|
|
|
public Cell standingCell;
|
2022-03-14 15:34:06 -07:00
|
|
|
[SerializeField]
|
|
|
|
private Cell destinationCell;
|
|
|
|
private CellMap cellMap;
|
|
|
|
|
2022-03-15 03:20:00 -07:00
|
|
|
public Side side;
|
|
|
|
|
|
|
|
public Transform modelArt;
|
|
|
|
public Transform attackText;
|
|
|
|
public Transform defenseText;
|
2022-03-14 15:34:06 -07:00
|
|
|
|
|
|
|
public bool walkingToDestination = false;
|
|
|
|
|
|
|
|
|
|
|
|
// Start is called before the first frame update
|
|
|
|
void Start()
|
|
|
|
{
|
|
|
|
cellMap = FindObjectOfType<CellMap>();
|
|
|
|
transform.position = standingCell.transform.position;
|
|
|
|
transform.position = new Vector3(transform.position.x, transform.position.y, -1);
|
|
|
|
standingCell.standingUnit = this;
|
2022-03-15 03:20:00 -07:00
|
|
|
|
|
|
|
basicAttack = cardData.attack;
|
|
|
|
basicMaxDefense = cardData.defense;
|
|
|
|
currentDefense = basicMaxDefense;
|
|
|
|
cardTags = cardData.cardTags;
|
|
|
|
|
|
|
|
foreach (var tag in cardData.cardTags)
|
|
|
|
{
|
|
|
|
if (tag.name == "Archer")
|
|
|
|
{
|
|
|
|
attackRange += 2;
|
|
|
|
}
|
|
|
|
else if (tag.name == "Cavalry")
|
|
|
|
{
|
|
|
|
movement += 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
modelArt.GetComponent<SpriteRenderer>().sprite = cardData.artwork;
|
2022-03-14 15:34:06 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// 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);
|
|
|
|
|
2022-03-15 03:20:00 -07:00
|
|
|
//Debug.Log("Manitude: " + moveVec.magnitude);
|
2022-03-14 15:34:06 -07:00
|
|
|
|
|
|
|
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;
|
2022-03-15 03:20:00 -07:00
|
|
|
//Debug.Log("Arrived.");
|
2022-03-14 15:34:06 -07:00
|
|
|
}
|
|
|
|
}
|
2022-03-15 03:20:00 -07:00
|
|
|
|
|
|
|
finalAttack = basicAttack; // For now.
|
|
|
|
|
|
|
|
attackText.GetComponent<TextMeshProUGUI>().text = finalAttack.ToString();
|
|
|
|
defenseText.GetComponent<TextMeshProUGUI>().text = currentDefense.ToString();
|
|
|
|
}
|
|
|
|
|
|
|
|
public void MoveAndAttack()
|
|
|
|
{
|
|
|
|
MoveAction();
|
|
|
|
AttackAction();
|
2022-03-14 15:34:06 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
public void MoveAction()
|
|
|
|
{
|
|
|
|
if (walkingToDestination)
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
2022-03-15 03:20:00 -07:00
|
|
|
foreach (var tag in cardData.cardTags)
|
|
|
|
{
|
|
|
|
if (tag.name == "Archer")
|
|
|
|
{
|
|
|
|
for (int i = 0; i < attackRange; ++i)
|
|
|
|
{
|
|
|
|
int attackXPos = standingCell.xPos + (i + 1) * (int)side;
|
|
|
|
if (attackXPos >= cellMap.column || attackXPos < 0)
|
|
|
|
{
|
|
|
|
// Attack Camp
|
|
|
|
currentCommand = CurrentCommand.defend;
|
|
|
|
break;
|
|
|
|
}
|
2022-03-14 15:34:06 -07:00
|
|
|
|
2022-03-15 03:20:00 -07:00
|
|
|
Cell nextCellInRange = cellMap.GetCell(attackXPos, standingCell.yPos);
|
|
|
|
Unit targetUnit = nextCellInRange.standingUnit;
|
|
|
|
if (targetUnit)
|
|
|
|
{
|
|
|
|
if (targetUnit.side != side)
|
|
|
|
{
|
|
|
|
// Add units to attack list.
|
|
|
|
currentCommand = CurrentCommand.defend;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
currentCommand = CurrentCommand.moveForward;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
//Debug.Log("Call MoveAction().");
|
2022-03-14 15:34:06 -07:00
|
|
|
switch (currentCommand)
|
|
|
|
{
|
|
|
|
case CurrentCommand.defend:
|
|
|
|
break;
|
|
|
|
case CurrentCommand.moveForward:
|
2022-03-15 03:20:00 -07:00
|
|
|
//Debug.Log("Try Move Forward.");
|
|
|
|
|
|
|
|
for (int i = 0; i < movement; i++)
|
2022-03-14 15:34:06 -07:00
|
|
|
{
|
2022-03-15 03:20:00 -07:00
|
|
|
if (checkDestinationCellEmpty())
|
|
|
|
{
|
|
|
|
// Attack()
|
|
|
|
//Debug.Log("Start Moving.");
|
|
|
|
MoveToCell(destinationCell);
|
|
|
|
walkingToDestination = true;
|
|
|
|
}
|
|
|
|
else
|
2022-03-14 15:34:06 -07:00
|
|
|
{
|
2022-03-15 03:20:00 -07:00
|
|
|
//Debug.Log("Can't Move.");
|
|
|
|
// Check the unit is facing the camp or unit.
|
|
|
|
if (destinationCell)
|
2022-03-14 15:34:06 -07:00
|
|
|
{
|
2022-03-15 03:20:00 -07:00
|
|
|
// Check the unit is Allie or Enemy.
|
|
|
|
if (destinationCell.standingUnit.side == side)
|
|
|
|
{
|
|
|
|
// Check the Allie is swappable or not.
|
|
|
|
if (!inDogFight(destinationCell.standingUnit))
|
|
|
|
{
|
|
|
|
//standingCell = cell;
|
|
|
|
//cell.standingUnit = this;
|
|
|
|
destinationCell.standingUnit.walkingToDestination = true;
|
|
|
|
standingCell.standingUnit = destinationCell.standingUnit;
|
|
|
|
standingCell.standingUnit.standingCell = standingCell;
|
|
|
|
standingCell.standingUnit.destinationCell = standingCell;
|
|
|
|
|
|
|
|
standingCell = destinationCell;
|
|
|
|
destinationCell.standingUnit = this;
|
|
|
|
walkingToDestination = true;
|
|
|
|
}
|
2022-03-14 15:34:06 -07:00
|
|
|
|
2022-03-15 03:20:00 -07:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// Can't move to cell this is standed by enemy.
|
|
|
|
break;
|
|
|
|
}
|
2022-03-14 15:34:06 -07:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2022-03-15 03:20:00 -07:00
|
|
|
// Facing the camp.
|
|
|
|
break;
|
2022-03-14 15:34:06 -07:00
|
|
|
}
|
2022-03-15 03:20:00 -07:00
|
|
|
destinationCell = standingCell;
|
2022-03-14 15:34:06 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case CurrentCommand.retreat:
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-03-15 03:20:00 -07:00
|
|
|
public void AttackAction()
|
|
|
|
{
|
|
|
|
// Check Attack Range
|
|
|
|
if (inDogFight(this))
|
|
|
|
{
|
|
|
|
foreach (var tag in cardData.cardTags)
|
|
|
|
{
|
|
|
|
if (tag.name == "Archer")
|
|
|
|
{
|
|
|
|
// Archer can't dog fight.
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
List<Unit> unitsInRange = new List<Unit>();
|
|
|
|
for (int i = 0; i < attackRange; ++i)
|
|
|
|
{
|
|
|
|
int attackXPos = standingCell.xPos + (i+1) * (int)side;
|
|
|
|
if (attackXPos >= cellMap.column || attackXPos < 0)
|
|
|
|
{
|
|
|
|
// Attack Camp
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
Cell nextCellInRange = cellMap.GetCell(attackXPos, standingCell.yPos);
|
|
|
|
Unit targetUnit = nextCellInRange.standingUnit;
|
|
|
|
if (targetUnit)
|
|
|
|
{
|
|
|
|
if (targetUnit.side != side)
|
|
|
|
{
|
|
|
|
// Add units to attack list.
|
|
|
|
unitsInRange.Add(targetUnit);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// It is allies. We don't attack it unless we are under some debuff.
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// Nothing to attack.
|
|
|
|
//Debug.Log("No unit to attack.");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if (side == Side.left)
|
|
|
|
{
|
|
|
|
unitsInRange = unitsInRange.OrderBy(x => x.standingCell.xPos).ToList<Unit>();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
unitsInRange = unitsInRange.OrderByDescending(x => x.standingCell.xPos).ToList<Unit>();
|
|
|
|
}
|
|
|
|
|
|
|
|
for (int i = 0; i < unitsInRange.Count; i++)
|
|
|
|
{
|
|
|
|
Debug.Log(unitsInRange[i].unitName + ": " + unitsInRange[i].standingCell.xPos + " , " + unitsInRange[i].standingCell.yPos);
|
|
|
|
// Do attack
|
|
|
|
//Debug.Log("Attack!");
|
|
|
|
unitsInRange[i].currentDefense -= finalAttack;
|
|
|
|
if (unitsInRange[i].currentDefense <= 0)
|
|
|
|
{
|
|
|
|
unitsInRange[i].standingCell.standingUnit = null;
|
|
|
|
unitsInRange[i].gameObject.SetActive(false); // For now
|
|
|
|
Unit unit = unitsInRange[i];
|
|
|
|
FindObjectOfType<UnitManager>().handleDiedUnit(unit);
|
|
|
|
//Debug.Log("Defeat Enemy");
|
|
|
|
}
|
|
|
|
|
|
|
|
break; // Most of units can only attack one target.
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public bool isUnitAlive()
|
|
|
|
{
|
|
|
|
return currentDefense > 0;
|
|
|
|
}
|
|
|
|
|
2022-03-14 15:34:06 -07:00
|
|
|
bool checkDestinationCellEmpty(bool retreat = false)
|
|
|
|
{
|
|
|
|
int currentXPos = standingCell.xPos;
|
|
|
|
int movePath = 0;
|
|
|
|
|
|
|
|
if (retreat)
|
|
|
|
{
|
2022-03-15 03:20:00 -07:00
|
|
|
movePath = 1 * -(int)side;
|
2022-03-14 15:34:06 -07:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2022-03-15 03:20:00 -07:00
|
|
|
movePath = 1 * (int)side;
|
2022-03-14 15:34:06 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
int destinationXPos = standingCell.xPos + movePath;
|
|
|
|
|
|
|
|
// Mean the destination cell is the Camp.
|
2022-03-15 03:20:00 -07:00
|
|
|
if (!checkPosInMap(destinationXPos))
|
2022-03-14 15:34:06 -07:00
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
destinationCell = cellMap.GetCell(destinationXPos, standingCell.yPos);
|
|
|
|
|
|
|
|
if (destinationCell.standingUnit == null)
|
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2022-03-15 03:20:00 -07:00
|
|
|
// Check privous cell is standable if we have more than 1 movement ability.
|
2022-03-14 15:34:06 -07:00
|
|
|
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;
|
|
|
|
}
|
2022-03-15 03:20:00 -07:00
|
|
|
|
|
|
|
// Check the allies is near by an enemy or not.
|
|
|
|
bool inDogFight(Unit unit)
|
|
|
|
{
|
|
|
|
int checkPos = unit.standingCell.xPos + (int)side * 1;
|
|
|
|
|
|
|
|
if (!checkPosInMap(checkPos))
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
var checkCell = cellMap.GetCell(checkPos, standingCell.yPos);
|
|
|
|
if (checkCell.standingUnit && checkCell.standingUnit.side != side)
|
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool checkPosInMap(int pos)
|
|
|
|
{
|
|
|
|
return pos >= 0 && pos < cellMap.column;
|
|
|
|
}
|
2022-03-14 15:34:06 -07:00
|
|
|
}
|