feat: rebuilt dragging mechanic slightly, and it's now more agnostic to collider type

This commit is contained in:
Chris
2025-12-05 20:37:06 -05:00
parent ff4ccfdb1b
commit a0ed17666d
3 changed files with 103 additions and 2 deletions

View File

@@ -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<UnitCombat>{
public BBParameter<List<Collider>> unitsToDrag;
public BBParameter<float> 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() {
}
}
}

View File

@@ -0,0 +1,2 @@
fileFormatVersion: 2
guid: a15fb8c5108ca6a47a12fcf6278579fd

View File

@@ -7,7 +7,8 @@ using UnityEngine;
using Random = UnityEngine.Random; using Random = UnityEngine.Random;
public class UnitCombat : UnitComponent { public class UnitCombat : UnitComponent {
public List<Collider> draggedUnits = new List<Collider>(); // public List<Collider> draggedUnits = new List<Collider>();
public Dictionary<Collider, float> draggedColliders = new Dictionary<Collider, float>();
private UnitMovementHandler movement; private UnitMovementHandler movement;
@@ -18,9 +19,15 @@ public class UnitCombat : UnitComponent {
private float speedDelta; private float speedDelta;
private float sinAmplitude; private float sinAmplitude;
private float sinOffset; private float sinOffset;
private List<Collider> activelyDraggedColliders;
private List<Collider> expiredColliders;
void Awake(){ void Awake(){
movement = GetComponent<UnitMovementHandler>(); movement = GetComponent<UnitMovementHandler>();
activelyDraggedColliders = new List<Collider>();
expiredColliders = new List<Collider>();
} }
void Start(){ void Start(){
@@ -31,7 +38,48 @@ public class UnitCombat : UnitComponent {
// Update is called once per frame // Update is called once per frame
void Update(){ void Update(){
// Calculate movement for dragged units
DragAttackedUnits(); 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(){ void DragAttackedUnits(){
@@ -58,7 +106,7 @@ public class UnitCombat : UnitComponent {
lastPosition = transform.position; lastPosition = transform.position;
// Apply the speed, direction, and rotation to each unit // Apply the speed, direction, and rotation to each unit
foreach (Collider draggedUnit in draggedUnits) { foreach (Collider draggedUnit in activelyDraggedColliders) {
UnitMovementHandler draggedUnitMovement = draggedUnit.GetComponent<UnitMovementHandler>(); UnitMovementHandler draggedUnitMovement = draggedUnit.GetComponent<UnitMovementHandler>();
if (!draggedUnitMovement) { if (!draggedUnitMovement) {
Debug.LogError($"No available UnitMovement on {draggedUnit.name}. Aborting drag on this unit."); Debug.LogError($"No available UnitMovement on {draggedUnit.name}. Aborting drag on this unit.");