|
| 1 | +using System; |
| 2 | +using System.Linq; |
| 3 | +using FunkEngine; |
| 4 | +using Godot; |
| 5 | + |
| 6 | +public partial class StageProducer : Node |
| 7 | +{ |
| 8 | + //Generate a map, starting as a width x height grid, pick a starting spot and do (path) paths from that to the last |
| 9 | + //row, connecting the path, then connect all at the end to the boss room. |
| 10 | + public static RandomNumberGenerator GlobalRng = new RandomNumberGenerator(); |
| 11 | + private ulong _seed; |
| 12 | + private ulong _lastRngState; |
| 13 | + |
| 14 | + private Stages _curStage = Stages.Title; |
| 15 | + private Node _curScene; |
| 16 | + |
| 17 | + private MapGrid _map = new MapGrid(); |
| 18 | + |
| 19 | + public class MapGrid |
| 20 | + { |
| 21 | + private int[,] _map; |
| 22 | + private Room[] _rooms; |
| 23 | + private int _curIdx = 0; |
| 24 | + private int _curRoom = 0; |
| 25 | + |
| 26 | + public class Room |
| 27 | + { |
| 28 | + public Room(int idx, int x, int y) |
| 29 | + { |
| 30 | + Idx = idx; |
| 31 | + X = x; |
| 32 | + Y = y; |
| 33 | + } |
| 34 | + |
| 35 | + public void SetType(string type) |
| 36 | + { |
| 37 | + Type = type; |
| 38 | + } |
| 39 | + |
| 40 | + public void AddChild(int newIdx) |
| 41 | + { |
| 42 | + if (Children.Contains(newIdx)) |
| 43 | + return; |
| 44 | + Children = Children.Append(newIdx).ToArray(); |
| 45 | + } |
| 46 | + |
| 47 | + private int Idx; |
| 48 | + private int[] Children = Array.Empty<int>(); |
| 49 | + private int X; |
| 50 | + private int Y; |
| 51 | + private string Type; |
| 52 | + } |
| 53 | + |
| 54 | + public void InitMapGrid(int width, int height, int paths) |
| 55 | + { |
| 56 | + _curIdx = 0; |
| 57 | + _rooms = Array.Empty<Room>(); |
| 58 | + _map = new int[width, height]; //x,y |
| 59 | + |
| 60 | + int startX = GlobalRng.RandiRange(0, width - 1); //TODO: Replace with seeding |
| 61 | + _rooms = _rooms.Append(new Room(_curIdx, startX, 0)).ToArray(); |
| 62 | + _map[startX, 0] = _curIdx++; |
| 63 | + |
| 64 | + for (int i = 0; i < paths; i++) |
| 65 | + { |
| 66 | + GeneratePath_r(startX, 0, width, height); |
| 67 | + } |
| 68 | + |
| 69 | + AddBossRoom(width, height); |
| 70 | + } |
| 71 | + |
| 72 | + //Start at x, y, assume prev room exists. Picks new x pos within +/- 1, attaches recursively |
| 73 | + private void GeneratePath_r(int x, int y, int width, int height) |
| 74 | + { |
| 75 | + int nextX = GlobalRng.RandiRange(Math.Max(x - 1, 0), Math.Min(x + 1, width - 1)); |
| 76 | + if (_map[nextX, y + 1] == 0) |
| 77 | + { |
| 78 | + _rooms = _rooms.Append(new Room(_curIdx, nextX, y + 1)).ToArray(); |
| 79 | + _map[nextX, y + 1] = _curIdx; |
| 80 | + _rooms[_map[x, y]].AddChild(_curIdx++); |
| 81 | + } |
| 82 | + else |
| 83 | + { |
| 84 | + _rooms[_map[x, y]].AddChild(_map[nextX, y + 1]); |
| 85 | + } |
| 86 | + if (y < height - 2) |
| 87 | + { |
| 88 | + GeneratePath_r(nextX, y + 1, width, height); |
| 89 | + } |
| 90 | + } |
| 91 | + |
| 92 | + private void AddBossRoom(int width, int height) |
| 93 | + { |
| 94 | + _rooms = _rooms.Append(new Room(_curIdx, 0, height)).ToArray(); |
| 95 | + _rooms[_curIdx].SetType("Boss"); |
| 96 | + for (int i = 0; i < width; i++) //Attach all last rooms to a boss room |
| 97 | + { |
| 98 | + if (_map[i, height - 1] != 0) |
| 99 | + { |
| 100 | + _rooms[_map[i, height - 1]].AddChild(_curIdx); |
| 101 | + } |
| 102 | + } |
| 103 | + } |
| 104 | + } |
| 105 | + |
| 106 | + public void StartGame() |
| 107 | + { |
| 108 | + _map.InitMapGrid(2, 2, 1); |
| 109 | + _seed = GlobalRng.Seed; |
| 110 | + _lastRngState = GlobalRng.State; |
| 111 | + } |
| 112 | + |
| 113 | + public void TransitionStage(Stages nextStage) |
| 114 | + { |
| 115 | + GD.Print(GetTree().CurrentScene); |
| 116 | + switch (nextStage) |
| 117 | + { |
| 118 | + case Stages.Title: |
| 119 | + GetTree().ChangeSceneToFile("res://scenes/SceneTransitions/TitleScreen.tscn"); |
| 120 | + break; |
| 121 | + case Stages.Battle: |
| 122 | + GetTree().ChangeSceneToFile("res://scenes/BattleDirector/test_battle_scene.tscn"); |
| 123 | + break; |
| 124 | + } |
| 125 | + |
| 126 | + _curStage = nextStage; |
| 127 | + } |
| 128 | +} |
0 commit comments