-
-
Notifications
You must be signed in to change notification settings - Fork 251
Expand file tree
/
Copy pathEntityInstance.hx
More file actions
382 lines (309 loc) · 11.4 KB
/
Copy pathEntityInstance.hx
File metadata and controls
382 lines (309 loc) · 11.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
package data.inst;
class EntityInstance {
public var _project : Project;
public var _li(default,null) : LayerInstance;
public var def(get,never) : data.def.EntityDef; inline function get_def() return _project.defs.getEntityDef(defUid);
public var iid : String;
public var defUid(default,null) : Int;
public var x : Int;
public var y : Int;
public var centerX(get,never) : Int;
public var centerY(get,never) : Int;
public var worldX(get,never) : Int;
public var worldY(get,never) : Int;
public var customWidth : Null<Int>;
public var customHeight: Null<Int>;
public var flips : Int;
public var width(get,never) : Int;
inline function get_width() return customWidth!=null ? customWidth : def.width;
public var height(get,never) : Int;
inline function get_height() return customHeight!=null ? customHeight : def.height;
public var fieldInstances : Map<Int, data.inst.FieldInstance> = new Map();
public var left(get,never) : Int; inline function get_left() return M.round( x - width*getAdjustedPivotX() );
public var right(get,never) : Int; inline function get_right() return left + width;
public var top(get,never) : Int; inline function get_top() return M.round( y - height*getAdjustedPivotY() );
public var bottom(get,never) : Int; inline function get_bottom() return top + height;
public function new(p:Project, li:LayerInstance, entityDefUid:Int, iid:String, flips:Int = 0) {
_project = p;
_li = li;
defUid = entityDefUid;
this.iid = iid;
this.flips = flips;
}
@:keep public function toString() {
return 'EntityInst "${def.identifier}" @$x,$y';
}
inline function get_centerX() return M.round( x + (0.5-getAdjustedPivotX())*width );
inline function get_centerY() return M.round( y + (0.5-getAdjustedPivotY())*height );
inline function get_worldX() return Std.int( x + _li.level.worldX );
inline function get_worldY() return Std.int( y + _li.level.worldY );
public function toJson(li:data.inst.LayerInstance) : ldtk.Json.EntityInstanceJson {
if( customWidth==def.width )
customWidth = null;
if( customHeight==def.height )
customHeight = null;
return {
// Fields preceded by "__" are only exported to facilitate parsing
__identifier: def.identifier,
__grid: [ getCx(li.def), getCy(li.def) ],
__pivot: [ JsonTools.writeFloat(def.pivotX), JsonTools.writeFloat(def.pivotY) ],
__tags: def.tags.toArray(),
__tile: getSmartTile(),
__smartColor: C.intToHex( getSmartColor(false) ),
__worldX: x + li.level.worldX,
__worldY: y + li.level.worldY,
iid: iid,
width: width,
height: height,
defUid: defUid,
px: [x,y],
f: flips,
fieldInstances: {
var all = [];
for(fd in def.fieldDefs)
all.push( getFieldInstance(fd,true).toJson() );
all;
}
}
}
public function toSimplifiedJson() : Dynamic {
var customFields = {};
for( fi in fieldInstances )
Reflect.setField(customFields, fi.def.identifier, fi.toJson().__value);
return {
id: def.identifier,
iid: iid,
layer: _li.def.identifier,
x : x,
y : y,
f: flips,
width: width,
height: height,
color: getSmartColor(false),
customFields: customFields,
}
}
public static function fromJson(project:Project, li:LayerInstance, json:ldtk.Json.EntityInstanceJson) {
if( (cast json).x!=null ) // Convert old coordinates
json.px = [ JsonTools.readInt( (cast json).x, 0 ), JsonTools.readInt((cast json).y,0) ];
if( (cast json).defId!=null ) // Convert renamed defId
json.defUid = (cast json).defId;
if( json.iid==null ) // Init IID
json.iid = project.generateUniqueId_UUID();
var ei = new EntityInstance(project, li, JsonTools.readInt(json.defUid), json.iid);
ei.x = JsonTools.readInt( json.px[0], 0 );
ei.y = JsonTools.readInt( json.px[1], 0 );
ei.flips = JsonTools.readInt( json.f, 0 );
ei.customWidth = JsonTools.readNullableInt( json.width );
if( ei.customWidth==ei.def.width )
ei.customWidth = null;
ei.customHeight = JsonTools.readNullableInt( json.height );
if( ei.customHeight==ei.def.height )
ei.customHeight = null;
for( fieldJson in JsonTools.readArray(json.fieldInstances) ) {
var fi = FieldInstance.fromJson(project, fieldJson);
ei.fieldInstances.set(fi.defUid, fi);
}
return ei;
}
public inline function getAdjustedPivotX() {
return (def.flipAroundPivot && M.hasBit(flips, 0)) ? ((width - 1) / width) - def.pivotX : def.pivotX;
}
public inline function getAdjustedPivotY() {
return (def.flipAroundPivot && M.hasBit(flips, 1)) ? ((height - 1) / height) - def.pivotY : def.pivotY;
}
public inline function getCx(ld:data.def.LayerDef) {
return Std.int( ( x + (getAdjustedPivotX()==1 ? -1 : 0) ) / ld.gridSize );
}
public inline function getCy(ld:data.def.LayerDef) {
return Std.int( ( y + (getAdjustedPivotY()==1 ? -1 : 0) ) / ld.gridSize );
}
public inline function getPointOriginX(ld:data.def.LayerDef) {
return def.resizableX ? centerX : ( getCx(ld)+0.5 ) * ld.gridSize;
}
public inline function getPointOriginY(ld:data.def.LayerDef) {
return def.resizableY ? centerY : ( getCy(ld)+0.5 ) * ld.gridSize;
}
public inline function getRefAttachX(fd:data.def.FieldDef) {
return fd.editorDisplayMode==RefLinkBetweenCenters ? centerX : x;
}
public inline function getRefAttachY(fd:data.def.FieldDef) {
return fd.editorDisplayMode==RefLinkBetweenCenters ? centerY : y;
}
public inline function getWorldRefAttachX(fd:data.def.FieldDef) {
return _li.level.worldX + ( fd.editorDisplayMode==RefLinkBetweenCenters ? centerX : x );
}
public inline function getWorldRefAttachY(fd:data.def.FieldDef) {
return _li.level.worldY + ( fd.editorDisplayMode==RefLinkBetweenCenters ? centerY : y );
}
final overShapePad = 3;
final overEdgePad = 4;
public inline function isOver(layerX:Int, layerY:Int) {
if( M.fabs(layerX-x) > width+overEdgePad*2 || M.fabs(layerY-y) > height+overEdgePad*2 ) // Fast check
return false;
else if( def.renderMode==Ellipse ) {
if( def.hollow ) {
final rxIn2 = M.pow(width*0.5-overEdgePad, 2);
final rxOut2 = M.pow(width*0.5+overEdgePad, 2);
final ryIn2 = M.pow(height*0.5-overEdgePad, 2);
final ryOut2 = M.pow(height*0.5+overEdgePad, 2);
return
M.pow(layerX-centerX, 2) * ryIn2 + M.pow(layerY-centerY, 2) * rxIn2 > rxIn2*ryIn2
&& M.pow(layerX-centerX, 2) * ryOut2 + M.pow(layerY-centerY, 2) * rxOut2 <= rxOut2*ryOut2;
}
else {
final rx2 = M.pow(width*0.5+overShapePad, 2);
final ry2 = M.pow(height*0.5+overShapePad, 2);
return M.pow(layerX-centerX, 2) * ry2 + M.pow(layerY-centerY, 2) * rx2 <= rx2*ry2;
}
}
else if( def.hollow ) {
return layerX >= left-overEdgePad && layerX<=right+overEdgePad && layerY>=top-overEdgePad && layerY<=bottom+overEdgePad
&& !( layerX >= left+overEdgePad && layerX<=right-overEdgePad && layerY>=top+overEdgePad && layerY<=bottom-overEdgePad );
}
else
return layerX>=left-overShapePad && layerX<=right+overShapePad && layerY>=top-overShapePad && layerY<=bottom+overShapePad;
}
public function getSmartColor(bright:Bool) : dn.Col {
var c : Null<Int> = null;
for(fd in def.fieldDefs) {
c = getFieldInstance(fd,true).getSmartColor();
if( c!=null )
return c;
}
return bright ? dn.legacy.Color.toWhite(def.color, 0.5) : def.color;
}
public function getSmartTile() : Null<ldtk.Json.TilesetRect> {
// Check for a tile provided by a field instance
for(fd in def.fieldDefs) {
var t = getFieldInstance(fd,true).getSmartTile();
if( t!=null )
return t;
}
return def.getDefaultTile();
}
public function isUsingTileset(td:data.def.TilesetDef) {
if( def.tilesetId==td.uid )
return true;
for(fi in fieldInstances)
if( fi.def.type==F_Tile && fi.def.tilesetUid==td.uid )
return true;
return false;
}
public inline function isInSameSpaceAs(ei:EntityInstance) {
return ei!=null
&& _li.level.worldDepth == ei._li.level.worldDepth
&& _li.level.isInWorld(ei._li.level._world);
}
public function isOutOfLayerBounds() {
return x<_li.pxTotalOffsetX || x>_li.pxTotalOffsetX+_li.cWid*_li.def.scaledGridSize
|| y<_li.pxTotalOffsetY || y>_li.pxTotalOffsetY+_li.cHei*_li.def.scaledGridSize;
}
public function tidy(p:data.Project, li:LayerInstance) {
_project = p;
_li = li;
_project.markIidAsUsed(iid);
var anyChange = false;
// Remove field instances whose def was removed
for(e in fieldInstances.keyValueIterator())
if( e.value.def==null ) {
App.LOG.add("tidy", 'Removed lost fieldInstance in $this');
fieldInstances.remove(e.key);
}
// Create missing field instances
for(fd in def.fieldDefs)
getFieldInstance(fd,true);
for(fi in fieldInstances)
fi.tidy(_project, li);
return anyChange;
}
// ** FIELDS **********************************
public function hasAnyFieldError() {
for(fi in fieldInstances)
if( fi.hasAnyErrorInValues(this) )
return true;
return false;
}
public function hasField(fieldDef:data.def.FieldDef) {
return fieldInstances.exists(fieldDef.uid);
}
public function getFieldInstance(fieldDef:data.def.FieldDef, createIfMissing:Bool) {
if( createIfMissing && !fieldInstances.exists(fieldDef.uid) )
fieldInstances.set(fieldDef.uid, new data.inst.FieldInstance(_project, fieldDef.uid));
return fieldInstances.get( fieldDef.uid );
}
public function getFieldInstancesOfType(type:ldtk.Json.FieldType) {
var all = [];
for(fi in fieldInstances)
if( fi.def.type.getIndex() == type.getIndex() )
all.push(fi);
return all;
}
/**
Return TRUE if target EntityInstance has a reference to This in given field.
**/
public inline function hasEntityRefTo(targetEi:EntityInstance, ?fd:data.def.FieldDef, onlyIfLinkIsDisplayed=false) {
return getEntityRefFieldTo(targetEi, fd, onlyIfLinkIsDisplayed) != null;
}
/**
Return TRUE if target EntityInstance has a reference to This in given field.
**/
public function getEntityRefFieldTo(targetEi:EntityInstance, ?onlyFd:data.def.FieldDef, onlyIfLinkIsDisplayed=false) : Null<FieldInstance> {
if( onlyFd==null ) {
// In any field
for(fi in fieldInstances)
for(i in 0...fi.getArrayLength())
if( fi.getEntityRefIid(i)==targetEi.iid )
return fi;
}
else {
// In specified field
if( onlyFd.type!=F_EntityRef )
return null;
var fi = getFieldInstance(onlyFd,false);
if( fi==null )
return null;
for(i in 0...fi.getArrayLength())
if( fi.getEntityRefIid(i)==targetEi.iid && ( !onlyIfLinkIsDisplayed || fi.def.editorDisplayMode==RefLinkBetweenCenters || fi.def.editorDisplayMode==RefLinkBetweenPivots ) )
return fi;
}
return null;
}
/**
Clear invalid asymmetrical refs between this EntityInstance and other ones
**/
public function tidyLostSymmetricalEntityRefs(fd:data.def.FieldDef, allowDeepSearch=true) {
if( fd.type!=F_EntityRef || !fd.symmetricalRef )
return;
var fi = getFieldInstance(fd, false);
if( fi==null )
return;
// Check own fields for lost symmetricals
var i = 0;
var needFieldsTidy = false;
while( i<fi.getArrayLength() ) {
if( fi.valueIsNull(i) )
i++;
else {
var targetEi = fi.getEntityRefInstance(i);
if( targetEi==null || !targetEi.hasEntityRefTo(this, fd) ) {
fi.removeArrayValue(i);
if( targetEi==null )
_project.unregisterReverseIidRef(this, targetEi);
else
needFieldsTidy = true;
}
else
i++;
}
}
if( needFieldsTidy )
_project.tidyFields();
// Check entities pointing at me
if( allowDeepSearch ) {
var reverseReferers = _project.getEntityInstancesReferingTo(this);
for( ei in reverseReferers )
ei.tidyLostSymmetricalEntityRefs(fd, false);
}
}
}