First draft of CombatManager and Ability

ability_system
Frank Xu 2022-03-21 12:31:59 +08:00
parent 00e1d7968a
commit 04539851c2
5 changed files with 207 additions and 49 deletions

View File

@ -7,19 +7,19 @@ public class Ability : ScriptableObject
public new string name;
public string description;
public virtual void OnSpawn(Unit unit) { }
public virtual void OnPlaceUnit(Unit unit) { }
public virtual void OnBeginRound(Unit unit) { }
public virtual void OnEndRound(Unit unit) { }
public virtual void OnBeginMove(Unit unit) { }
public virtual void OnBeginMovePhase(Unit unit) { }
public virtual void OnMove(Unit unit, int travelDistance) { }
public virtual void OnEndMove(Unit unit) { }
public virtual void OnEndMovePhase(Unit unit) { }
public virtual void OnBeginAttackPhrase(Unit unit) { }
public virtual void OnBeginAttackPhase(Unit unit) { }
public virtual void OnBeginAttack(Unit unit, List<Unit> targets) { }
public virtual void OnAttack(Unit unit, List<Unit> targets) { }
public virtual void OnEndAttack(Unit unit, List<Unit> targets) { }
public virtual void OnEndAttackPhrase(Unit unit) { }
public virtual void OnEndAttackPhase(Unit unit) { }
public virtual void OnExecute(Unit unit, Unit target) { }
public virtual void OnAllyDeath(Unit unit) { }
public virtual void OnEnemyDeath(Unit unit) { }
@ -37,5 +37,5 @@ public class Ability : ScriptableObject
public virtual void OnEquippingArmor(Unit unit, int armorAmount) { }
public virtual void OnDiscard(Unit unit) { }
public virtual void OnCasting(Unit unit) { }
public virtual void OnCastStragedy(List<Unit> targets) { }
}

View File

@ -0,0 +1,162 @@
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
// Left is Player, right is enemy.
public enum Side { left = 1, right = -1 }
public class CombatManager : MonoBehaviour
{
[SerializeField] private List<Unit> leftSideUnits = new List<Unit>();
[SerializeField] private List<Unit> rightSideUnits = new List<Unit>();
[SerializeField] private CellMap cellMap;
// Start is called before the first frame update
void Start()
{
// hack
cellMap = FindObjectOfType<CellMap>();
foreach (var unit in rightSideUnits)
{
foreach (var ability in unit.Abilities)
{
ability.OnPlaceUnit(unit);
}
}
foreach (var unit in rightSideUnits)
{
foreach (var ability in unit.Abilities)
{
ability.OnEnterBattleGround(unit);
}
}
}
List<Unit> QueryTargets(Unit attacker)
{
// TO DO:
return new List<Unit>();
}
// Called once before drawing cards
void BeginFight()
{
}
void BeginRound(Side side)
{
List<Unit> units = side == Side.left ? leftSideUnits : rightSideUnits;
foreach (var unit in units)
{
foreach (var ability in unit.Abilities)
{
ability.OnBeginRound(unit);
}
}
}
void BeginMovePhase(Side side)
{
List<Unit> units = side == Side.left ? leftSideUnits : rightSideUnits;
foreach (var unit in units)
{
foreach (var ability in unit.Abilities)
{
ability.OnBeginMovePhase(unit);
}
}
}
void EndMovePhase(Side side)
{
List<Unit> units = side == Side.left ? leftSideUnits : rightSideUnits;
foreach (var unit in units)
{
foreach (var ability in unit.Abilities)
{
ability.OnEndMovePhase(unit);
}
}
}
void BeginAttackPhase(Side side)
{
List<Unit> units = side == Side.left ? leftSideUnits : rightSideUnits;
foreach (var unit in units)
{
foreach (var ability in unit.Abilities)
{
ability.OnBeginAttackPhase(unit);
}
}
}
void UnitAttack(Unit unit, List<Unit> targets)
{
foreach (var ability in unit.Abilities)
{
ability.OnBeginAttack(unit, targets);
}
foreach (var ability in unit.Abilities)
{
ability.OnAttack(unit, targets);
}
foreach (var ability in unit.Abilities)
{
ability.OnEndAttack(unit, targets);
}
foreach (var target in targets)
{
foreach (var ability in target.Abilities)
{
ability.OnCounterAttack(target, unit);
}
}
}
void Attack(Side side)
{
List<Unit> units = side == Side.left ? leftSideUnits : rightSideUnits;
foreach (var unit in units)
{
UnitAttack(unit, QueryTargets(unit));
}
}
void EndAttackPhase(Side side)
{
List<Unit> units = side == Side.left ? leftSideUnits : rightSideUnits;
foreach (var unit in units)
{
foreach (var ability in unit.Abilities)
{
ability.OnEndAttackPhase(unit);
}
}
}
void EndRound(Side side)
{
List<Unit> units = side == Side.left ? leftSideUnits : rightSideUnits;
foreach (var unit in units)
{
foreach (var ability in unit.Abilities)
{
ability.OnEndRound(unit);
}
}
}
// Called once after result
void EndFight()
{
}
}

View File

@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: ab8c50f6a5fd9be488ffeb604ccbc9fc
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@ -7,9 +7,6 @@ using TMPro;
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;
@ -34,6 +31,11 @@ public class Unit : MonoBehaviour
[SerializeField]
private List<Ability> abilities;
public List<Ability> Abilities
{
get { return abilities; }
set { abilities = value; }
}
[SerializeField]
private int meleeAttackRange = 1;
@ -96,6 +98,7 @@ public class Unit : MonoBehaviour
currentDefense = basicMaxDefense;
cardTags = baseData.tags.ToList();
unitClasses = baseData.unitClasses.ToList();
abilities = baseData.abilities.ToList();
foreach (var unitClass in unitClasses)
{
@ -165,39 +168,34 @@ public class Unit : MonoBehaviour
{
return;
}
foreach (var tag in cardTags)
// hack
if (rangedAttackRange > 0)
{
if (tag.name == "Archer")
for (int i = 0; i < rangedAttackRange; ++i)
{
for (int i = 0; i < meleeAttackRange; ++i)
int attackXPos = standingCell.xPos + (i + 1) * (int)side;
if (attackXPos >= cellMap.column || attackXPos < 0)
{
int attackXPos = standingCell.xPos + (i + 1) * (int)side;
if (attackXPos >= cellMap.column || attackXPos < 0)
// Attack Camp
currentCommand = CurrentCommand.defend;
break;
}
Cell nextCellInRange = cellMap.GetCell(attackXPos, standingCell.yPos);
Unit targetUnit = nextCellInRange.standingUnit;
if (targetUnit)
{
if (targetUnit.side != side)
{
// Attack Camp
// Add units to attack list.
currentCommand = CurrentCommand.defend;
break;
}
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;
currentCommand = CurrentCommand.moveForward;
}
}
//Debug.Log("Call MoveAction().");
switch (currentCommand)
{
@ -265,17 +263,10 @@ public class Unit : MonoBehaviour
public void AttackAction()
{
// Check Attack Range
if (inDogFight(this))
// Check if this unit is ranged unit
if (rangedAttackRange > 0)
{
foreach (var tag in cardTags)
{
if (tag.name == "Archer")
{
// Archer can't dog fight.
return;
}
}
return;
}
List<Unit> unitsInRange = new List<Unit>();

View File

@ -25,12 +25,6 @@ public class UnitManager : MonoBehaviour
}
}
// Update is called once per frame
void Update()
{
}
public void handleDiedUnit(Unit unit)
{
if (unit.side == Side.left)
@ -39,7 +33,7 @@ public class UnitManager : MonoBehaviour
}
else if (unit.side == Side.right)
{
leftSideUnits.Remove(unit);
rightSideUnits.Remove(unit);
}
}