Reworked DSL for better architecture, implemented wall-phasing movement.
This commit is contained in:
+15
-17
@@ -7,6 +7,7 @@ public partial class CodingWindow : PanelContainer
|
||||
[Export] VBoxContainer codeBlocks;
|
||||
[Export] VBoxContainer editorWindow;
|
||||
public Dictionary<ProgramNode, PackedScene> DSLNodes;
|
||||
Robot robot;
|
||||
|
||||
|
||||
// Called when the node enters the scene tree for the first time.
|
||||
@@ -19,11 +20,13 @@ public partial class CodingWindow : PanelContainer
|
||||
// Called every frame. 'delta' is the elapsed time since the previous frame.
|
||||
public override void _Process(double delta)
|
||||
{
|
||||
//TODO: If robot that was pressed has a script, load script
|
||||
if (Input.IsActionJustPressed("codingwindow"))
|
||||
{
|
||||
Visible = !Visible;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public void ShowWindow(Robot robot)
|
||||
{
|
||||
Visible = true;
|
||||
this.robot = robot;
|
||||
}
|
||||
|
||||
//Move, Harvest, Craft
|
||||
@@ -57,23 +60,18 @@ public partial class CodingWindow : PanelContainer
|
||||
|
||||
public void CompileProgram()
|
||||
{
|
||||
bool didCompile;
|
||||
List<ProgramNode> nodes = new List<ProgramNode>();
|
||||
|
||||
for (int i = 0; i < editorWindow.GetChildCount(); i++)
|
||||
{
|
||||
if (i + 1 < editorWindow.GetChildCount())
|
||||
{
|
||||
editorWindow.GetChild<NodeDisplay>(i).node.LinkNode(editorWindow.GetChild<NodeDisplay>(i + 1).node);
|
||||
}
|
||||
editorWindow.GetChild<NodeDisplay>(i).node.ReadParameters(editorWindow.GetChild<NodeDisplay>(i));
|
||||
nodes.Add(editorWindow.GetChild<NodeDisplay>(i).node.Duplicate());
|
||||
if (i != 0)
|
||||
{
|
||||
nodes[i-1].nextNode = nodes[i];
|
||||
}
|
||||
}
|
||||
|
||||
ProgramInterpreter interpreter = new ProgramInterpreter(editorWindow.GetChild<NodeDisplay>(0).node);
|
||||
didCompile = interpreter.Execute(GameData.robots[0]);
|
||||
|
||||
if (!didCompile)
|
||||
{
|
||||
|
||||
}
|
||||
robot.SetupExecution(nodes);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -6,12 +6,12 @@ public class CraftNode : ProgramNode
|
||||
{
|
||||
DisplayText = "Craft";
|
||||
}
|
||||
public override bool Execute(Robot robot)
|
||||
public override bool Execute(Robot robot, double delta)
|
||||
{
|
||||
GD.Print("Craft");
|
||||
if (nextNode != null)
|
||||
{
|
||||
return nextNode.Execute(robot);
|
||||
return nextNode.Execute(robot, delta);
|
||||
}
|
||||
else
|
||||
{
|
||||
@@ -23,4 +23,10 @@ public class CraftNode : ProgramNode
|
||||
{
|
||||
//
|
||||
}
|
||||
|
||||
public override ProgramNode Duplicate()
|
||||
{
|
||||
CraftNode duplicate = new CraftNode();
|
||||
return duplicate;
|
||||
}
|
||||
}
|
||||
@@ -6,12 +6,12 @@ public class HarvestNode : ProgramNode
|
||||
{
|
||||
DisplayText = "Harvest";
|
||||
}
|
||||
public override bool Execute(Robot robot)
|
||||
public override bool Execute(Robot robot, double delta)
|
||||
{
|
||||
GD.Print("Harvest");
|
||||
if (nextNode != null)
|
||||
{
|
||||
return nextNode.Execute(robot);
|
||||
return nextNode.Execute(robot, delta);
|
||||
}
|
||||
else
|
||||
{
|
||||
@@ -23,4 +23,10 @@ public class HarvestNode : ProgramNode
|
||||
{
|
||||
//
|
||||
}
|
||||
|
||||
public override ProgramNode Duplicate()
|
||||
{
|
||||
HarvestNode duplicate = new HarvestNode();
|
||||
return duplicate;
|
||||
}
|
||||
}
|
||||
@@ -8,19 +8,20 @@ public class MoveNode : ProgramNode
|
||||
{
|
||||
DisplayText = "Move";
|
||||
}
|
||||
public override bool Execute(Robot robot)
|
||||
public override bool Execute(Robot robot, double delta)
|
||||
{
|
||||
Vector3 worldTarget = GameData.map[targetPosition.Y].tiles[targetPosition.X, targetPosition.Z].Position;
|
||||
GD.Print(startPosition + "/" + worldTarget);
|
||||
startPosition = robot.Position;
|
||||
GD.Print(targetPosition);
|
||||
robot.Position = GameData.map[targetPosition.Y].tiles[targetPosition.X, targetPosition.Z].Position;
|
||||
if (nextNode != null)
|
||||
{
|
||||
return nextNode.Execute(robot);
|
||||
}
|
||||
else
|
||||
Vector3 direction = worldTarget - startPosition;
|
||||
robot.Translate(direction.Normalized() * (float)delta * GameData.robotSpeed);
|
||||
float distance = direction.Length();
|
||||
if (distance < 0.1f)
|
||||
{
|
||||
robot.Position = worldTarget;
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
public override void ReadParameters(NodeDisplay display)
|
||||
@@ -34,5 +35,12 @@ public class MoveNode : ProgramNode
|
||||
targetPosition = new Vector3I(posX, posY, posZ);
|
||||
}
|
||||
|
||||
|
||||
public override ProgramNode Duplicate()
|
||||
{
|
||||
MoveNode duplicate = new MoveNode
|
||||
{
|
||||
targetPosition = targetPosition
|
||||
};
|
||||
return duplicate;
|
||||
}
|
||||
}
|
||||
@@ -2,17 +2,10 @@ using Godot;
|
||||
|
||||
public abstract class ProgramNode
|
||||
{
|
||||
protected ProgramNode nextNode;
|
||||
public ProgramNode nextNode;
|
||||
public string DisplayText;
|
||||
public ProgramNode()
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
public abstract bool Execute(Robot robot);
|
||||
public abstract bool Execute(Robot robot, double delta);
|
||||
public abstract void ReadParameters(NodeDisplay display);
|
||||
public void LinkNode(ProgramNode nextNode)
|
||||
{
|
||||
this.nextNode = nextNode;
|
||||
}
|
||||
public abstract ProgramNode Duplicate();
|
||||
}
|
||||
@@ -1,18 +0,0 @@
|
||||
using System.Collections.Generic;
|
||||
using Godot;
|
||||
|
||||
public class ProgramInterpreter
|
||||
{
|
||||
ProgramNode startNode;
|
||||
public ProgramInterpreter(ProgramNode startNode)
|
||||
{
|
||||
this.startNode = startNode;
|
||||
}
|
||||
|
||||
public bool Execute(Robot robot)
|
||||
{
|
||||
return startNode.Execute(robot);
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
@@ -1 +0,0 @@
|
||||
uid://hui5wolo5rbr
|
||||
@@ -12,6 +12,7 @@ public partial class GameData
|
||||
public static bool canMove = true;
|
||||
public static int maxRobotCount = 1000;
|
||||
public static List<Robot> robots = new List<Robot>();
|
||||
public static float robotSpeed = 5f;
|
||||
|
||||
//--- PLAYER ADJUSTABLE VALUES ---
|
||||
//Color used in primary objects (e.g. Robots)
|
||||
|
||||
@@ -6,7 +6,6 @@ public partial class UIHandler : Control
|
||||
{
|
||||
[Export] CodingWindow codingWindow;
|
||||
[Export] RobotList robotList;
|
||||
[Export] PanelContainer robotNaming;
|
||||
[Export] Information information;
|
||||
[Export] Camera3D mainCam;
|
||||
public override void _Ready()
|
||||
@@ -19,7 +18,8 @@ public partial class UIHandler : Control
|
||||
{
|
||||
robotList.OnRobotJumpTo += (robot) =>
|
||||
{
|
||||
mainCam.Position = new Vector3(robot.Position.X, mainCam.Position.Y, robot.Position.Z);
|
||||
mainCam.Position = new Vector3(robot.Position.X, mainCam.Position.Y, robot.Position.Z + 3f);
|
||||
ShowCodingWindow(robot);
|
||||
};
|
||||
}
|
||||
|
||||
@@ -29,25 +29,8 @@ public partial class UIHandler : Control
|
||||
LightHandler.RedrawLights(color);
|
||||
}
|
||||
|
||||
public void ShowNamingPopup(Robot robot)
|
||||
public void ShowCodingWindow(Robot robot)
|
||||
{
|
||||
robotNaming.Visible = true;
|
||||
GameData.canMove = false;
|
||||
LineEdit name = robotNaming.GetNode<LineEdit>("./VBoxContainer/LineEdit");
|
||||
name.Text = robot.Name;
|
||||
Button button = robotNaming.GetNode<Button>("./VBoxContainer/Button");
|
||||
|
||||
Action handler = null;
|
||||
handler = () =>
|
||||
{
|
||||
robot.Name = name.Text;
|
||||
name.Text = "";
|
||||
robotNaming.Visible = false;
|
||||
GameData.canMove = true;
|
||||
button.ButtonUp -= handler;
|
||||
};
|
||||
|
||||
button.ButtonUp += handler;
|
||||
codingWindow.ShowWindow(robot);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
+25
-4
@@ -1,18 +1,32 @@
|
||||
using System.Collections.Generic;
|
||||
using System.Threading.Tasks;
|
||||
using Godot;
|
||||
|
||||
public partial class Robot : Node3D
|
||||
{
|
||||
public ProgramInterpreter interpreter;
|
||||
List<ProgramNode> nodes;
|
||||
bool isExecuting = false;
|
||||
ProgramNode currentNode;
|
||||
|
||||
public override void _Ready()
|
||||
{
|
||||
|
||||
|
||||
}
|
||||
|
||||
public override void _Process(double delta)
|
||||
{
|
||||
|
||||
if (isExecuting)
|
||||
{
|
||||
if (currentNode.Execute(this, delta))
|
||||
{
|
||||
currentNode = currentNode.nextNode;
|
||||
if (currentNode == null)
|
||||
{
|
||||
isExecuting = false;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
public void Move()
|
||||
{
|
||||
@@ -21,7 +35,14 @@ public partial class Robot : Node3D
|
||||
|
||||
public void OnClicked()
|
||||
{
|
||||
GetNode<UIHandler>("/root/Main/CanvasLayer/UIHandler").ShowNamingPopup(this);
|
||||
GetNode<UIHandler>("/root/Main/CanvasLayer/UIHandler").ShowCodingWindow(this);
|
||||
}
|
||||
|
||||
public void SetupExecution(List<ProgramNode> nodes)
|
||||
{
|
||||
this.nodes = [.. nodes];
|
||||
isExecuting = true;
|
||||
currentNode = nodes[0];
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -40,6 +40,7 @@ public partial class RobotList : PanelContainer
|
||||
display.OnRobotJumpTo += (robot) =>
|
||||
{
|
||||
EmitSignal(SignalName.OnRobotJumpTo, robot);
|
||||
Visible = false;
|
||||
};
|
||||
robotList.AddChild(display);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user