Working tile rendering! 🎉

This commit is contained in:
3wc
2024-02-12 13:53:47 -03:00
parent 6777d3f64e
commit a85a478f1d
7 changed files with 107 additions and 0 deletions

6
src/Const.hx Normal file
View File

@ -0,0 +1,6 @@
class Const {
public static inline var W = 960;
public static inline var H = 640;
}

17
src/Game.hx Normal file
View File

@ -0,0 +1,17 @@
import Const;
@:publicFields
class Game extends hxd.App {
public var scene : h2d.Scene;
public var font : h2d.Font;
public var world : World;
override function init() {
scene = s2d;
s2d.setFixedSize(Const.W, Const.H + 12);
world = new World(Res.map, Res.tiles);
s2d.add(world.root, 0);
}
public static var inst : Game;
}

View File

@ -41,6 +41,9 @@ class Main extends hxd.App {
onResize();
var root = new ContainerComp(Right, center);
root.btnNewGame.onClick = function() {
Game.inst = new Game();
}
root.btnContinueGame.onClick = function() {
root.btnContinueGame.dom.addClass("highlight");
}

1
src/Res.hx Normal file
View File

@ -0,0 +1 @@
typedef Res = hxd.Res;

54
src/World.hx Normal file
View File

@ -0,0 +1,54 @@
class World {
var game : Game;
var map : hxd.res.TiledMap.TiledMapData;
public var root : h2d.Object;
var layers : Map < String, { name : String, data : Array<Int>, g : h2d.TileGroup, alpha : Float } > ;
var tiles : Array<h2d.Tile>;
public var width : Int;
public var height : Int;
public function new( r : hxd.res.TiledMap, tiles : hxd.res.Image ) {
game = Game.inst;
root = new h2d.Object();
map = r.toMap();
root = new h2d.Object();
width = map.width;
height = map.height;
var t = tiles.toTile();
layers = new Map();
var font : h2d.Font = hxd.res.DefaultFont.get();
this.tiles = [for( y in 0...Std.int(t.height) >> 5 ) for( x in 0...Std.int(t.width) >> 5 ) t.sub(x * 32, y * 32, 32, 32)];
for( ld in map.layers ) {
if( ld.name == "hotspots") continue;
var l = {
name : ld.name,
data : ld.data,
g : new h2d.TileGroup(t, root),
alpha : ld.opacity,
}
// l.g.colorKey = 0x1D8700;
l.g.alpha = ld.opacity;
layers.set(ld.name, l);
rebuildLayer(ld.name);
}
}
function rebuildLayer( name : String ) {
var l = layers.get(name);
if( l == null ) return;
var pos = 0;
var g = l.g;
g.clear();
while( g.numChildren > 0 )
g.getChildAt(0).remove();
for( y in 0...height )
for( x in 0...width ) {
var t = l.data[pos++] - 1;
if( t < 0 ) continue;
g.add(x * 32, y * 32, tiles[t]);
}
}
}