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 let entityDef = this.toEntity();
79 entityDef.ondespawn && entityDef.ondespawn(this);
82 this.map.entities = this.map.entities.filter(entity => {
83 return entity.id != id;
86 clearInterval(this.physicInterval);
87 clearInterval(this.tickInterval);
89 let display = document.getElementById("dragonblocks.entity[" + this.id + "]");
90 display && display.remove();
95 this.tx0 = new Date().getTime() / 1000;
96 this.ty0 = new Date().getTime() / 1000;
106 if (this.x + this.width > this.map.width)
109 return this.collision();
117 if (this.y + this.height > this.map.height)
120 return this.collision();
125 for (let ix = Math.floor(this.x); ix <= Math.ceil(this.x + this.width - 0.01) - 1; ix++)
126 for (let iy = Math.floor(this.y); iy <= Math.ceil(this.y + this.height - 0.01) - 1; iy++)
127 if (this.map.getNode(ix, iy).mobstable)
134 this.tx0 = new Date().getTime() / 1000;
137 this.x = Math.round(this.x * 10) / 10;
142 this.ty0 = new Date().getTime() / 1000;
145 this.y = Math.round(this.y * 10) / 10;
150 let t = new Date().getTime() / 1000;
153 let dtx = t - this.tx0;
156 this.x = this.ax * dtx * dtx + this.vx * dtx + this.x0;
158 this.x = this.vx * dtx + this.x0;
160 if (! this.collisionX()) {
162 this.physicsResetX();
163 this.toEntity().oncollide && this.toEntity().oncollide(this);
167 let dty = t - this.ty0;
170 this.y = this.ay * dty * dty + this.vy * dty + this.y0;
172 this.y = this.vy * dty + this.y0;
174 if (! this.collisionY()) {
176 this.physicsResetY();
177 this.toEntity().oncollide && this.toEntity().oncollide(this);
180 this.y = Math.round(this.y * 50) / 50;
182 this.updateGraphics();
189 for (let ix = Math.floor(this.x); ix <= Math.ceil(this.x + this.width - 0.01) - 1; ix++)
190 for (let iy = Math.floor(this.y); iy <= Math.ceil(this.y + this.height - 0.01) - 1; iy++)
191 if (iy == y && ix == x)
197 let display = document.getElementById("dragonblocks.map").appendChild(document.createElement("div"));
198 display.id = "dragonblocks.entity[" + this.id + "]";
199 display.style.position = "absolute";
200 display.style.width = this.width * dragonblocks.settings.map.scale + "px";
201 display.style.height = this.height * dragonblocks.settings.map.scale + "px";
202 display.style.zIndex = "0";
204 display.addEventListener("mouseover", event => {
205 event.srcElement.style.boxShadow = "0 0 0 1px black inset";
208 display.addEventListener("mouseleave", event => {
209 event.srcElement.style.boxShadow = "none";
214 display.addEventListener("mousedown", event => {
215 let entityDef = self.toEntity();
217 switch (event.which) {
219 entityDef.onpunch && entityDef.onpunch(self);
223 entityDef.onclick && entityDef.onclick(self);
228 this.updateTexture();
229 this.updateGraphics();
232 async updateGraphics()
234 let display = document.getElementById("dragonblocks.entity[" + this.id + "]");
239 display.style.left = (this.x - this.map.displayLeft) * dragonblocks.settings.map.scale + "px";
240 display.style.top = (this.y - this.map.displayTop) * dragonblocks.settings.map.scale + "px";
245 let display = document.getElementById("dragonblocks.entity[" + this.id + "]");
246 display.style.background = dragonblocks.getTexture(this.texture);
247 display.style.backgroundSize = "cover";
252 this.physicsResetX();
253 this.physicsResetY();
260 if (this.vx == -this.horizontalSpeed)
263 if (this.movingRight)
264 this.movingRight = false;
266 this.movingLeft = true;
267 this.physicsResetX();
268 this.vx = -this.horizontalSpeed;
273 if (this.vx == this.horizontalSpeed)
277 this.movingLeft = false;
279 this.movingRight = true;
280 this.physicsResetX();
281 this.vx = this.horizontalSpeed;
286 this.movingLeft = false;
287 this.movingRight = false;
288 this.physicsResetX();
293 if (this.vy == this.verticalSpeed)
297 this.movingDown = false;
299 this.movingDown = true;
300 this.physicsResetY();
301 this.vy = this.verticalSpeed;
306 if (this.vy == -this.verticalSpeed)
310 this.movingUp = false;
312 this.movingUp = true;
313 this.physicsResetY();
314 this.vy = -this.verticalSpeed;
319 this.movingUp = false;
320 this.movingDown = false;
321 this.physicsResetY();
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);
330 if (y - entityY <= 0.01) {
331 let node = this.map.getNode(x, y);
333 if (! node || node.mobstable)
341 if (! this.canJump())
345 this.vy = -this.verticalSpeed;
350 this.jumping = false;
355 this.vy = -this.verticalSpeed;
360 this._gravity = value;
363 this.ay = dragonblocks.settings.physics.gravity;
370 return this._gravity;
377 if (this.movingRight)