import { ExcaliburCamera , TiledPoint } from "./tiled-types" ; import { TiledEntity } from "./tiled-entity" ; import { RawTiledLayer , RawTiledObject } from "." ; import { Util } from "excalibur" ; export class TiledObjectGroup extends TiledEntity { public objects : TiledObject [ ] = [ ] ; public rawObjectGroup ! : RawTiledLayer ; public order ! : number ; public getCamera ( ) { const camera = this . getObjectByType ( 'camera' ) ; if ( camera ) { const zoom = camera . getProperty < number > ( 'zoom' ) ; return ( { x : camera . x , y : camera . y , zoom : zoom ? + ( zoom ?. value ?? 1 ) : 1 } ) ; } } public getObjectByType ( type ) { return this . getObjectsByType ( type ) [ 0 ] ; } public getObjectsByType ( type ) { return this . objects . filter ( o => o . type ?. toLocaleLowerCase ( ) === type . toLocaleLowerCase ( ) ) ; } public getObjectByName ( name ) { return this . getObjectsByName ( name ) [ 0 ] ; } public getObjectsByName ( name ) { return this . objects . filter ( o => o . name ?. toLocaleLowerCase ( ) === name . toLocaleLowerCase ( ) ) ; } public getPoints ( ) { return this . objects . filter ( o => ! ! o . point ) ; } public getEllipses ( ) { return this . objects . filter ( o => ! ! o . ellipse ) ; } public getText ( ) { return this . objects . filter ( o => ! ! o . text ) ; } public getPolyLines ( ) { return this . objects . filter ( o => ! ! o . polyline ) ; } public getPolygons ( ) { return this . objects . filter ( o => ! ! o . polygon ) ; } public getInsertedTiles ( ) { return this . objects . filter ( o => ! ! o . gid ) ; } public static parse ( objectGroup ) { if ( objectGroup . type !== 'objectgroup' ) throw Error ( 'Cannot parse non objectgroup type layer' ) ; const resultObjectGroup = new TiledObjectGroup ( ) ; resultObjectGroup . id = + objectGroup . id ; resultObjectGroup . name = objectGroup . name ; resultObjectGroup . properties = objectGroup . properties ?? [ ] ; resultObjectGroup . rawObjectGroup = objectGroup ; resultObjectGroup . order = objectGroup . order ; for ( let object of objectGroup . objects ) { resultObjectGroup . objects . push ( TiledObject . parse ( object ) ) ; } return resultObjectGroup ; } } export class TiledObject extends TiledEntity { public type ? : string ; public x ! : number ; public y ! : number ; public visible ! : boolean ; public rotation ! : number ; public width ? : number ; public height ? : number ; /**
     * Present on point objects
     */ public point ? : boolean ; /**
     * Present on ellipse objects
     */ public ellipse ? : boolean ; /**
     * Present on text objects
     */ public text ? : TiledText ; /**
     * Present on polyline objects
     */ public polyline ? : TiledPoint [ ] ; /**
     * Present on polygon objects
     */ public polygon ? : TiledPoint [ ] ; /**
     * Present on inserted tile objects
     */ public gid ? : number ; public rawObject ! : RawTiledObject ; public static parse ( object ) { const resultObject = new TiledObject ( ) ; resultObject . id = + object . id ; resultObject . gid = object . gid ; resultObject . visible = object . visible ?? true ; resultObject . name = object . name ; resultObject . type = object . type ; resultObject . x = + object . x ; resultObject . y = + object . y ; resultObject . rotation = object . rotation ? Util . toRadians ( object . rotation ) : 0 ; resultObject . width = object . width ?? 0 ; resultObject . height = object . height ?? 0 ; resultObject . point = object . point ; resultObject . ellipse = object . ellipse ; resultObject . polyline = object . polyline ; resultObject . polygon = object . polygon ; resultObject . rawObject = object ; if ( object . text ) { resultObject . text = { ... object . text , pixelSize : object . text . pixelsize , fontFamily : object . text . fontfamily } ; } resultObject . properties = object . properties ?? [ ] ; return resultObject ; } } export interface TiledText { text : string ; color ? : string ; fontFamily : string ; pixelSize : number ; bold : boolean ; italic : boolean ; underline : boolean ; strikeout : boolean ; kerning : boolean ; } export interface TiledInsertedTile extends TiledObject { gid : number ; }