Skip to content

Commit

Permalink
🏯 Begin boundrary detection work.
Browse files Browse the repository at this point in the history
  • Loading branch information
mxbaylee committed Jul 11, 2024
1 parent 988755d commit a2580ea
Show file tree
Hide file tree
Showing 3 changed files with 1,420 additions and 1,319 deletions.
81 changes: 58 additions & 23 deletions Main.hx
Original file line number Diff line number Diff line change
@@ -1,23 +1,28 @@
import hxd.res.TiledMap;

typedef Bound = { x: Int, y : Int, height: Int, width : Int, name : String, type : String };

class Main extends hxd.App {
var wizard: h2d.Object;
var timer: Float = 0.0;
var currentScene: h2d.Scene;
var boundaries: Array<Bound>;

override private function init():Void {
super.init();

var myscene = new h2d.Scene();
var myScene = new h2d.Scene();
currentScene = myScene;

this.setScene(myscene);
this.setScene(myScene);

myscene.scaleMode = h2d.Scene.ScaleMode.Stretch(300, 200);
myScene.scaleMode = h2d.Scene.ScaleMode.Stretch(300, 200);

var rlTiles = roguelikeTiles();

myscene.addChild(toSprite(rlTiles[DirtPathRL.Top]));
myscene.addChild(toSprite(rlTiles[DirtPathRL.Middle], 0, 16));
myscene.addChild(toSprite(rlTiles[DirtPathRL.Bottom], 0, 32));
myScene.addChild(toSprite(rlTiles[DirtPathRL.Top]));
myScene.addChild(toSprite(rlTiles[DirtPathRL.Middle], 0, 16));
myScene.addChild(toSprite(rlTiles[DirtPathRL.Bottom], 0, 32));

var mapJson = hxd.Res.dungeon_crawler_v2_tmj.entry.getText();

Expand All @@ -28,7 +33,7 @@ class Main extends hxd.App {
var tileImage = hxd.Res.tinydungeon.toTile();

// create a TileGroup for fast tile rendering, attach to 2d scene
var group = new h2d.TileGroup(tileImage, myscene);
var group = new h2d.TileGroup(tileImage, myScene);

var tw = 16;
var th = 16;
Expand All @@ -49,37 +54,67 @@ class Main extends hxd.App {
for(layer in mapData.layers) {
// iterate on x and y
for(y in 0 ... mh) for (x in 0 ... mw) {
if (layer?.data == null) continue;
// get the tile id at the current position
var tid = layer.data[x + y * mw];
if (tid != 0) { // skip transparent tiles
// add a tile to the TileGroup
group.add(x * tw, y * th, tiles[tid - 1]);
if (layer?.objects != null) {
this.boundaries = layer.objects;
} else if (layer?.data != null) {
// get the tile id at the current position
var tid = layer.data[x + y * mw];
if (tid != 0) { // skip transparent tiles
// add a tile to the TileGroup
group.add(x * tw, y * th, tiles[tid - 1]);
}
}
}
}

/*
var tdTiles = tinydungeonTiles();
wizard = toSprite(tdTiles[CharacterTD.Wizard]);
myScene.addChild(wizard);
}

myscene.addChild(wizard);
myscene.addChild(toSprite(tdTiles[CharacterTD.VillagerOne], 16, 16));
*/
public function move(object: h2d.Object, x: Float, y: Float): Void {
// knows where object is, and is going
// can make accurate prediction on collision detection
var hit: Bool = false;
for (boundary in this.boundaries) {
if (detectCollision(object, boundary, x, y)) {
hit = true;
break; // Exit the loop early if a collision is detected
}
}
if (!hit) {
object.x = x;
object.y = y;
}
}

public function detectCollision(obj: h2d.Object, boundary: Bound, x: Float, y: Float): Bool {
// New position of the object
var newX = x;
var newY = y;

// Check for collision with the boundary
if (newX < boundary.x + boundary.width &&
newX + obj.width > boundary.x &&
newY < boundary.y + boundary.height &&
newY + obj.height > boundary.y) {
return true;
}

return false;
}

override private function update(dt: Float) {
timer = timer + dt;
if (timer > 0.1) {
if (hxd.Key.isDown(hxd.Key.UP))
wizard.y = wizard.y - 16;
this.move(wizard, wizard.x, wizard.y - 16);
if (hxd.Key.isDown(hxd.Key.DOWN))
this.move(wizard, wizard.x, wizard.y + 16);
if (hxd.Key.isDown(hxd.Key.RIGHT))
wizard.x = wizard.x + 16;
this.move(wizard, wizard.x + 16, wizard.y);
if (hxd.Key.isDown(hxd.Key.LEFT))
wizard.x = wizard.x - 16;
if (hxd.Key.isDown(hxd.Key.DOWN))
wizard.y = wizard.y + 16;
this.move(wizard, wizard.x - 16, wizard.y);
timer = 0.0;
}
}
Expand Down
2 changes: 1 addition & 1 deletion Util.hx
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ function tinydungeonTiles() {
];
}

function toSprite(tile, x = 0, y = 0) {
function toSprite(tile, x = 0, y = 0): h2d.Bitmap {
var sprite = new h2d.Bitmap(tile);

sprite.x = x;
Expand Down
Loading

0 comments on commit a2580ea

Please sign in to comment.