mirror of
https://github.com/DerTyp7/fps-citybuild-unity.git
synced 2025-10-29 12:22:07 +01:00
Merge branch 'NPC' into main
This commit is contained in:
346
Assets/Scenes/NPCTest.unity
Normal file
346
Assets/Scenes/NPCTest.unity
Normal file
@@ -0,0 +1,346 @@
|
||||
%YAML 1.1
|
||||
%TAG !u! tag:unity3d.com,2011:
|
||||
--- !u!29 &1
|
||||
OcclusionCullingSettings:
|
||||
m_ObjectHideFlags: 0
|
||||
serializedVersion: 2
|
||||
m_OcclusionBakeSettings:
|
||||
smallestOccluder: 5
|
||||
smallestHole: 0.25
|
||||
backfaceThreshold: 100
|
||||
m_SceneGUID: 00000000000000000000000000000000
|
||||
m_OcclusionCullingData: {fileID: 0}
|
||||
--- !u!104 &2
|
||||
RenderSettings:
|
||||
m_ObjectHideFlags: 0
|
||||
serializedVersion: 9
|
||||
m_Fog: 0
|
||||
m_FogColor: {r: 0.5, g: 0.5, b: 0.5, a: 1}
|
||||
m_FogMode: 3
|
||||
m_FogDensity: 0.01
|
||||
m_LinearFogStart: 0
|
||||
m_LinearFogEnd: 300
|
||||
m_AmbientSkyColor: {r: 0.212, g: 0.227, b: 0.259, a: 1}
|
||||
m_AmbientEquatorColor: {r: 0.114, g: 0.125, b: 0.133, a: 1}
|
||||
m_AmbientGroundColor: {r: 0.047, g: 0.043, b: 0.035, a: 1}
|
||||
m_AmbientIntensity: 1
|
||||
m_AmbientMode: 0
|
||||
m_SubtractiveShadowColor: {r: 0.42, g: 0.478, b: 0.627, a: 1}
|
||||
m_SkyboxMaterial: {fileID: 10304, guid: 0000000000000000f000000000000000, type: 0}
|
||||
m_HaloStrength: 0.5
|
||||
m_FlareStrength: 1
|
||||
m_FlareFadeSpeed: 3
|
||||
m_HaloTexture: {fileID: 0}
|
||||
m_SpotCookie: {fileID: 10001, guid: 0000000000000000e000000000000000, type: 0}
|
||||
m_DefaultReflectionMode: 0
|
||||
m_DefaultReflectionResolution: 128
|
||||
m_ReflectionBounces: 1
|
||||
m_ReflectionIntensity: 1
|
||||
m_CustomReflection: {fileID: 0}
|
||||
m_Sun: {fileID: 0}
|
||||
m_IndirectSpecularColor: {r: 0.44657826, g: 0.49641263, b: 0.57481676, a: 1}
|
||||
m_UseRadianceAmbientProbe: 0
|
||||
--- !u!157 &3
|
||||
LightmapSettings:
|
||||
m_ObjectHideFlags: 0
|
||||
serializedVersion: 12
|
||||
m_GIWorkflowMode: 1
|
||||
m_GISettings:
|
||||
serializedVersion: 2
|
||||
m_BounceScale: 1
|
||||
m_IndirectOutputScale: 1
|
||||
m_AlbedoBoost: 1
|
||||
m_EnvironmentLightingMode: 0
|
||||
m_EnableBakedLightmaps: 1
|
||||
m_EnableRealtimeLightmaps: 0
|
||||
m_LightmapEditorSettings:
|
||||
serializedVersion: 12
|
||||
m_Resolution: 2
|
||||
m_BakeResolution: 40
|
||||
m_AtlasSize: 1024
|
||||
m_AO: 0
|
||||
m_AOMaxDistance: 1
|
||||
m_CompAOExponent: 1
|
||||
m_CompAOExponentDirect: 0
|
||||
m_ExtractAmbientOcclusion: 0
|
||||
m_Padding: 2
|
||||
m_LightmapParameters: {fileID: 0}
|
||||
m_LightmapsBakeMode: 1
|
||||
m_TextureCompression: 1
|
||||
m_FinalGather: 0
|
||||
m_FinalGatherFiltering: 1
|
||||
m_FinalGatherRayCount: 256
|
||||
m_ReflectionCompression: 2
|
||||
m_MixedBakeMode: 2
|
||||
m_BakeBackend: 1
|
||||
m_PVRSampling: 1
|
||||
m_PVRDirectSampleCount: 32
|
||||
m_PVRSampleCount: 512
|
||||
m_PVRBounces: 2
|
||||
m_PVREnvironmentSampleCount: 256
|
||||
m_PVREnvironmentReferencePointCount: 2048
|
||||
m_PVRFilteringMode: 1
|
||||
m_PVRDenoiserTypeDirect: 1
|
||||
m_PVRDenoiserTypeIndirect: 1
|
||||
m_PVRDenoiserTypeAO: 1
|
||||
m_PVRFilterTypeDirect: 0
|
||||
m_PVRFilterTypeIndirect: 0
|
||||
m_PVRFilterTypeAO: 0
|
||||
m_PVREnvironmentMIS: 1
|
||||
m_PVRCulling: 1
|
||||
m_PVRFilteringGaussRadiusDirect: 1
|
||||
m_PVRFilteringGaussRadiusIndirect: 5
|
||||
m_PVRFilteringGaussRadiusAO: 2
|
||||
m_PVRFilteringAtrousPositionSigmaDirect: 0.5
|
||||
m_PVRFilteringAtrousPositionSigmaIndirect: 2
|
||||
m_PVRFilteringAtrousPositionSigmaAO: 1
|
||||
m_ExportTrainingData: 0
|
||||
m_TrainingDataDestination: TrainingData
|
||||
m_LightProbeSampleCountMultiplier: 4
|
||||
m_LightingDataAsset: {fileID: 0}
|
||||
m_LightingSettings: {fileID: 0}
|
||||
--- !u!196 &4
|
||||
NavMeshSettings:
|
||||
serializedVersion: 2
|
||||
m_ObjectHideFlags: 0
|
||||
m_BuildSettings:
|
||||
serializedVersion: 2
|
||||
agentTypeID: 0
|
||||
agentRadius: 0.5
|
||||
agentHeight: 2
|
||||
agentSlope: 45
|
||||
agentClimb: 0.4
|
||||
ledgeDropHeight: 0
|
||||
maxJumpAcrossDistance: 0
|
||||
minRegionArea: 2
|
||||
manualCellSize: 0
|
||||
cellSize: 0.16666667
|
||||
manualTileSize: 0
|
||||
tileSize: 256
|
||||
accuratePlacement: 0
|
||||
maxJobWorkers: 0
|
||||
preserveTilesOutsideBounds: 0
|
||||
debug:
|
||||
m_Flags: 0
|
||||
m_NavMeshData: {fileID: 0}
|
||||
--- !u!1 &329646042
|
||||
GameObject:
|
||||
m_ObjectHideFlags: 0
|
||||
m_CorrespondingSourceObject: {fileID: 0}
|
||||
m_PrefabInstance: {fileID: 0}
|
||||
m_PrefabAsset: {fileID: 0}
|
||||
serializedVersion: 6
|
||||
m_Component:
|
||||
- component: {fileID: 329646044}
|
||||
- component: {fileID: 329646043}
|
||||
m_Layer: 0
|
||||
m_Name: Directional Light
|
||||
m_TagString: Untagged
|
||||
m_Icon: {fileID: 0}
|
||||
m_NavMeshLayer: 0
|
||||
m_StaticEditorFlags: 0
|
||||
m_IsActive: 1
|
||||
--- !u!108 &329646043
|
||||
Light:
|
||||
m_ObjectHideFlags: 0
|
||||
m_CorrespondingSourceObject: {fileID: 0}
|
||||
m_PrefabInstance: {fileID: 0}
|
||||
m_PrefabAsset: {fileID: 0}
|
||||
m_GameObject: {fileID: 329646042}
|
||||
m_Enabled: 1
|
||||
serializedVersion: 10
|
||||
m_Type: 1
|
||||
m_Shape: 0
|
||||
m_Color: {r: 1, g: 0.95686275, b: 0.8392157, a: 1}
|
||||
m_Intensity: 1
|
||||
m_Range: 10
|
||||
m_SpotAngle: 30
|
||||
m_InnerSpotAngle: 21.80208
|
||||
m_CookieSize: 10
|
||||
m_Shadows:
|
||||
m_Type: 2
|
||||
m_Resolution: -1
|
||||
m_CustomResolution: -1
|
||||
m_Strength: 1
|
||||
m_Bias: 0.05
|
||||
m_NormalBias: 0.4
|
||||
m_NearPlane: 0.2
|
||||
m_CullingMatrixOverride:
|
||||
e00: 1
|
||||
e01: 0
|
||||
e02: 0
|
||||
e03: 0
|
||||
e10: 0
|
||||
e11: 1
|
||||
e12: 0
|
||||
e13: 0
|
||||
e20: 0
|
||||
e21: 0
|
||||
e22: 1
|
||||
e23: 0
|
||||
e30: 0
|
||||
e31: 0
|
||||
e32: 0
|
||||
e33: 1
|
||||
m_UseCullingMatrixOverride: 0
|
||||
m_Cookie: {fileID: 0}
|
||||
m_DrawHalo: 0
|
||||
m_Flare: {fileID: 0}
|
||||
m_RenderMode: 0
|
||||
m_CullingMask:
|
||||
serializedVersion: 2
|
||||
m_Bits: 4294967295
|
||||
m_RenderingLayerMask: 1
|
||||
m_Lightmapping: 4
|
||||
m_LightShadowCasterMode: 0
|
||||
m_AreaSize: {x: 1, y: 1}
|
||||
m_BounceIntensity: 1
|
||||
m_ColorTemperature: 6570
|
||||
m_UseColorTemperature: 0
|
||||
m_BoundingSphereOverride: {x: 0, y: 0, z: 0, w: 0}
|
||||
m_UseBoundingSphereOverride: 0
|
||||
m_UseViewFrustumForShadowCasterCull: 1
|
||||
m_ShadowRadius: 0
|
||||
m_ShadowAngle: 0
|
||||
--- !u!4 &329646044
|
||||
Transform:
|
||||
m_ObjectHideFlags: 0
|
||||
m_CorrespondingSourceObject: {fileID: 0}
|
||||
m_PrefabInstance: {fileID: 0}
|
||||
m_PrefabAsset: {fileID: 0}
|
||||
m_GameObject: {fileID: 329646042}
|
||||
m_LocalRotation: {x: 0.40821788, y: -0.23456968, z: 0.10938163, w: 0.8754261}
|
||||
m_LocalPosition: {x: 0, y: 3, z: 0}
|
||||
m_LocalScale: {x: 1, y: 1, z: 1}
|
||||
m_Children: []
|
||||
m_Father: {fileID: 0}
|
||||
m_RootOrder: 1
|
||||
m_LocalEulerAnglesHint: {x: 50, y: -30, z: 0}
|
||||
--- !u!1 &1141765714
|
||||
GameObject:
|
||||
m_ObjectHideFlags: 0
|
||||
m_CorrespondingSourceObject: {fileID: 0}
|
||||
m_PrefabInstance: {fileID: 0}
|
||||
m_PrefabAsset: {fileID: 0}
|
||||
serializedVersion: 6
|
||||
m_Component:
|
||||
- component: {fileID: 1141765717}
|
||||
- component: {fileID: 1141765716}
|
||||
- component: {fileID: 1141765715}
|
||||
m_Layer: 0
|
||||
m_Name: Main Camera
|
||||
m_TagString: MainCamera
|
||||
m_Icon: {fileID: 0}
|
||||
m_NavMeshLayer: 0
|
||||
m_StaticEditorFlags: 0
|
||||
m_IsActive: 1
|
||||
--- !u!81 &1141765715
|
||||
AudioListener:
|
||||
m_ObjectHideFlags: 0
|
||||
m_CorrespondingSourceObject: {fileID: 0}
|
||||
m_PrefabInstance: {fileID: 0}
|
||||
m_PrefabAsset: {fileID: 0}
|
||||
m_GameObject: {fileID: 1141765714}
|
||||
m_Enabled: 1
|
||||
--- !u!20 &1141765716
|
||||
Camera:
|
||||
m_ObjectHideFlags: 0
|
||||
m_CorrespondingSourceObject: {fileID: 0}
|
||||
m_PrefabInstance: {fileID: 0}
|
||||
m_PrefabAsset: {fileID: 0}
|
||||
m_GameObject: {fileID: 1141765714}
|
||||
m_Enabled: 1
|
||||
serializedVersion: 2
|
||||
m_ClearFlags: 1
|
||||
m_BackGroundColor: {r: 0.19215687, g: 0.3019608, b: 0.4745098, a: 0}
|
||||
m_projectionMatrixMode: 1
|
||||
m_GateFitMode: 2
|
||||
m_FOVAxisMode: 0
|
||||
m_SensorSize: {x: 36, y: 24}
|
||||
m_LensShift: {x: 0, y: 0}
|
||||
m_FocalLength: 50
|
||||
m_NormalizedViewPortRect:
|
||||
serializedVersion: 2
|
||||
x: 0
|
||||
y: 0
|
||||
width: 1
|
||||
height: 1
|
||||
near clip plane: 0.3
|
||||
far clip plane: 1000
|
||||
field of view: 60
|
||||
orthographic: 0
|
||||
orthographic size: 5
|
||||
m_Depth: -1
|
||||
m_CullingMask:
|
||||
serializedVersion: 2
|
||||
m_Bits: 4294967295
|
||||
m_RenderingPath: -1
|
||||
m_TargetTexture: {fileID: 0}
|
||||
m_TargetDisplay: 0
|
||||
m_TargetEye: 3
|
||||
m_HDR: 1
|
||||
m_AllowMSAA: 1
|
||||
m_AllowDynamicResolution: 0
|
||||
m_ForceIntoRT: 0
|
||||
m_OcclusionCulling: 1
|
||||
m_StereoConvergence: 10
|
||||
m_StereoSeparation: 0.022
|
||||
--- !u!4 &1141765717
|
||||
Transform:
|
||||
m_ObjectHideFlags: 0
|
||||
m_CorrespondingSourceObject: {fileID: 0}
|
||||
m_PrefabInstance: {fileID: 0}
|
||||
m_PrefabAsset: {fileID: 0}
|
||||
m_GameObject: {fileID: 1141765714}
|
||||
m_LocalRotation: {x: 0, y: 0, z: 0, w: 1}
|
||||
m_LocalPosition: {x: 0, y: 1, z: -10}
|
||||
m_LocalScale: {x: 1, y: 1, z: 1}
|
||||
m_Children: []
|
||||
m_Father: {fileID: 0}
|
||||
m_RootOrder: 0
|
||||
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
|
||||
--- !u!1 &1438121554
|
||||
GameObject:
|
||||
m_ObjectHideFlags: 0
|
||||
m_CorrespondingSourceObject: {fileID: 0}
|
||||
m_PrefabInstance: {fileID: 0}
|
||||
m_PrefabAsset: {fileID: 0}
|
||||
serializedVersion: 6
|
||||
m_Component:
|
||||
- component: {fileID: 1438121555}
|
||||
- component: {fileID: 1438121556}
|
||||
m_Layer: 0
|
||||
m_Name: Map
|
||||
m_TagString: Untagged
|
||||
m_Icon: {fileID: 0}
|
||||
m_NavMeshLayer: 0
|
||||
m_StaticEditorFlags: 0
|
||||
m_IsActive: 1
|
||||
--- !u!4 &1438121555
|
||||
Transform:
|
||||
m_ObjectHideFlags: 0
|
||||
m_CorrespondingSourceObject: {fileID: 0}
|
||||
m_PrefabInstance: {fileID: 0}
|
||||
m_PrefabAsset: {fileID: 0}
|
||||
m_GameObject: {fileID: 1438121554}
|
||||
m_LocalRotation: {x: 0, y: 0, z: 0, w: 1}
|
||||
m_LocalPosition: {x: 32.52278, y: 6.42986, z: 12.960086}
|
||||
m_LocalScale: {x: 1, y: 1, z: 1}
|
||||
m_Children: []
|
||||
m_Father: {fileID: 0}
|
||||
m_RootOrder: 2
|
||||
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
|
||||
--- !u!114 &1438121556
|
||||
MonoBehaviour:
|
||||
m_ObjectHideFlags: 0
|
||||
m_CorrespondingSourceObject: {fileID: 0}
|
||||
m_PrefabInstance: {fileID: 0}
|
||||
m_PrefabAsset: {fileID: 0}
|
||||
m_GameObject: {fileID: 1438121554}
|
||||
m_Enabled: 1
|
||||
m_EditorHideFlags: 0
|
||||
m_Script: {fileID: 11500000, guid: ef9b1c03bb478e84b931f1cbb3bbab8c, type: 3}
|
||||
m_Name:
|
||||
m_EditorClassIdentifier:
|
||||
startPoint: {x: 0, y: 0, z: 0}
|
||||
endPoint: {x: 200, y: 0, z: 200}
|
||||
7
Assets/Scenes/NPCTest.unity.meta
Normal file
7
Assets/Scenes/NPCTest.unity.meta
Normal file
@@ -0,0 +1,7 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 3fa94fc28affbde4c8273b7f6e39d2d6
|
||||
DefaultImporter:
|
||||
externalObjects: {}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -1,5 +1,5 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 3cbe760ab4339094882e9b35d42a7d39
|
||||
guid: ef793abf4edd2e04b8782a5259797638
|
||||
folderAsset: yes
|
||||
DefaultImporter:
|
||||
externalObjects: {}
|
||||
|
||||
44
Assets/Scripts/NPC/NPCController.cs
Normal file
44
Assets/Scripts/NPC/NPCController.cs
Normal file
@@ -0,0 +1,44 @@
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
|
||||
public class NPCController : MonoBehaviour
|
||||
{
|
||||
|
||||
PathMap Map;
|
||||
private GameObject[,] ball;
|
||||
public List<PathNode> path;
|
||||
private int rows;
|
||||
[SerializeField] Vector3 startPoint;
|
||||
[SerializeField] Vector3 endPoint;
|
||||
|
||||
// Start is called before the first frame update
|
||||
void Start()
|
||||
{
|
||||
startPoint = new Vector3(0,0,0);
|
||||
endPoint = new Vector3(100,0,100);
|
||||
rows = 45;
|
||||
Map = new PathMap(new Vector3(0, 0, 0), rows, rows, 100);
|
||||
Debug.Log("yeet");
|
||||
Map.setupMapWithNextLayer();
|
||||
//Looking through the low res search
|
||||
//path = Map.QueryNodes(startPoint,endPoint);
|
||||
if (path != null) {
|
||||
for (int i = 0; i < path.Count - 1; i++) {
|
||||
int x = path[i].index.x;
|
||||
int y = path[i].index.y;
|
||||
Debug.Log(path[i].index);
|
||||
Debug.Log(path[i].Position);
|
||||
GameObject sphere = GameObject.CreatePrimitive(PrimitiveType.Cube);
|
||||
sphere.transform.position = Map.map[x, y].Position;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
// Update is called once per frame
|
||||
void Update()
|
||||
{
|
||||
|
||||
}
|
||||
}
|
||||
@@ -1,5 +1,5 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 0f50f0a081672ea4a9bc9f13de003f0a
|
||||
guid: ef9b1c03bb478e84b931f1cbb3bbab8c
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
520
Assets/Scripts/NPC/PathMap.cs
Normal file
520
Assets/Scripts/NPC/PathMap.cs
Normal file
@@ -0,0 +1,520 @@
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
|
||||
public class PathMap
|
||||
{
|
||||
|
||||
|
||||
private Vector3 position;
|
||||
public PathNode[,] map;
|
||||
public PathMap parent;
|
||||
public Vector2Int parentIndex;
|
||||
|
||||
private int rows = 100;
|
||||
private int cols = 100;
|
||||
private float spacing = 1f;
|
||||
private float height = 0;
|
||||
public Terrain t;
|
||||
private float w, h;
|
||||
private bool divided = false;
|
||||
|
||||
private List<PathNode> openList;
|
||||
private List<PathNode> closedList;
|
||||
|
||||
private List<PathNode> path;
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
public PathMap(Vector3 Position, int Rows, int Cols, float Width)
|
||||
{
|
||||
setup(Position, Rows, Cols, Width);
|
||||
|
||||
|
||||
}
|
||||
public void setupMap()
|
||||
{
|
||||
Generate();
|
||||
//Add the references to the neighbors of all nodes
|
||||
AddAllNeighbors();
|
||||
|
||||
|
||||
}
|
||||
private void setup(Vector3 Position, int Rows, int Cols, float Width)
|
||||
{
|
||||
position = Position;
|
||||
rows = Rows;
|
||||
cols = Cols;
|
||||
w = Width;
|
||||
spacing = w / rows;
|
||||
|
||||
//Array of all pathnodes in this chunk.
|
||||
map = new PathNode[rows, cols];
|
||||
|
||||
|
||||
//only for debugging
|
||||
|
||||
|
||||
|
||||
openList = new List<PathNode>();
|
||||
closedList = new List<PathNode>();
|
||||
|
||||
//Path that will be returned at the end.
|
||||
path = new List<PathNode>();
|
||||
|
||||
}
|
||||
public void setupMapWithNextLayer()
|
||||
{
|
||||
Generate();
|
||||
AddAllNeighbors();
|
||||
//addNextLayer();
|
||||
|
||||
//Add the references to the neighbors of all nodes
|
||||
|
||||
|
||||
}
|
||||
public void Generate()
|
||||
{
|
||||
//Add all nodes into the map.
|
||||
for (int r = 0; r < rows; r++)
|
||||
{
|
||||
for (int c = 0; c < cols; c++)
|
||||
{
|
||||
PathNode node = new PathNode();
|
||||
node.Position = new Vector3(this.position.x + r * spacing, this.position.y + height, this.position.z + c * spacing);
|
||||
node.index = new Vector2Int(r, c);
|
||||
node.ConditionWeight = 1 + (float)Random.Range(1, 1000) / 1000;
|
||||
map[r, c] = node;
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
public List<PathNode> QueryNodes(Vector3 Vstart, Vector3 Vend)
|
||||
{
|
||||
bool finished = false;
|
||||
|
||||
PathNode start = FindClosestNode(Vstart);
|
||||
start.Gscore = 0;
|
||||
PathNode end = FindClosestNode(Vend);
|
||||
Debug.Log(start);
|
||||
Debug.Log("Searching a path from " + start.Position + " to " + end.Position);
|
||||
openList.Add(start);
|
||||
|
||||
PathNode current;
|
||||
int d = 0;
|
||||
while (!finished)
|
||||
{
|
||||
d++;
|
||||
if (d > 1000)
|
||||
{
|
||||
Debug.Log("Too many trys");
|
||||
return null;
|
||||
}
|
||||
int winner = 0;
|
||||
for (int i = 0; i < openList.Count; i++)
|
||||
{
|
||||
|
||||
if (openList[i].Fscore < openList[winner].Fscore) winner = i;
|
||||
}
|
||||
current = openList[winner];
|
||||
openList.RemoveAt(winner);
|
||||
closedList.Add(current);
|
||||
|
||||
|
||||
|
||||
if (current != end)
|
||||
{
|
||||
foreach (PathNode p in current.neigbors)
|
||||
{
|
||||
if (!closedList.Contains(p) && !p.Blocked)
|
||||
{
|
||||
float tempG = current.Gscore + heuristic(p, current);
|
||||
bool newPath = false;
|
||||
if (openList.Contains(p))
|
||||
{
|
||||
if (tempG < p.Gscore)
|
||||
{
|
||||
p.Gscore = tempG;
|
||||
newPath = true;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
p.Gscore = tempG;
|
||||
newPath = true;
|
||||
openList.Add(p);
|
||||
}
|
||||
if (newPath)
|
||||
{
|
||||
p.Hscore = heuristic(p, end);
|
||||
p.Fscore = p.Gscore + p.Hscore;
|
||||
p.Previous = current;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
Debug.Log("Path Has Been Found");
|
||||
PathNode temp = end;
|
||||
path.Add(temp);
|
||||
while (temp.Previous != null)
|
||||
{
|
||||
path.Add(temp.Previous);
|
||||
temp = temp.Previous;
|
||||
}
|
||||
path.Add(start);
|
||||
finished = true;
|
||||
return path;
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public List<PathNode> LowerQueryNodes(Vector3 Vstart, Vector3 Vend, PathNode[] parentPath)
|
||||
{
|
||||
bool finished = false;
|
||||
|
||||
PathNode start = FindClosestNode(Vstart);
|
||||
start.Gscore = 0;
|
||||
PathNode end = FindClosestNode(Vend);
|
||||
|
||||
Debug.Log("Searching a path from " + start.Position + " to " + end.Position + " but through the lower level.");
|
||||
openList.Add(start);
|
||||
|
||||
PathNode current;
|
||||
int d = 0;
|
||||
while (!finished)
|
||||
{
|
||||
d++;
|
||||
if (d > 1000)
|
||||
{
|
||||
Debug.Log("Too many trys");
|
||||
return null;
|
||||
}
|
||||
int winner = 0;
|
||||
for (int i = 0; i < openList.Count; i++)
|
||||
{
|
||||
|
||||
if (openList[i].Fscore < openList[winner].Fscore) winner = i;
|
||||
}
|
||||
current = openList[winner];
|
||||
openList.RemoveAt(winner);
|
||||
closedList.Add(current);
|
||||
|
||||
if (current != end)
|
||||
{
|
||||
foreach (PathNode p in current.neigbors)
|
||||
{
|
||||
if (!closedList.Contains(p) && !p.Blocked)
|
||||
{
|
||||
float tempG = current.Gscore + heuristic(p, current);
|
||||
bool newPath = false;
|
||||
if (openList.Contains(p))
|
||||
{
|
||||
if (tempG < p.Gscore)
|
||||
{
|
||||
p.Gscore = tempG;
|
||||
newPath = true;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
p.Gscore = tempG;
|
||||
newPath = true;
|
||||
openList.Add(p);
|
||||
}
|
||||
if (newPath)
|
||||
{
|
||||
p.Hscore = heuristic(p, end);
|
||||
p.Fscore = p.Gscore + p.Hscore;
|
||||
p.Previous = current;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
Debug.Log("Path Has Been Found");
|
||||
PathNode temp = end;
|
||||
path.Add(temp);
|
||||
while (temp.Previous != null)
|
||||
{
|
||||
path.Add(temp.Previous);
|
||||
temp = temp.Previous;
|
||||
}
|
||||
path.Add(start);
|
||||
finished = true;
|
||||
return path;
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
/* public void setupNeighborsLower() {
|
||||
//schleife f<>r alle parent Nodes
|
||||
for (int i = 0; i < rows;i++) {
|
||||
for (int j = 0; j < rows;j++)
|
||||
{
|
||||
for (int x = 0; x < rows; x++)
|
||||
{
|
||||
for (int y = 0; y < cols; y++)
|
||||
{
|
||||
//looking left and right and top and bottom
|
||||
for (int x = -1; x <= 1; x++)
|
||||
{
|
||||
for (int y = -1; y <= 1; y++)
|
||||
{
|
||||
if (x != 0 && y != 0)
|
||||
{
|
||||
if (!indexOutOfBounds(index.x + x, index.y + y, rows, cols))
|
||||
{
|
||||
map[(int)index.x, (int)index.y].neigbors.Add(map[(int)index.x + x, (int)index.y + y]);
|
||||
}
|
||||
else
|
||||
{
|
||||
//Rechts
|
||||
if (x == 1 && y == 0)
|
||||
{
|
||||
|
||||
|
||||
}
|
||||
//Links
|
||||
if (x == -1 && y == 0)
|
||||
{
|
||||
|
||||
|
||||
}
|
||||
//Oben
|
||||
if (x == 0 && y == -1)
|
||||
{
|
||||
|
||||
|
||||
}
|
||||
//Unten
|
||||
if (x == 0 && y == 1)
|
||||
{
|
||||
|
||||
|
||||
}
|
||||
|
||||
//Diagonal
|
||||
|
||||
//Oben-Rechts
|
||||
if (x == 1 && y == -1)
|
||||
{
|
||||
|
||||
|
||||
}
|
||||
//Unten-Rechts
|
||||
if (x == 1 && y == 1)
|
||||
{
|
||||
|
||||
|
||||
}
|
||||
//Unten-Links
|
||||
if (x == -1 && y == 1)
|
||||
{
|
||||
|
||||
|
||||
}
|
||||
//Oben-Links
|
||||
if (x == -1 && y == -1)
|
||||
{
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
||||
}*/
|
||||
|
||||
|
||||
public void addNextLayer()
|
||||
{
|
||||
|
||||
|
||||
PathNode[,] full = new PathNode[rows * rows, cols * cols];
|
||||
for (int i = 0; i < rows * rows; i++)
|
||||
{
|
||||
for (int j = 0; j < cols * cols; j++)
|
||||
{
|
||||
|
||||
PathNode node = new PathNode();
|
||||
node.Position = new Vector3(this.position.x + i * spacing/rows,this.position.y,this.position.z + j * spacing/cols);
|
||||
full[i, j] = node;
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
for (int i = 0; i < rows * rows; i++)
|
||||
{
|
||||
for (int j = 0; j < cols * cols; j++)
|
||||
{
|
||||
for (int x = -1; x <= 1; x++)
|
||||
{
|
||||
for (int y = -1; y <= 1; y++)
|
||||
{
|
||||
if (!(x == 0 && y == 0))
|
||||
{
|
||||
if (!indexOutOfBounds(i + x, j + y, rows * rows, cols * cols))
|
||||
{
|
||||
//try
|
||||
//{
|
||||
full[i, j].neigbors.Add(full[i + x, j + y]);
|
||||
//}
|
||||
//catch
|
||||
//{
|
||||
// Debug.Log("poggers");
|
||||
//}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
for (int r = 0; r < rows; r++)
|
||||
{
|
||||
for (int c = 0; c < cols; c++)
|
||||
{
|
||||
PathNode node = map[r, c];
|
||||
Vector3 pos = node.Position;
|
||||
node.lowerLevel = new PathMap(pos, rows, cols, spacing);
|
||||
for (int i = 0; i < rows; i++)
|
||||
{
|
||||
for (int j = 0; j < cols; j++)
|
||||
{
|
||||
PathNode n = full[r*rows+i,c*cols + j];
|
||||
Debug.Log(n);
|
||||
node.lowerLevel.map[i,j].index = new Vector2Int(i, j);
|
||||
node.ConditionWeight = 1 + (float)Random.Range(1, 1000) / 1000;
|
||||
node.lowerLevel.map[i, j] = node;
|
||||
|
||||
}
|
||||
}
|
||||
node.lowerLevel.parent = this;
|
||||
|
||||
node.HasLowerLevel = true;
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
divided = true;
|
||||
}
|
||||
private float heuristic(PathNode p1, PathNode p2)
|
||||
{
|
||||
//Calculates the HScore for a node.
|
||||
float dist = Vector3.Distance(p1.Position, p2.Position) / 2;
|
||||
float dist1 = dist * p1.ConditionWeight;
|
||||
float dist2 = dist * p2.ConditionWeight;
|
||||
|
||||
return dist1 + dist2;
|
||||
|
||||
}
|
||||
private PathNode FindClosestNode(Vector3 pos)
|
||||
{
|
||||
if (pos.x > 0 && pos.x < rows * spacing && pos.z > 0 && pos.z < cols * spacing && false)
|
||||
{
|
||||
|
||||
return map[Mathf.RoundToInt(pos.x / spacing), Mathf.RoundToInt(pos.z / spacing)];
|
||||
}
|
||||
Vector2Int best = new Vector2Int(0, 0);
|
||||
|
||||
for (int r = 0; r < rows; r++)
|
||||
{
|
||||
for (int c = 0; c < cols; c++)
|
||||
{
|
||||
if (Vector3.Distance(map[best.x, best.y].Position, pos) > Vector3.Distance(map[r, c].Position, pos))
|
||||
{
|
||||
best.Set(r, c);
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
return map[best.x, best.y];
|
||||
}
|
||||
private void AddAllNeighbors()
|
||||
{
|
||||
for (int r = 0; r < rows; r++)
|
||||
{
|
||||
for (int c = 0; c < cols; c++)
|
||||
{
|
||||
AddNeighbors(new Vector2Int(r, c));
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
private void lookThroughLowerLevel()
|
||||
{
|
||||
|
||||
|
||||
}
|
||||
private bool indexOutOfBounds(int x, int y, int rows, int cols)
|
||||
{
|
||||
if (x < 0 || x >= rows || y < 0 || y >= cols)
|
||||
{
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
private void AddNeighbors(Vector2Int index)
|
||||
{
|
||||
for (int x = -1; x <= 1; x++)
|
||||
{
|
||||
for (int y = -1; y <= 1; y++)
|
||||
{
|
||||
if (!(x == 0 && y == 0))
|
||||
{
|
||||
if (x == 0 || y == 0)
|
||||
{
|
||||
if (!indexOutOfBounds(index.x + x, index.y + y, rows, cols))
|
||||
{
|
||||
map[index.x, index.y].neigbors.Add(map[index.x + x, index.y + y]);
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
44
Assets/Scripts/NPC/PathNode.cs
Normal file
44
Assets/Scripts/NPC/PathNode.cs
Normal file
@@ -0,0 +1,44 @@
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
|
||||
public class PathNode
|
||||
{
|
||||
|
||||
private Vector3 position;
|
||||
public Vector2Int index;
|
||||
public Vector2Int parentIndex;
|
||||
private float scoreF;
|
||||
private float scoreG;
|
||||
private float scoreH;
|
||||
public List<PathNode> neigbors;
|
||||
public PathMap lowerLevel;
|
||||
private bool hasLowerLevel = false;
|
||||
private float conditionWeight = 1f;
|
||||
private PathNode previous;
|
||||
private bool blocked;
|
||||
|
||||
|
||||
public PathNode()
|
||||
{
|
||||
neigbors = new List<PathNode>();
|
||||
position = Vector3.zero;
|
||||
scoreG = Mathf.Infinity;
|
||||
scoreF = Mathf.Infinity;
|
||||
scoreH = Mathf.Infinity;
|
||||
}
|
||||
|
||||
public void activateNextLevel()
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
public Vector3 Position { get => position; set => position = value; }
|
||||
public float Hscore { get => scoreH; set => scoreH = value; }
|
||||
public float Gscore { get => scoreG; set => scoreG = value; }
|
||||
public float Fscore { get => scoreF; set => scoreF = value; }
|
||||
public PathNode Previous { get => previous; set => previous = value; }
|
||||
public float ConditionWeight { get => conditionWeight; set => conditionWeight = value; }
|
||||
public bool Blocked { get => blocked; set => blocked = value; }
|
||||
public bool HasLowerLevel { get => hasLowerLevel; set => hasLowerLevel = value; }
|
||||
}
|
||||
11
Assets/Scripts/NPC/PathNode.cs.meta
Normal file
11
Assets/Scripts/NPC/PathNode.cs.meta
Normal file
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 8c0191b07f1e4654d9dd893623534cb6
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -1,17 +0,0 @@
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
|
||||
public class PathNode
|
||||
{
|
||||
|
||||
private Vector3 position;
|
||||
public Vector2 index;
|
||||
private float score;
|
||||
public PathNode(Vector3 Pos, float Score) {
|
||||
|
||||
position = Pos;
|
||||
score = Score;
|
||||
}
|
||||
public Vector3 Position { get => position; set => position = value; }
|
||||
}
|
||||
@@ -1,119 +0,0 @@
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
|
||||
public class PathMap : MonoBehaviour
|
||||
{
|
||||
private Vector3 position;
|
||||
private PathNode[,] map;
|
||||
private GameObject[,] ball;
|
||||
private int rows = 40;
|
||||
private int cols = 40;
|
||||
private float spacing = 1f;
|
||||
private float height = 0;
|
||||
|
||||
private PathNode[] uncheckedNodes;
|
||||
private List<PathNode> openList;
|
||||
private List<PathNode> closedList;
|
||||
private List<PathNode> nextList;
|
||||
|
||||
void Start()
|
||||
{
|
||||
map = new PathNode[40, 40];
|
||||
ball = new GameObject[rows, cols];
|
||||
openList = new List<PathNode>();
|
||||
closedList = new List<PathNode>();
|
||||
nextList = new List<PathNode>();
|
||||
|
||||
for (int r = 0; r < rows; r++)
|
||||
{
|
||||
for (int c = 0; c < cols; c++)
|
||||
{
|
||||
PathNode node = new PathNode(new Vector3(r * spacing, height, c * spacing), 1f);
|
||||
node.index = new Vector2(r,c);
|
||||
map[r, c] = node;
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
for (int r = 0; r < rows; r++)
|
||||
{
|
||||
for (int c = 0; c < cols; c++)
|
||||
{
|
||||
GameObject sphere = GameObject.CreatePrimitive(PrimitiveType.Sphere);
|
||||
sphere.transform.position = map[r,c].Position;
|
||||
ball[r,c] = sphere;
|
||||
|
||||
}
|
||||
}
|
||||
FindClosestNode(new Vector3(0.7f,2,0.7f));
|
||||
}
|
||||
private void AddAllNeigbors(Vector2 index) {
|
||||
if ((int)index.x - 1 >= 0 && (int) index.y - 1 >= 0 && !openList.Contains(map[(int)index.x - 1, (int)index.y - 1])) {
|
||||
openList.Add(map[(int)index.x - 1, (int)index.y - 1]);
|
||||
}
|
||||
if ((int)index.y - 1 >= 0 && !openList.Contains(map[(int)index.x, (int)index.y - 1]))
|
||||
{
|
||||
openList.Add(map[(int)index.x, (int)index.y - 1]);
|
||||
}
|
||||
if ((int)index.x + 1 <= rows && (int)index.y - 1 >= 0 && !openList.Contains(map[(int)index.x + 1, (int)index.y - 1]))
|
||||
{
|
||||
openList.Add(map[(int)index.x + 1, (int)index.y - 1]);
|
||||
}
|
||||
|
||||
if ((int)index.x - 1 >= 0 && !openList.Contains(map[(int)index.x - 1, (int)index.y]))
|
||||
{
|
||||
openList.Add(map[(int)index.x - 1, (int)index.y]);
|
||||
}
|
||||
if ((int)index.x + 1 <= rows && (int)index.y - 1 >= 0 && !openList.Contains(map[(int)index.x + 1, (int)index.y]))
|
||||
{
|
||||
openList.Add(map[(int)index.x + 1, (int)index.y]);
|
||||
}
|
||||
|
||||
|
||||
|
||||
if ((int)index.x - 1 >= 0 && (int)index.y + 1 <= cols && !openList.Contains(map[(int)index.x - 1, (int)index.y + 1]))
|
||||
{
|
||||
openList.Add(map[(int)index.x - 1, (int)index.y + 1]);
|
||||
}
|
||||
if ((int)index.y + 1 <= cols && !openList.Contains(map[(int)index.x, (int)index.y + 1]))
|
||||
{
|
||||
openList.Add(map[(int)index.x, (int)index.y + 1]);
|
||||
}
|
||||
if ((int)index.x + 1 <= rows && (int)index.y + 1 <= cols && !openList.Contains(map[(int)index.x + 1, (int)index.y + 1]))
|
||||
{
|
||||
openList.Add(map[(int)index.x + 1, (int)index.y + 1]);
|
||||
}
|
||||
|
||||
}
|
||||
private PathNode FindClosestNode(Vector3 pos) {
|
||||
if (pos.x > 0 && pos.x < rows * spacing && pos.z > 0 && pos.z < cols * spacing)
|
||||
{
|
||||
Destroy(ball[Mathf.RoundToInt(pos.x / spacing), Mathf.RoundToInt(pos.z / spacing)]);
|
||||
return map[Mathf.RoundToInt(pos.x / 2), Mathf.RoundToInt(pos.z / 2)];
|
||||
}
|
||||
else {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
public void QueryNodes() {
|
||||
PathNode currentNode;
|
||||
for (int r = 0; r < rows; r++)
|
||||
{
|
||||
for (int c = 0; c < cols; c++)
|
||||
{
|
||||
currentNode = map[r,c];
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
// Update is called once per frame
|
||||
void Update()
|
||||
{
|
||||
|
||||
|
||||
}
|
||||
}
|
||||
@@ -11,6 +11,9 @@ EditorUserSettings:
|
||||
RecentlyUsedScenePath-1:
|
||||
value: 22424703114646680e0b0227036c6c111b07142f1f2b233e2867083debf42d
|
||||
flags: 0
|
||||
RecentlyUsedScenePath-2:
|
||||
value: 22424703114646680e0b0227036c712035231d393866333e243d04
|
||||
flags: 0
|
||||
vcSharedLogLevel:
|
||||
value: 0d5e400f0650
|
||||
flags: 0
|
||||
|
||||
Reference in New Issue
Block a user