-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstructure.ts
282 lines (267 loc) · 11.8 KB
/
structure.ts
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
/// <reference path="primitives.ts" />
/// <reference path="protocols.ts" />
/// <reference path="mapobject.ts" />
/// <reference path="wall.ts" />
/// <reference path="enginehelpers.ts" />
namespace MapObjects {
export class Structure extends MapObject implements IPlaceable,IJoinable {
elements:MapObject[] = [];
position:Point;
area:Placement;
joints:Point[];
hovering:MapObject;
pressed:boolean;
shadowedPosition:Point;
mousePosition:Point;
constructor() {
super();
this.area = Placement.OnMap;
}
getFeatures():Feature[] {
return [Feature.Placeable,Feature.Joinable];
}
customFocusRenderer():boolean {
return true;
}
handlesMouseEvents():boolean {
return true;
}
draw(context:CanvasRenderingContext2D,focused:boolean,offscreencontext:CanvasRenderingContext2D) {
if(focused) {
offscreencontext.save();
offscreencontext.clearRect(0,0,offscreencontext.canvas.width,offscreencontext.canvas.height);
offscreencontext.translate(this.position.x,this.position.y);
this.elements.forEach(object => {
if(object !== this.hovering) {
MapEngine.renderObject(object,offscreencontext);
}
});
offscreencontext.globalCompositeOperation = "source-in";
offscreencontext.fillStyle="#00A";
offscreencontext.fillRect(-this.position.x,-this.position.y,offscreencontext.canvas.width,offscreencontext.canvas.height);
offscreencontext.restore();
}
this.elements.forEach(object => {
if(object !== this.hovering) {
MapEngine.renderObject(object,context);
}
});
if(focused) {
context.save();
context.globalCompositeOperation="lighter";
context.drawImage(offscreencontext.canvas,-this.position.x,-this.position.y);
context.restore();
}
if(this.hovering != null) {
MapEngine.renderObject(this.hovering,context);
}
}
hitTest(pt:Point):boolean {
//the point is already cleaned for the structures position itself.
//HOWEVER, we have to delegate hit testing to every object. Fun!
for(let idx in this.elements) {
let object = this.elements[idx];
let repositioned = MapEngine.transformPointForHitTesting(pt,object);
if(object.hitTest(repositioned)) {
return true;
}
}
return false;
}
addElement(object:MapObject) {
if(object instanceof Structure) {
let diff = object.position.subtractPoint(this.position);
object.elements.forEach(object => {
if(object.hasFeature(Feature.Placeable)) {
let casted = <IPlaceable><any>object;
casted.position = casted.position.addPoint(diff);
}
this.elements.push(object);
});
} else {
if(object.hasFeature(Feature.Placeable)) {
let cast = <IPlaceable><any>object;
cast.position = cast.position.subtractPoint(this.position);
}
this.elements.push(object);
}
this.rebuildJoints();
}
rebuildJoints() {
//we have to calculate all joints, as walls are placed within our own coordinate system.
//and rotated within it.
let joints:Point[] = [];
this.elements.forEach(object => {
if(object.hasFeature(Feature.Joinable)) {
let casted = <IJoinable><any>object;
let pt = new Point(0,0);
if(object.hasFeature(Feature.Placeable)) {
pt = (<IPlaceable><any>object).position;
}
let objjoints = MapEngine.transformedJoints(casted,pt);
objjoints = objjoints.filter((value:Point):boolean => {
for(let idx in joints) {
let point = joints[idx];
let diff=point.subtractPoint(value);
if(diff.length() < 2*Number.EPSILON) {
return false;
}
}
return true;
})
joints = joints.concat(objjoints);
}
});
this.joints = joints;
}
mouseDown(pt:Point,button:number,engine:MapObjects.IEngine) {
this.pressed = true;
}
mouseMove(pt:Point,engine:MapObjects.IEngine) {
if(this.pressed && this.hovering.hasFeature(Feature.Placeable)) {
let casted = <MapObjects.IPlaceable><any>(this.hovering);
let pos = casted.position;
if(this.shadowedPosition != null) {
pos = this.shadowedPosition;
}
let diff = pt.subtractPoint(this.mousePosition);
pos = pos.addPoint(diff);
/*
as we now have the "new" position, we have to do a snap-test against "everything".
*/
let snapPt = MapEngine.snapTest(this.hovering,pos,this.elements,engine.scaling);
if(snapPt != null) {
casted.position=snapPt[0];
this.shadowedPosition = pos;
} else {
this.shadowedPosition = null;
casted.position=pos;
}
engine.render();
} else {
this.hovering = null;
this.elements.forEach(object => {
let converted = MapEngine.transformPointForHitTesting(pt,object);
if(object.hitTest(converted)) {
this.hovering = object;
engine.render()
//now i need to trigger a render run...
}
});
}
this.mousePosition = pt;
}
mouseUp(pt:Point,button:number,engine:MapObjects.IEngine) {
if(this.shadowedPosition == null && this.hovering != null) {
//so... we have broken up the structure.
//first things first: we have to get rid of the object.
//therefore we have to translate the position of the object
//back into the global space.
if(this.hovering.hasFeature(Feature.Placeable)) {
let casted = <IPlaceable><any>this.hovering;
casted.position = casted.position.addPoint(this.position);
}
let idx = this.elements.indexOf(this.hovering);
this.elements.splice(idx,1);
engine.objects.push(this.hovering);
}
//as a result, we have to check for multiple things:
/*
* 1. is this structure down to one element? -> Remove the structure
* 2. Did this break up the structure into two discrete structures? -> build a new structure
* 3. Did the new structures each contain only one element? -> remove them.
*/
//first: the complex part.
let split = this.findDisconnectedElements();
//first of all: let's remove all found elements from our own array.
split.forEach(object => {
let idx = this.elements.indexOf(object);
this.elements.splice(idx,1);
});
if(split.length==1) {
//this is a single object, so we have to "free it".
let object = split[0];
if(object.hasFeature(Feature.Placeable)) {
let cast = <IPlaceable><any>object;
cast.position = cast.position.addPoint(this.position);
}
engine.objects.push(object);
} else if(split.length > 1) {
//so now we have another set which we have to create a structure for.
let structure = new Structure();
structure.position=this.position;
split.forEach(object => {
structure.addElement(object);
});
engine.objects.push(structure);
}
if(this.elements.length==1) {
let object = this.elements[0];
if(object.hasFeature(Feature.Placeable)) {
let casted = <IPlaceable><any>object;
casted.position = casted.position.addPoint(this.position);
engine.objects.push(object);
let idx = engine.objects.indexOf(this);
engine.objects.splice(idx,1);
}
}
this.hovering=null;
engine.render();
this.pressed=false;
this.shadowedPosition = null;
}
findDisconnectedElements():MapObject[] {
let candidates = this.elements.slice();
let found:MapObject[] = [this.elements[0]];
candidates.splice(0,1);
/*
* here is the idea:
* we pick a pivot element (first round: first wall.)
* we compare every wall with the pivot element and check if they have a linked joint
* if they are linked, remove the found wall from candidates and add it from found.
* afterwards: pick a new pivot element from found and remove that wall from found again.
* repeat until found is empty.
*/
while(found.length>0) {
let pivot = found[0];
let pt = new Point(0,0);
if(pivot.hasFeature(Feature.Placeable)) {
let cast = <IPlaceable><any>pivot;
pt = cast.position;
}
let joints = MapEngine.transformedJoints(<IJoinable><any>pivot,pt);
found.splice(0,1);
this.elements.forEach(object => {
if(object === pivot) {
return;
}
if(candidates.indexOf(object) == -1) {
//we already took care of this one.
return;
}
let pt = new Point(0,0);
if(object.hasFeature(Feature.Placeable)) {
let cast = <IPlaceable><any>object;
pt = cast.position;
}
let objjoints = MapEngine.transformedJoints(<IJoinable><any>object,pt);
for (let idx in joints) {
for(let objidx in objjoints) {
if((joints[idx].subtractPoint(objjoints[objidx]).length() < 2*Number.EPSILON)) {
//we found a joint.
let candidateidx = candidates.indexOf(object);
if(candidateidx != -1 ) {
candidates.splice(candidateidx,1);
}
if(found.indexOf(object) == -1) {
found.push(object);
}
}
}
}
});
}
return candidates;
}
}
}