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.jumping = this.movingRight = this.movingLeft = this.movingUp = this.movingDown = false;
39 this.meta = this.meta || {};
40 this.toEntity().oninit && this.toEntity().oninit(this);
43 this.restorePhysics();
48 this.tickInterval = setInterval(_ => {
52 this.physicInterval = setInterval(_=>{
56 let entityDef = this.toEntity();
57 entityDef.onspawn && entityDef.onspawn(this);
59 addEventListener("focus", _ => {
60 self.restorePhysics();
63 addEventListener("blur", _ => {
64 self.restorePhysics();
67 map.entities.push(this);
72 return dragonblocks.entities[this.name];
77 this.tmp.display = map.entityContainer.appendChild(this.display);
84 let entityDef = this.toEntity();
85 entityDef.ondespawn && entityDef.ondespawn(this);
88 this.map.entities = this.map.entities.filter(entity => {
89 return entity != self;
92 clearInterval(this.physicInterval);
93 clearInterval(this.tickInterval);
95 this.tmp.display && this.tmp.display.remove();
100 this.tx0 = new Date().getTime() / 1000;
101 this.ty0 = new Date().getTime() / 1000;
111 if (this.x + this.width > this.map.width)
114 return this.collision();
122 if (this.y + this.height > this.map.height)
125 return this.collision();
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)
139 this.tx0 = new Date().getTime() / 1000;
142 this.x = Math.round(this.x * 10) / 10;
147 this.ty0 = new Date().getTime() / 1000;
150 this.y = Math.round(this.y * 10) / 10;
155 let t = new Date().getTime() / 1000;
158 let dtx = t - this.tx0;
161 this.x = this.ax * dtx * dtx + this.vx * dtx + this.x0;
163 this.x = this.vx * dtx + this.x0;
165 if (! this.collisionX()) {
167 this.physicsResetX();
168 this.toEntity().oncollide && this.toEntity().oncollide(this);
172 let dty = t - this.ty0;
175 this.y = this.ay * dty * dty + this.vy * dty + this.y0;
177 this.y = this.vy * dty + this.y0;
179 if (! this.collisionY()) {
181 this.physicsResetY();
182 this.toEntity().oncollide && this.toEntity().oncollide(this);
185 this.y = Math.round(this.y * 50) / 50;
187 this.updateGraphics();
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)
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";
208 this.tmp.display.addEventListener("mouseover", event => {
209 event.srcElement.style.boxShadow = "0 0 0 1px black inset";
212 this.tmp.display.addEventListener("mouseleave", event => {
213 event.srcElement.style.boxShadow = "none";
218 this.tmp.display.addEventListener("mousedown", event => {
219 let entityDef = self.toEntity();
221 switch (event.which) {
223 entityDef.onpunch && entityDef.onpunch(self);
227 entityDef.onclick && entityDef.onclick(self);
232 this.updateTexture();
233 this.updateGraphics();
236 async updateGraphics()
238 if (! this.tmp.display)
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";
247 this.tmp.display.style.background = dragonblocks.getTexture(this.texture);
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)