67 lines
2.1 KiB
C#
67 lines
2.1 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using Godot;
|
|
|
|
public class MoveNode : ProgramNode
|
|
{
|
|
public Vector3 startPosition;
|
|
public Vector3I targetPosition;
|
|
public List<Vector3> pathPoints;
|
|
public MoveNode()
|
|
{
|
|
DisplayText = "Move";
|
|
}
|
|
public override bool Execute(Robot robot, double delta)
|
|
{
|
|
pathPoints ??= [.. Pathfinding.GetPath(Pathfinding.GetClosestStartPoint(robot.Position), targetPosition)];
|
|
|
|
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);
|
|
GameData.map[mapIndex.Y].tiles[mapIndex.X, mapIndex.Z].wasVisited = true;
|
|
pathPoints.Remove(pathPoints[0]);
|
|
if (pathPoints.Count <= 0)
|
|
{
|
|
pathPoints = null;
|
|
return true;
|
|
}
|
|
|
|
return false;
|
|
}
|
|
|
|
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 false;
|
|
}
|
|
|
|
public override void ReadParameters(NodeDisplay display)
|
|
{
|
|
HBoxContainer valueContainer = display.GetNode<HBoxContainer>("./EditorDisplay/HBoxContainer/");
|
|
|
|
GD.Print(valueContainer.GetNode<SpinBox>("./CoordinateX").Value);
|
|
int posX = (int)valueContainer.GetNode<SpinBox>("./CoordinateX").Value;
|
|
int posY = (int)valueContainer.GetNode<SpinBox>("./CoordinateY").Value;
|
|
int posZ = (int)valueContainer.GetNode<SpinBox>("./CoordinateZ").Value;
|
|
targetPosition = new Vector3I(posX, posY, posZ);
|
|
}
|
|
|
|
public override ProgramNode Duplicate()
|
|
{
|
|
MoveNode duplicate = new MoveNode
|
|
{
|
|
targetPosition = targetPosition
|
|
};
|
|
return duplicate;
|
|
}
|
|
} |