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

@@ -7,7 +7,8 @@ using UnityEngine;
using Random = UnityEngine.Random;
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;
@@ -18,9 +19,15 @@ public class UnitCombat : UnitComponent {
private float speedDelta;
private float sinAmplitude;
private float sinOffset;
private List<Collider> activelyDraggedColliders;
private List<Collider> expiredColliders;
void Awake(){
movement = GetComponent<UnitMovementHandler>();
activelyDraggedColliders = new List<Collider>();
expiredColliders = new List<Collider>();
}
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<UnitMovementHandler>();
if (!draggedUnitMovement) {
Debug.LogError($"No available UnitMovement on {draggedUnit.name}. Aborting drag on this unit.");