]> git.lizzy.rs Git - dragonblocks.git/blob - engine/spawned_entity.js
47969059010b806a628d804337f73897193f60e9
[dragonblocks.git] / engine / spawned_entity.js
1 /*
2  * spawned_entity.js
3  *
4  * Copyright 2020 Elias Fleckenstein <eliasfleckenstein@web.de>
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
19  * MA 02110-1301, USA.
20  *
21  *
22  */
23
24 dragonblocks.SpawnedEntity = class
25 {
26         constructor(def, map, x, y)
27         {
28                 dblib.copy(this, def);
29                 this.tmp = {map};
30
31                 if (def instanceof dragonblocks.Entity) {
32                         this.jumping = this.movingRight = this.movingLeft = this.movingUp = this.movingDown = false;
33                         this.x = x;
34                         this.y = y;
35                         this.ax = 0;
36                         this.ay = 0;
37                         this.physicsResetX();
38                         this.physicsResetY();
39                         this.meta = this.meta || {};
40                         this.toEntity().oninit && this.toEntity().oninit(this);
41                 }
42
43                 this.restorePhysics();
44                 this.addGraphics();
45
46                 let self = this;
47
48                 this.tickInterval = setInterval(_ => {
49                         self.tick();
50                 }, 100);
51
52                 this.physicInterval = setInterval(_=>{
53                         self.physicsTick();
54                 });
55
56                 let entityDef = this.toEntity();
57                 entityDef.onspawn && entityDef.onspawn(this);
58
59                 addEventListener("focus", _ => {
60                         self.restorePhysics();
61                 });
62
63                 addEventListener("blur", _ => {
64                         self.restorePhysics();
65                 });
66
67                 map.entities.push(this);
68         }
69
70         toEntity()
71         {
72                 return dragonblocks.entities[this.name];
73         }
74
75         setMap(map, x, y)
76         {
77                 this.tmp.display = map.entityContainer.appendChild(this.display);
78                 this.tmp.map = map;
79                 this.teleport(x, y);
80         }
81
82         despawn()
83         {
84                 let entityDef = this.toEntity();
85                 entityDef.ondespawn && entityDef.ondespawn(this);
86
87                 let self = this;
88                 this.map.entities = this.map.entities.filter(entity => {
89                         return entity != self;
90                 });
91
92                 clearInterval(this.physicInterval);
93                 clearInterval(this.tickInterval);
94
95                 this.tmp.display && this.tmp.display.remove();
96         }
97
98         restorePhysics()
99         {
100                 this.tx0 = new Date().getTime() / 1000;
101                 this.ty0 = new Date().getTime() / 1000;
102                 this.x0 = this.x;
103                 this.y0 = this.y;
104         }
105
106         collisionX()
107         {
108                 if (this.x < 0)
109                         return false;
110
111                 if (this.x + this.width > this.map.width)
112                         return false;
113
114                 return this.collision();
115         }
116
117         collisionY()
118         {
119                 if (this.y < 0)
120                         return false;
121
122                 if (this.y + this.height > this.map.height)
123                         return false;
124
125                 return this.collision();
126         }
127
128         collision()
129         {
130                 for (let ix = Math.floor(this.x); ix <= Math.ceil(this.x + this.width - 0.01) - 1; ix++)
131                         for (let iy = Math.floor(this.y); iy <= Math.ceil(this.y + this.height - 0.01) - 1; iy++)
132                                 if (this.map.getNode(ix, iy).mobstable)
133                                         return false;
134                 return true;
135         }
136
137         physicsResetX()
138         {
139                 this.tx0 = new Date().getTime() / 1000;
140                 this.vx = 0;
141                 this.x0 = this.x;
142                 this.x = Math.round(this.x * 10) / 10;
143         }
144
145         physicsResetY()
146         {
147                 this.ty0 = new Date().getTime() / 1000;
148                 this.vy = 0;
149                 this.y0 = this.y;
150                 this.y = Math.round(this.y * 10) / 10;
151         }
152
153         physicsTick()
154         {
155                 let t = new Date().getTime() / 1000;
156
157                 let oldX = this.x;
158                 let dtx = t - this.tx0;
159
160                 if (this.ax)
161                         this.x = this.ax * dtx * dtx + this.vx * dtx + this.x0;
162                 else if (this.vx)
163                         this.x = this.vx * dtx + this.x0;
164
165                 if (! this.collisionX()) {
166                         this.x = oldX;
167                         this.physicsResetX();
168                         this.toEntity().oncollide && this.toEntity().oncollide(this);
169                 }
170
171                 let oldY = this.y;
172                 let dty = t - this.ty0;
173
174                 if (this.ay)
175                         this.y = this.ay * dty * dty + this.vy * dty + this.y0;
176                 else if (this.vy)
177                         this.y = this.vy * dty + this.y0;
178
179                 if (! this.collisionY()) {
180                         this.y = oldY;
181                         this.physicsResetY();
182                         this.toEntity().oncollide && this.toEntity().oncollide(this);
183                 }
184
185                 this.y = Math.round(this.y * 50) / 50;
186
187                 this.updateGraphics();
188         }
189
190         touch(map, x, y)
191         {
192                 if (map != this.map)
193                         return false;
194                 for (let ix = Math.floor(this.x); ix <= Math.ceil(this.x + this.width - 0.01) - 1; ix++)
195                         for (let iy = Math.floor(this.y); iy <= Math.ceil(this.y + this.height - 0.01) - 1; iy++)
196                                 if (iy == y && ix == x)
197                                         return true;
198         }
199
200         addGraphics(obj)
201         {
202                 this.tmp.display = this.map.entityContainer.appendChild(document.createElement("div"));
203                 this.tmp.display.style.position = "absolute";
204                 this.tmp.display.style.width = this.width * dragonblocks.settings.mapDisplay.scale + "px";
205                 this.tmp.display.style.height = this.height * dragonblocks.settings.mapDisplay.scale + "px";
206                 this.tmp.display.style.zIndex = "0";
207
208                 this.tmp.display.addEventListener("mouseover", event => {
209                         event.srcElement.style.boxShadow = "0 0 0 1px black inset";
210                 });
211
212                 this.tmp.display.addEventListener("mouseleave", event => {
213                         event.srcElement.style.boxShadow = "none";
214                 });
215
216                 let self = this;
217
218                 this.tmp.display.addEventListener("mousedown", event => {
219                         let entityDef = self.toEntity();
220
221                         switch (event.which) {
222                                 case 1:
223                                         entityDef.onpunch && entityDef.onpunch(self);
224                                         break;
225
226                                 case 3:
227                                         entityDef.onclick && entityDef.onclick(self);
228                                         break;
229                         }
230                 });
231
232                 this.updateTexture();
233                 this.updateGraphics();
234         }
235
236         async updateGraphics()
237         {
238                 if (! this.tmp.display)
239                         return;
240
241                 this.tmp.display.style.left = this.x * dragonblocks.settings.mapDisplay.scale + "px";
242                 this.tmp.display.style.top = this.y * dragonblocks.settings.mapDisplay.scale + "px";
243         }
244
245         updateTexture()
246         {
247                 this.tmp.display.style.background = dragonblocks.getTexture(this.texture);
248         }
249
250         teleport(x, y)
251         {
252                 this.physicsResetX();
253                 this.physicsResetY();
254                 this.x = x;
255                 this.y = y;
256         }
257
258         moveLeft()
259         {
260                 if (this.vx == -this.horizontalSpeed)
261                         return;
262
263                 if (this.movingRight)
264                         this.movingRight = false;
265
266                 this.movingLeft = true;
267                 this.physicsResetX();
268                 this.vx = -this.horizontalSpeed;
269         }
270
271         moveRight()
272         {
273                 if (this.vx == this.horizontalSpeed)
274                         return;
275
276                 if (this.movingLeft)
277                         this.movingLeft = false;
278
279                 this.movingRight = true;
280                 this.physicsResetX();
281                 this.vx = this.horizontalSpeed;
282         }
283
284         stop()
285         {
286                 this.movingLeft = false;
287                 this.movingRight = false;
288                 this.physicsResetX();
289         }
290
291         moveDown()
292         {
293                 if (this.vy == this.verticalSpeed)
294                         return;
295
296                 if (this.movingDown)
297                         this.movingDown = false;
298
299                 this.movingDown = true;
300                 this.physicsResetY();
301                 this.vy = this.verticalSpeed;
302         }
303
304         moveUp()
305         {
306                 if (this.vy == -this.verticalSpeed)
307                         return;
308
309                 if (this.movingUp)
310                         this.movingUp = false;
311
312                 this.movingUp = true;
313                 this.physicsResetY();
314                 this.vy = -this.verticalSpeed;
315         }
316
317         stopFly()
318         {
319                 this.movingUp = false;
320                 this.movingDown = false;
321                 this.physicsResetY();
322         }
323
324         canJump()
325         {
326                 for (let x = Math.floor(this.x); x <= Math.ceil(this.x + this.width - 0.01) - 1; x++) {
327                         let entityY = this.y + this.height;
328                         let y = Math.ceil(entityY);
329
330                         if (y - entityY <= 0.01) {
331                                 let node = this.map.getNode(x, y);
332
333                                 if (! node || node.mobstable)
334                                         return true;
335                         }
336                 }
337         }
338
339         jump()
340         {
341                 if (! this.canJump())
342                         return;
343
344                 this.jumping = true;
345                 this.vy = -this.verticalSpeed;
346         }
347
348         stopJump()
349         {
350                 this.jumping = false;
351         }
352
353         jumpOnce()
354         {
355                 this.vy = -this.verticalSpeed;
356         }
357
358         set gravity(value)
359         {
360                 this._gravity = value;
361
362                 if (this._gravity)
363                         this.ay = dragonblocks.settings.physics.gravity;
364                 else
365                         this.ay = 0;
366         }
367
368         get gravity()
369         {
370                 return this._gravity;
371         }
372
373         tick()
374         {
375                 if (this.movingLeft)
376                         this.moveLeft();
377                 if (this.movingRight)
378                         this.moveRight();
379                 if (this.movingUp)
380                         this.moveUp();
381                 if (this.movingDown)
382                         this.moveDown();
383
384                 if (this.jumping)
385                         this.jump();
386
387                 if (this.gravity)
388                         this.gravity = true;
389         }
390
391         get map()
392         {
393                 return this.tmp.map;
394         }
395 };