lab6 dop
This commit is contained in:
1 parent
bca661e132
commit
ed0437638b
9 files changed
+3442
-17
No files matched your search
@@ -66,7 +66,7 @@ namespace Code.Lab5
|
|||||||
|
|
||||||
private void TryFireAtTarget()
|
private void TryFireAtTarget()
|
||||||
{
|
{
|
||||||
if (!wanderingTarget || !projectilePrefab) return;
|
if (wanderingTarget == null || projectilePrefab == null) return;
|
||||||
|
|
||||||
var targetPos = wanderingTarget.transform.position;
|
var targetPos = wanderingTarget.transform.position;
|
||||||
var targetVelocity = GetTargetVelocity();
|
var targetVelocity = GetTargetVelocity();
|
||||||
@@ -99,10 +99,10 @@ namespace Code.Lab5
|
|||||||
|
|
||||||
private Vector3 GetTargetVelocity()
|
private Vector3 GetTargetVelocity()
|
||||||
{
|
{
|
||||||
if (wanderingTarget) return Vector3.zero;
|
if (wanderingTarget == null) return Vector3.zero;
|
||||||
|
|
||||||
var targetAgent = wanderingTarget.GetComponent<Agent>();
|
var targetAgent = wanderingTarget.GetComponent<Agent>();
|
||||||
if (targetAgent)
|
if (targetAgent != null)
|
||||||
{
|
{
|
||||||
return targetAgent.velocity;
|
return targetAgent.velocity;
|
||||||
}
|
}
|
||||||
@@ -166,7 +166,7 @@ namespace Code.Lab5
|
|||||||
var projectile = projObj.GetComponent<Projectile>();
|
var projectile = projObj.GetComponent<Projectile>();
|
||||||
projectile.Set(transform.position, direction, launchSpeed);
|
projectile.Set(transform.position, direction, launchSpeed);
|
||||||
|
|
||||||
if (showPredictedPosition && targetCirclePrefab)
|
if (showPredictedPosition && targetCirclePrefab != null)
|
||||||
{
|
{
|
||||||
SpawnTargetCircle(targetPos, "ShotTarget");
|
SpawnTargetCircle(targetPos, "ShotTarget");
|
||||||
}
|
}
|
||||||
@@ -174,7 +174,7 @@ namespace Code.Lab5
|
|||||||
|
|
||||||
private void UpdatePredictedTargetVisual()
|
private void UpdatePredictedTargetVisual()
|
||||||
{
|
{
|
||||||
if (wanderingTarget) return;
|
if (wanderingTarget == null) return;
|
||||||
|
|
||||||
var targetPos = wanderingTarget.transform.position;
|
var targetPos = wanderingTarget.transform.position;
|
||||||
var targetVelocity = GetTargetVelocity();
|
var targetVelocity = GetTargetVelocity();
|
||||||
@@ -187,10 +187,10 @@ namespace Code.Lab5
|
|||||||
Debug.DrawLine(targetPos, predictedPos, Color.yellow);
|
Debug.DrawLine(targetPos, predictedPos, Color.yellow);
|
||||||
}
|
}
|
||||||
|
|
||||||
private void SpawnTargetCircle(Vector3 position, string name1 = "TargetCircle")
|
private void SpawnTargetCircle(Vector3 position, string name = "TargetCircle")
|
||||||
{
|
{
|
||||||
var circle = Instantiate(targetCirclePrefab, position, Quaternion.identity);
|
var circle = Instantiate(targetCirclePrefab, position, Quaternion.identity);
|
||||||
circle.name = name1;
|
circle.name = name;
|
||||||
|
|
||||||
var circlePos = circle.transform.position;
|
var circlePos = circle.transform.position;
|
||||||
circlePos.y = 0.05f;
|
circlePos.y = 0.05f;
|
||||||
|
|||||||
@@ -240,5 +240,128 @@ namespace Code.Lab6
|
|||||||
path.Reverse();
|
path.Reverse();
|
||||||
return path;
|
return path;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public List<Vertex> GetPathWeights(GameObject srcObj, GameObject dstObj)
|
||||||
|
{
|
||||||
|
if (!srcObj || !dstObj)
|
||||||
|
return new List<Vertex>();
|
||||||
|
|
||||||
|
var src = GetNearestVertex(srcObj.transform.position);
|
||||||
|
var dst = GetNearestVertex(dstObj.transform.position);
|
||||||
|
|
||||||
|
if (!src || !dst)
|
||||||
|
{
|
||||||
|
Debug.LogWarning("Source or destination vertex is null");
|
||||||
|
return new List<Vertex>();
|
||||||
|
}
|
||||||
|
|
||||||
|
Debug.Log($"Dijkstra from {src.id} to {dst.id}");
|
||||||
|
|
||||||
|
var distances = new Dictionary<int, float>();
|
||||||
|
var previous = new Dictionary<int, int>();
|
||||||
|
var unvisited = new HashSet<int>();
|
||||||
|
|
||||||
|
foreach (var vertex in Vertices.Where(vertex => vertex))
|
||||||
|
{
|
||||||
|
distances[vertex.id] = float.MaxValue;
|
||||||
|
unvisited.Add(vertex.id);
|
||||||
|
}
|
||||||
|
|
||||||
|
distances[src.id] = 0;
|
||||||
|
previous[src.id] = src.id;
|
||||||
|
|
||||||
|
while (unvisited.Count > 0)
|
||||||
|
{
|
||||||
|
var minDist = float.MaxValue;
|
||||||
|
var currentId = -1;
|
||||||
|
|
||||||
|
foreach (var id in unvisited)
|
||||||
|
{
|
||||||
|
if (distances.TryGetValue(id, out var dist) && dist < minDist)
|
||||||
|
{
|
||||||
|
minDist = dist;
|
||||||
|
currentId = id;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (currentId == -1) break;
|
||||||
|
|
||||||
|
if (currentId == dst.id)
|
||||||
|
{
|
||||||
|
Debug.Log($"Dijkstra: Path found! Total cost: {distances[dst.id]:F2}");
|
||||||
|
return BuildPathWeights(src.id, dst.id, previous);
|
||||||
|
}
|
||||||
|
|
||||||
|
unvisited.Remove(currentId);
|
||||||
|
|
||||||
|
var currentVertex = GetVertexById(currentId);
|
||||||
|
if (!currentVertex) continue;
|
||||||
|
|
||||||
|
var edges = GetEdges(currentVertex);
|
||||||
|
|
||||||
|
foreach (var edge in edges)
|
||||||
|
{
|
||||||
|
if (!edge.Vertex) continue;
|
||||||
|
|
||||||
|
var neighbourId = edge.Vertex.id;
|
||||||
|
if (!unvisited.Contains(neighbourId)) continue;
|
||||||
|
|
||||||
|
var newDist = distances[currentId] + edge.cost;
|
||||||
|
|
||||||
|
if (newDist < distances[neighbourId])
|
||||||
|
{
|
||||||
|
distances[neighbourId] = newDist;
|
||||||
|
previous[neighbourId] = currentId;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
Debug.LogWarning("Dijkstra: No path found");
|
||||||
|
return new List<Vertex>();
|
||||||
|
}
|
||||||
|
|
||||||
|
private List<Vertex> BuildPathWeights(int srcId, int dstId, Dictionary<int, int> previous)
|
||||||
|
{
|
||||||
|
var path = new List<Vertex>();
|
||||||
|
|
||||||
|
if (!previous.ContainsKey(dstId))
|
||||||
|
{
|
||||||
|
Debug.LogError("Destination not in previous dictionary");
|
||||||
|
return path;
|
||||||
|
}
|
||||||
|
|
||||||
|
var currentId = dstId;
|
||||||
|
|
||||||
|
while (currentId != srcId)
|
||||||
|
{
|
||||||
|
var currentVertex = GetVertexById(currentId);
|
||||||
|
if (!currentVertex)
|
||||||
|
{
|
||||||
|
Debug.LogError($"Vertex with id {currentId} not found");
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
path.Add(currentVertex);
|
||||||
|
|
||||||
|
if (!previous.TryGetValue(currentId, out var prevId))
|
||||||
|
{
|
||||||
|
Debug.LogError($"Previous not found for id {currentId}");
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
currentId = prevId;
|
||||||
|
}
|
||||||
|
|
||||||
|
var srcVertex = GetVertexById(srcId);
|
||||||
|
if (srcVertex is not null)
|
||||||
|
{
|
||||||
|
path.Add(srcVertex);
|
||||||
|
}
|
||||||
|
|
||||||
|
path.Reverse();
|
||||||
|
|
||||||
|
Debug.Log($"Path built with {path.Count} vertices");
|
||||||
|
return path;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -20,6 +20,11 @@ namespace Code.Lab6
|
|||||||
private int _numRows;
|
private int _numRows;
|
||||||
private GameObject[] _vertexObj;
|
private GameObject[] _vertexObj;
|
||||||
private bool[,] _mapVertices;
|
private bool[,] _mapVertices;
|
||||||
|
|
||||||
|
public bool useWeightedMap = true;
|
||||||
|
public Texture2D weightMapTexture;
|
||||||
|
public float minWeight = 0.5f;
|
||||||
|
public float maxWeight = 5f;
|
||||||
|
|
||||||
private Dictionary<int, int> _gridIdToVertexIndex;
|
private Dictionary<int, int> _gridIdToVertexIndex;
|
||||||
|
|
||||||
@@ -32,6 +37,309 @@ namespace Code.Lab6
|
|||||||
{
|
{
|
||||||
return new Vector2Int(id % _numCols, id / _numCols);
|
return new Vector2Int(id % _numCols, id / _numCols);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private void LoadMapWithWeights(string filename)
|
||||||
|
{
|
||||||
|
var path = Application.dataPath + "/" + MapsDir + "/" + filename;
|
||||||
|
|
||||||
|
try
|
||||||
|
{
|
||||||
|
var strmRdr = new StreamReader(path);
|
||||||
|
using (strmRdr)
|
||||||
|
{
|
||||||
|
strmRdr.ReadLine();
|
||||||
|
|
||||||
|
var line = strmRdr.ReadLine();
|
||||||
|
if (line != null && line.StartsWith("height"))
|
||||||
|
_numRows = int.Parse(line.Split(' ')[1]);
|
||||||
|
|
||||||
|
line = strmRdr.ReadLine();
|
||||||
|
if (line != null && line.StartsWith("width"))
|
||||||
|
_numCols = int.Parse(line.Split(' ')[1]);
|
||||||
|
|
||||||
|
strmRdr.ReadLine();
|
||||||
|
|
||||||
|
Vertices = new List<Vertex>();
|
||||||
|
Neighbours = new List<List<Vertex>>();
|
||||||
|
Costs = new List<List<float>>();
|
||||||
|
_vertexObj = new GameObject[_numRows * _numCols];
|
||||||
|
_mapVertices = new bool[_numRows, _numCols];
|
||||||
|
_gridIdToVertexIndex = new Dictionary<int, int>();
|
||||||
|
|
||||||
|
Debug.Log($"Loading weighted map {_numRows}x{_numCols}");
|
||||||
|
|
||||||
|
for (var row = 0; row < _numRows; row++)
|
||||||
|
{
|
||||||
|
line = strmRdr.ReadLine();
|
||||||
|
if (string.IsNullOrEmpty(line)) continue;
|
||||||
|
|
||||||
|
for (var col = 0; col < _numCols && col < line.Length; col++)
|
||||||
|
{
|
||||||
|
var cellChar = line[col];
|
||||||
|
var isGround = cellChar != '#';
|
||||||
|
|
||||||
|
_mapVertices[row, col] = isGround;
|
||||||
|
|
||||||
|
var position = new Vector3(col * cellSize, 0f, row * cellSize);
|
||||||
|
var gridId = GridToId(col, row);
|
||||||
|
|
||||||
|
GameObject obj;
|
||||||
|
|
||||||
|
if (isGround && vertexPrefab != null)
|
||||||
|
{
|
||||||
|
obj = Instantiate(vertexPrefab, position, Quaternion.identity);
|
||||||
|
}
|
||||||
|
else if (!isGround && obstaclePrefab != null)
|
||||||
|
{
|
||||||
|
obj = Instantiate(obstaclePrefab, position, Quaternion.identity);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
obj = GameObject.CreatePrimitive(PrimitiveType.Cube);
|
||||||
|
obj.transform.position = position;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (obj == null) continue;
|
||||||
|
|
||||||
|
obj.name = $"{cellChar}_{gridId}";
|
||||||
|
obj.transform.parent = transform;
|
||||||
|
|
||||||
|
var scale = obj.transform.localScale;
|
||||||
|
scale.x = cellSize;
|
||||||
|
scale.z = cellSize;
|
||||||
|
scale.y = isGround ? 0.1f : 1f;
|
||||||
|
obj.transform.localScale = scale;
|
||||||
|
|
||||||
|
_vertexObj[gridId] = obj;
|
||||||
|
|
||||||
|
if (!isGround) continue;
|
||||||
|
|
||||||
|
var v = obj.GetComponent<Vertex>();
|
||||||
|
if (v == null)
|
||||||
|
v = obj.AddComponent<Vertex>();
|
||||||
|
|
||||||
|
v.id = gridId;
|
||||||
|
v.neighbours = new List<Edge>();
|
||||||
|
|
||||||
|
v.weight = GetWeightFromChar(cellChar);
|
||||||
|
|
||||||
|
var renderer1 = obj.GetComponent<Renderer>();
|
||||||
|
if (renderer1 != null)
|
||||||
|
{
|
||||||
|
renderer1.material.color = cellChar switch
|
||||||
|
{
|
||||||
|
'r' => new Color(0.8f, 0.2f, 0.2f),
|
||||||
|
'm' => new Color(0.6f, 0.4f, 0.2f),
|
||||||
|
'h' => new Color(0.2f, 0.8f, 0.2f),
|
||||||
|
_ => new Color(0.8f, 0.8f, 0.8f)
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
var vertexIndex = Vertices.Count;
|
||||||
|
Vertices.Add(v);
|
||||||
|
Neighbours.Add(new List<Vertex>());
|
||||||
|
Costs.Add(new List<float>());
|
||||||
|
|
||||||
|
_gridIdToVertexIndex[gridId] = vertexIndex;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
foreach (var kvp in _gridIdToVertexIndex)
|
||||||
|
{
|
||||||
|
var gridId = kvp.Key;
|
||||||
|
var vertexIndex = kvp.Value;
|
||||||
|
var gridPos = IdToGrid(gridId);
|
||||||
|
SetNeighboursWithWeights(gridPos.x, gridPos.y, vertexIndex);
|
||||||
|
}
|
||||||
|
|
||||||
|
Debug.Log($"Weighted map loaded. {Vertices.Count} walkable vertices");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
catch (Exception e)
|
||||||
|
{
|
||||||
|
Debug.LogError($"Failed to load weighted map: {e.Message}");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private float GetWeightFromChar(char c)
|
||||||
|
{
|
||||||
|
return c switch
|
||||||
|
{
|
||||||
|
'h' => 0.5f,
|
||||||
|
'.' => 1f,
|
||||||
|
'm' => 3f,
|
||||||
|
'r' => 5f,
|
||||||
|
'#' => float.MaxValue,
|
||||||
|
_ => 1f
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
private float CalculateVertexWeight(int row, int col, Texture2D weightTexture)
|
||||||
|
{
|
||||||
|
if (weightTexture != null)
|
||||||
|
{
|
||||||
|
var texX = Mathf.FloorToInt((float)col / _numCols * weightTexture.width);
|
||||||
|
var texY = Mathf.FloorToInt((float)row / _numRows * weightTexture.height);
|
||||||
|
|
||||||
|
var pixel = weightTexture.GetPixel(texX, texY);
|
||||||
|
var gray = pixel.grayscale;
|
||||||
|
|
||||||
|
return Mathf.Lerp(minWeight, maxWeight, gray);
|
||||||
|
}
|
||||||
|
|
||||||
|
var centerX = _numCols / 2f;
|
||||||
|
var centerY = _numRows / 2f;
|
||||||
|
var distFromCenter = Vector2.Distance(new Vector2(col, row), new Vector2(centerX, centerY));
|
||||||
|
var maxDist = Mathf.Max(_numCols, _numRows) / 2f;
|
||||||
|
var centerWeight = Mathf.Lerp(minWeight, maxWeight, distFromCenter / maxDist);
|
||||||
|
|
||||||
|
var noise = Mathf.PerlinNoise(col * 0.1f, row * 0.1f);
|
||||||
|
var noiseWeight = Mathf.Lerp(minWeight, maxWeight, noise);
|
||||||
|
|
||||||
|
var riverX = _numCols * 0.3f + Mathf.Sin(row * 0.3f) * 5f;
|
||||||
|
var distToRiver = Mathf.Abs(col - riverX);
|
||||||
|
var riverWeight = distToRiver < 3f ? maxWeight : minWeight;
|
||||||
|
|
||||||
|
var finalWeight = (centerWeight * 0.3f + noiseWeight * 0.3f + riverWeight * 0.4f);
|
||||||
|
|
||||||
|
return Mathf.Clamp(finalWeight, minWeight, maxWeight);
|
||||||
|
}
|
||||||
|
|
||||||
|
protected void SetNeighboursWithWeights(int x, int y, int vertexIndex)
|
||||||
|
{
|
||||||
|
var currentVertex = Vertices[vertexIndex];
|
||||||
|
|
||||||
|
Neighbours[vertexIndex].Clear();
|
||||||
|
Costs[vertexIndex].Clear();
|
||||||
|
currentVertex.neighbours.Clear();
|
||||||
|
|
||||||
|
Vector2Int[] directions;
|
||||||
|
|
||||||
|
if (get8Vicinity)
|
||||||
|
{
|
||||||
|
directions = new[]
|
||||||
|
{
|
||||||
|
new Vector2Int(x, y - 1), new Vector2Int(x + 1, y - 1),
|
||||||
|
new Vector2Int(x + 1, y), new Vector2Int(x + 1, y + 1),
|
||||||
|
new Vector2Int(x, y + 1), new Vector2Int(x - 1, y + 1),
|
||||||
|
new Vector2Int(x - 1, y), new Vector2Int(x - 1, y - 1)
|
||||||
|
};
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
directions = new[]
|
||||||
|
{
|
||||||
|
new Vector2Int(x, y - 1), new Vector2Int(x + 1, y),
|
||||||
|
new Vector2Int(x, y + 1), new Vector2Int(x - 1, y)
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
const float diagonalMultiplier = 1.4f;
|
||||||
|
|
||||||
|
foreach (var dir in directions)
|
||||||
|
{
|
||||||
|
if (dir.x < 0 || dir.x >= _numCols || dir.y < 0 || dir.y >= _numRows)
|
||||||
|
continue;
|
||||||
|
|
||||||
|
if (!_mapVertices[dir.y, dir.x])
|
||||||
|
continue;
|
||||||
|
|
||||||
|
var neighbourGridId = GridToId(dir.x, dir.y);
|
||||||
|
|
||||||
|
if (!_gridIdToVertexIndex.TryGetValue(neighbourGridId, out var neighbourIndex))
|
||||||
|
continue;
|
||||||
|
|
||||||
|
var neighbourVertex = Vertices[neighbourIndex];
|
||||||
|
|
||||||
|
var baseCost = (currentVertex.weight + neighbourVertex.weight) / 2f;
|
||||||
|
|
||||||
|
if (dir.x != x && dir.y != y)
|
||||||
|
{
|
||||||
|
baseCost *= diagonalMultiplier;
|
||||||
|
}
|
||||||
|
|
||||||
|
Neighbours[vertexIndex].Add(neighbourVertex);
|
||||||
|
Costs[vertexIndex].Add(baseCost);
|
||||||
|
currentVertex.neighbours.Add(new Edge(neighbourVertex, baseCost));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public void GenerateLargeWeightedMap(int width, int height)
|
||||||
|
{
|
||||||
|
_numCols = width;
|
||||||
|
_numRows = height;
|
||||||
|
|
||||||
|
Vertices = new List<Vertex>();
|
||||||
|
Neighbours = new List<List<Vertex>>();
|
||||||
|
Costs = new List<List<float>>();
|
||||||
|
_vertexObj = new GameObject[width * height];
|
||||||
|
_mapVertices = new bool[height, width];
|
||||||
|
_gridIdToVertexIndex = new Dictionary<int, int>();
|
||||||
|
|
||||||
|
for (var y = 0; y < height; y++)
|
||||||
|
{
|
||||||
|
for (var x = 0; x < width; x++)
|
||||||
|
{
|
||||||
|
_mapVertices[y, x] = true;
|
||||||
|
|
||||||
|
var position = new Vector3(x * cellSize, 0f, y * cellSize);
|
||||||
|
var gridId = GridToId(x, y);
|
||||||
|
|
||||||
|
var obj = Instantiate(vertexPrefab, position, Quaternion.identity);
|
||||||
|
obj.name = $"Vertex_{gridId}";
|
||||||
|
obj.transform.parent = transform;
|
||||||
|
|
||||||
|
var v = obj.GetComponent<Vertex>();
|
||||||
|
if (v == null)
|
||||||
|
v = obj.AddComponent<Vertex>();
|
||||||
|
|
||||||
|
v.id = gridId;
|
||||||
|
v.neighbours = new List<Edge>();
|
||||||
|
|
||||||
|
v.weight = CalculateVertexWeight(y, x, null);
|
||||||
|
|
||||||
|
var renderer1 = obj.GetComponent<Renderer>();
|
||||||
|
if (renderer1 != null)
|
||||||
|
{
|
||||||
|
var t = Mathf.InverseLerp(minWeight, maxWeight, v.weight);
|
||||||
|
renderer1.material.color = Color.Lerp(Color.white, Color.black, t);
|
||||||
|
}
|
||||||
|
|
||||||
|
var vertexIndex = Vertices.Count;
|
||||||
|
Vertices.Add(v);
|
||||||
|
Neighbours.Add(new List<Vertex>());
|
||||||
|
Costs.Add(new List<float>());
|
||||||
|
|
||||||
|
_gridIdToVertexIndex[gridId] = vertexIndex;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
for (var y = 0; y < height; y++)
|
||||||
|
{
|
||||||
|
for (var x = 0; x < width; x++)
|
||||||
|
{
|
||||||
|
var gridId = GridToId(x, y);
|
||||||
|
if (_gridIdToVertexIndex.TryGetValue(gridId, out var vertexIndex))
|
||||||
|
{
|
||||||
|
SetNeighboursWithWeights(x, y, vertexIndex);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
Debug.Log($"Generated large weighted map: {width}x{height} ({Vertices.Count} vertices)");
|
||||||
|
}
|
||||||
|
|
||||||
|
public override void Load()
|
||||||
|
{
|
||||||
|
if (useWeightedMap)
|
||||||
|
{
|
||||||
|
LoadMapWithWeights(mapName);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
LoadMap(mapName);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
private void LoadMap(string filename)
|
private void LoadMap(string filename)
|
||||||
{
|
{
|
||||||
@@ -226,11 +534,6 @@ namespace Code.Lab6
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public override void Load()
|
|
||||||
{
|
|
||||||
LoadMap(mapName);
|
|
||||||
}
|
|
||||||
|
|
||||||
public override Vertex GetNearestVertex(Vector3 position)
|
public override Vertex GetNearestVertex(Vector3 position)
|
||||||
{
|
{
|
||||||
var col = Mathf.RoundToInt(position.x / cellSize);
|
var col = Mathf.RoundToInt(position.x / cellSize);
|
||||||
|
|||||||
@@ -9,6 +9,11 @@ namespace Code.Lab6
|
|||||||
{
|
{
|
||||||
public int id = -1;
|
public int id = -1;
|
||||||
public List<Edge> neighbours;
|
public List<Edge> neighbours;
|
||||||
|
|
||||||
|
[Header("Weight Settings")]
|
||||||
|
[Range(0.1f, 10f)]
|
||||||
|
public float weight = 1f;
|
||||||
|
|
||||||
[HideInInspector]
|
[HideInInspector]
|
||||||
public Vertex prev;
|
public Vertex prev;
|
||||||
|
|
||||||
@@ -19,18 +24,27 @@ namespace Code.Lab6
|
|||||||
|
|
||||||
private void OnDrawGizmos()
|
private void OnDrawGizmos()
|
||||||
{
|
{
|
||||||
Gizmos.color = Color.cyan;
|
var t = Mathf.InverseLerp(0.1f, 10f, weight);
|
||||||
Gizmos.DrawSphere(transform.position, 0.2f);
|
Gizmos.color = Color.Lerp(Color.white, Color.black, t);
|
||||||
|
Gizmos.DrawSphere(transform.position, 0.2f + weight * 0.05f);
|
||||||
}
|
}
|
||||||
|
|
||||||
private void OnDrawGizmosSelected()
|
private void OnDrawGizmosSelected()
|
||||||
{
|
{
|
||||||
if (neighbours == null) return;
|
if (neighbours == null) return;
|
||||||
|
|
||||||
Gizmos.color = Color.yellow;
|
|
||||||
foreach (var edge in neighbours.Where(edge => edge?.Vertex != null))
|
foreach (var edge in neighbours.Where(edge => edge?.Vertex != null))
|
||||||
{
|
{
|
||||||
|
var t = Mathf.InverseLerp(0.1f, 10f, edge.cost);
|
||||||
|
Gizmos.color = Color.Lerp(Color.green, Color.red, t);
|
||||||
Gizmos.DrawLine(transform.position, edge.Vertex.transform.position);
|
Gizmos.DrawLine(transform.position, edge.Vertex.transform.position);
|
||||||
|
|
||||||
|
#if UNITY_EDITOR
|
||||||
|
var midPoint = (transform.position + edge.Vertex.transform.position) / 2f;
|
||||||
|
midPoint.y += 0.5f;
|
||||||
|
UnityEditor.Handles.color = Color.yellow;
|
||||||
|
UnityEditor.Handles.Label(midPoint, $"{edge.cost:F1}");
|
||||||
|
#endif
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -10,6 +10,7 @@ namespace Code.UI
|
|||||||
{
|
{
|
||||||
public class Lab6Controller : MonoBehaviour
|
public class Lab6Controller : MonoBehaviour
|
||||||
{
|
{
|
||||||
|
public bool isDop;
|
||||||
public Graph graph;
|
public Graph graph;
|
||||||
public Agent agent;
|
public Agent agent;
|
||||||
public Transform targetMarker;
|
public Transform targetMarker;
|
||||||
@@ -171,8 +172,15 @@ namespace Code.UI
|
|||||||
targetPos.y = 0.5f;
|
targetPos.y = 0.5f;
|
||||||
targetMarker.position = targetPos;
|
targetMarker.position = targetPos;
|
||||||
|
|
||||||
var path = _useBfs ? graph.GetPathBfs(agent.gameObject, targetMarker.gameObject) : graph.GetPathDfs(agent.gameObject, targetMarker.gameObject);
|
List<Vertex> path;
|
||||||
|
|
||||||
|
if (!isDop)
|
||||||
|
path = _useBfs
|
||||||
|
? graph.GetPathBfs(agent.gameObject, targetMarker.gameObject)
|
||||||
|
: graph.GetPathDfs(agent.gameObject, targetMarker.gameObject);
|
||||||
|
else
|
||||||
|
path = graph.GetPathWeights(agent.gameObject, targetMarker.gameObject);
|
||||||
|
|
||||||
if (path is { Count: > 0 })
|
if (path is { Count: > 0 })
|
||||||
{
|
{
|
||||||
_currentPath = path;
|
_currentPath = path;
|
||||||
|
|||||||
@@ -0,0 +1,34 @@
|
|||||||
|
type octile
|
||||||
|
height 30
|
||||||
|
width 40
|
||||||
|
map
|
||||||
|
..........rrrrrrrrrrrrrrrrrrrr................
|
||||||
|
.########.r.........r.........r.########.......
|
||||||
|
..........r.rrrrrrr.r.rrrrrrr.r..........rrrrrr
|
||||||
|
.########.r.r.....r.r.r.....r.r.########.r.....
|
||||||
|
..........r.r.....r.r.r.....r.r..........r.....
|
||||||
|
.########.r.r.....r.r.r.....r.r.########.r.....
|
||||||
|
..........r.r.....r.r.r.....r.r..........r.....
|
||||||
|
.########.r.rrrrrrr.r.rrrrrrr.r.########.r.....
|
||||||
|
..........r.........r.........r..........r.....
|
||||||
|
.########.rrrrrrrrrrrrrrrrrrrr.########.rrrrrr.
|
||||||
|
...............................................
|
||||||
|
.........mmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmm......
|
||||||
|
.........m......................m......m......
|
||||||
|
.........m.mmmmmmmmmmmmmmmmmmmm.m......m......
|
||||||
|
.........m.m................m.m.m......m......
|
||||||
|
.........m.m.mmmmmmmmmmmmmm.m.m.m......m......
|
||||||
|
.........m.m.m............m.m.m.m......m......
|
||||||
|
.........m.m.m............m.m.m.m......m......
|
||||||
|
.........m.m.m............m.m.m.m......m......
|
||||||
|
.........m.m.mmmmmmmmmmmmmm.m.m.mmmmmmm.m......
|
||||||
|
.........m.m................m.m........m......
|
||||||
|
.........m.mmmmmmmmmmmmmmmmmm.m........m......
|
||||||
|
.........m....................mmmmmmmmmm......
|
||||||
|
.........mmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmm......
|
||||||
|
...............................................
|
||||||
|
...........hhhhhhhhhhhhhhhhhhhhhhhhhh..........
|
||||||
|
...........h......................h..........
|
||||||
|
...........h......................h..........
|
||||||
|
...........hhhhhhhhhhhhhhhhhhhhhhhhhh..........
|
||||||
|
...............................................
|
||||||
@@ -0,0 +1,3 @@
|
|||||||
|
fileFormatVersion: 2
|
||||||
|
guid: 23b303bc398e4b5bb9beadbba49db1a1
|
||||||
|
timeCreated: 1772313223
|
||||||
File diff suppressed because it is too large.
Load diff
@@ -0,0 +1,7 @@
|
|||||||
|
fileFormatVersion: 2
|
||||||
|
guid: 72811e1e311d22b0bb02e7783eb4a155
|
||||||
|
DefaultImporter:
|
||||||
|
externalObjects: {}
|
||||||
|
userData:
|
||||||
|
assetBundleName:
|
||||||
|
assetBundleVariant:
|
||||||
Reference in new issue
Block a user