mirror of
https://gitee.com/jisol/jisol-game/
synced 2025-11-11 08:38:45 +00:00
no message
This commit is contained in:
@@ -0,0 +1,134 @@
|
||||
using HPJ.Presentation.Agents;
|
||||
using HPJ.Simulation;
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
|
||||
namespace HPJ.Presentation
|
||||
{
|
||||
[RequireComponent(typeof(NavigationAgent))]
|
||||
public class AgentAreaDisplay : MonoBehaviour
|
||||
{
|
||||
[SerializeField]
|
||||
private AreaDisplayTile _tilePrefab;
|
||||
private Transform TileSpawn;
|
||||
[SerializeField]
|
||||
private bool _showTilesOutOfReach = true;
|
||||
|
||||
[SerializeField]
|
||||
private int _range = 25;
|
||||
|
||||
[SerializeField]
|
||||
private float _updatePeriod = 1;
|
||||
private float _updateTime = 0;
|
||||
|
||||
private List<AreaDisplayTile> _activeTiles = new List<AreaDisplayTile>();
|
||||
private List<AreaDisplayTile> _pool = new List<AreaDisplayTile>();
|
||||
|
||||
private NavigationAgent _agent;
|
||||
|
||||
private void Awake()
|
||||
{
|
||||
TileSpawn = new GameObject($"Tile Spawn for {name}").transform;
|
||||
_agent = GetComponent<NavigationAgent>();
|
||||
_updateTime = _updatePeriod + 1;
|
||||
}
|
||||
|
||||
// Update is called once per frame
|
||||
void Update()
|
||||
{
|
||||
if (NavigationManager.Instance == null)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
if (!NavigationManager.Instance)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
if (NavigationManager.Instance.MapSets.Count <= 0)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
_updateTime += Time.deltaTime;
|
||||
if (_updateTime >= _updatePeriod)
|
||||
{
|
||||
_updateTime = 0;
|
||||
UpdateTiles();
|
||||
}
|
||||
}
|
||||
|
||||
public void UpdateTiles()
|
||||
{
|
||||
//Debug.Log("Update Tiles");
|
||||
ReturnAllTiles();
|
||||
|
||||
List<IntVector2> TilesInRangeBySteps = _agent.GetTraversableTilesInRangeBySteps(_range);
|
||||
|
||||
if (TilesInRangeBySteps.Count == 0 && _updatePeriod > 1)
|
||||
{
|
||||
Invoke(nameof(UpdateTiles), 0.1f);
|
||||
return;
|
||||
}
|
||||
|
||||
if (_showTilesOutOfReach)
|
||||
{
|
||||
List<IntVector2> TilesInRange = _agent.GetTraversableTilesInRange(_range);
|
||||
|
||||
foreach (IntVector2 Tile in TilesInRange)
|
||||
{
|
||||
AreaDisplayTile NewTile = GetTile();
|
||||
NewTile.Set(_agent.CurrentMap, Tile, TilesInRangeBySteps.Contains(Tile));
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
foreach (IntVector2 Tile in TilesInRangeBySteps)
|
||||
{
|
||||
AreaDisplayTile NewTile = GetTile();
|
||||
NewTile.Set(_agent.CurrentMap, Tile, true);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
protected AreaDisplayTile GetTile()
|
||||
{
|
||||
if (_pool.Count > 0)
|
||||
{
|
||||
AreaDisplayTile NewTile = _pool[0];
|
||||
_pool.RemoveAt(0);
|
||||
NewTile.gameObject.SetActive(true);
|
||||
_activeTiles.Add(NewTile);
|
||||
|
||||
return NewTile;
|
||||
}
|
||||
else
|
||||
{
|
||||
AreaDisplayTile NewTile = Instantiate(_tilePrefab, TileSpawn);
|
||||
NewTile.Initialize(_agent.CurrentMap.SetSettings.MapSettings.TileSize);
|
||||
_activeTiles.Add(NewTile);
|
||||
|
||||
return NewTile;
|
||||
}
|
||||
}
|
||||
|
||||
protected void ReturnTile(AreaDisplayTile Tile)
|
||||
{
|
||||
Tile.gameObject.SetActive(false);
|
||||
_pool.Add(Tile);
|
||||
_activeTiles.Remove(Tile);
|
||||
}
|
||||
|
||||
protected void ReturnAllTiles()
|
||||
{
|
||||
foreach(AreaDisplayTile Tile in _activeTiles)
|
||||
{
|
||||
Tile.gameObject.SetActive(false);
|
||||
_pool.Add(Tile);
|
||||
}
|
||||
_activeTiles.Clear();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: d7e0c2278be67a94fa98c85390af48ea
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -0,0 +1,38 @@
|
||||
using HPJ.Simulation;
|
||||
using HPJ.Simulation.Map;
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
|
||||
namespace HPJ.Presentation
|
||||
{
|
||||
public class AreaDisplayTile : MonoBehaviour
|
||||
{
|
||||
[SerializeField]
|
||||
private Material _canReachMaterial;
|
||||
[SerializeField]
|
||||
private Material _cannotReachMaterial;
|
||||
[SerializeField]
|
||||
private MeshRenderer _meshRenderer;
|
||||
public IntVector2 CurrentTileIndex { get; protected set; }
|
||||
|
||||
public void Initialize(int TileSize)
|
||||
{
|
||||
transform.localScale = new Vector3(0.9f * TileSize, 0.5f * TileSize, 0.9f * TileSize) / 100f;
|
||||
}
|
||||
|
||||
public void Set(MapSet Map, IntVector2 TileIndex, bool CanReach)
|
||||
{
|
||||
if (CanReach)
|
||||
{
|
||||
_meshRenderer.material = _canReachMaterial;
|
||||
}
|
||||
else
|
||||
{
|
||||
_meshRenderer.material = _cannotReachMaterial;
|
||||
}
|
||||
CurrentTileIndex = TileIndex;
|
||||
transform.position = NavigationManager.Instance.GetTileCenterWorldPosition(TileIndex, Map);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 08ba8e78e34cf95459d2de572d20afbf
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -0,0 +1,40 @@
|
||||
using HPJ.Presentation.Agents;
|
||||
using UnityEngine;
|
||||
|
||||
namespace HPJ.Presentation
|
||||
{
|
||||
/// <summary>
|
||||
/// Am Example script to display the local avoidance feature
|
||||
/// </summary>
|
||||
public class LocalAvoidanceSceneScript : MonoBehaviour
|
||||
{
|
||||
public Transform StartTransform;
|
||||
public Transform EndTransform;
|
||||
public NavigationAgent AgentPrefab;
|
||||
|
||||
private NavigationAgent _firstAgent;
|
||||
|
||||
private bool _direction = false;
|
||||
|
||||
private void Start()
|
||||
{
|
||||
_firstAgent = Instantiate(AgentPrefab, StartTransform.position, Quaternion.identity);
|
||||
_firstAgent.OnMovingComplete.AddListener(RepeatDestination);
|
||||
_firstAgent?.GroundCast?.TeleportUp();
|
||||
_firstAgent.SetDestination(EndTransform.position);
|
||||
}
|
||||
|
||||
private void RepeatDestination(NavigationAgent Agent)
|
||||
{
|
||||
if (_direction)
|
||||
{
|
||||
Agent.SetDestination(EndTransform.position);
|
||||
}
|
||||
else
|
||||
{
|
||||
Agent.SetDestination(StartTransform.position);
|
||||
}
|
||||
_direction = !_direction;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 10ae043c214aca94bbf2d32548004712
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -0,0 +1,149 @@
|
||||
using HPJ.Simulation.Enums;
|
||||
using HPJ.Simulation.Map;
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
|
||||
namespace HPJ.Presentation
|
||||
{
|
||||
/// <summary>
|
||||
/// The Map Sprite Renderer is a set of sprite renderers that render the tile state of a desired map.
|
||||
/// </summary>
|
||||
[RequireComponent(typeof(SpriteRenderer))]
|
||||
public class MapSpriteRenderer : MonoBehaviour
|
||||
{
|
||||
[SerializeField]
|
||||
protected float UpdatePeriod = 1; // How often the sprite updates
|
||||
protected float UpdateTimer = 0;
|
||||
[SerializeField]
|
||||
protected int DesiredMapIndex; // The desired map you want
|
||||
[SerializeField]
|
||||
protected List<TileColorData> ColorData = new List<TileColorData>(); // Colors you want to display for each tile type
|
||||
protected Dictionary<TileTypes, TileColorData> SortedData = new Dictionary<TileTypes, TileColorData>();
|
||||
protected SpriteRenderer SpriteRen; // The Base Sprite renderer for the tile colors
|
||||
[SerializeField]
|
||||
protected SpriteRenderer GridSpriteRen; // The grid sprite renderer
|
||||
|
||||
private void Awake()
|
||||
{
|
||||
FirstUpdate = true;
|
||||
SpriteRen = GetComponent<SpriteRenderer>();
|
||||
UpdateTimer = UpdatePeriod;
|
||||
|
||||
foreach(TileTypes TileType in System.Enum.GetValues(typeof(TileTypes)))
|
||||
{
|
||||
foreach(TileColorData Data in ColorData)
|
||||
{
|
||||
if (Data.TileType == TileType)
|
||||
{
|
||||
SortedData.Add(TileType, Data);
|
||||
continue;
|
||||
}
|
||||
}
|
||||
if (SortedData.ContainsKey(TileType))
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
SortedData.Add(TileType, new TileColorData(Color.clear, TileType));
|
||||
}
|
||||
}
|
||||
|
||||
private void Update()
|
||||
{
|
||||
if (NavigationManager.Instance == null)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
if (!NavigationManager.Instance)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
if (DesiredMapIndex < 0 || DesiredMapIndex >= NavigationManager.Instance.MapSets.Count)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
UpdateTimer += Time.deltaTime;
|
||||
if (UpdateTimer >= UpdatePeriod)
|
||||
{
|
||||
UpdateTimer = 0;
|
||||
UpdateSprite();
|
||||
}
|
||||
}
|
||||
|
||||
protected bool FirstUpdate = true;
|
||||
protected Texture2D tex;
|
||||
protected Color[] colors;
|
||||
/// <summary>
|
||||
/// Updates the sprite to match the current state of the desired map
|
||||
/// </summary>
|
||||
public void UpdateSprite()
|
||||
{
|
||||
MapSet Map = NavigationManager.Instance.MapSets[DesiredMapIndex];
|
||||
|
||||
if (FirstUpdate)
|
||||
{
|
||||
//Debug.Log("Set Position");
|
||||
tex = new Texture2D(Map.SetSettings.MapSettings.MapWidth, Map.SetSettings.MapSettings.MapHeight);
|
||||
colors = new Color[Map.SetSettings.MapSettings.MapWidth * Map.SetSettings.MapSettings.MapHeight];
|
||||
|
||||
transform.localScale = new Vector3((Map.SetSettings.MapSettings.MapHeight + 1) * Map.SetSettings.MapSettings.TileSize / 100f, (Map.SetSettings.MapSettings.MapHeight + 1) * Map.SetSettings.MapSettings.TileSize / 100f, 1);
|
||||
|
||||
Vector3 Position = new Vector3();
|
||||
Position.x = Map.SetSettings.MapSettings.MapWidth * Map.SetSettings.MapSettings.TileSize / 200f + Map.SetSettings.MapSettings.Offset.x / 100f;
|
||||
Position.y = Map.SetSettings.MapSettings.Offset.y / 100f + 0.005f;
|
||||
Position.z = Map.SetSettings.MapSettings.MapHeight * Map.SetSettings.MapSettings.TileSize / 200f + Map.SetSettings.MapSettings.Offset.z / 100f;
|
||||
|
||||
transform.position = Position;
|
||||
|
||||
GridSpriteRen.transform.SetParent(null);
|
||||
|
||||
GridSpriteRen.transform.localScale = new Vector3(Map.SetSettings.MapSettings.TileSize / 100f, Map.SetSettings.MapSettings.TileSize / 100f, 1);
|
||||
GridSpriteRen.size = new Vector2(Map.SetSettings.MapSettings.MapWidth * Map.SetSettings.MapSettings.TileSize / 100f, Map.SetSettings.MapSettings.MapHeight * Map.SetSettings.MapSettings.TileSize / 100f) * 100f / Map.SetSettings.MapSettings.TileSize;
|
||||
GridSpriteRen.transform.position += new Vector3(0, 0.01f, 0);
|
||||
|
||||
GridSpriteRen.transform.SetParent(transform);
|
||||
|
||||
FirstUpdate = false;
|
||||
}
|
||||
|
||||
|
||||
for(int x = 0; x < Map.SetSettings.MapSettings.MapWidth; x++)
|
||||
{
|
||||
for (int y = 0; y < Map.SetSettings.MapSettings.MapHeight; y++)
|
||||
{
|
||||
int index = y * Map.SetSettings.MapSettings.MapWidth + x;
|
||||
|
||||
colors[index] = SortedData[Map.GetTileType(x, y)].TileColor;
|
||||
}
|
||||
}
|
||||
|
||||
tex.SetPixels(colors);
|
||||
tex.filterMode = FilterMode.Point;
|
||||
tex.Apply();
|
||||
|
||||
Sprite sprite = Sprite.Create(tex, new Rect(0, 0, tex.width, tex.height), new Vector2(0.5f, 0.5f));
|
||||
SpriteRen.sprite = sprite;
|
||||
//Debug.Log("Update SpriteRenderer");
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// The Color data for a given tile type
|
||||
/// </summary>
|
||||
[System.Serializable]
|
||||
public struct TileColorData
|
||||
{
|
||||
public Color TileColor;
|
||||
public TileTypes TileType;
|
||||
|
||||
public TileColorData(Color color, TileTypes Type)
|
||||
{
|
||||
TileColor = color;
|
||||
TileType = Type;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 5c4d029a33c7005498d6fa43f0f85d9b
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -0,0 +1,38 @@
|
||||
using UnityEngine;
|
||||
|
||||
namespace HPJ.Presentation
|
||||
{
|
||||
public class MoveTowardsPoint : MonoBehaviour
|
||||
{
|
||||
public Transform target;
|
||||
public float speed = 5f;
|
||||
public float acceleration = 2f;
|
||||
|
||||
|
||||
private void Start()
|
||||
{
|
||||
transform.SetParent(null);
|
||||
}
|
||||
|
||||
private void Update()
|
||||
{
|
||||
// Get the direction to the target
|
||||
Vector3 direction = target.position - transform.position;
|
||||
|
||||
// Calculate the distance to the target
|
||||
float distance = direction.magnitude;
|
||||
// Move towards the target
|
||||
if (distance > 0.1f)
|
||||
{
|
||||
|
||||
// Calculate the current speed
|
||||
float currentSpeed = speed + acceleration * distance;
|
||||
|
||||
// Normalize the direction vector
|
||||
direction.Normalize();
|
||||
|
||||
transform.position += direction * currentSpeed * Time.deltaTime;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 497eadcd7a52d844fa7c95e8ebf69c6d
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -0,0 +1,168 @@
|
||||
using HPJ.Presentation.Agents;
|
||||
using HPJ.Simulation;
|
||||
using HPJ.Simulation.Enums;
|
||||
using HPJ.Simulation.Map;
|
||||
using HPJ.Simulation.Pathing;
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
|
||||
namespace HPJ.Presentation
|
||||
{
|
||||
/// <summary>
|
||||
/// The example scene to display random pathing
|
||||
/// </summary>
|
||||
public class RandomPathsExampleScene : MonoBehaviour
|
||||
{
|
||||
[SerializeField]
|
||||
private float _getRandomPointEvery = 0.5f;
|
||||
private float _timer = 0;
|
||||
[SerializeField]
|
||||
private int _randomTileDistanceVariance = 5;
|
||||
[SerializeField]
|
||||
private int _randomRange = 25;
|
||||
[SerializeField]
|
||||
private NavigationAgent _testAgent;
|
||||
[SerializeField]
|
||||
private LineRenderer _testLine;
|
||||
|
||||
private NavigationJob _testJob;
|
||||
|
||||
private List<Vector3> _linePoints = new List<Vector3>();
|
||||
public bool PaththingCompleted { get; set; }
|
||||
public IntVector3 PreviousRandomDestination { get; protected set; }
|
||||
public int RandomMode = 0;
|
||||
|
||||
private void Awake()
|
||||
{
|
||||
_testJob = new NavigationJob(OnCompleteCallback);
|
||||
Cursor.lockState = CursorLockMode.Confined;
|
||||
Cursor.visible = true;
|
||||
}
|
||||
|
||||
public void OnCompleteCallback()
|
||||
{
|
||||
_testJob.CurrentStep = PathfindingCalculationStep.Finalized;
|
||||
PaththingCompleted = true;
|
||||
}
|
||||
|
||||
// Update is called once per frame
|
||||
void Update()
|
||||
{
|
||||
if (_testJob.CurrentStep == PathfindingCalculationStep.Pathfinding)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
_timer += Time.deltaTime;
|
||||
if (_timer >= _getRandomPointEvery)
|
||||
{
|
||||
_timer = 0;
|
||||
|
||||
// Define the plane in world space
|
||||
Plane plane = new Plane(Vector3.up, Vector3.zero);
|
||||
|
||||
// Cast a ray from the camera to the mouse position
|
||||
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
|
||||
|
||||
// Calculate the distance from the ray origin to the intersection with the plane
|
||||
float distance;
|
||||
if (plane.Raycast(ray, out distance))
|
||||
{
|
||||
Vector3 hitPoint = ray.GetPoint(distance);
|
||||
|
||||
if (_testAgent.CurrentMap != null)
|
||||
{
|
||||
if (NavigationManager.Instance.ValidPoint(hitPoint, _testAgent.CurrentMap))
|
||||
{
|
||||
_testJob.Clear();
|
||||
IntVector3 RandomDestination;
|
||||
|
||||
if (RandomMode == 1)
|
||||
{
|
||||
RandomDestination = _testAgent.GetRandomDestination(_randomRange);
|
||||
}
|
||||
else if (RandomMode == 2)
|
||||
{
|
||||
RandomDestination = _testAgent.GetRandomDestinationAwayFromPoint(hitPoint, _randomTileDistanceVariance);
|
||||
}
|
||||
else
|
||||
{
|
||||
RandomDestination = _testAgent.GetRandomDestinationNearPoint(hitPoint, _randomTileDistanceVariance);
|
||||
}
|
||||
_testJob.SetDestinationInfo(NavigationManager.Instance.GetTileWorldPosition(_testAgent.transform.position), RandomDestination, _testAgent.TraversableTiles, _testAgent.PrefferedTiles, _testAgent.NavigationType, _testAgent.CurrentMap, true);
|
||||
NavigationManager.Instance.SetDestination(_testJob);
|
||||
PreviousRandomDestination = RandomDestination;
|
||||
}
|
||||
else if (NavigationManager.Instance.ValidPoint(hitPoint, out MapSet HitMap))
|
||||
{
|
||||
_testJob.Clear();
|
||||
IntVector3 RandomDestination;
|
||||
|
||||
if (RandomMode == 1)
|
||||
{
|
||||
RandomDestination = _testAgent.GetRandomDestination(_randomRange);
|
||||
}
|
||||
else if (RandomMode == 2)
|
||||
{
|
||||
RandomDestination = _testAgent.GetRandomDestinationAwayFromPoint(hitPoint, _randomTileDistanceVariance);
|
||||
}
|
||||
else
|
||||
{
|
||||
RandomDestination = _testAgent.GetRandomDestinationNearPoint(hitPoint, _randomTileDistanceVariance);
|
||||
}
|
||||
_testJob.SetDestinationInfo(NavigationManager.Instance.GetTileWorldPosition(_testAgent.transform.position), RandomDestination, _testAgent.TraversableTiles, _testAgent.PrefferedTiles, _testAgent.NavigationType, _testAgent.CurrentMap, true);
|
||||
NavigationManager.Instance.SetDestination(_testJob);
|
||||
PreviousRandomDestination = RandomDestination;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
else if (PaththingCompleted)
|
||||
{
|
||||
PaththingCompleted = false;
|
||||
UpdateLine();
|
||||
}
|
||||
|
||||
if (Input.GetMouseButtonDown(1))
|
||||
{
|
||||
_testAgent.SetDestination(PreviousRandomDestination);
|
||||
}
|
||||
|
||||
if (Input.GetKeyDown(KeyCode.Alpha1))
|
||||
{
|
||||
RandomMode = 0;
|
||||
}
|
||||
else if (Input.GetKeyDown(KeyCode.Alpha2))
|
||||
{
|
||||
RandomMode = 1;
|
||||
}
|
||||
else if (Input.GetKeyDown(KeyCode.Alpha3))
|
||||
{
|
||||
RandomMode = 2;
|
||||
}
|
||||
}
|
||||
|
||||
private void UpdateLine()
|
||||
{
|
||||
//Debug.Log("Update Line");
|
||||
_linePoints.Clear();
|
||||
|
||||
if (_testJob.CurrentPath.Path.Count <= 0)
|
||||
{
|
||||
_testLine.positionCount = 0;
|
||||
return;
|
||||
}
|
||||
|
||||
for (int i = 0; i < _testJob.CurrentPath.Path.Count; i++)
|
||||
{
|
||||
Vector3 NextPoint = NavigationManager.Instance.GetTileCenterWorldPosition(_testJob.CurrentPath.Path[i], _testAgent.CurrentMap);
|
||||
NextPoint.y += 0.01f;
|
||||
_linePoints.Add(NextPoint);
|
||||
}
|
||||
|
||||
_testLine.positionCount = _testJob.CurrentPath.Path.Count;
|
||||
_testLine.SetPositions(_linePoints.ToArray());
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: ac9cbf46f291f16429ede3c990e2d99e
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -0,0 +1,156 @@
|
||||
using HPJ.Presentation.Agents;
|
||||
using HPJ.Simulation;
|
||||
using HPJ.Simulation.Map;
|
||||
using System.Collections;
|
||||
using UnityEngine;
|
||||
|
||||
namespace HPJ.Presentation.Sample
|
||||
{
|
||||
/// <summary>
|
||||
/// The all round example scene script
|
||||
/// </summary>
|
||||
public class SampleSceneScript : MonoBehaviour
|
||||
{
|
||||
[SerializeField]
|
||||
private int _agentSpawnCount = 100;
|
||||
[SerializeField]
|
||||
private NavigationAgent TestAgents;
|
||||
[SerializeField]
|
||||
private bool _rollOutSlow = false;
|
||||
[SerializeField]
|
||||
private bool _closeRangeRandomPosition = false;
|
||||
[SerializeField]
|
||||
private int _closeRangeRadius = 50;
|
||||
[SerializeField]
|
||||
private bool _keepAgentsOnSameMap = false;
|
||||
|
||||
private void Start()
|
||||
{
|
||||
if (_rollOutSlow)
|
||||
{
|
||||
StartCoroutine(RollOutSlow());
|
||||
}
|
||||
else
|
||||
{
|
||||
StartCoroutine(RollOut());
|
||||
}
|
||||
}
|
||||
|
||||
private IEnumerator RollOutSlow()
|
||||
{
|
||||
while (NavigationManager.Instance == null)
|
||||
{
|
||||
yield return null;
|
||||
}
|
||||
|
||||
while (!NavigationManager.Instance.Initialized)
|
||||
{
|
||||
yield return null;
|
||||
}
|
||||
|
||||
yield return null;
|
||||
|
||||
for (int i = 0; i < _agentSpawnCount; i++)
|
||||
{
|
||||
Vector3 RandomPosition = NavigationManager.Instance.GetRandomValidPosition(TestAgents.TraversableTiles, true);
|
||||
NavigationAgent Agent = Instantiate(TestAgents, RandomPosition, Quaternion.identity);
|
||||
Agent.transform.SetParent(transform);
|
||||
Agent.OnMovingComplete.AddListener(SetRandomDestination);
|
||||
Agent?.GroundCast?.TeleportUp();
|
||||
Vector3 RandomDestination;
|
||||
if (_closeRangeRandomPosition)
|
||||
{
|
||||
IntVector3 RandomPosition_WorldIndex = NavigationManager.Instance.GetTileWorldPosition(RandomPosition);
|
||||
MapSet map = NavigationManager.Instance.GetCurrentMap(RandomPosition);
|
||||
RandomDestination = NavigationManager.Instance.GetRandomValidPositionAtPosition(Agent.TraversableTiles, NavigationManager.Instance.GetTileIndexForMap(RandomPosition_WorldIndex, map), _closeRangeRadius, true);
|
||||
}
|
||||
else
|
||||
{
|
||||
if (_keepAgentsOnSameMap)
|
||||
{
|
||||
MapSet map = NavigationManager.Instance.GetCurrentMap(RandomPosition);
|
||||
Agent.SetCurrentMap(map);
|
||||
RandomDestination = NavigationManager.Instance.GetRandomValidPosition(Agent.TraversableTiles, map, true);
|
||||
}
|
||||
else
|
||||
{
|
||||
RandomDestination = NavigationManager.Instance.GetRandomValidPosition(Agent.TraversableTiles, true);
|
||||
}
|
||||
}
|
||||
Agent.SetDestination(RandomDestination);
|
||||
yield return null;
|
||||
yield return null;
|
||||
}
|
||||
enabled = false;
|
||||
}
|
||||
|
||||
private IEnumerator RollOut()
|
||||
{
|
||||
while (NavigationManager.Instance == null)
|
||||
{
|
||||
yield return null;
|
||||
}
|
||||
|
||||
while (!NavigationManager.Instance.Initialized)
|
||||
{
|
||||
yield return null;
|
||||
}
|
||||
|
||||
yield return null;
|
||||
|
||||
for (int i = 0; i < _agentSpawnCount; i++)
|
||||
{
|
||||
Vector3 RandomPosition = NavigationManager.Instance.GetRandomValidPosition(TestAgents.TraversableTiles, true);
|
||||
NavigationAgent Agent = Instantiate(TestAgents, RandomPosition, Quaternion.identity);
|
||||
Agent.OnMovingComplete.AddListener(SetRandomDestination);
|
||||
Agent?.GroundCast?.TeleportUp();
|
||||
Vector3 RandomDestination;
|
||||
if (_closeRangeRandomPosition)
|
||||
{
|
||||
IntVector3 RandomPosition_WorldIndex = NavigationManager.Instance.GetTileWorldPosition(RandomPosition);
|
||||
MapSet map = NavigationManager.Instance.GetCurrentMap(RandomPosition);
|
||||
RandomDestination = NavigationManager.Instance.GetRandomValidPositionAtPosition(Agent.TraversableTiles, NavigationManager.Instance.GetTileIndexForMap(RandomPosition_WorldIndex, map), _closeRangeRadius, true);
|
||||
}
|
||||
else
|
||||
{
|
||||
if (_keepAgentsOnSameMap)
|
||||
{
|
||||
MapSet map = NavigationManager.Instance.GetCurrentMap(RandomPosition);
|
||||
Agent.SetCurrentMap(map);
|
||||
RandomDestination = NavigationManager.Instance.GetRandomValidPosition(Agent.TraversableTiles, map, true);
|
||||
}
|
||||
else
|
||||
{
|
||||
RandomDestination = NavigationManager.Instance.GetRandomValidPosition(Agent.TraversableTiles, true);
|
||||
}
|
||||
}
|
||||
Agent.SetDestination(RandomDestination);
|
||||
}
|
||||
enabled = false;
|
||||
}
|
||||
|
||||
private void SetRandomDestination(NavigationAgent Agent)
|
||||
{
|
||||
Vector3 RandomDestination;
|
||||
if (_closeRangeRandomPosition)
|
||||
{
|
||||
IntVector3 RandomPosition_WorldIndex = NavigationManager.Instance.GetTileWorldPosition(transform.position);
|
||||
MapSet map = NavigationManager.Instance.GetCurrentMap(transform.position);
|
||||
RandomDestination = NavigationManager.Instance.GetRandomValidPositionAtPosition(Agent.TraversableTiles, NavigationManager.Instance.GetTileIndexForMap(RandomPosition_WorldIndex, map), _closeRangeRadius, true);
|
||||
}
|
||||
else
|
||||
{
|
||||
if (_keepAgentsOnSameMap)
|
||||
{
|
||||
RandomDestination = NavigationManager.Instance.GetRandomValidPosition(Agent.TraversableTiles, Agent.StartingMap, true);
|
||||
//Debug.Log($"Random Destination {RandomDestination}");
|
||||
}
|
||||
else
|
||||
{
|
||||
RandomDestination = NavigationManager.Instance.GetRandomValidPosition(Agent.TraversableTiles, true);
|
||||
}
|
||||
}
|
||||
Agent.SetDestination(RandomDestination);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 726955db545851e45b09d589d5fbe540
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -0,0 +1,131 @@
|
||||
using HPJ.Presentation.Agents;
|
||||
using HPJ.Simulation;
|
||||
using HPJ.Simulation.Map;
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
|
||||
namespace HPJ.Presentation
|
||||
{
|
||||
/// <summary>
|
||||
/// This sample script is to spawn and control multiple units to see how swarming runs
|
||||
/// </summary>
|
||||
public class SwarmSample : MonoBehaviour
|
||||
{
|
||||
[SerializeField]
|
||||
private int _testSwarmAmount = 50;
|
||||
[SerializeField]
|
||||
private SimNavigationAgent _testAgentPrefab;
|
||||
[SerializeField]
|
||||
private LayerMask _targetLayer;
|
||||
|
||||
public List<SimNavigationAgent> Agents { get; protected set; }
|
||||
|
||||
private void Awake()
|
||||
{
|
||||
Cursor.lockState = CursorLockMode.Confined;
|
||||
Cursor.visible = false;
|
||||
Agents = new List<SimNavigationAgent>();
|
||||
}
|
||||
|
||||
private void Start()
|
||||
{
|
||||
StartCoroutine(RollOut());
|
||||
}
|
||||
|
||||
private IEnumerator RollOut()
|
||||
{
|
||||
while (NavigationManager.Instance == null)
|
||||
{
|
||||
yield return null;
|
||||
}
|
||||
|
||||
while (!NavigationManager.Instance.Initialized)
|
||||
{
|
||||
yield return null;
|
||||
}
|
||||
|
||||
yield return null;
|
||||
|
||||
for (int i = 0; i < _testSwarmAmount; i++)
|
||||
{
|
||||
Vector3 RandomPosition = NavigationManager.Instance.GetRandomValidPosition(_testAgentPrefab.TraversableTiles, true);
|
||||
SimNavigationAgent Agent = Instantiate(_testAgentPrefab, RandomPosition, Quaternion.identity);
|
||||
Agent.transform.SetParent(transform);
|
||||
Agent?.GroundCast?.TeleportUp();
|
||||
Agents.Add(Agent);
|
||||
|
||||
MapSet map = NavigationManager.Instance.GetCurrentMap(RandomPosition);
|
||||
Agent.SetCurrentMap(map);
|
||||
|
||||
yield return null;
|
||||
}
|
||||
}
|
||||
|
||||
// Update is called once per frame
|
||||
void Update()
|
||||
{
|
||||
// Calculate the distance from the ray origin to the intersection with the plane
|
||||
if (Input.GetMouseButtonDown(1))
|
||||
{
|
||||
RaycastHit hit;
|
||||
if (Physics.Raycast(Camera.main.transform.position, Camera.main.transform.forward, out hit, 300f, _targetLayer))
|
||||
{
|
||||
Vector3 hitPoint = hit.point;
|
||||
Debug.Log($"Hit Point {hitPoint}");
|
||||
for (int i = 0; i < Agents.Count; i++)
|
||||
{
|
||||
if (Agents[i].CurrentMap != null)
|
||||
{
|
||||
if (NavigationManager.Instance.ValidPoint(hitPoint, Agents[i].CurrentMap))
|
||||
{
|
||||
IntVector3 TargetDestination = NavigationManager.Instance.GetTileWorldPosition(hitPoint);
|
||||
|
||||
Agents[i].Idle();
|
||||
Agents[i].SetDestination(TargetDestination, true, true);
|
||||
}
|
||||
else if (NavigationManager.Instance.ValidPoint(hitPoint, out MapSet HitMap))
|
||||
{
|
||||
Agents[i].SetCurrentMap(HitMap);
|
||||
IntVector3 TargetDestination = NavigationManager.Instance.GetTileWorldPosition(hitPoint);
|
||||
|
||||
Agents[i].Idle();
|
||||
Agents[i].SetDestination(TargetDestination, true, true);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
Agents[i].SetCurrentMap(NavigationManager.Instance.GetMapAtWorldPosition(Agents[i].transform.position));
|
||||
if (NavigationManager.Instance.ValidPoint(hitPoint, Agents[i].CurrentMap))
|
||||
{
|
||||
IntVector3 TargetDestination = NavigationManager.Instance.GetTileWorldPosition(hitPoint);
|
||||
|
||||
Agents[i].Idle();
|
||||
Agents[i].SetDestination(TargetDestination, true, true);
|
||||
}
|
||||
else if (NavigationManager.Instance.ValidPoint(hitPoint, out MapSet HitMap))
|
||||
{
|
||||
Agents[i].SetCurrentMap(HitMap);
|
||||
IntVector3 TargetDestination = NavigationManager.Instance.GetTileWorldPosition(hitPoint);
|
||||
|
||||
Agents[i].Idle();
|
||||
Agents[i].SetDestination(TargetDestination, true, true);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
Debug.Log("Did not hit on Raycast");
|
||||
}
|
||||
}
|
||||
else if (Input.GetKeyDown(KeyCode.Escape))
|
||||
{
|
||||
for (int i = 0; i < Agents.Count; i++)
|
||||
{
|
||||
Agents[i].Idle();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: f64ce63d04a12b344b9487da433e35f3
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -0,0 +1,143 @@
|
||||
using HPJ.Presentation.Agents;
|
||||
using HPJ.Simulation;
|
||||
using HPJ.Simulation.Enums;
|
||||
using HPJ.Simulation.Map;
|
||||
using HPJ.Simulation.Pathing;
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
|
||||
namespace HPJ.Presentation.Sample
|
||||
{
|
||||
public class TargetAgentMoveScript : MonoBehaviour
|
||||
{
|
||||
[SerializeField]
|
||||
private NavigationAgent _testAgent;
|
||||
[SerializeField]
|
||||
private LayerMask _targetLayer;
|
||||
|
||||
private NavigationJob _testJob;
|
||||
|
||||
private List<Vector3> _linePoints = new List<Vector3>();
|
||||
public bool PaththingCompleted { get; set; }
|
||||
public IntVector3 PreviousDestination { get; protected set; }
|
||||
|
||||
private void Awake()
|
||||
{
|
||||
_testJob = new NavigationJob(OnCompleteCallback);
|
||||
Cursor.lockState = CursorLockMode.Confined;
|
||||
Cursor.visible = true;
|
||||
}
|
||||
|
||||
private void Start()
|
||||
{
|
||||
_testAgent.SetCurrentMap(NavigationManager.Instance.GetMapAtWorldPosition(_testAgent.transform.position));
|
||||
}
|
||||
|
||||
public void OnCompleteCallback()
|
||||
{
|
||||
_testJob.CurrentStep = PathfindingCalculationStep.Finalized;
|
||||
PaththingCompleted = true;
|
||||
}
|
||||
|
||||
// Update is called once per frame
|
||||
void Update()
|
||||
{
|
||||
if (_testJob.CurrentStep == PathfindingCalculationStep.Pathfinding)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
if (PaththingCompleted)
|
||||
{
|
||||
PaththingCompleted = false;
|
||||
}
|
||||
|
||||
// Calculate the distance from the ray origin to the intersection with the plane
|
||||
if (Input.GetMouseButtonDown(1))
|
||||
{
|
||||
RaycastHit hit;
|
||||
if (Physics.Raycast(Camera.main.transform.position, Camera.main.transform.forward, out hit, Mathf.Infinity, _targetLayer))
|
||||
{
|
||||
Vector3 hitPoint = hit.point;
|
||||
|
||||
if (_testAgent.CurrentMap != null)
|
||||
{
|
||||
if (NavigationManager.Instance.ValidPoint(hitPoint, _testAgent.CurrentMap))
|
||||
{
|
||||
_testJob.Clear();
|
||||
IntVector3 RandomDestination = NavigationManager.Instance.GetTileWorldPosition(hitPoint);
|
||||
|
||||
_testJob.SetDestinationInfo(NavigationManager.Instance.GetTileWorldPosition(_testAgent.transform.position), RandomDestination, _testAgent.TraversableTiles, _testAgent.PrefferedTiles, _testAgent.NavigationType, _testAgent.CurrentMap, true);
|
||||
NavigationManager.Instance.SetDestination(_testJob);
|
||||
PreviousDestination = RandomDestination;
|
||||
|
||||
if (PreviousDestination != new IntVector3())
|
||||
{
|
||||
_testAgent.Idle();
|
||||
_testAgent.SetDestination(PreviousDestination);
|
||||
}
|
||||
}
|
||||
else if (NavigationManager.Instance.ValidPoint(hitPoint, out MapSet HitMap))
|
||||
{
|
||||
_testJob.Clear();
|
||||
IntVector3 RandomDestination = NavigationManager.Instance.GetTileWorldPosition(hitPoint);
|
||||
|
||||
_testJob.SetDestinationInfo(NavigationManager.Instance.GetTileWorldPosition(_testAgent.transform.position), RandomDestination, _testAgent.TraversableTiles, _testAgent.PrefferedTiles, _testAgent.NavigationType, _testAgent.CurrentMap, true);
|
||||
NavigationManager.Instance.SetDestination(_testJob);
|
||||
PreviousDestination = RandomDestination;
|
||||
|
||||
if (PreviousDestination != new IntVector3())
|
||||
{
|
||||
_testAgent.Idle();
|
||||
_testAgent.SetDestination(PreviousDestination);
|
||||
}
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
_testAgent.SetCurrentMap(NavigationManager.Instance.GetMapAtWorldPosition(_testAgent.transform.position));
|
||||
if (NavigationManager.Instance.ValidPoint(hitPoint, _testAgent.CurrentMap))
|
||||
{
|
||||
_testJob.Clear();
|
||||
IntVector3 RandomDestination = NavigationManager.Instance.GetTileWorldPosition(hitPoint);
|
||||
|
||||
_testJob.SetDestinationInfo(NavigationManager.Instance.GetTileWorldPosition(_testAgent.transform.position), RandomDestination, _testAgent.TraversableTiles, _testAgent.PrefferedTiles, _testAgent.NavigationType, _testAgent.CurrentMap, true);
|
||||
NavigationManager.Instance.SetDestination(_testJob);
|
||||
PreviousDestination = RandomDestination;
|
||||
|
||||
if (PreviousDestination != new IntVector3())
|
||||
{
|
||||
_testAgent.Idle();
|
||||
_testAgent.SetDestination(PreviousDestination);
|
||||
}
|
||||
}
|
||||
else if (NavigationManager.Instance.ValidPoint(hitPoint, out MapSet HitMap))
|
||||
{
|
||||
_testJob.Clear();
|
||||
IntVector3 RandomDestination = NavigationManager.Instance.GetTileWorldPosition(hitPoint);
|
||||
|
||||
_testJob.SetDestinationInfo(NavigationManager.Instance.GetTileWorldPosition(_testAgent.transform.position), RandomDestination, _testAgent.TraversableTiles, _testAgent.PrefferedTiles, _testAgent.NavigationType, _testAgent.CurrentMap, true);
|
||||
NavigationManager.Instance.SetDestination(_testJob);
|
||||
PreviousDestination = RandomDestination;
|
||||
|
||||
if (PreviousDestination != new IntVector3())
|
||||
{
|
||||
_testAgent.Idle();
|
||||
_testAgent.SetDestination(PreviousDestination);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
Debug.Log("Did not hit on Raycast");
|
||||
}
|
||||
|
||||
}
|
||||
else if (Input.GetKeyDown(KeyCode.Escape))
|
||||
{
|
||||
_testAgent.Idle();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: a2417001634632a448e070301ffb1e9c
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
Reference in New Issue
Block a user