@@ -41,7 +41,7 @@ class GameMap extends FlxGroup
4141
4242 public function generate (): Void
4343 {
44- var TILE_SIZE : Int = 16 ;
44+ var TILE_SIZE : Int = Constants . TILE_SIZE ;
4545 var tilesWide : Int = Std .int (FlxG .width / TILE_SIZE );
4646 var tilesHigh : Int = Std .int (FlxG .height / TILE_SIZE );
4747 var totalW : Int = Std .int (Math .max (48 , tilesWide * 4 ));
@@ -187,8 +187,36 @@ class GameMap extends FlxGroup
187187 // increase room fraction so rooms are larger (65% - 90% of partition)
188188 var rW = Std .int (Math .max (3 , maxRW * (0.65 + FlxG .random .float () * 0.25 )));
189189 var rH = Std .int (Math .max (3 , maxRH * (0.65 + FlxG .random .float () * 0.25 )));
190- var rx = leaf .x + margin + Std .int (FlxG .random .float () * Math .max (0 , leaf .w - rW - margin * 2 ));
191- var ry = leaf .y + margin + Std .int (FlxG .random .float () * Math .max (0 , leaf .h - rH - margin * 2 ));
190+ // Try to pick a room position that keeps some separation from existing rooms
191+ var minRoomSeparation : Int = 6 ; // tiles
192+ var attemptsPos : Int = 0 ;
193+ var rx : Int = 0 ;
194+ var ry : Int = 0 ;
195+ var maxPosAttempts : Int = 12 ;
196+ while (attemptsPos < maxPosAttempts )
197+ {
198+ attemptsPos ++ ;
199+ rx = leaf .x + margin + Std .int (FlxG .random .float () * Math .max (0 , leaf .w - rW - margin * 2 ));
200+ ry = leaf .y + margin + Std .int (FlxG .random .float () * Math .max (0 , leaf .h - rH - margin * 2 ));
201+ var cxTry = rx + Std .int (rW / 2 );
202+ var cyTry = ry + Std .int (rH / 2 );
203+ var okPos : Bool = true ;
204+ // check against already placed rooms' centroids
205+ for (existingRoom in roomsInfo )
206+ {
207+ if (existingRoom == null )
208+ continue ;
209+ var exC : Int = Std .int (existingRoom .centroid .x );
210+ var eyC : Int = Std .int (existingRoom .centroid .y );
211+ if (Math .abs (exC - cxTry ) <= minRoomSeparation && Math .abs (eyC - cyTry ) <= minRoomSeparation )
212+ {
213+ okPos = false ;
214+ break ;
215+ }
216+ }
217+ if (okPos )
218+ break ;
219+ }
192220
193221 var cx = rx + Std .int (rW / 2 );
194222 var cy = ry + Std .int (rH / 2 );
@@ -496,17 +524,48 @@ class GameMap extends FlxGroup
496524 walkableTiles .push (yy * totalW + xx );
497525
498526 // --- pick a random room as the portal room and a random tile within it ---
527+ // Prefer small-to-medium rooms so the player doesn't start in a huge empty room.
499528 portalRoomIndex = - 1 ;
500529 portalTileX = portalTileY = - 1 ;
501530 portalPixelX = portalPixelY = - 1 ;
502- var candidateRooms : Array <Int > = [];
531+ // Build list of valid rooms (non-corridor, positive area)
532+ var validRooms : Array <Int > = [];
503533 for (i in 0 ... roomsInfo .length )
504534 {
505- if (roomsInfo [i ].area > 0 )
506- candidateRooms .push (i );
535+ var rr : RoomInfo = roomsInfo [i ];
536+ if (rr != null && rr .area > 0 && ! rr .isCorridor )
537+ validRooms .push (i );
507538 }
508- if (candidateRooms .length > 0 )
539+ if (validRooms .length > 0 )
509540 {
541+ // Compute a size cutoff around the 60th percentile to prefer small/medium rooms
542+ var areas : Array <Int > = [];
543+ for (idx in validRooms )
544+ areas .push (roomsInfo [idx ].area );
545+ areas .sort (function (a : Int , b : Int ): Int
546+ {
547+ return a - b ;
548+ });
549+ var cutoffIndex : Int = Std .int (Math .floor (validRooms .length * 0.6 ));
550+ if (cutoffIndex < 0 )
551+ cutoffIndex = 0 ;
552+ if (cutoffIndex >= areas .length )
553+ cutoffIndex = areas .length - 1 ;
554+ var cutoffArea : Int = areas [cutoffIndex ];
555+
556+ // Choose candidate rooms that are <= cutoffArea but not trivially small
557+ var candidateRooms : Array <Int > = [];
558+ for (idx in validRooms )
559+ {
560+ var rr2 : RoomInfo = roomsInfo [idx ];
561+ if (rr2 .area <= cutoffArea && rr2 .area >= 6 )
562+ candidateRooms .push (idx );
563+ }
564+
565+ // Fallback: if none match the heuristic, use all valid rooms
566+ if (candidateRooms .length == 0 )
567+ candidateRooms = validRooms ;
568+
510569 var ri : Int = Std .int (FlxG .random .float () * candidateRooms .length );
511570 if (ri < 0 )
512571 ri = 0 ;
0 commit comments