Added exploration node to DSL and worked on vertical pathfinding (Closed gates blocked horizontal movement as well)
This commit is contained in:
@@ -0,0 +1,90 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using Godot;
|
||||
|
||||
public class ExploreNode : ProgramNode
|
||||
{
|
||||
public Vector3 startPosition;
|
||||
public Vector3I targetPosition;
|
||||
public List<Vector3> pathPoints;
|
||||
public ExploreNode()
|
||||
{
|
||||
DisplayText = "Explore";
|
||||
}
|
||||
public override NodeResult Execute(Robot robot, double delta)
|
||||
{
|
||||
if (pathPoints == null)
|
||||
{
|
||||
int safetyCounter = 0;
|
||||
while (true)
|
||||
{
|
||||
targetPosition = new Vector3I(GameData.rand.Next(GameData.layerSize), GameData.currentLayer, GameData.rand.Next(GameData.layerSize));
|
||||
if (!GameData.map[targetPosition.Y].tiles[targetPosition.X, targetPosition.Z].wasVisited) break;
|
||||
safetyCounter++;
|
||||
if (safetyCounter > Math.Pow(GameData.layerSize, 2) * 2)
|
||||
{
|
||||
lastExecutionMessage = "No tiles left to explore";
|
||||
return NodeResult.SUCCESS;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pathPoints ??= [.. Pathfinding.GetPath(Pathfinding.GetClosestStartPoint(robot.Position), targetPosition)];
|
||||
|
||||
if (pathPoints.Count <= 0)
|
||||
{
|
||||
lastExecutionMessage = "No path available";
|
||||
return NodeResult.FAILURE;
|
||||
}
|
||||
|
||||
startPosition = robot.Position;
|
||||
Vector3 target = pathPoints[0] - startPosition;
|
||||
float distance = target.Length();
|
||||
|
||||
if (distance < 0.1f)
|
||||
{
|
||||
robot.Position = pathPoints[0];
|
||||
Vector3I mapIndex = Pathfinding.GetClosestStartPoint(robot.Position);
|
||||
Tile tile = GameData.map[mapIndex.Y].tiles[mapIndex.X, mapIndex.Z];
|
||||
if (!tile.wasVisited)
|
||||
{
|
||||
tile.wasVisited = true;
|
||||
tile.ContentNode.Visible = true;
|
||||
}
|
||||
pathPoints.Remove(pathPoints[0]);
|
||||
if (pathPoints.Count <= 0)
|
||||
{
|
||||
lastExecutionMessage = "Current exploration finished";
|
||||
pathPoints = null;
|
||||
return NodeResult.RUNNING;
|
||||
}
|
||||
|
||||
lastExecutionMessage = "";
|
||||
return NodeResult.RUNNING;
|
||||
}
|
||||
|
||||
Vector3 direction = target / distance;
|
||||
Vector3 lookDirection = new Vector3(direction.X, 0, direction.Z);
|
||||
if (lookDirection.Length() > 0.1f)
|
||||
{
|
||||
robot.LookAt(robot.GlobalPosition + lookDirection, Vector3.Up);
|
||||
}
|
||||
robot.GlobalPosition += direction * (float)delta * GameData.robotSpeed;
|
||||
|
||||
return NodeResult.RUNNING;
|
||||
}
|
||||
|
||||
public override ProgramNode Duplicate()
|
||||
{
|
||||
ExploreNode duplicate = new ExploreNode
|
||||
{
|
||||
targetPosition = targetPosition
|
||||
};
|
||||
return duplicate;
|
||||
}
|
||||
|
||||
public override void ReadParameters(NodeDisplay display)
|
||||
{
|
||||
//Currently does nothing
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1 @@
|
||||
uid://cj4t6w5rd0cv2
|
||||
Reference in New Issue
Block a user