diff --git a/Assets/Scripts/Core/Graph Tasks/DragUnits.cs b/Assets/Scripts/Core/Graph Tasks/DragUnits.cs new file mode 100644 index 0000000..515a14e --- /dev/null +++ b/Assets/Scripts/Core/Graph Tasks/DragUnits.cs @@ -0,0 +1,51 @@ +using System.Collections.Generic; +using NodeCanvas.Framework; +using ParadoxNotion.Design; +using UnityEngine; + + +namespace Reset.Units { + + [Category("Reset/Units/Combat")] + [Description("Drag a collection of units along with the attackers movement or actions")] + public class DragUnits : ActionTask{ + public BBParameter> unitsToDrag; + public BBParameter dragTime; + + //Use for initialization. This is called only once in the lifetime of the task. + //Return null if init was successfull. Return an error string otherwise + protected override string OnInit() { + return null; + } + + //This is called once each time the task is enabled. + //Call EndAction() to mark the action as finished, either in success or failure. + //EndAction can be called from anywhere. + protected override void OnExecute() { + foreach (Collider col in unitsToDrag.value) { + if (dragTime.value == 0f) { + agent.AddDragCollider(col); + } else { + agent.AddDragCollider(col, dragTime.value); + } + } + + EndAction(true); + } + + //Called once per frame while the action is active. + protected override void OnUpdate() { + + } + + //Called when the task is disabled. + protected override void OnStop() { + + } + + //Called when the task is paused. + protected override void OnPause() { + + } + } +} \ No newline at end of file diff --git a/Assets/Scripts/Core/Graph Tasks/DragUnits.cs.meta b/Assets/Scripts/Core/Graph Tasks/DragUnits.cs.meta new file mode 100644 index 0000000..2c0259c --- /dev/null +++ b/Assets/Scripts/Core/Graph Tasks/DragUnits.cs.meta @@ -0,0 +1,2 @@ +fileFormatVersion: 2 +guid: a15fb8c5108ca6a47a12fcf6278579fd \ No newline at end of file diff --git a/Assets/Scripts/Units/Combat/UnitCombat.cs b/Assets/Scripts/Units/Combat/UnitCombat.cs index 220fc0a..541a8bf 100644 --- a/Assets/Scripts/Units/Combat/UnitCombat.cs +++ b/Assets/Scripts/Units/Combat/UnitCombat.cs @@ -7,7 +7,8 @@ using UnityEngine; using Random = UnityEngine.Random; public class UnitCombat : UnitComponent { - public List draggedUnits = new List(); + // public List draggedUnits = new List(); + public Dictionary draggedColliders = new Dictionary(); private UnitMovementHandler movement; @@ -18,9 +19,15 @@ public class UnitCombat : UnitComponent { private float speedDelta; private float sinAmplitude; private float sinOffset; + + private List activelyDraggedColliders; + private List expiredColliders; void Awake(){ movement = GetComponent(); + + activelyDraggedColliders = new List(); + expiredColliders = new List(); } void Start(){ @@ -31,7 +38,48 @@ public class UnitCombat : UnitComponent { // Update is called once per frame void Update(){ + // Calculate movement for dragged units DragAttackedUnits(); + + // Check timers to make sure an object doesn't stay dragged + CheckUnitTimers(); + } + + // dragTime is set as a maximum. If no time is provided then it defaults to two seconds. + public void AddDragCollider(Collider newCollider, float dragTime = 2f){ + if (draggedColliders.ContainsKey(newCollider)) { + draggedColliders[newCollider] = dragTime; + } else { + draggedColliders.Add(newCollider, dragTime); + activelyDraggedColliders.Add(newCollider); + } + } + + public void RemoveDragCollider(Collider colliderToRemove){ + draggedColliders.Remove(colliderToRemove); + activelyDraggedColliders.Remove(colliderToRemove); + } + + void CheckUnitTimers(){ + // Decrease the timer of the dragged colliders + foreach (Collider thisCollider in activelyDraggedColliders) { + draggedColliders[thisCollider] -= 1f * Time.deltaTime; + + // Pend them for removal when ready + if (draggedColliders[thisCollider] < 0f) { + expiredColliders.Add(thisCollider); + } + } + + // Remove expired colliders + for (int i = 0; i < expiredColliders.Count; i++) { + RemoveDragCollider(expiredColliders[i]); + } + + // Clear list if not empty + if (expiredColliders.Count > 0f) { + expiredColliders.Clear(); + } } void DragAttackedUnits(){ @@ -58,7 +106,7 @@ public class UnitCombat : UnitComponent { lastPosition = transform.position; // Apply the speed, direction, and rotation to each unit - foreach (Collider draggedUnit in draggedUnits) { + foreach (Collider draggedUnit in activelyDraggedColliders) { UnitMovementHandler draggedUnitMovement = draggedUnit.GetComponent(); if (!draggedUnitMovement) { Debug.LogError($"No available UnitMovement on {draggedUnit.name}. Aborting drag on this unit.");