mirror of
https://gitlab.com/MaddoScientisto/cirnogodot.git
synced 2026-06-01 10:55:35 +00:00
94 lines
No EOL
3.1 KiB
C#
94 lines
No EOL
3.1 KiB
C#
using System;
|
|
using System.Linq;
|
|
using Cirno.Scripts.Resources;
|
|
using Cirno.Scripts.Resources.Roguelite;
|
|
using Godot;
|
|
using Godot.Collections;
|
|
|
|
namespace Cirno.Scripts.Controllers;
|
|
|
|
public partial class RogueliteRoom : Node2D
|
|
{
|
|
[Export] public RogueliteRoomResource RoomResource { get; set; }
|
|
|
|
public Vector2I GridPosition { get; set; } // Set by dungeon manager
|
|
|
|
[Export] public PackedScene DoorPrefab { get; private set; }
|
|
|
|
private static readonly Dictionary<string, Vector2I> DirectionMap = new()
|
|
{
|
|
{ "North", new Vector2I(0, -1) },
|
|
{ "South", new Vector2I(0, 1) },
|
|
{ "East", new Vector2I(1, 0) },
|
|
{ "West", new Vector2I(-1, 0) },
|
|
};
|
|
|
|
private Array<EnemyResource> SpawnableEnemies => RoomResource.SpawnableEnemies;
|
|
|
|
public RogueliteRoom Spawn(Func<Vector2I, bool> connectionChecker)
|
|
{
|
|
SpawnEnemies();
|
|
HandleDoors(connectionChecker);
|
|
return this;
|
|
}
|
|
|
|
private void HandleDoors(Func<Vector2I, bool> connectionChecker)
|
|
{
|
|
if (!HasNode("Doors")) return;
|
|
var doorsNode = GetNode("Doors");
|
|
|
|
foreach (Node child in doorsNode.GetChildren())
|
|
{
|
|
if (child is not DoorMarker marker) continue;
|
|
|
|
var baseDir = marker.GetWorldDirection();
|
|
|
|
// WallIndex determines the offset *along* the edge of the room
|
|
Vector2I offset = marker.Direction switch
|
|
{
|
|
RoomDirection.North or RoomDirection.South => new Vector2I(marker.WallIndex, 0),
|
|
RoomDirection.East or RoomDirection.West => new Vector2I(0, marker.WallIndex),
|
|
_ => Vector2I.Zero
|
|
};
|
|
|
|
// Combine GridPosition + offset to locate where this door aligns
|
|
Vector2I doorEdge = GridPosition + offset;
|
|
Vector2I neighborPos = doorEdge + baseDir;
|
|
|
|
bool connected = connectionChecker.Invoke(neighborPos);
|
|
|
|
if (!connected) continue;
|
|
|
|
var door = this.CreateChild<Door>(DoorPrefab, marker.GlobalPosition);
|
|
|
|
door.State = DoorState.Open;
|
|
|
|
// PackedScene prefab = hasConnection
|
|
// ? GD.Load<PackedScene>("res://Prefabs/Door.tscn")
|
|
// : GD.Load<PackedScene>("res://Prefabs/Wall.tscn");
|
|
//
|
|
// var instance = prefab.Instantiate<Node2D>();
|
|
// AddChild(instance);
|
|
// instance.GlobalPosition = ((Node2D)child).GlobalPosition;
|
|
// instance.Rotation = ((Node2D)child).GlobalRotation;
|
|
}
|
|
}
|
|
|
|
private void SpawnEnemies()
|
|
{
|
|
if (SpawnableEnemies is null || !SpawnableEnemies.Any()) return;
|
|
|
|
var enemySpawners = this.GetNode("EnemySpawners").GetChildren().Cast<Marker2D>();
|
|
|
|
foreach (var spawner in enemySpawners)
|
|
{
|
|
|
|
var index = GD.RandRange(0, SpawnableEnemies.Count - 1);
|
|
|
|
var e = SpawnableEnemies[index];
|
|
|
|
var enemyScene = GD.Load<PackedScene>(e.PrefabPath);
|
|
var spawnedEnemy = spawner.CreateChild<Node2D>(enemyScene);
|
|
}
|
|
}
|
|
} |