multiplayer-test/common/prop.js

49 lines
1.5 KiB
JavaScript
Raw Normal View History

2024-08-30 02:56:03 +00:00
import Player from "./player.js";
import { WORLD_SIZE } from "./world.js";
export default class Prop {
static SIZE = 50.0;
constructor(name, x, y, colour, sprite) {
this.x = x;
this.y = y;
this.xv = 0.0;
this.yv = 0.0;
this.draw_x = x;
this.draw_y = y;
this.name = name;
this.colour = colour;
this.sprite = sprite;
}
update(delta, players) {
players.forEach(player => {
if (this.x - Prop.SIZE / 2 < player.x + Player.SIZE / 2 &&
this.x + Prop.SIZE / 2 > player.x - Player.SIZE / 2 &&
this.y - Prop.SIZE / 2 < player.y + Player.SIZE / 2 &&
this.y + Prop.SIZE / 2 > player.y - Player.SIZE / 2) {
this.xv += player.in_x * Player.SPEED * delta * 20.0;
this.yv += player.in_y * Player.SPEED * delta * 20.0;
}
});
if (this.xv != 0) this.x += this.xv * delta;
if (this.yv != 0) this.y += this.yv * delta;
this.xv *= 0.95;
this.yv *= 0.95;
// bounce off walls
if (this.x + Prop.SIZE / 2 > WORLD_SIZE ||
this.x - Prop.SIZE / 2 < 0.0) {
this.x = Math.min(Math.max(this.x, 0.0), WORLD_SIZE);
this.xv *= -1;
}
if (this.y + Prop.SIZE / 2 > WORLD_SIZE ||
this.y - Prop.SIZE / 2 < 0.0) {
this.y = Math.min(Math.max(this.y, 0.0), WORLD_SIZE);
this.yv *= -1;
}
}
}