Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- */
- using System;
- using System.Collections.Generic;
- using Godot;
- using levels;
- namespace Graph
- {
- public partial class TheGraphSimply : Node2D
- {
- string abs_name = "graph_node";
- Node2D abs_rootNode;
- public string name;
- public static List<TheGraphSimply> graph;
- public static TheGraphSimply headNode;
- public Node2D[] arrRef;
- public Node2D levelNode;
- string[] verties = { "balcony", "entrance", "kitchen", "mainroom", "room_1" };
- Dictionary<string, int> myDictionary;
- PackedScene scene;
- public string[] Con = new string[4]; // Ensure that this array has enough elements
- [Export] string[] arr;
- public override void _Ready()
- {
- GD.Print("LOLLL");
- Console.WriteLine("LOL");
- abs_rootNode = this;
- headNode = null;
- graph = new List<TheGraphSimply>(verties.Length); // Initialize graph with capacity for all nodes
- // Initialize the dictionary with indices for vertices
- myDictionary = new Dictionary<string, int>
- {
- { "balcony", 0 },
- { "entrance", 1 },
- { "kitchen", 2 },
- { "mainroom", 3 },
- { "room_1", 4 }
- };
- // Initialize graph with null elements
- for (int i = 0; i < verties.Length; i++)
- {
- graph.Add(null); // Add null initially
- }
- // Now for vertices initialization in the graph
- createGraph();
- // Print all children for debugging
- PrintAllChildren();
- }
- public void createGraph()
- {
- for (int i = 0; i < verties.Length; i++)
- {
- int index = myDictionary[verties[i]];
- GD.Print("Index: " + index);
- TheGraphSimply node = graph[index];
- if (node == null)
- {
- createLevel(verties[i]); // Create level if the node is not yet initialized
- }
- // Ensure Con array has proper length, and process each connected node
- if (graph[i] != null && graph[i].Con != null)
- {
- for (int j = 0; j < graph[i].Con.Length; j++)
- {
- GD.Print("Processing connection: " + graph[i].Con[j]);
- if (!string.IsNullOrEmpty(graph[i].Con[j]))
- {
- string connectedVertex = graph[i].Con[j];
- int connectedIndex = myDictionary[connectedVertex];
- TheGraphSimply node2 = graph[connectedIndex];
- if (node2 == null)
- {
- createLevel(connectedVertex);
- }
- if (arrRef != null && arrRef.Length > j)
- {
- arrRef[j] = node2; // Ensure arrRef is not null and has enough space
- }
- }
- else
- {
- GD.Print("Empty or null string in Con array");
- }
- }
- }
- }
- }
- void createLevel(string input)
- {
- string name = input.ToLower();
- switch (name)
- {
- case "kitchen":
- createAndInstantiateLevel<Kitchen>(name);
- break;
- case "mainroom":
- createAndInstantiateLevel<Mainroom>(name);
- break;
- case "balcony":
- createAndInstantiateLevel<Balcony>(name);
- break;
- case "entrance":
- createAndInstantiateLevel<Entrance>(name);
- break;
- case "room_1":
- createAndInstantiateLevel<Room1>(name);
- break;
- default:
- GD.Print("Invalid input for level creation: " + name);
- break;
- }
- }
- private void createAndInstantiateLevel<T>(string name) where T : TheGraphSimply, new()
- {
- GD.Print("Creating level: " + name);
- // Load the scene for the level
- string scenePath = "res://" + name + ".tscn";
- scene = (PackedScene)GD.Load(scenePath);
- if (scene != null)
- {
- Node2D node = (Node2D)scene.Instantiate();
- AddChild(node); // Add scene to the graph
- int index = myDictionary[name];
- graph[index] = new T(); // Assign a new level instance to the graph
- // Update arr and set levelNode
- arr[index] = graph[index].name;
- if (node is Node2D)
- {
- graph[index].levelNode = GetLastChild();
- }
- else
- {
- GD.Print("Error: Child is not of proper file format");
- }
- }
- else
- {
- GD.Print("Error: Scene " + scenePath + " is null");
- }
- }
- public Node2D GetLastChild()
- {
- int childCount = GetChildCount();
- if (childCount > 0)
- {
- return (Node2D)GetChild(childCount - 1); // Get the last child
- }
- return null; // Return null if there are no children
- }
- public void PrintAllChildren()
- {
- int childCount = GetChildCount();
- GD.Print($"Node {Name} has {childCount} children:");
- for (int i = 0; i < childCount; i++)
- {
- Node child = GetChild(i);
- GD.Print("Child name: " + child.Name);
- }
- }
- public override void _Process(double delta)
- {
- // Traversal or other logic can go here
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement