Created
December 7, 2014 03:53
-
-
Save Ohmnivore/7afad77f574a6c1207dc to your computer and use it in GitHub Desktop.
Basic HaxeFlixel Ogmo Editor loader
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package util; | |
import flixel.FlxG; | |
import haxe.xml.Fast; | |
import flixel.tile.FlxTilemap; | |
import haxe.Json; | |
import flixel.tile.FlxTile; | |
import flixel.FlxObject; | |
import openfl.Assets; | |
import entities.Spawn; | |
/** | |
* ... | |
* @author Ohmnivore | |
*/ | |
class OgmoLoader | |
{ | |
public static var tilemaps:Map<String, String>; | |
public static var entities:Map<Int, Dynamic>; | |
public static inline var bgColor:Int = 0xffDEDEDE; | |
static public function initEntities():Void | |
{ | |
//Add all entity classes here so that | |
//the compiler can find them as this is | |
//probably the sole reference to them | |
entities = new Map<Int, Dynamic>(); | |
} | |
static private function ensureCompilation():Void | |
{ | |
Spawn; | |
} | |
static public function initTilemaps():Void | |
{ | |
tilemaps = new Map<String, String>(); | |
tilemaps.set("Tiles", "assets/images/tiles.png"); | |
} | |
static public function loadXML(XML:String, State:PlayState):Void | |
{ | |
Spawn.clear(); | |
FlxG.cameras.bgColor = bgColor; | |
var xml = Xml.parse(XML); | |
var fast = new Fast(xml.firstElement()); | |
for (x in fast.elements) | |
{ | |
if (x.has.tileset) //Tilemap | |
{ | |
var td:TileDef = loadFile(tilemaps.get(x.att.tileset)); | |
var map:FlxTilemap = new FlxTilemap(); | |
#if (mobile || desktop) | |
map.loadMap(x.innerData, | |
ArtifactFix.artefactFix(tilemaps.get(x.att.tileset), 16, 16), | |
16, 16, 0, 0, 0, 0); | |
#else | |
map.loadMap(x.innerData, tilemaps.get(x.att.tileset), 16, 16, 0, 0, 0, 0); | |
#end | |
map.tileScaleHack = 1.05; | |
State.maps.add(map); | |
FlxG.worldBounds.set(map.x - 16, map.y - FlxG.height / 2, | |
map.width + 32, map.height + FlxG.height / 2); | |
interpretData(td, map); | |
} | |
else | |
{ | |
if (!x.has.exportMode) //Entity layer | |
{ | |
for (ent in x.elements) | |
{ | |
var c:Class<Dynamic> = Type.resolveClass("entities." + ent.name); | |
var funct:Dynamic = Reflect.field(c, "makeFromXML"); | |
var entityInstance:Dynamic = Reflect.callMethod(c, funct, [ent]); | |
entities.set(Std.parseInt(ent.att.id), entityInstance); | |
} | |
} | |
else //Grid layer | |
{ | |
} | |
} | |
} | |
} | |
//Tiledef parsing code | |
static public function loadFile(P:String):TileDef | |
{ | |
var cutoff:Int = P.lastIndexOf("/"); | |
var cutoff2:Int = P.lastIndexOf("."); | |
var filepath:String = "assets/data/" + P.substring(cutoff + 1, cutoff2) + ".json"; | |
//return new TileDef(cast Json.parse(File.getContent(filepath))); | |
return new TileDef(cast Json.parse(Assets.getText(filepath))); | |
} | |
static public function interpretData(T:TileDef, M:FlxTilemap):Void | |
{ | |
var tileObjects:Array<FlxTile> = cast Reflect.field(M, "_tileObjects"); | |
for (tile in tileObjects) | |
{ | |
M.setTileProperties(tile.index, FlxObject.NONE); | |
} | |
for (t in T.tiles.keys()) | |
{ | |
var collisions:Int = FlxObject.NONE; | |
var props:Map<String, Dynamic> = T.getTileProps(t); | |
if (props.get("Solid") == true) | |
{ | |
collisions = FlxObject.ANY; | |
} | |
M.setTileProperties(t, collisions); | |
} | |
} | |
} | |
class TileDef | |
{ | |
public var width:Int; | |
public var height:Int; | |
public var fields:Array<String>; | |
public var brushes:Map<Int, Array<Dynamic>>; | |
public var tiles:Map<Int, Int>; | |
public function new(Data:Array<Dynamic>) | |
{ | |
var arr_info:Array<Dynamic> = Data[0]; | |
var arr_fields:Array<String> = Data[1]; | |
var arr_brush_key:Array<Int> = Data[2]; | |
var arr_brush_value:Array<Dynamic> = Data[3]; | |
var arr_tile_key:Array<Int> = Data[4]; | |
var arr_tile_value:Array<Int> = Data[5]; | |
width = cast arr_info[1]; | |
height = cast arr_info[2]; | |
fields = arr_fields; | |
brushes = new Map<Int, Array<Dynamic>>(); | |
var i:Int = 0; | |
while (i < arr_brush_key.length) | |
{ | |
brushes.set(arr_brush_key[i], arr_brush_value[i][0]); | |
i++; | |
} | |
tiles = new Map<Int, Int>(); | |
i = 0; | |
while (i < arr_tile_key.length) | |
{ | |
tiles.set(arr_tile_key[i], arr_tile_value[i]); | |
i++; | |
} | |
} | |
public function getTileProps(Index:Int):Map<String, Dynamic> | |
{ | |
var brush:Int = tiles.get(Index); | |
var brush_arr:Array<Dynamic> = brushes.get(brush); | |
var ret:Map<String, Dynamic> = new Map<String, Dynamic>(); | |
var i:Int = 0; | |
while (i < fields.length) | |
{ | |
ret.set(fields[i], brush_arr[i]); | |
i++; | |
} | |
return ret; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment