mirror of
https://gitlab.com/MaddoScientisto/cirnogodot.git
synced 2026-06-16 13:33:47 +00:00
Fisher-Yates shuffle
This commit is contained in:
parent
60f755aca5
commit
aa838aeda1
6 changed files with 176 additions and 7 deletions
17
Resources/RogueliteMaps/TestRGMapLarge.tres
Normal file
17
Resources/RogueliteMaps/TestRGMapLarge.tres
Normal file
|
|
@ -0,0 +1,17 @@
|
|||
[gd_resource type="Resource" script_class="RogueliteRoomResource" load_steps=6 format=3 uid="uid://cjtcksew0qy6d"]
|
||||
|
||||
[ext_resource type="Resource" uid="uid://cocl3qontm3be" path="res://Resources/Enemies/Base_Fairy.tres" id="1_hqy32"]
|
||||
[ext_resource type="Resource" uid="uid://cqfyuurvqb8m6" path="res://Resources/Enemies/Base_Fairy_Special.tres" id="2_gpwow"]
|
||||
[ext_resource type="Resource" uid="uid://qbo6avc7x64b" path="res://Resources/Enemies/Fairy_Guard.tres" id="3_m1jol"]
|
||||
[ext_resource type="Resource" uid="uid://cfdvg162u65sr" path="res://Resources/Enemies/Thermathron.tres" id="4_xpv8o"]
|
||||
[ext_resource type="Script" uid="uid://bl2ne8w12e3a" path="res://Scripts/Resources/Roguelite/RogueliteRoomResource.cs" id="5_nda2n"]
|
||||
|
||||
[resource]
|
||||
script = ExtResource("5_nda2n")
|
||||
RoomName = &"Test"
|
||||
Type = 1
|
||||
ScenePath = &"uid://c36nvk7cw3emg"
|
||||
Size = Vector2i(3, 1)
|
||||
DoorGridPositions = Array[Vector2i]([Vector2i(0, -1), Vector2i(0, 1), Vector2i(1, 0), Vector2i(-1, 0)])
|
||||
SpawnableEnemies = Array[Object]([ExtResource("1_hqy32"), ExtResource("2_gpwow"), ExtResource("3_m1jol"), ExtResource("4_xpv8o")])
|
||||
metadata/_custom_type_script = "uid://bl2ne8w12e3a"
|
||||
|
|
@ -4,7 +4,7 @@
|
|||
[ext_resource type="PackedScene" uid="uid://c4pr2707hbeph" path="res://Scenes/Actors/fsm_player.tscn" id="2_3fyis"]
|
||||
[ext_resource type="Resource" uid="uid://6ek4lmtuij4t" path="res://Resources/Maps/Roguelite.tres" id="2_k5t51"]
|
||||
[ext_resource type="Script" uid="uid://bt2qjgnf1wc2r" path="res://Scripts/Controllers/RogueliteRoomManager.cs" id="4_jtlua"]
|
||||
[ext_resource type="Resource" uid="uid://b5x83li01qrav" path="res://Resources/RogueliteMaps/TestRGMap.tres" id="5_gwtv6"]
|
||||
[ext_resource type="Resource" uid="uid://cjtcksew0qy6d" path="res://Resources/RogueliteMaps/TestRGMapLarge.tres" id="5_pfafs"]
|
||||
[ext_resource type="Resource" uid="uid://ly8l7asedjpx" path="res://Resources/RogueliteMaps/TestRGMap2.tres" id="6_gwtv6"]
|
||||
[ext_resource type="Resource" uid="uid://dn3ai56rrxfnk" path="res://Resources/RogueliteMaps/Beginner1.tres" id="7_wbqvu"]
|
||||
[ext_resource type="Resource" uid="uid://cgac12krx7vbf" path="res://Resources/RogueliteMaps/Boss1.tres" id="8_3fyis"]
|
||||
|
|
@ -29,7 +29,7 @@ SpawnMarkers = Dictionary[int, NodePath]({
|
|||
[node name="Maps" type="Node2D" parent="." groups=["navigation_polygon_source_geometry_group"]]
|
||||
process_mode = 1
|
||||
script = ExtResource("4_jtlua")
|
||||
Rooms = Array[Object]([ExtResource("5_gwtv6"), ExtResource("6_gwtv6"), ExtResource("7_wbqvu"), ExtResource("8_3fyis"), ExtResource("9_go1yg")])
|
||||
Rooms = Array[Object]([ExtResource("5_pfafs"), ExtResource("6_gwtv6"), ExtResource("7_wbqvu"), ExtResource("8_3fyis"), ExtResource("9_go1yg"), ExtResource("5_pfafs")])
|
||||
DungeonLength = 4
|
||||
|
||||
[node name="CameraController" type="Camera2D" parent="."]
|
||||
|
|
|
|||
|
|
@ -1,7 +1,7 @@
|
|||
[gd_scene load_steps=9 format=4 uid="uid://da7hmajaaiohm"]
|
||||
|
||||
[ext_resource type="Script" uid="uid://b2j00riayxkit" path="res://Scripts/Controllers/RogueliteRoom.cs" id="1_dm6kf"]
|
||||
[ext_resource type="Resource" uid="uid://b5x83li01qrav" path="res://Resources/RogueliteMaps/TestRGMap.tres" id="2_5s4nm"]
|
||||
[ext_resource type="Resource" uid="uid://cjtcksew0qy6d" path="res://Resources/RogueliteMaps/TestRGMapLarge.tres" id="2_rlq0q"]
|
||||
[ext_resource type="PackedScene" uid="uid://l84on3kv2s52" path="res://Scenes/Door_Horizontal.tscn" id="3_sid7o"]
|
||||
[ext_resource type="PackedScene" uid="uid://b0k2grrc8xp1l" path="res://Scenes/Props/BigTank.tscn" id="4_rlq0q"]
|
||||
[ext_resource type="TileSet" uid="uid://6k28roiljylj" path="res://Tilesets/factory_tileset.tres" id="5_q5p8k"]
|
||||
|
|
@ -18,7 +18,7 @@ source_geometry_mode = 1
|
|||
[node name="Map" type="Node2D"]
|
||||
process_mode = 1
|
||||
script = ExtResource("1_dm6kf")
|
||||
RoomResource = ExtResource("2_5s4nm")
|
||||
RoomResource = ExtResource("2_rlq0q")
|
||||
DoorPrefab = ExtResource("3_sid7o")
|
||||
WallPrefab = ExtResource("4_rlq0q")
|
||||
|
||||
|
|
|
|||
104
Scenes/Maps/RogueliteMaps/RgLarge.tscn
Normal file
104
Scenes/Maps/RogueliteMaps/RgLarge.tscn
Normal file
File diff suppressed because one or more lines are too long
|
|
@ -2,6 +2,7 @@
|
|||
using System.Linq;
|
||||
using Cirno.Scripts.Enums;
|
||||
using Cirno.Scripts.Resources.Roguelite;
|
||||
using Cirno.Scripts.Utils;
|
||||
using Godot;
|
||||
using Godot.Collections;
|
||||
|
||||
|
|
@ -79,6 +80,8 @@ public partial class RogueliteRoomManager : Node2D
|
|||
var starterRoom = Rooms.Where(r => r.Type == RoomType.Starter).PickRandom();
|
||||
SpawnRoom(starterRoom, origin);
|
||||
_mainPath.Add(origin);
|
||||
|
||||
var randomRoomsList = RegularRooms.Shuffle(DungeonLength).ToList();
|
||||
|
||||
var currentPos = origin;
|
||||
Vector2I nextPos;
|
||||
|
|
@ -87,13 +90,13 @@ public partial class RogueliteRoomManager : Node2D
|
|||
{
|
||||
nextPos = currentPos + new Vector2I(0, 1);
|
||||
|
||||
var roomToSpawn = Rooms.Where(x => x.Type == RoomType.Regular).PickRandom();
|
||||
//var roomToSpawn = Rooms.Where(x => x.Type == RoomType.Regular).PickRandom();
|
||||
var roomToSpawn = randomRoomsList[i];
|
||||
|
||||
if (!SpawnRoom(roomToSpawn, nextPos)) break;
|
||||
|
||||
_mainPath.Add(nextPos);
|
||||
|
||||
|
||||
if (roomToSpawn.Size.Y > 1)
|
||||
{
|
||||
nextPos += new Vector2I(0, roomToSpawn.Size.Y - 1);
|
||||
|
|
@ -328,7 +331,7 @@ public partial class RogueliteRoomManager : Node2D
|
|||
// for reference
|
||||
//SpawnRoom(room, origin + (room.Size * new Vector2(i, j) * tileSize));
|
||||
spawnedScene.Spawn();
|
||||
MarkRoom(SpawnOrigin, room.Size, spawnedScene);
|
||||
MarkRoom(gridPos, room.Size, spawnedScene);
|
||||
return true;
|
||||
}
|
||||
|
||||
|
|
|
|||
45
Scripts/Utils/ShuffleExtensions.cs
Normal file
45
Scripts/Utils/ShuffleExtensions.cs
Normal file
|
|
@ -0,0 +1,45 @@
|
|||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using Godot;
|
||||
|
||||
namespace Cirno.Scripts.Utils;
|
||||
|
||||
public static class ShuffleExtensions
|
||||
{
|
||||
//private static Random rng = new Random();
|
||||
|
||||
public static IEnumerable<T> Shuffle<T>(this IEnumerable<T> source, int? desiredLength = null)
|
||||
{
|
||||
if (source == null) throw new ArgumentNullException(nameof(source));
|
||||
|
||||
var list = source.ToList();
|
||||
int originalCount = list.Count;
|
||||
|
||||
if (originalCount == 0) yield break;
|
||||
|
||||
// Repeat elements to meet the desired length
|
||||
if (desiredLength.HasValue && desiredLength > originalCount)
|
||||
{
|
||||
int countToAdd = desiredLength.Value - originalCount;
|
||||
for (int i = 0; i < countToAdd; i++)
|
||||
{
|
||||
list.Add(list[i % originalCount]);
|
||||
}
|
||||
}
|
||||
|
||||
// Fisher-Yates shuffle
|
||||
for (int i = list.Count - 1; i > 0; i--)
|
||||
{
|
||||
//int j = rng.Next(i + 1);
|
||||
int j = GD.RandRange(0, i);
|
||||
(list[i], list[j]) = (list[j], list[i]);
|
||||
}
|
||||
|
||||
// Return only up to desired length if specified
|
||||
foreach (var item in list.Take(desiredLength ?? list.Count))
|
||||
{
|
||||
yield return item;
|
||||
}
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue