Fixed robot save mechanic to include nodes and their states.
This commit is contained in:
@@ -23,6 +23,7 @@ public partial class Robot : Node3D
|
||||
public bool isBroken = false;
|
||||
public string robotType = "iron_robot";
|
||||
public bool showOnMap = true;
|
||||
public List<ProgramNode> nodes = new List<ProgramNode>();
|
||||
|
||||
private RobotTypeStats TypeStats =>
|
||||
GameData.robotStats.RobotTypes.TryGetValue(robotType, out RobotTypeStats stats)
|
||||
@@ -107,6 +108,7 @@ public partial class Robot : Node3D
|
||||
programStartNode = nodes[0];
|
||||
currentNode = nodes[0];
|
||||
currentMessage = "";
|
||||
this.nodes = nodes;
|
||||
}
|
||||
|
||||
public void StopExecution(string message)
|
||||
@@ -173,7 +175,11 @@ public partial class Robot : Node3D
|
||||
Heat = heat,
|
||||
Maintenance = maintenance,
|
||||
IsCoolingDown = isCoolingDown,
|
||||
IsBroken = isBroken
|
||||
IsBroken = isBroken,
|
||||
RunningProgramNodes = CreateProgramSaveData(),
|
||||
ProgramStartNode = programStartNode?.EditorNodeId,
|
||||
CurrentNode = currentNode?.EditorNodeId,
|
||||
IsExecuting = isExecuting
|
||||
};
|
||||
}
|
||||
|
||||
@@ -188,6 +194,7 @@ public partial class Robot : Node3D
|
||||
maintenance = saveData.Maintenance;
|
||||
isCoolingDown = saveData.IsCoolingDown;
|
||||
isBroken = saveData.IsBroken;
|
||||
LoadProgramState(saveData);
|
||||
}
|
||||
|
||||
private bool CanExecute(double delta)
|
||||
@@ -283,4 +290,78 @@ public partial class Robot : Node3D
|
||||
isCoolingDown = false;
|
||||
}
|
||||
}
|
||||
|
||||
private List<NodeSaveData> CreateProgramSaveData()
|
||||
{
|
||||
List<NodeSaveData> result = new List<NodeSaveData>();
|
||||
if (nodes == null) return result;
|
||||
|
||||
foreach (ProgramNode node in nodes)
|
||||
{
|
||||
if (node == null) continue;
|
||||
result.Add(node.CreateSaveData());
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
private void LoadProgramState(RobotSaveData saveData)
|
||||
{
|
||||
nodes = new List<ProgramNode>();
|
||||
programStartNode = null;
|
||||
currentNode = null;
|
||||
isExecuting = false;
|
||||
|
||||
if (saveData.RunningProgramNodes == null || saveData.RunningProgramNodes.Count <= 0)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
Dictionary<string, ProgramNode> loadedNodes = new Dictionary<string, ProgramNode>();
|
||||
foreach (NodeSaveData nodeSaveData in saveData.RunningProgramNodes)
|
||||
{
|
||||
ProgramNode node = ProgramNode.CreateFromSaveData(nodeSaveData);
|
||||
if (node == null) continue;
|
||||
|
||||
node.Load(nodeSaveData);
|
||||
nodes.Add(node);
|
||||
|
||||
if (!string.IsNullOrEmpty(node.EditorNodeId) && !loadedNodes.ContainsKey(node.EditorNodeId))
|
||||
{
|
||||
loadedNodes.Add(node.EditorNodeId, node);
|
||||
}
|
||||
}
|
||||
|
||||
foreach (NodeSaveData nodeSaveData in saveData.RunningProgramNodes)
|
||||
{
|
||||
if (string.IsNullOrEmpty(nodeSaveData.EditorNodeId)) continue;
|
||||
if (!loadedNodes.ContainsKey(nodeSaveData.EditorNodeId)) continue;
|
||||
|
||||
loadedNodes[nodeSaveData.EditorNodeId].RestoreConnections(nodeSaveData, loadedNodes);
|
||||
}
|
||||
|
||||
programStartNode = GetLoadedNode(saveData.ProgramStartNode, loadedNodes);
|
||||
if (programStartNode == null && nodes.Count > 0)
|
||||
{
|
||||
programStartNode = nodes[0];
|
||||
}
|
||||
|
||||
currentNode = GetLoadedNode(saveData.CurrentNode, loadedNodes);
|
||||
if (currentNode == null)
|
||||
{
|
||||
currentNode = programStartNode;
|
||||
}
|
||||
|
||||
isExecuting = saveData.IsExecuting && currentNode != null;
|
||||
}
|
||||
|
||||
private ProgramNode GetLoadedNode(
|
||||
string nodeId,
|
||||
Dictionary<string, ProgramNode> loadedNodes
|
||||
)
|
||||
{
|
||||
if (string.IsNullOrEmpty(nodeId)) return null;
|
||||
if (!loadedNodes.ContainsKey(nodeId)) return null;
|
||||
|
||||
return loadedNodes[nodeId];
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user