Swapping cylinder for default human and adding animation states
This commit is contained in:
parent
f05734e19b
commit
ab35ad0e32
33 changed files with 513 additions and 137 deletions
|
|
@ -78,45 +78,11 @@ namespace TD.Gameplay
|
|||
|
||||
// ----- Inspector --------------------------------------------------
|
||||
|
||||
[Header("Movement")]
|
||||
[Tooltip("Speed at which the builder moves toward its target position, in world " +
|
||||
"units per second.")]
|
||||
[SerializeField] private float moveSpeed = 8f;
|
||||
|
||||
[Tooltip("Distance below which the builder is considered to have arrived at its " +
|
||||
"target. Smaller = more precise but more jitter; larger = less precise " +
|
||||
"but smoother.")]
|
||||
[SerializeField] private float arrivalThreshold = 0.05f;
|
||||
|
||||
[Tooltip("Degrees per second the builder rotates to face its movement direction. " +
|
||||
"Lower = lazier turns; higher = snappier. The builder only rotates while " +
|
||||
"moving; it keeps its last facing when idle.")]
|
||||
[SerializeField] private float turnRateDegPerSec = 540f;
|
||||
|
||||
[Header("Height tracking")]
|
||||
[Tooltip("Vertical offset above the terrain at which the builder hovers. " +
|
||||
"Re-evaluated every server tick by raycasting straight down.")]
|
||||
[SerializeField] private float heightOffset = 2f;
|
||||
|
||||
[Tooltip("Maximum distance to cast downward when sampling terrain height. Should " +
|
||||
"exceed your map's vertical range.")]
|
||||
[SerializeField] private float terrainRaycastMaxDistance = 100f;
|
||||
|
||||
[Tooltip("Physics layer mask used for terrain height sampling. Towers MUST NOT be " +
|
||||
"on this layer — only ground geometry. Falls back to the buildable plane Y " +
|
||||
"if no terrain hit.")]
|
||||
[SerializeField] private LayerMask terrainLayerMask;
|
||||
|
||||
[Header("Build range")]
|
||||
[Tooltip("Maximum distance from the builder's center to a tower's anchor tile center " +
|
||||
"for placement to be allowed, measured in world units (== tiles).")]
|
||||
[SerializeField] private float buildRange = 6f;
|
||||
[Header("Settings")]
|
||||
[Tooltip("Shared tunable values for all builders. Create via TD/Builder Settings.")]
|
||||
[SerializeField] private BuilderSettings settings;
|
||||
|
||||
[Header("Build queue")]
|
||||
[Tooltip("Maximum number of pending build jobs. Bounds memory and prevents a player " +
|
||||
"from spamming queue entries faster than the server can process them.")]
|
||||
[SerializeField] private int maxQueueDepth = 32;
|
||||
|
||||
[Tooltip("Build-site visual prefab. Spawned at queue-time as a green ghost; " +
|
||||
"transitions to staged-construction visuals on arrival; despawned on " +
|
||||
"completion (replaced by the real TowerInstance) or cancellation.")]
|
||||
|
|
@ -128,7 +94,12 @@ namespace TD.Gameplay
|
|||
"BuildRangeIndicator, or any other visual that has its own color rules. " +
|
||||
"If left empty, the builder will not be tinted (other meshes' colors " +
|
||||
"from the prefab are preserved).")]
|
||||
[SerializeField] private MeshRenderer[] tintedRenderers;
|
||||
[SerializeField] private SkinnedMeshRenderer[] tintedRenderers;
|
||||
|
||||
[Header("Animation")]
|
||||
[Tooltip("Animator on the character model child. Drives IsMoving and IsConstructing " +
|
||||
"bool parameters each frame on all clients.")]
|
||||
[SerializeField] private Animator animator;
|
||||
|
||||
// ----- Networked state --------------------------------------------
|
||||
|
||||
|
|
@ -173,16 +144,16 @@ namespace TD.Gameplay
|
|||
public Vector3 TargetPosition => targetPosition.Value;
|
||||
|
||||
/// <summary>True if the builder has arrived at its target (within
|
||||
/// <see cref="arrivalThreshold"/>).</summary>
|
||||
/// <see cref="BuilderSettings.arrivalThreshold"/>).</summary>
|
||||
public bool IsAtTarget =>
|
||||
Vector3.SqrMagnitude(transform.position - targetPosition.Value)
|
||||
< arrivalThreshold * arrivalThreshold;
|
||||
< settings.arrivalThreshold * settings.arrivalThreshold;
|
||||
|
||||
/// <summary>Build range in world units.</summary>
|
||||
public float BuildRange => buildRange;
|
||||
public float BuildRange => settings.buildRange;
|
||||
|
||||
/// <summary>Maximum jobs allowed in the queue.</summary>
|
||||
public int MaxQueueDepth => maxQueueDepth;
|
||||
public int MaxQueueDepth => settings.maxQueueDepth;
|
||||
|
||||
/// <summary>True if a tile is currently part of any queued or constructing job.</summary>
|
||||
/// <remarks>
|
||||
|
|
@ -299,17 +270,37 @@ namespace TD.Gameplay
|
|||
}
|
||||
}
|
||||
|
||||
// ----- Per-frame movement (server only) ---------------------------
|
||||
// ----- Animation parameter hashes (cached to avoid per-frame string lookup) ---
|
||||
|
||||
private static readonly int IsMovingHash = Animator.StringToHash("IsMoving");
|
||||
private static readonly int IsConstructingHash = Animator.StringToHash("IsConstructing");
|
||||
|
||||
// ----- Per-frame update -------------------------------------------
|
||||
|
||||
private void Update()
|
||||
{
|
||||
if (!IsServer) return;
|
||||
if (IsServer)
|
||||
{
|
||||
ServerDriveQueue();
|
||||
ServerStepMovement();
|
||||
}
|
||||
|
||||
// Step 1: drive movement target from the queue head, if appropriate.
|
||||
ServerDriveQueue();
|
||||
UpdateAnimatorState();
|
||||
}
|
||||
|
||||
// Step 2: move toward the target on XZ, sample terrain Y.
|
||||
ServerStepMovement();
|
||||
private void UpdateAnimatorState()
|
||||
{
|
||||
if (animator == null) return;
|
||||
|
||||
Vector3 flatCurrent = new Vector3(transform.position.x, 0f, transform.position.z);
|
||||
Vector3 flatTarget = new Vector3(targetPosition.Value.x, 0f, targetPosition.Value.z);
|
||||
bool isMoving = Vector3.SqrMagnitude(flatCurrent - flatTarget)
|
||||
> settings.arrivalThreshold * settings.arrivalThreshold;
|
||||
|
||||
bool isConstructing = jobs.Count > 0 && jobs[0].Stage == BuildStage.Constructing;
|
||||
|
||||
animator.SetBool(IsMovingHash, isMoving);
|
||||
animator.SetBool(IsConstructingHash, isConstructing);
|
||||
}
|
||||
|
||||
private void ServerStepMovement()
|
||||
|
|
@ -323,20 +314,20 @@ namespace TD.Gameplay
|
|||
|
||||
Vector3 newXZ;
|
||||
bool moving;
|
||||
if (Vector3.SqrMagnitude(currentXZ - targetXZ) <= arrivalThreshold * arrivalThreshold)
|
||||
if (Vector3.SqrMagnitude(currentXZ - targetXZ) <= settings.arrivalThreshold * settings.arrivalThreshold)
|
||||
{
|
||||
newXZ = targetXZ;
|
||||
moving = false;
|
||||
}
|
||||
else
|
||||
{
|
||||
newXZ = Vector3.MoveTowards(currentXZ, targetXZ, moveSpeed * Time.deltaTime);
|
||||
newXZ = Vector3.MoveTowards(currentXZ, targetXZ, settings.moveSpeed * Time.deltaTime);
|
||||
moving = true;
|
||||
}
|
||||
|
||||
// Resolve Y from terrain.
|
||||
float groundY = SampleTerrainY(new Vector3(newXZ.x, 0f, newXZ.z));
|
||||
transform.position = new Vector3(newXZ.x, groundY + heightOffset, newXZ.z);
|
||||
transform.position = new Vector3(newXZ.x, groundY + settings.heightOffset, newXZ.z);
|
||||
|
||||
// Smoothly face the movement direction. We rotate on the server only;
|
||||
// NetworkTransform replicates the rotation to clients alongside position.
|
||||
|
|
@ -348,7 +339,7 @@ namespace TD.Gameplay
|
|||
{
|
||||
Quaternion desired = Quaternion.LookRotation(dir, Vector3.up);
|
||||
transform.rotation = Quaternion.RotateTowards(
|
||||
transform.rotation, desired, turnRateDegPerSec * Time.deltaTime);
|
||||
transform.rotation, desired, settings.turnRateDegPerSec * Time.deltaTime);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -361,9 +352,9 @@ namespace TD.Gameplay
|
|||
private float SampleTerrainY(Vector3 xzPos)
|
||||
{
|
||||
// Ray origin: high above the map. terrainRaycastMaxDistance defines how far to cast.
|
||||
Vector3 origin = new Vector3(xzPos.x, terrainRaycastMaxDistance, xzPos.z);
|
||||
Vector3 origin = new Vector3(xzPos.x, settings.terrainRaycastMaxDistance, xzPos.z);
|
||||
if (Physics.Raycast(origin, Vector3.down, out RaycastHit hit,
|
||||
terrainRaycastMaxDistance * 2f, terrainLayerMask))
|
||||
settings.terrainRaycastMaxDistance * 2f, settings.terrainLayerMask))
|
||||
{
|
||||
return hit.point.y;
|
||||
}
|
||||
|
|
@ -448,7 +439,7 @@ namespace TD.Gameplay
|
|||
Vector3 nearestPoint = new Vector3(nearestX, 0f, nearestZ);
|
||||
|
||||
return Vector3.SqrMagnitude(builderXZ - nearestPoint)
|
||||
<= buildRange * buildRange;
|
||||
<= settings.buildRange * settings.buildRange;
|
||||
}
|
||||
|
||||
// ===================================================================
|
||||
|
|
@ -466,7 +457,7 @@ namespace TD.Gameplay
|
|||
{
|
||||
jobId = 0;
|
||||
if (!IsServer) return false;
|
||||
if (jobs.Count >= maxQueueDepth) return false;
|
||||
if (jobs.Count >= settings.maxQueueDepth) return false;
|
||||
|
||||
jobId = nextJobId++;
|
||||
var job = BuildJob.CreateQueued(jobId, anchor, towerTypeId, goldSpent);
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue