Added resources to the game and prepared scripts for later usage

This commit is contained in:
=
2026-04-29 18:03:43 +02:00
parent 253c7d9f89
commit c54ff3088e
15 changed files with 102 additions and 56 deletions
+10 -7
View File
@@ -5,7 +5,6 @@ using System.Linq;
using static WFC;
public partial class Layer : Node3D
{
Random rand = new Random();
private Node3D decorationRoot;
public Tile[,] tiles;
int layerSize;
@@ -14,10 +13,12 @@ public partial class Layer : Node3D
bool updateFailed = false;
public bool hasContentGenerated = false;
public Vector2I gateCoordinate;
public List<string> currentResources;
// Called when the node enters the scene tree for the first time.
public override void _Ready()
{
currentResources = new();
decorationRoot = new Node3D
{
Name = "Decorations"
@@ -28,6 +29,7 @@ public partial class Layer : Node3D
// Called every frame. 'delta' is the elapsed time since the previous frame.
public override void _Process(double delta)
{
}
public void ClearDecorations()
@@ -62,14 +64,14 @@ public partial class Layer : Node3D
{
Vector3 position;
float offsetX;
float offsetY = level * 4 * -1;
float offsetY = level * GameData.tileHeight * -1;
float offsetZ;
for (int x = 0; x < layerSize; x++)
{
offsetX = x * 6;
offsetX = x * GameData.tileWidth;
for (int y = 0; y < layerSize; y++)
{
offsetZ = y * 6;
offsetZ = y * GameData.tileWidth;
position = new Vector3(offsetX, offsetY, offsetZ);
tile = new Tile();
tile.SetMeshes(tileMeshes);
@@ -120,7 +122,7 @@ public partial class Layer : Node3D
if (possibilities.Count == 0)
continue;
tile.Collapse(possibilities[rand.Next(possibilities.Count)]);
tile.Collapse(possibilities[GameData.rand.Next(possibilities.Count)]);
Propagate(new Vector2I(x, z));
}
}
@@ -136,14 +138,15 @@ public partial class Layer : Node3D
int posX, posY;
while (true)
{
posX = rand.Next(layerSize);
posY = rand.Next(layerSize);
posX = GameData.rand.Next(layerSize);
posY = GameData.rand.Next(layerSize);
if (tiles[posX, posY].collapsedMesh != null) continue;
if (tiles[posX, posY].tileMeshes.ContainsKey("gate"))
{
tiles[posX, posY].Collapse("gate");
gateCoordinate = new Vector2I(posX, posY);
GD.Print(gateCoordinate);
Propagate(gateCoordinate);
break;
}
@@ -0,0 +1,29 @@
using System.Collections.Generic;
using System.Linq;
using Godot;
public class ResourceDistributor
{
public static List<string> resourceNames = new()
{
"Iron ore",
"Stone",
"Copper ore",
"Spiderweb",
"Mushroom",
"Tin ore"
};
public static string GetResource(List<string> current)
{
List<string> diff = resourceNames.Except(current).ToList();
if (diff.Count <= 0)
{
return resourceNames[GameData.rand.Next(resourceNames.Count)];
}
else
{
return diff[GameData.rand.Next(diff.Count)];
}
}
}
@@ -0,0 +1 @@
uid://fao1lrpbrhuc
+2 -2
View File
@@ -7,12 +7,12 @@ public partial class Tile
{
public Dictionary<string, MeshInstance3D> tileMeshes;
public string collapsedMesh;
Random rand = new Random();
public Vector3 Position;
public Vector2I GridPosition;
public Node3D ContentNode;
public bool containsLight, containsDecoration, containsResource;
public GameResource resource;
public void SetMeshes(Dictionary<string, MeshInstance3D> tileMeshes)
@@ -38,7 +38,7 @@ public partial class Tile
totalWeight += WFC.weights[tile];
}
float r = (float)(rand.NextDouble() * totalWeight);
float r = (float)(GameData.rand.NextDouble() * totalWeight);
float cumulative = 0f;
+1 -2
View File
@@ -6,7 +6,6 @@ using Godot;
public class WFC
{
public static Dictionary<string, Dictionary<Direction, List<string>>> adjacency = new Dictionary<string, Dictionary<Direction, List<string>>>();
public static Random rand = new Random();
public enum Direction
{
Backward,
@@ -217,7 +216,7 @@ public class WFC
while (true)
{
if (toCheck.Count <= 0) break;
int index = rand.Next(toCheck.Count);
int index = GameData.rand.Next(toCheck.Count);
position = toCheck[index];
toCheck[index] = toCheck[^1];
toCheck.RemoveAt(toCheck.Count - 1);
+2 -1
View File
@@ -6,7 +6,6 @@ using static GameData;
public partial class World : Node3D
{
Random rand = new Random();
public Dictionary<string, MeshInstance3D> tileMeshes;
public Dictionary<string, MeshInstance3D> contentMeshes;
public Dictionary<string, List<Placeholder>> tilePlaceholders;
@@ -179,6 +178,8 @@ public partial class World : Node3D
posY = rand.Next(layerSize);
if (layer.tiles[posX, posY].containsResource) continue;
layer.tiles[posX, posY].containsResource = true;
layer.tiles[posX, posY].resource = new GameResource(ResourceDistributor.GetResource(layer.currentResources));
layer.currentResources.Add(layer.tiles[posX, posY].resource.name);
currentResource++;
}
}