Skip to content

Instantly share code, notes, and snippets.

@oscarcs
Last active April 12, 2021 20:08
Show Gist options
  • Save oscarcs/899a29310fee307b4058 to your computer and use it in GitHub Desktop.
Save oscarcs/899a29310fee307b4058 to your computer and use it in GitHub Desktop.
Haxe roguelike autotiling example
public static function borderAutoTile(xt:Int, yt:Int, context:ITileable, cur:RLTile, values:BorderValues):Int
{
var val:Int = 1;
var u:RLTile = context.read(xt, yt - 1);
var r:RLTile = context.read(xt + 1, yt);
var d:RLTile = context.read(xt, yt + 1);
var l:RLTile = context.read(xt - 1, yt);
//check whether the surrounding tiles are walls.
var UP:Bool = false;
var RIGHT:Bool = false;
var DOWN:Bool = false;
var LEFT:Bool = false;
if(u != null) UP = u.rt == cur.rt;
if(r != null) RIGHT = r.rt == cur.rt;
if(d != null) DOWN = d.rt == cur.rt;
if(l != null) LEFT = l.rt == cur.rt;
//o = is a wall
//x = no wall
//e.g. oxox
//up, !right, down, !left
if( UP && !RIGHT && DOWN && !LEFT) val = values.oxox; // │
else if( UP && !RIGHT && DOWN && LEFT) val = values.oxoo; // ┤
else if(!UP && !RIGHT && DOWN && LEFT) val = values.xxoo; // ┐
else if( UP && RIGHT && !DOWN && !LEFT) val = values.ooxx; // └
else if( UP && RIGHT && !DOWN && LEFT) val = values.ooxo; // ┴
else if(!UP && RIGHT && DOWN && LEFT) val = values.xooo; // ┬
else if( UP && RIGHT && DOWN && !LEFT) val = values.ooox; // ├
else if(!UP && RIGHT && !DOWN && LEFT) val = values.xoxo; // ─
else if( UP && RIGHT && DOWN && LEFT) val = values.oooo; // ┼
else if( UP && !RIGHT && !DOWN && LEFT) val = values.oxxo; // ┘
else if(!UP && RIGHT && DOWN && !LEFT) val = values.xoox; // ┌
//special cases
else if (UP || DOWN) val = values.oxox;
else if (LEFT || RIGHT) val = values.xoxo;
else val = values.oxox;
return val;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment