Big project cleanup with overhaul of file responsibilities (KISS) and code (DRY, YAGNI)
This commit is contained in:
@@ -4,90 +4,127 @@ 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;
|
||||
int layerRange = Math.Max(GameData.lowestLayer, 1);
|
||||
while (true)
|
||||
{
|
||||
targetPosition = new Vector3I(GameData.rand.Next(GameData.layerSize), GameData.rand.Next(layerRange), 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;
|
||||
}
|
||||
}
|
||||
}
|
||||
public Vector3 startPosition;
|
||||
public Vector3I targetPosition;
|
||||
public List<Vector3> pathPoints;
|
||||
|
||||
pathPoints ??= new List<Vector3>(Pathfinding.GetPath(Pathfinding.GetClosestStartPoint(robot.Position), targetPosition));
|
||||
|
||||
if (pathPoints.Count <= 0)
|
||||
{
|
||||
lastExecutionMessage = $"No path available {targetPosition}";
|
||||
return NodeResult.FAILURE;
|
||||
}
|
||||
public ExploreNode()
|
||||
{
|
||||
DisplayText = "Explore";
|
||||
}
|
||||
|
||||
startPosition = robot.Position;
|
||||
Vector3 target = pathPoints[0] - startPosition;
|
||||
float distance = target.Length();
|
||||
public override NodeResult Execute(Robot robot, double delta)
|
||||
{
|
||||
if (pathPoints == null && !TrySelectTarget())
|
||||
{
|
||||
lastExecutionMessage = "No tiles left to explore";
|
||||
return NodeResult.SUCCESS;
|
||||
}
|
||||
|
||||
float movementSpeed = robot.GetMovementSpeed();
|
||||
if (pathPoints == null)
|
||||
{
|
||||
pathPoints = new List<Vector3>(
|
||||
Pathfinding.GetPath(Pathfinding.GetClosestStartPoint(robot.Position), targetPosition)
|
||||
);
|
||||
}
|
||||
|
||||
if (distance < 0.1f * Mathf.Sqrt(movementSpeed))
|
||||
{
|
||||
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.VisitTile();
|
||||
}
|
||||
if (pathPoints.Count <= 0)
|
||||
{
|
||||
lastExecutionMessage = $"No path available {targetPosition}";
|
||||
return NodeResult.FAILURE;
|
||||
}
|
||||
|
||||
pathPoints.Remove(pathPoints[0]);
|
||||
if (pathPoints.Count <= 0)
|
||||
{
|
||||
lastExecutionMessage = "Current exploration finished";
|
||||
pathPoints = null;
|
||||
return NodeResult.RUNNING;
|
||||
}
|
||||
return MoveAlongPath(robot, delta);
|
||||
}
|
||||
|
||||
lastExecutionMessage = "";
|
||||
return NodeResult.RUNNING;
|
||||
}
|
||||
private bool TrySelectTarget()
|
||||
{
|
||||
int safetyCounter = 0;
|
||||
int layerRange = Math.Max(GameData.lowestLayer, 1);
|
||||
int maximumAttempts = (int)Math.Pow(GameData.layerSize, 2) * 2;
|
||||
|
||||
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 * movementSpeed;
|
||||
while (safetyCounter <= maximumAttempts)
|
||||
{
|
||||
targetPosition = new Vector3I(
|
||||
GameData.rand.Next(GameData.layerSize),
|
||||
GameData.rand.Next(layerRange),
|
||||
GameData.rand.Next(GameData.layerSize)
|
||||
);
|
||||
if (!GameData.map[targetPosition.Y].tiles[targetPosition.X, targetPosition.Z].wasVisited)
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
return NodeResult.RUNNING;
|
||||
}
|
||||
safetyCounter++;
|
||||
}
|
||||
|
||||
public override ProgramNode Duplicate()
|
||||
{
|
||||
ExploreNode duplicate = new ExploreNode
|
||||
{
|
||||
targetPosition = targetPosition
|
||||
};
|
||||
return duplicate;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
public override string Save()
|
||||
{
|
||||
return $"Name: {DisplayText}";
|
||||
}
|
||||
private NodeResult MoveAlongPath(Robot robot, double delta)
|
||||
{
|
||||
startPosition = robot.Position;
|
||||
Vector3 target = pathPoints[0] - startPosition;
|
||||
float distance = target.Length();
|
||||
float movementSpeed = robot.GetMovementSpeed();
|
||||
|
||||
if (distance < 0.1f * Mathf.Sqrt(movementSpeed))
|
||||
{
|
||||
return FinishCurrentStep(robot);
|
||||
}
|
||||
|
||||
Vector3 direction = target / distance;
|
||||
RotateRobot(robot, direction);
|
||||
robot.GlobalPosition += direction * (float)delta * movementSpeed;
|
||||
|
||||
return NodeResult.RUNNING;
|
||||
}
|
||||
|
||||
private NodeResult FinishCurrentStep(Robot robot)
|
||||
{
|
||||
robot.Position = pathPoints[0];
|
||||
VisitCurrentTile(robot);
|
||||
pathPoints.RemoveAt(0);
|
||||
|
||||
if (pathPoints.Count > 0)
|
||||
{
|
||||
lastExecutionMessage = "";
|
||||
return NodeResult.RUNNING;
|
||||
}
|
||||
|
||||
lastExecutionMessage = "Current exploration finished";
|
||||
pathPoints = null;
|
||||
return NodeResult.RUNNING;
|
||||
}
|
||||
|
||||
private void VisitCurrentTile(Robot robot)
|
||||
{
|
||||
Vector3I mapIndex = Pathfinding.GetClosestStartPoint(robot.Position);
|
||||
Tile tile = GameData.map[mapIndex.Y].tiles[mapIndex.X, mapIndex.Z];
|
||||
if (!tile.wasVisited)
|
||||
{
|
||||
tile.VisitTile();
|
||||
}
|
||||
}
|
||||
|
||||
private void RotateRobot(Robot robot, Vector3 direction)
|
||||
{
|
||||
Vector3 lookDirection = new Vector3(direction.X, 0, direction.Z);
|
||||
if (lookDirection.Length() <= 0.1f) return;
|
||||
|
||||
robot.LookAt(robot.GlobalPosition + lookDirection, Vector3.Up);
|
||||
}
|
||||
|
||||
public override ProgramNode Duplicate()
|
||||
{
|
||||
return new ExploreNode
|
||||
{
|
||||
targetPosition = targetPosition
|
||||
};
|
||||
}
|
||||
|
||||
public override string Save()
|
||||
{
|
||||
return $"Name: {DisplayText}";
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user