This commit is contained in:
PC-20230316NUNE\Administrator
2024-02-02 15:38:13 +08:00
parent 877dca3b43
commit 00f56e11c7
228 changed files with 36944 additions and 20084 deletions

View File

@@ -1,5 +1,7 @@
using UnityEngine;
using System.Collections.Generic;
using Game.Plugins.App.Sync;
using Game.Plugins.App.Util;
using Pathfinding.RVO;
namespace Pathfinding.Examples {
@@ -19,7 +21,7 @@ namespace Pathfinding.Examples {
/// [Open online documentation to see images]
/// </summary>
[HelpURL("https://arongranberg.com/astar/documentation/stable/class_pathfinding_1_1_examples_1_1_lightweight_r_v_o.php")]
public class LightweightRVO : MonoBehaviour {
public class LightweightRVO : JNGSyncFrameDefault {
/// <summary>Number of agents created at start</summary>
public int agentCount = 100;
@@ -137,7 +139,7 @@ namespace Pathfinding.Examples {
}
private float uniformDistance (float radius) {
float v = Random.value + Random.value;
float v = JNRandom.value + JNRandom.value;
if (v > 1) return radius * (2-v);
else return radius * v;
@@ -157,7 +159,7 @@ namespace Pathfinding.Examples {
float circleRad = Mathf.Sqrt(agentCount * radius * radius * 4 / Mathf.PI) * exampleScale * 0.05f;
for (int i = 0; i < agentCount; i++) {
Vector3 pos = new Vector3(Mathf.Cos(i * Mathf.PI * 2.0f / agentCount), 0, Mathf.Sin(i * Mathf.PI * 2.0f / agentCount)) * circleRad * (1 + Random.value * 0.01f);
Vector3 pos = new Vector3(Mathf.Cos(i * Mathf.PI * 2.0f / agentCount), 0, Mathf.Sin(i * Mathf.PI * 2.0f / agentCount)) * circleRad * (1 + JNRandom.value * 0.01f);
IAgent agent = sim.AddAgent(new Vector2(pos.x, pos.z), pos.y);
agents.Add(agent);
goals.Add(-pos);
@@ -183,8 +185,8 @@ namespace Pathfinding.Examples {
float circleRad = Mathf.Sqrt(agentCount * radius * radius * 4 / Mathf.PI) * exampleScale * 0.05f;
for (int i = 0; i < agentCount; i++) {
float angle = Random.value * Mathf.PI * 2.0f;
float targetAngle = Random.value * Mathf.PI * 2.0f;
float angle = JNRandom.value * Mathf.PI * 2.0f;
float targetAngle = JNRandom.value * Mathf.PI * 2.0f;
Vector3 pos = new Vector3(Mathf.Cos(angle), 0, Mathf.Sin(angle)) * uniformDistance(circleRad);
IAgent agent = sim.AddAgent(new Vector2(pos.x, pos.z), pos.y);
agents.Add(agent);
@@ -199,7 +201,7 @@ namespace Pathfinding.Examples {
const int AgentsPerDistance = 10;
for (int i = 0; i < agentCount; i++) {
float angle = ((i % directions)/(float)directions) * Mathf.PI * 2.0f;
var dist = distanceBetweenGroups * ((i/(directions*AgentsPerDistance) + 1) + 0.3f*Random.value);
var dist = distanceBetweenGroups * ((i/(directions*AgentsPerDistance) + 1) + 0.3f*JNRandom.value);
Vector3 pos = new Vector3(Mathf.Cos(angle), 0, Mathf.Sin(angle)) * dist;
IAgent agent = sim.AddAgent(new Vector2(pos.x, pos.z), pos.y);
agent.Priority = (i % directions) == 0 ? 1 : 0.01f;
@@ -255,7 +257,7 @@ namespace Pathfinding.Examples {
// Move agent
// This is the responsibility of this script, not the RVO system
Vector2 pos = agent.Position;
var deltaPosition = Vector2.ClampMagnitude(agent.CalculatedTargetPoint - pos, agent.CalculatedSpeed * Time.deltaTime);
var deltaPosition = Vector2.ClampMagnitude(agent.CalculatedTargetPoint - pos, agent.CalculatedSpeed * GetSync().Time.deltaTime);
pos += deltaPosition;
agent.Position = pos;
@@ -270,7 +272,7 @@ namespace Pathfinding.Examples {
interpolatedVelocities[i] += deltaPosition;
if (interpolatedVelocities[i].magnitude > maxSpeed*0.1f) {
interpolatedVelocities[i] = Vector2.ClampMagnitude(interpolatedVelocities[i], maxSpeed*0.1f);
interpolatedRotations[i] = Vector2.Lerp(interpolatedRotations[i], interpolatedVelocities[i], agent.CalculatedSpeed * Time.deltaTime*4f);
interpolatedRotations[i] = Vector2.Lerp(interpolatedRotations[i], interpolatedVelocities[i], agent.CalculatedSpeed * GetSync().Time.deltaTime*4f);
}
//Debug.DrawRay(new Vector3(pos.x, 0, pos.y), new Vector3(interpolatedVelocities[i].x, 0, interpolatedVelocities[i].y) * 10);

View File

@@ -1,5 +1,7 @@
using UnityEngine;
using System.Collections.Generic;
using Game.Plugins.App.Sync;
using Game.Plugins.App.Util;
using Pathfinding.RVO;
namespace Pathfinding.Examples {
@@ -16,7 +18,7 @@ namespace Pathfinding.Examples {
[RequireComponent(typeof(RVOController))]
[RequireComponent(typeof(Seeker))]
[HelpURL("https://arongranberg.com/astar/documentation/stable/class_pathfinding_1_1_examples_1_1_r_v_o_example_agent.php")]
public class RVOExampleAgent : MonoBehaviour {
public class RVOExampleAgent : JNGSyncFrameDefault {
public float repathRate = 1;
private float nextRepath = 0;
@@ -81,7 +83,7 @@ namespace Pathfinding.Examples {
public void RecalculatePath () {
canSearchAgain = false;
nextRepath = Time.time+repathRate*(Random.value+0.5f);
nextRepath = GetSync().Time.time+repathRate*(JNRandom.value+0.5f);
seeker.StartPath(transform.position, target, OnPathComplete);
}
@@ -124,7 +126,7 @@ namespace Pathfinding.Examples {
}
public void Update () {
if (Time.time >= nextRepath && canSearchAgain) {
if (GetSync().Time.time >= nextRepath && canSearchAgain) {
RecalculatePath();
}
@@ -169,18 +171,18 @@ namespace Pathfinding.Examples {
// Get a processed movement delta from the rvo controller and move the character.
// This is based on information from earlier frames.
var movementDelta = controller.CalculateMovementDelta(Time.deltaTime);
var movementDelta = controller.CalculateMovementDelta(GetSync().Time.deltaTime);
pos += movementDelta;
// Rotate the character if the velocity is not extremely small
if (Time.deltaTime > 0 && movementDelta.magnitude / Time.deltaTime > 0.01f) {
if (GetSync().Time.deltaTime > 0 && movementDelta.magnitude / GetSync().Time.deltaTime > 0.01f) {
var rot = transform.rotation;
var targetRot = Quaternion.LookRotation(movementDelta, controller.To3D(Vector2.zero, 1));
const float RotationSpeed = 5;
if (controller.movementPlane == MovementPlane.XY) {
targetRot = targetRot * Quaternion.Euler(-90, 180, 0);
}
transform.rotation = Quaternion.Slerp(rot, targetRot, Time.deltaTime * RotationSpeed);
transform.rotation = Quaternion.Slerp(rot, targetRot, GetSync().Time.deltaTime * RotationSpeed);
}
if (controller.movementPlane == MovementPlane.XZ) {

View File

@@ -1,3 +1,4 @@
using Game.Plugins.App.Sync;
using UnityEngine;
namespace Pathfinding.Examples {
@@ -6,7 +7,7 @@ namespace Pathfinding.Examples {
/// Helper script in the example scene called 'Moving'.
/// </summary>
[HelpURL("https://arongranberg.com/astar/documentation/stable/class_pathfinding_1_1_examples_1_1_bezier_mover.php")]
public class BezierMover : MonoBehaviour {
public class BezierMover : JNGSyncFrameDefault {
public Transform[] points;
public float speed = 1;
@@ -57,7 +58,7 @@ namespace Pathfinding.Examples {
float mid = (mn+mx)/2;
Vector3 p = Evaluate(mid, out var dummy1, out var dummy2, out var dummy3);
if ((p-transform.position).sqrMagnitude > (speed*Time.deltaTime)*(speed*Time.deltaTime)) {
if ((p-transform.position).sqrMagnitude > (speed*GetSync().Time.deltaTime)*(speed*GetSync().Time.deltaTime)) {
mx = mid;
} else {
mn = mid;
@@ -68,7 +69,7 @@ namespace Pathfinding.Examples {
transform.position = Evaluate(time, out var derivative, out var dummy, out var curvature);
averageCurvature = Vector3.Lerp(averageCurvature, curvature, Time.deltaTime);
averageCurvature = Vector3.Lerp(averageCurvature, curvature, GetSync().Time.deltaTime);
// Estimate the acceleration at the current point and use it to tilt the object inwards on the curve
var centripetalAcceleration = -Vector3.Cross(derivative.normalized, averageCurvature);

View File

@@ -1,13 +1,15 @@
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using Game.Plugins.App.Sync;
using Pathfinding;
using Plugins.JNGame.Sync.Frame.Entity;
using UnityEngine.EventSystems;
namespace Pathfinding.Examples {
/// <summary>Helper script in the example scene 'Turn Based'</summary>
[HelpURL("https://arongranberg.com/astar/documentation/stable/class_pathfinding_1_1_examples_1_1_turn_based_manager.php")]
public class TurnBasedManager : MonoBehaviour {
public class TurnBasedManager : JNGSyncFrameDefault {
TurnBasedAI selected;
public float movementSpeed;
@@ -136,7 +138,7 @@ namespace Pathfinding.Examples {
var interpolatedPoint = AstarSplines.CatmullRom(p0, p1, p2, p3, distanceAlongSegment / segmentLength);
unit.transform.position = interpolatedPoint;
yield return null;
distanceAlongSegment += Time.deltaTime * speed;
distanceAlongSegment += JNTime.Time.deltaTime * speed;
}
distanceAlongSegment -= segmentLength;

View File

@@ -53,14 +53,16 @@ namespace Pathfinding.Examples {
}
/// <summary>Update is called once per frame</summary>
void Update () {
public override void OnSyncUpdate(int dt, JNFrameInfo frame, Object input)
{
base.OnSyncUpdate(dt, frame, input);
var aiBase = ai as AIBase;
aiBase.canMove = false;
// aiBase.updatePosition = false;
// aiBase.updateRotation = false;
}
/// <summary>Calculate position of the currently grounded foot</summary>
Vector3 CalculateBlendPoint () {
// Fall back to rotating around the transform position if no feet could be found
@@ -72,8 +74,8 @@ namespace Pathfinding.Examples {
// This is the same calculation that Unity uses for
// Animator.pivotWeight and Animator.pivotPosition
// but those properties do not work for all animations apparently.
var footVelocity1 = (leftFootPos - prevFootPos[0]) / Time.deltaTime;
var footVelocity2 = (rightFootPos - prevFootPos[1]) / Time.deltaTime;
var footVelocity1 = (leftFootPos - prevFootPos[0]) / GetSync().Time.deltaTime;
var footVelocity2 = (rightFootPos - prevFootPos[1]) / GetSync().Time.deltaTime;
float denominator = footVelocity1.magnitude + footVelocity2.magnitude;
var pivotWeight = denominator > 0 ? footVelocity1.magnitude / denominator : 0.5f;
prevFootPos[0] = leftFootPos;
@@ -86,7 +88,7 @@ namespace Pathfinding.Examples {
Vector3 nextPosition;
Quaternion nextRotation;
ai.MovementUpdate(Time.deltaTime, out nextPosition, out nextRotation);
ai.MovementUpdate(GetSync().Time.deltaTime, out nextPosition, out nextRotation);
//var desiredVelocity = (ai.steeringTarget - tr.position).normalized * 2;//ai.desiredVelocity;
var desiredVelocity = ai.desiredVelocity;
@@ -98,7 +100,7 @@ namespace Pathfinding.Examples {
// Calculate the desired velocity relative to the character (+Z = forward, +X = right)
var localDesiredVelocity = tr.InverseTransformDirection(desiredVelocityWithoutGrav);
smoothedVelocity = Vector3.Lerp(smoothedVelocity, localDesiredVelocity, velocitySmoothing > 0 ? Time.deltaTime / velocitySmoothing : 1);
smoothedVelocity = Vector3.Lerp(smoothedVelocity, localDesiredVelocity, velocitySmoothing > 0 ? GetSync().Time.deltaTime / velocitySmoothing : 1);
if (smoothedVelocity.magnitude < 0.4f) {
smoothedVelocity = smoothedVelocity.normalized * 0.4f;
}
@@ -116,7 +118,7 @@ namespace Pathfinding.Examples {
}
// Calculate how much the agent should rotate during this frame
var newRot = RotateTowards(desiredVelocityWithoutGrav, Time.deltaTime * rotationSpeed);
var newRot = RotateTowards(desiredVelocityWithoutGrav, GetSync().Time.deltaTime * rotationSpeed);
// Rotate the character around the currently grounded foot to prevent foot sliding
nextPosition = ai.position;
nextRotation = ai.rotation;
@@ -129,7 +131,7 @@ namespace Pathfinding.Examples {
// Use gravity from the movement script, not from animation
var deltaPos = anim.deltaPosition;
deltaPos.y = desiredVelocity.y * Time.deltaTime;
deltaPos.y = desiredVelocity.y * GetSync().Time.deltaTime;
nextPosition += deltaPos;
// Call the movement script to perform the final movement

View File

@@ -1,5 +1,6 @@
using UnityEngine;
using System.Collections;
using Game.Plugins.App.Sync;
namespace Pathfinding.Examples {
using Pathfinding.RVO;
@@ -13,7 +14,7 @@ namespace Pathfinding.Examples {
/// </summary>
[RequireComponent(typeof(RVOController))]
[HelpURL("https://arongranberg.com/astar/documentation/stable/class_pathfinding_1_1_examples_1_1_manual_r_v_o_agent.php")]
public class ManualRVOAgent : MonoBehaviour {
public class ManualRVOAgent : JNGSyncFrameDefault {
RVOController rvo;
public float speed = 1;
@@ -22,7 +23,9 @@ namespace Pathfinding.Examples {
rvo = GetComponent<RVOController>();
}
void Update () {
public override void OnSyncUpdate(int dt, JNFrameInfo frame, Object input)
{
base.OnSyncUpdate(dt, frame, input);
var x = Input.GetAxis("Horizontal");
var y = Input.GetAxis("Vertical");
@@ -30,7 +33,7 @@ namespace Pathfinding.Examples {
// Override the RVOController's velocity. This will disable local avoidance calculations for one simulation step.
rvo.velocity = v;
transform.position += v * Time.deltaTime;
transform.position += v * GetSync().Time.deltaTime;
}
}
}

View File

@@ -57,7 +57,9 @@ namespace Pathfinding.Examples {
}
}
protected void Update () {
public override void OnSyncUpdate(int dt, JNFrameInfo frame, Object input)
{
base.OnSyncUpdate(dt, frame, input);
if (ai.reachedEndOfPath) {
if (!isAtDestination) OnTargetReached();
isAtDestination = true;

View File

@@ -1,10 +1,11 @@
using UnityEngine;
using System.Collections;
using Game.Plugins.App.Sync;
namespace Pathfinding.Examples {
/// <summary>Small sample script for placing obstacles</summary>
[HelpURL("https://arongranberg.com/astar/documentation/stable/class_pathfinding_1_1_examples_1_1_object_placer.php")]
public class ObjectPlacer : MonoBehaviour {
public class ObjectPlacer : JNGSyncFrameDefault {
/// <summary>
/// GameObject to place.
/// When using a Grid Graph you need to make sure the object's layer is included in the collision mask in the GridGraph settings.
@@ -19,11 +20,12 @@ namespace Pathfinding.Examples {
float lastPlacedTime;
/// <summary>Update is called once per frame</summary>
void Update () {
public override void OnSyncUpdate(int dt, JNFrameInfo frame, Object input)
{
base.OnSyncUpdate(dt, frame, input);
// Check if P is being pressed.
// Don't place objects if ctrl is pressed to avoid conflicts with the pause shortcut (ctrl+shift+P)
if (!Input.GetKey(KeyCode.LeftControl) && (Input.GetKeyDown("p") || (Input.GetKey("p") && Time.time - lastPlacedTime > 0.3f))) {
if (!Input.GetKey(KeyCode.LeftControl) && (Input.GetKeyDown("p") || (Input.GetKey("p") && GetSync().Time.time - lastPlacedTime > 0.3f))) {
PlaceObject();
}
@@ -31,9 +33,9 @@ namespace Pathfinding.Examples {
StartCoroutine(RemoveObject());
}
}
public void PlaceObject () {
lastPlacedTime = Time.time;
lastPlacedTime = GetSync().Time.time;
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
RaycastHit hit;

View File

@@ -1,3 +1,4 @@
using Game.Plugins.App.Sync;
using UnityEngine;
namespace Pathfinding {
@@ -43,7 +44,7 @@ namespace Pathfinding {
/// </summary>
[AddComponentMenu("Pathfinding/Navmesh/RecastTileUpdateHandler")]
[HelpURL("https://arongranberg.com/astar/documentation/stable/class_pathfinding_1_1_recast_tile_update_handler.php")]
public class RecastTileUpdateHandler : MonoBehaviour {
public class RecastTileUpdateHandler : JNGSyncFrameDefault {
/// <summary>Graph that handles the updates</summary>
RecastGraph graph;
@@ -96,7 +97,7 @@ namespace Pathfinding {
if (touching.Width * touching.Height > 0) {
if (!anyDirtyTiles) {
earliestDirty = Time.time;
earliestDirty = GetSync().Time.time;
anyDirtyTiles = true;
}
@@ -116,8 +117,10 @@ namespace Pathfinding {
RecastTileUpdate.OnNeedUpdates -= ScheduleUpdate;
}
void Update () {
if (anyDirtyTiles && Time.time - earliestDirty >= maxThrottlingDelay && graph != null) {
public override void OnSyncUpdate(int dt, JNFrameInfo frame, Object input)
{
base.OnSyncUpdate(dt, frame, input);
if (anyDirtyTiles && GetSync().Time.time - earliestDirty >= maxThrottlingDelay && graph != null) {
UpdateDirtyTiles();
}
}

View File

@@ -15,7 +15,9 @@ namespace Pathfinding.Examples {
public float rotationDamping = 10.0f;
public bool staticOffset = false;
void LateUpdate () {
public override void OnLateSyncUpdate(int dt, JNFrameInfo frame, Object input)
{
base.OnLateSyncUpdate(dt, frame, input);
Vector3 wantedPosition;
if (staticOffset) {
@@ -23,12 +25,12 @@ namespace Pathfinding.Examples {
} else {
wantedPosition = target.TransformPoint(0, height, -distance);
}
transform.position = Vector3.Lerp(transform.position, wantedPosition, Time.deltaTime * damping);
transform.position = Vector3.Lerp(transform.position, wantedPosition, GetSync().Time.deltaTime * damping);
if (enableRotation) {
if (smoothRotation) {
Quaternion wantedRotation = Quaternion.LookRotation(target.position - transform.position, target.up);
transform.rotation = Quaternion.Slerp(transform.rotation, wantedRotation, Time.deltaTime * rotationDamping);
transform.rotation = Quaternion.Slerp(transform.rotation, wantedRotation, GetSync().Time.deltaTime * rotationDamping);
} else transform.LookAt(target, target.up);
}
}

View File

@@ -1,5 +1,6 @@
using UnityEngine;
using System.Linq;
using Game.Plugins.App.Sync;
namespace Pathfinding {
/// <summary>
@@ -11,7 +12,7 @@ namespace Pathfinding {
/// It is not meant to be pretty, but it does the job.
/// </summary>
[HelpURL("https://arongranberg.com/astar/documentation/stable/class_pathfinding_1_1_target_mover.php")]
public class TargetMover : MonoBehaviour {
public class TargetMover : JNGSyncFrameDefault {
/// <summary>Mask for the raycast placement</summary>
public LayerMask mask;