cleaned up

This commit is contained in:
Janis M
2022-03-09 13:38:39 +01:00
parent 675481d8ad
commit 598cce9611
5 changed files with 107 additions and 58 deletions

View File

@@ -0,0 +1,53 @@
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PathfindingSystem : MonoBehaviour
{
public static PathfindingSystem instance { get; private set; }
public Pathfinding pathfinding;
int originX = 0;
int originY = 0;
void Start()
{
instance = this;
pathfinding = new Pathfinding(20, 10);
}
void Update()
{/*
if (Input.GetMouseButtonDown(0))
{
Vector3 mouseWorldPosition = Camera.main.ScreenToWorldPoint(Input.mousePosition);
pathfinding.GetGrid().GetXY(mouseWorldPosition, out int x, out int y);
List<PathNode> path = pathfinding.FindPath(originX, originY, x, y);
if (path != null)
{
float cellSize = pathfinding.GetGrid().GetCellSize();
for (int i = 0; i < path.Count - 1; i++)
{
Debug.DrawLine(new Vector3(path[i].x, path[i].y) * cellSize + Vector3.one * cellSize / 2, new Vector3(path[i + 1].x, path[i + 1].y) * cellSize + Vector3.one * cellSize / 2, Color.green, 5f);
}
}
//characterPathfinding.SetTargetPosition(mouseWorldPosition);
}
if (Input.GetMouseButtonDown(1))
{
Vector3 mouseWorldPosition = Camera.main.ScreenToWorldPoint(Input.mousePosition);
pathfinding.GetGrid().GetXY(mouseWorldPosition, out int x, out int y);
pathfinding.GetNode(x, y).SetIsWalkable(!pathfinding.GetNode(x, y).isWalkable);
originX = x;
originY = y;
}
if (Input.GetMouseButtonDown(2))
{
Vector3 mouseWorldPosition = Camera.main.ScreenToWorldPoint(Input.mousePosition);
pathfinding.GetGrid().GetXY(mouseWorldPosition, out int x, out int y);
pathfinding.GetNode(x, y).SetIsWalkable(!pathfinding.GetNode(x, y).isWalkable);
}*/
}
}

View File

@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: db134add0ff65f04aa54ed5edf6d6df2
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -383,7 +383,7 @@ PrefabInstance:
objectReference: {fileID: 0}
- target: {fileID: 3391280458837659708, guid: 4b478a82c8aed4e4e9f09083bf6afa01, type: 3}
propertyPath: m_IsActive
value: 0
value: 1
objectReference: {fileID: 0}
- target: {fileID: 3391280458837659710, guid: 4b478a82c8aed4e4e9f09083bf6afa01, type: 3}
propertyPath: m_RootOrder
@@ -391,11 +391,11 @@ PrefabInstance:
objectReference: {fileID: 0}
- target: {fileID: 3391280458837659710, guid: 4b478a82c8aed4e4e9f09083bf6afa01, type: 3}
propertyPath: m_LocalPosition.x
value: 6.52
value: 0
objectReference: {fileID: 0}
- target: {fileID: 3391280458837659710, guid: 4b478a82c8aed4e4e9f09083bf6afa01, type: 3}
propertyPath: m_LocalPosition.y
value: 6.484
value: 0
objectReference: {fileID: 0}
- target: {fileID: 3391280458837659710, guid: 4b478a82c8aed4e4e9f09083bf6afa01, type: 3}
propertyPath: m_LocalPosition.z
@@ -727,6 +727,7 @@ GameObject:
m_Component:
- component: {fileID: 1522634446}
- component: {fileID: 1522634445}
- component: {fileID: 1522634447}
m_Layer: 0
m_Name: Grid
m_TagString: Untagged
@@ -763,6 +764,18 @@ Transform:
m_Father: {fileID: 0}
m_RootOrder: 3
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
--- !u!114 &1522634447
MonoBehaviour:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 1522634444}
m_Enabled: 1
m_EditorHideFlags: 0
m_Script: {fileID: 11500000, guid: db134add0ff65f04aa54ed5edf6d6df2, type: 3}
m_Name:
m_EditorClassIdentifier:
--- !u!114 &1538992898 stripped
MonoBehaviour:
m_CorrespondingSourceObject: {fileID: 3180108334736411172, guid: a0f5cce80f851794aa28b176e811a46e, type: 3}

View File

@@ -4,43 +4,6 @@ using UnityEngine;
public class Testing : MonoBehaviour {
private Pathfinding pathfinding;
int originX = 0;
int originY = 0;
private void Start() {
pathfinding = new Pathfinding(20, 10);
}
private void Update() {
if (Input.GetMouseButtonDown(0)) {
Vector3 mouseWorldPosition = Camera.main.ScreenToWorldPoint(Input.mousePosition);
pathfinding.GetGrid().GetXY(mouseWorldPosition, out int x, out int y);
List<PathNode> path = pathfinding.FindPath(originX, originY, x, y);
if (path != null) {
float cellSize = pathfinding.GetGrid().GetCellSize();
for (int i=0; i<path.Count - 1; i++) {
Debug.DrawLine(new Vector3(path[i].x, path[i].y) * cellSize + Vector3.one * cellSize/2, new Vector3(path[i+1].x, path[i+1].y) * cellSize + Vector3.one * cellSize/2, Color.green, 5f);
}
}
//characterPathfinding.SetTargetPosition(mouseWorldPosition);
}
if (Input.GetMouseButtonDown(1)) {
Vector3 mouseWorldPosition = Camera.main.ScreenToWorldPoint(Input.mousePosition);
pathfinding.GetGrid().GetXY(mouseWorldPosition, out int x, out int y);
pathfinding.GetNode(x, y).SetIsWalkable(!pathfinding.GetNode(x, y).isWalkable);
originX = x;
originY = y;
}
if (Input.GetMouseButtonDown(2))
{
Vector3 mouseWorldPosition = Camera.main.ScreenToWorldPoint(Input.mousePosition);
pathfinding.GetGrid().GetXY(mouseWorldPosition, out int x, out int y);
pathfinding.GetNode(x, y).SetIsWalkable(!pathfinding.GetNode(x, y).isWalkable);
}
}
}

View File

@@ -1,52 +1,61 @@
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.AI;
public class PersonMovement : MonoBehaviour
{
private int currentPathIndex;
[SerializeField] private List<Vector3> pathVectorList;
[SerializeField] private List<Vector3> pathVectorList = new List<Vector3>();
private const float speed = 40f;
private void Awake()
{
//agent.avoidancePriority = Random.Range(1, 100);
}
private void Update()
{
HandleMovement();
if (Input.GetMouseButton(0))
if (Input.GetMouseButtonDown(0))
{
SetTarget(Camera.main.ScreenToWorldPoint(Input.mousePosition));
Pathfinding pathfinding = PathfindingSystem.instance.pathfinding;
Vector3 mouseWorldPosition = Camera.main.ScreenToWorldPoint(Input.mousePosition);
Vector3 personPosition = GetPosition();
pathfinding.GetGrid().GetXY(mouseWorldPosition, out int endX, out int endY);
pathfinding.GetGrid().GetXY(personPosition, out int startX, out int startY);
List<PathNode> path = pathfinding.FindPath(startX, startY, endX, endY);
if (path != null)
{
float cellSize = pathfinding.GetGrid().GetCellSize();
for (int i = 0; i < path.Count - 1; i++)
{
Debug.DrawLine(new Vector3(path[i].x, path[i].y) * cellSize + Vector3.one * cellSize / 2, new Vector3(path[i + 1].x, path[i + 1].y) * cellSize + Vector3.one * cellSize / 2, Color.green, 5f);
}
}
SetTarget(mouseWorldPosition);
}
}
void HandleMovement()
void HandleMovementList()
{
if (pathVectorList != null)
if(pathVectorList.Count > 0)
{
Vector3 targetPosition = pathVectorList[currentPathIndex];
if (Vector3.Distance(transform.position, targetPosition) > 1f)
if(GetPosition() == pathVectorList[0])
{
Vector3 moveDir = (targetPosition - transform.position).normalized;
float distanceBefore = Vector3.Distance(transform.position, targetPosition);
transform.position = transform.position + moveDir * speed * Time.deltaTime;
}
else
{
currentPathIndex++;
}
}
}
public Vector3 GetPosition()
{
return transform.position;
}
public void SetTarget(Vector3 targetTransform)
{
{
pathVectorList = Pathfinding.Instance.FindPath(GetPosition(), targetTransform);
Debug.Log(pathVectorList);
}
}