Big project cleanup with overhaul of file responsibilities (KISS) and code (DRY, YAGNI)
This commit is contained in:
@@ -3,79 +3,98 @@ using Godot;
|
||||
|
||||
public class MoveNode : ProgramNode
|
||||
{
|
||||
public Vector3 startPosition;
|
||||
public Vector3I targetPosition;
|
||||
public List<Vector3> pathPoints;
|
||||
public MoveNode()
|
||||
{
|
||||
DisplayText = "Move";
|
||||
}
|
||||
public override NodeResult Execute(Robot robot, double delta)
|
||||
{
|
||||
Vector3I closestPosition = Pathfinding.GetClosestStartPoint(robot.Position);
|
||||
pathPoints ??= new List<Vector3>(Pathfinding.GetPath(closestPosition, targetPosition));
|
||||
public Vector3 startPosition;
|
||||
public Vector3I targetPosition;
|
||||
public List<Vector3> pathPoints;
|
||||
|
||||
if (pathPoints.Count <= 0)
|
||||
{
|
||||
if ((closestPosition - targetPosition).Length() == 0)
|
||||
{
|
||||
lastExecutionMessage = "";
|
||||
return NodeResult.SUCCESS;
|
||||
}
|
||||
lastExecutionMessage = "No path available";
|
||||
return NodeResult.FAILURE;
|
||||
}
|
||||
public MoveNode()
|
||||
{
|
||||
DisplayText = "Move";
|
||||
}
|
||||
|
||||
startPosition = robot.Position;
|
||||
Vector3 target = pathPoints[0] - startPosition;
|
||||
float distance = target.Length();
|
||||
|
||||
float movementSpeed = robot.GetMovementSpeed();
|
||||
public override NodeResult Execute(Robot robot, double delta)
|
||||
{
|
||||
Vector3I closestPosition = Pathfinding.GetClosestStartPoint(robot.Position);
|
||||
if (pathPoints == null)
|
||||
{
|
||||
pathPoints = new List<Vector3>(Pathfinding.GetPath(closestPosition, 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)
|
||||
{
|
||||
if ((closestPosition - targetPosition).Length() == 0)
|
||||
{
|
||||
lastExecutionMessage = "";
|
||||
return NodeResult.SUCCESS;
|
||||
}
|
||||
|
||||
pathPoints.Remove(pathPoints[0]);
|
||||
if (pathPoints.Count <= 0)
|
||||
{
|
||||
pathPoints = null;
|
||||
lastExecutionMessage = "";
|
||||
return NodeResult.SUCCESS;
|
||||
}
|
||||
lastExecutionMessage = "No path available";
|
||||
return NodeResult.FAILURE;
|
||||
}
|
||||
|
||||
lastExecutionMessage = "";
|
||||
return NodeResult.RUNNING;
|
||||
}
|
||||
return MoveAlongPath(robot, delta);
|
||||
}
|
||||
|
||||
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;
|
||||
private NodeResult MoveAlongPath(Robot robot, double delta)
|
||||
{
|
||||
startPosition = robot.Position;
|
||||
Vector3 target = pathPoints[0] - startPosition;
|
||||
float distance = target.Length();
|
||||
float movementSpeed = robot.GetMovementSpeed();
|
||||
|
||||
return NodeResult.RUNNING;
|
||||
}
|
||||
if (distance < 0.1f * Mathf.Sqrt(movementSpeed))
|
||||
{
|
||||
return FinishCurrentStep(robot);
|
||||
}
|
||||
|
||||
public override ProgramNode Duplicate()
|
||||
{
|
||||
MoveNode duplicate = new MoveNode
|
||||
{
|
||||
targetPosition = targetPosition
|
||||
};
|
||||
return duplicate;
|
||||
}
|
||||
Vector3 direction = target / distance;
|
||||
RotateRobot(robot, direction);
|
||||
robot.GlobalPosition += direction * (float)delta * movementSpeed;
|
||||
|
||||
public override string Save()
|
||||
{
|
||||
return $"Name: {DisplayText}, Position: ({targetPosition.X}|{targetPosition.Y}|{targetPosition.Z})";
|
||||
}
|
||||
return NodeResult.RUNNING;
|
||||
}
|
||||
|
||||
private NodeResult FinishCurrentStep(Robot robot)
|
||||
{
|
||||
robot.Position = pathPoints[0];
|
||||
VisitCurrentTile(robot);
|
||||
pathPoints.RemoveAt(0);
|
||||
lastExecutionMessage = "";
|
||||
|
||||
if (pathPoints.Count > 0) return NodeResult.RUNNING;
|
||||
|
||||
pathPoints = null;
|
||||
return NodeResult.SUCCESS;
|
||||
}
|
||||
|
||||
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 MoveNode
|
||||
{
|
||||
targetPosition = targetPosition
|
||||
};
|
||||
}
|
||||
|
||||
public override string Save()
|
||||
{
|
||||
return $"Name: {DisplayText}, Position: ({targetPosition.X}|{targetPosition.Y}|{targetPosition.Z})";
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user