4 * Copyright 2020 Elias Fleckenstein <eliasfleckenstein@web.de>
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.
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.
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,
24 dragonblocks.SpawnedEntity = class
26 constructor(def, map, x, y)
28 dblib.copy(this, def);
31 if (def instanceof dragonblocks.Entity) {
32 this.id = dragonblocks.getToken();
33 this.jumping = this.movingRight = this.movingLeft = this.movingUp = this.movingDown = false;
40 this.meta = this.meta || {};
41 this.toEntity().oninit && this.toEntity().oninit(this);
44 this.restorePhysics();
49 this.tickInterval = setInterval(_ => {
53 this.physicInterval = setInterval(_=>{
57 let entityDef = this.toEntity();
58 entityDef.onspawn && entityDef.onspawn(this);
60 addEventListener("focus", _ => {
61 self.restorePhysics();
64 addEventListener("blur", _ => {
65 self.restorePhysics();
68 map.entities.push(this);
73 return dragonblocks.entities[this.name];
78 this.tmp.display = map.entityContainer.appendChild(this.display);
85 let entityDef = this.toEntity();
86 entityDef.ondespawn && entityDef.ondespawn(this);
89 this.map.entities = this.map.entities.filter(entity => {
90 return entity.id != id;
93 clearInterval(this.physicInterval);
94 clearInterval(this.tickInterval);
96 this.tmp.display && this.tmp.display.remove();
101 this.tx0 = new Date().getTime() / 1000;
102 this.ty0 = new Date().getTime() / 1000;
112 if (this.x + this.width > this.map.width)
115 return this.collision();
123 if (this.y + this.height > this.map.height)
126 return this.collision();
131 for (let ix = Math.floor(this.x); ix <= Math.ceil(this.x + this.width - 0.01) - 1; ix++)
132 for (let iy = Math.floor(this.y); iy <= Math.ceil(this.y + this.height - 0.01) - 1; iy++)
133 if (this.map.getNode(ix, iy).mobstable)
140 this.tx0 = new Date().getTime() / 1000;
143 this.x = Math.round(this.x * 10) / 10;
148 this.ty0 = new Date().getTime() / 1000;
151 this.y = Math.round(this.y * 10) / 10;
156 let t = new Date().getTime() / 1000;
159 let dtx = t - this.tx0;
162 this.x = this.ax * dtx * dtx + this.vx * dtx + this.x0;
164 this.x = this.vx * dtx + this.x0;
166 if (! this.collisionX()) {
168 this.physicsResetX();
169 this.toEntity().oncollide && this.toEntity().oncollide(this);
173 let dty = t - this.ty0;
176 this.y = this.ay * dty * dty + this.vy * dty + this.y0;
178 this.y = this.vy * dty + this.y0;
180 if (! this.collisionY()) {
182 this.physicsResetY();
183 this.toEntity().oncollide && this.toEntity().oncollide(this);
186 this.y = Math.round(this.y * 50) / 50;
188 this.updateGraphics();
195 for (let ix = Math.floor(this.x); ix <= Math.ceil(this.x + this.width - 0.01) - 1; ix++)
196 for (let iy = Math.floor(this.y); iy <= Math.ceil(this.y + this.height - 0.01) - 1; iy++)
197 if (iy == y && ix == x)
203 this.tmp.display = this.map.entityContainer.appendChild(document.createElement("div"));
204 this.tmp.display.style.position = "absolute";
205 this.tmp.display.style.width = this.width * dragonblocks.settings.mapDisplay.scale + "px";
206 this.tmp.display.style.height = this.height * dragonblocks.settings.mapDisplay.scale + "px";
207 this.tmp.display.style.zIndex = "0";
209 this.tmp.display.addEventListener("mouseover", event => {
210 event.srcElement.style.boxShadow = "0 0 0 1px black inset";
213 this.tmp.display.addEventListener("mouseleave", event => {
214 event.srcElement.style.boxShadow = "none";
219 this.tmp.display.addEventListener("mousedown", event => {
220 let entityDef = self.toEntity();
222 switch (event.which) {
224 entityDef.onpunch && entityDef.onpunch(self);
228 entityDef.onclick && entityDef.onclick(self);
233 this.updateTexture();
234 this.updateGraphics();
237 async updateGraphics()
239 if (! this.tmp.display)
242 this.tmp.display.style.left = this.x * dragonblocks.settings.mapDisplay.scale + "px";
243 this.tmp.display.style.top = this.y * dragonblocks.settings.mapDisplay.scale + "px";
248 this.tmp.display.style.background = dragonblocks.getTexture(this.texture);
249 this.tmp.display.style.backgroundSize = "cover";
254 this.physicsResetX();
255 this.physicsResetY();
262 if (this.vx == -this.horizontalSpeed)
265 if (this.movingRight)
266 this.movingRight = false;
268 this.movingLeft = true;
269 this.physicsResetX();
270 this.vx = -this.horizontalSpeed;
275 if (this.vx == this.horizontalSpeed)
279 this.movingLeft = false;
281 this.movingRight = true;
282 this.physicsResetX();
283 this.vx = this.horizontalSpeed;
288 this.movingLeft = false;
289 this.movingRight = false;
290 this.physicsResetX();
295 if (this.vy == this.verticalSpeed)
299 this.movingDown = false;
301 this.movingDown = true;
302 this.physicsResetY();
303 this.vy = this.verticalSpeed;
308 if (this.vy == -this.verticalSpeed)
312 this.movingUp = false;
314 this.movingUp = true;
315 this.physicsResetY();
316 this.vy = -this.verticalSpeed;
321 this.movingUp = false;
322 this.movingDown = false;
323 this.physicsResetY();
328 for (let x = Math.floor(this.x); x <= Math.ceil(this.x + this.width - 0.01) - 1; x++) {
329 let entityY = this.y + this.height;
330 let y = Math.ceil(entityY);
332 if (y - entityY <= 0.01) {
333 let node = this.map.getNode(x, y);
335 if (! node || node.mobstable)
343 if (! this.canJump())
347 this.vy = -this.verticalSpeed;
352 this.jumping = false;
357 this.vy = -this.verticalSpeed;
362 this._gravity = value;
365 this.ay = dragonblocks.settings.physics.gravity;
372 return this._gravity;
379 if (this.movingRight)