mirror of
https://github.com/kuhyx/WUT_Computer_Science.git
synced 2026-07-04 17:43:12 +02:00
Added soldier's queue functionality
+ actions: basic actions that can the soldier creates on its own + interrupts: actions that are enqueued as a result of receiving orders. Executed before basic actions +/- renamed "UpdateTarget" to "TryAttackEnemy" + made "speedAttack" (attack delay) functional + prepared target position (modified by movement orders)
This commit is contained in:
parent
2515dfe567
commit
70c7414a0b
@ -86,7 +86,7 @@ MonoBehaviour:
|
||||
rangeAttack: 3
|
||||
rangeView: 1
|
||||
damageAttack: 1
|
||||
speedAttack: 1
|
||||
speedAttack: 5
|
||||
nameText: {fileID: 6838251604148675782}
|
||||
healthPointsText: {fileID: 8012357605284019061}
|
||||
--- !u!1 &2026481257847620386
|
||||
|
||||
@ -5,6 +5,33 @@ using UnityEngine;
|
||||
|
||||
public class Soldier : MonoBehaviour
|
||||
{
|
||||
private Queue<Action> actions = new Queue<Action>();
|
||||
private Queue<Action> interrupts = new Queue<Action>();
|
||||
#region Action Queue Items
|
||||
abstract class Action // action "template"
|
||||
{
|
||||
public virtual void Execute(Soldier soldier, TickSystem.OnTickEventArgs tickEventArgs) { } // called by Soldier when action is supposed to be done
|
||||
}
|
||||
|
||||
private class Movement : Action
|
||||
{
|
||||
public override void Execute(Soldier soldier, TickSystem.OnTickEventArgs tickEventArgs)
|
||||
{//TO DO: CALL PROPER FUNCTION TO MOVE
|
||||
Debug.LogWarning($"(tick: {tickEventArgs.tickNumber}) Trying to teleport to {soldier.movementDestination}");
|
||||
throw new System.NotImplementedException();
|
||||
//tileMap.Teleport(movementDestination)
|
||||
}
|
||||
}
|
||||
private class TryAttack : Action
|
||||
{
|
||||
public override void Execute(Soldier soldier, TickSystem.OnTickEventArgs tickEventArgs)
|
||||
{
|
||||
//Debug.LogWarning($"(tick: {tickEventArgs.tickNumber}) Looking for enemy in range");
|
||||
if(soldier.TryAttackEnemy())
|
||||
soldier.lastAttackTick = tickEventArgs.tickNumber;
|
||||
}
|
||||
}
|
||||
#endregion
|
||||
public enum SoldierType
|
||||
{
|
||||
Ally,
|
||||
@ -18,11 +45,14 @@ public class Soldier : MonoBehaviour
|
||||
[SerializeField] private float rangeAttack = 100;
|
||||
[SerializeField] private float rangeView = 1;
|
||||
[SerializeField] private float damageAttack = 1;
|
||||
[SerializeField] private float speedAttack = 1;
|
||||
[SerializeField] private int speedAttack = 1; // ticks between attacks
|
||||
[SerializeField] private int lastAttackTick = -1;
|
||||
|
||||
[SerializeField] private TMP_Text nameText = null;
|
||||
[SerializeField] private TMP_Text healthPointsText = null;
|
||||
|
||||
private Vector3Int movementDestination = Vector3Int.zero;
|
||||
|
||||
public SoldierType TempGetOwnType()
|
||||
{
|
||||
return ourType;
|
||||
@ -78,11 +108,23 @@ public class Soldier : MonoBehaviour
|
||||
|
||||
private void HandleTick(TickSystem.OnTickEventArgs tickEventArgs)
|
||||
{
|
||||
UpdateTarget();
|
||||
// ADD TICK SYSTEM PREFAB ON SCENE
|
||||
ref Queue<Action> queueToHandle = ref interrupts;
|
||||
if (interrupts.Count < 1) // if no interrupt actions to do, handle regular queue
|
||||
queueToHandle = actions;
|
||||
|
||||
if(queueToHandle.Count > 0)
|
||||
queueToHandle.Dequeue().Execute(this, tickEventArgs);
|
||||
else
|
||||
{
|
||||
if(lastAttackTick + speedAttack <= tickEventArgs.tickNumber)
|
||||
{
|
||||
queueToHandle.Enqueue(new TryAttack());
|
||||
queueToHandle.Dequeue().Execute(this, tickEventArgs);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void UpdateTarget ()
|
||||
bool TryAttackEnemy () //returns true if an enemy was attacked
|
||||
{
|
||||
// Enemies are the game objects tagged with the "Enemy"
|
||||
//GameObject[] enemies = GameObject.FindGameObjectsWithTag(enemyType);
|
||||
@ -122,6 +164,8 @@ public class Soldier : MonoBehaviour
|
||||
|
||||
if (target != null)
|
||||
target.ReduceHP(damageAttack);
|
||||
|
||||
return target != null;
|
||||
}
|
||||
|
||||
// Update is called once per frame
|
||||
|
||||
Loading…
Reference in New Issue
Block a user