Преглед изворни кода

improved bullet collision detection

master
mort пре 8 година
родитељ
комит
1c16642747
1 измењених фајлова са 16 додато и 3 уклоњено
  1. 16
    3
      js/game.js

+ 16
- 3
js/game.js Прегледај датотеку



intersectsPoint(e) { intersectsPoint(e) {
let rect = this.boundingRect; let rect = this.boundingRect;
let r = new Rectangle(e.pos.x, e.pos.y, e.vel.x, e.vel.y);
return rect.intersects(r);
return ( return (
e.pos.x > rect.x && e.pos.x < rect.x + rect.width && e.pos.x > rect.x && e.pos.x < rect.x + rect.width &&
e.pos.y > rect.y && e.pos.y < rect.y + rect.height e.pos.y > rect.y && e.pos.y < rect.y + rect.height
}); });


sock.on("close", () => this.despawn()); sock.on("close", () => this.despawn());

this.healthInterval = setInterval(() => {
if (this.health < 100)
this.health += 1;
}, 200);
}

despawn() {
super.despawn();
clearInterval(this.healthInterval);
} }


update(dt) { update(dt) {
if (this.keys.right) if (this.keys.right)
this.rotForce += 0.03; this.rotForce += 0.03;


//Shoot
if (this.keys.shoot && this.canShoot) { if (this.keys.shoot && this.canShoot) {
let vel = new Vec2(0, -1).rotate(this.rot).add(this.vel); let vel = new Vec2(0, -1).rotate(this.rot).add(this.vel);


let b = new Bullet(pos, vel, this.id, this.game.id, this.game); let b = new Bullet(pos, vel, this.id, this.game.id, this.game);
this.game.spawn(b); this.game.spawn(b);
this.canShoot = false; this.canShoot = false;
setTimeout(() => this.canShoot = true, 100);
setTimeout(() => this.canShoot = true, 300);
} }


f.rotate(this.rot); f.rotate(this.rot);
this.force(f.x, f.y); this.force(f.x, f.y);


this.vel.scale(0.9); this.vel.scale(0.9);
this.rotVel *= 0.84;
this.rotVel *= 0.8;


//Detect collissions //Detect collissions
this.game.entities.forEach((e) => { this.game.entities.forEach((e) => {
if (e instanceof Bullet) { if (e instanceof Bullet) {
if (e.ownerId !== this.id && this.intersectsPoint(e)) { if (e.ownerId !== this.id && this.intersectsPoint(e)) {
this.health -= 10;
this.health -= 20;
e.despawn(); e.despawn();
if (this.health <= 0) if (this.health <= 0)
this.despawn(); this.despawn();

Loading…
Откажи
Сачувај