public void Execute(ArchetypeChunk chunk, int chunkIndex, int firstEntityIndex) { NativeArray <PlayerMovementData> playerMovements = chunk.GetNativeArray(this.playerMovementType); BufferAccessor <PlayerNavigationNodeElement> nodeBufferAccessor = chunk.GetBufferAccessor(this.pathNodeBufferType); for (int ci = 0, cn = chunk.Count; ci < cn; ci++) { PlayerMovementData playerMovement = playerMovements[ci]; DynamicBuffer <PlayerNavigationNodeElement> pathNodeBuffer = nodeBufferAccessor[ci]; // Set path if found if (pathFound[0] != 0) { // Copy path to player's path node buffer pathNodeBuffer.Clear(); for (int i = 0, n = pathFound[1]; i < n; i++) { float3 worldPosition = navigationGrid[0].GridToWorldPosition(pathNodes[i].XCoord, pathNodes[i].YCoord); pathNodeBuffer.Add(new PlayerNavigationNode(pathNodes[i].XCoord, pathNodes[i].YCoord, pathNodes[i].Index, worldPosition)); } // Start player movement playerMovement.StartMovement(pathNodeBuffer[0]); // Apply changes playerMovements[ci] = playerMovement; } } }
public void Execute(ArchetypeChunk chunk, int chunkIndex, int firstEntityIndex) { NativeArray <Translation> translations = chunk.GetNativeArray(this.translationType); NativeArray <Rotation> rotations = chunk.GetNativeArray(this.rotationType); NativeArray <PlayerMovementData> playerMovements = chunk.GetNativeArray(this.playerMovementType); BufferAccessor <PlayerNavigationNodeElement> nodeBufferAccessor = chunk.GetBufferAccessor(this.pathNodeBufferType); for (int ci = 0, cn = chunk.Count; ci < cn; ci++) { Translation translation = translations[ci]; Rotation rotation = rotations[ci]; PlayerMovementData playerMovement = playerMovements[ci]; DynamicBuffer <PlayerNavigationNodeElement> pathNodeBuffer = nodeBufferAccessor[ci]; // Move player along path if moving if (playerMovement.isMoving) { float3 currentPosition = translation.Value; // Current/target nodes PlayerNavigationNode currentNode = playerMovement.currentNode; PlayerNavigationNode targetNode = pathNodeBuffer[playerMovement.movementTargetPathNodeIndex].Value; float3 fromPosition = currentNode.WorldPosition; float3 toPosition = targetNode.WorldPosition; // Get velocity and move float d = math.length(toPosition - fromPosition); float secs = d / playerMovement.movementVelocity; playerMovement.movementT += playerMovement.movementVelocity / secs; currentPosition = math.lerp(fromPosition, toPosition, playerMovement.movementT); if (playerMovement.movementT >= 1f) { if (playerMovement.movementTargetPathNodeIndex < pathNodeBuffer.Length - 1) { playerMovement.ToNextPathNode(targetNode); } else { playerMovement.FinishMovement(targetNode); currentPosition = toPosition; } } translation.Value = currentPosition; // Rotate toward movement direction quaternion targetRot = quaternion.LookRotation(math.normalize(toPosition - fromPosition), math.up()); float3 rotationDir = math.lerp(math.forward(rotation.Value), math.normalize(toPosition - fromPosition), playerMovement.rotationSmoothFactor * deltaTime); rotation.Value = UnityEngine.Quaternion.Lerp(rotation.Value, targetRot, playerMovement.rotationSmoothFactor * deltaTime); // Apply changes translations[ci] = translation; rotations[ci] = rotation; playerMovements[ci] = playerMovement; } } }