Skip to content

Commit 5d73cb0

Browse files
committed
before camera logic
1 parent 1bd4639 commit 5d73cb0

10 files changed

Lines changed: 335 additions & 17 deletions

File tree

assets/images/enemies.png

446 Bytes
Loading

assets/images/enemies.xml

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<TextureAtlas imagePath="002a-sprites.png">
3+
<!--
4+
Created with ShoeBox
5+
http://renderhjs.net/shoebox/
6+
-->
7+
<SubTexture name="001a.png" x="18" y="15" width="11" height="13" frameX="-2" frameY="-3" frameWidth="16" frameHeight="16"/>
8+
<SubTexture name="001b.png" x="18" y="0" width="12" height="13" frameX="-2" frameY="-3" frameWidth="16" frameHeight="16"/>
9+
<SubTexture name="002a.png" x="0" y="0" width="16" height="16" frameX="-0" frameY="-0" frameWidth="16" frameHeight="16"/>
10+
<SubTexture name="002b.png" x="0" y="18" width="16" height="15" frameX="-0" frameY="-1" frameWidth="16" frameHeight="16"/>
11+
</TextureAtlas>

source/Constants.hx

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package;
2+
3+
class Constants
4+
{
5+
// Global tile size used throughout the game (pixels)
6+
public static inline var TILE_SIZE:Int = 16;
7+
}

source/Enemy.hx

Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
package;
2+
3+
import flixel.FlxG;
4+
import flixel.graphics.frames.FlxAtlasFrames;
5+
import haxe.ds.StringMap;
6+
7+
class Enemy extends GameObject
8+
{
9+
// shared frames and variant list (initialized once)
10+
public static var SHARED_FRAMES:FlxAtlasFrames = null;
11+
public static var VARIANTS:Array<String> = [];
12+
// map from variant id -> array of frame names (as present in the atlas)
13+
public static var VARIANT_FRAMES:StringMap<Array<String>> = null;
14+
15+
private static function ensureFrames():Void
16+
{
17+
if (SHARED_FRAMES != null)
18+
return;
19+
SHARED_FRAMES = FlxAtlasFrames.fromSparrow("assets/images/enemies.png", "assets/images/enemies.xml");
20+
VARIANTS = [];
21+
VARIANT_FRAMES = new StringMap<Array<String>>();
22+
23+
// Iterate atlas frames and collect prefixes where frame names end with a single letter suffix (a,b,c...)
24+
for (f in 0...SHARED_FRAMES.numFrames)
25+
{
26+
var rawName:String = SHARED_FRAMES.getByIndex(f).name;
27+
// strip extension if present
28+
var base = rawName;
29+
var dot = base.lastIndexOf('.');
30+
if (dot >= 0)
31+
base = base.substr(0, dot);
32+
if (base.length < 2)
33+
continue;
34+
var lastChar = base.charAt(base.length - 1);
35+
if (lastChar >= 'a' && lastChar <= 'z')
36+
{
37+
var prefix = base.substr(0, base.length - 1);
38+
var list = VARIANT_FRAMES.get(prefix);
39+
if (list == null)
40+
{
41+
list = [];
42+
VARIANT_FRAMES.set(prefix, list);
43+
VARIANTS.push(prefix);
44+
}
45+
// store the atlas frame name (use rawName as that's what the atlas uses)
46+
list.push(rawName);
47+
}
48+
}
49+
}
50+
51+
public static function pickVariant():String
52+
{
53+
ensureFrames();
54+
if (VARIANTS == null || VARIANTS.length == 0)
55+
return null;
56+
var idx:Int = Std.int(FlxG.random.float() * VARIANTS.length);
57+
if (idx < 0)
58+
idx = 0;
59+
if (idx >= VARIANTS.length)
60+
idx = VARIANTS.length - 1;
61+
return VARIANTS[idx];
62+
}
63+
64+
public function new(tileX:Int, tileY:Int, ?variant:String)
65+
{
66+
super(tileX, tileY);
67+
speed = 50;
68+
}
69+
70+
public override function buildGraphics():Void
71+
{
72+
ensureFrames();
73+
if (SHARED_FRAMES != null)
74+
{
75+
this.frames = SHARED_FRAMES;
76+
var variant:String = pickVariant();
77+
if (variant != null)
78+
{
79+
var names = VARIANT_FRAMES.get(variant);
80+
if (names != null && names.length > 0)
81+
{
82+
animation.addByNames(variant, names, 12, true);
83+
animation.play(variant);
84+
}
85+
else
86+
{
87+
animation.addByPrefix(variant, variant, 12, true);
88+
animation.play(variant);
89+
}
90+
}
91+
}
92+
}
93+
}

source/GameMap.hx

Lines changed: 66 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -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;

source/GameObject.hx

Lines changed: 39 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,24 +2,59 @@ package;
22

33
import flixel.FlxSprite;
44

5+
56
class GameObject extends FlxSprite
67
{
7-
public var speed:Float = 80;
8+
public var speed(default, set):Float = 80;
89
public var moveAngle:Float = 0;
910

10-
public function new(X:Float = 0, Y:Float = 0)
11+
// Constructor expects pixel coordinates (top-left). `x`/`y` are pixel
12+
// positions where the object should spawn (commonly tileX * TILE_SIZE).
13+
// GameObject will call `buildGraphics()` and apply the standard inset
14+
// hitbox so subclasses only need to override `buildGraphics()`.
15+
public function new(x:Int = 0, y:Int = 0)
16+
{
17+
// call FlxSprite with a 1px inset so sprite sits inside the tile
18+
super(x + 1, y + 1);
19+
// initialize maxVelocity via the speed setter
20+
speed = speed;
21+
22+
// Let subclass set up graphics/frames, then apply the standard inset hitbox
23+
buildGraphics();
24+
setOffsetAmount(1);
25+
}
26+
27+
// Set a symmetric inset (in pixels) from each tile edge. For Size=1 and
28+
// TILE_SIZE=16 this gives width=height=14 and offset=1.
29+
public function setOffsetAmount(Size:Int):Void
1130
{
12-
super(X, Y);
13-
maxVelocity.set(speed, speed);
31+
var ts = Constants.TILE_SIZE;
32+
var w = ts - Size * 2;
33+
this.width = w;
34+
this.height = w;
35+
this.offset.x = Size;
36+
this.offset.y = Size;
1437
}
1538

39+
// Subclasses should override this to load graphics/animations. It is not
40+
// called automatically so they can control ordering (e.g. pick a variant
41+
// then load frames). Callers should call `buildGraphics()` and then
42+
// `setOffsetAmount(1)` from the subclass constructor.
43+
public function buildGraphics():Void {}
44+
1645
public function move(angleDegrees:Float):Void
1746
{
1847
moveAngle = angleDegrees;
1948
var rad:Float = angleDegrees * Math.PI / 180.0;
2049
velocity.set(Math.cos(rad) * speed, Math.sin(rad) * speed);
2150
}
2251

52+
private function set_speed(Value:Float):Float
53+
{
54+
maxVelocity.set(Value, Value);
55+
return Value;
56+
}
57+
2358
public function stop():Void
2459
{
2560
velocity.set(0, 0);

source/Main.hx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,6 @@ class Main extends Sprite
88
public function new()
99
{
1010
super();
11-
addChild(new FlxGame(640, 480, PlayState));
11+
addChild(new FlxGame(320, 240, PlayState));
1212
}
1313
}

0 commit comments

Comments
 (0)