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
+7 -38
View File
@@ -11,7 +11,14 @@ public partial class Camera3d : Camera3D
public override void _Process(double delta)
{
if (canMove) MoveCamera(delta);
if (!canMove) return;
}
public void MoveCamera(double delta)
{
float d = (float)delta;
var rotation = RotationDegrees;
@@ -37,42 +44,4 @@ public partial class Camera3d : Camera3D
Position = new Vector3(Position.X, 10 - visibleLayer * 4, Position.Z);
}
}
public override void _Input(InputEvent @event)
{
if (@event is InputEventMouseButton mouse &&
mouse.ButtonIndex == MouseButton.Left &&
mouse.Pressed)
{
var camera = GetViewport().GetCamera3D();
Vector2 mousePos = mouse.Position;
Vector3 from = camera.ProjectRayOrigin(mousePos);
Vector3 to = from + camera.ProjectRayNormal(mousePos) * 1000f;
var spaceState = GetWorld3D().DirectSpaceState;
var query = PhysicsRayQueryParameters3D.Create(from, to);
query.CollisionMask = 1 << 1;
var result = spaceState.IntersectRay(query);
if (result.Count > 0)
{
Variant colliderVariant = result["collider"];
Node hit = colliderVariant.As<Node>();
GD.Print($"Clicked robot: {hit.Name}");
// Optional: call method on robot
if (hit is Node robot)
{
robot.Call("OnClicked");
}
}
}
}
}
+5
View File
@@ -0,0 +1,5 @@
using Godot;
public class Building
{
}
+1
View File
@@ -0,0 +1 @@
uid://cl2yvllo35qbb
+30
View File
@@ -0,0 +1,30 @@
using Godot;
public class GameResource
{
int maxAmount;
int currentAmount;
bool isEndless;
float extractionSpeed;
public string name;
public GameResource(string name)
{
this.name = name;
maxAmount = GameData.rand.Next(1000, 10000);
currentAmount = maxAmount;
isEndless = false;
extractionSpeed = 1f;
}
public bool Extract()
{
if(isEndless) return true;
if (currentAmount > 0)
{
currentAmount--;
return true;
}
return false;
}
}
+1
View File
@@ -0,0 +1 @@
uid://bh4m8ufiv5kt8
+5
View File
@@ -0,0 +1,5 @@
using Godot;
public class Item
{
}
+1
View File
@@ -0,0 +1 @@
uid://dpq18vk7hbakh
+6
View File
@@ -1,8 +1,10 @@
using System;
using System.Collections.Generic;
using Godot;
public partial class GameData
{
public static Random rand = new Random(seed);
public static Layer[] map;
//Current layer the player wants to see
public static int currentLayer = 0;
@@ -13,6 +15,8 @@ public partial class GameData
public static int maxRobotCount = 1000;
public static List<Robot> robots = new List<Robot>();
public static float robotSpeed = 5f;
public static float tileWidth = 6;
public static float tileHeight = 4;
//--- PLAYER ADJUSTABLE VALUES ---
//Color used in primary objects (e.g. Robots)
@@ -23,5 +27,7 @@ public partial class GameData
public static int ruinSize = 10;
//Width+Height of layers
public static int layerSize = 20;
//Seed used for all random generation except WFC
public static int seed = 12345;
}
-5
View File
@@ -33,11 +33,6 @@ public partial class Robot : Node3D
}
public void OnClicked()
{
GetNode<UIHandler>("/root/Main/CanvasLayer/UIHandler").ShowCodingWindow(this);
}
public void SetupExecution(List<ProgramNode> nodes)
{
this.nodes = [.. nodes];
+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++;
}
}