1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
|
import Anime from "./Animation";
import Bullet from "./drawables/Bullet";
import { BULLET_VEL, canvas, ctx, FIRE_DELAY, PLAYER_VEL } from "./consts";
import Drawable, { Rectangle } from "./Drawable";
import { keys } from "./Events";
import { IMGS } from "./Images";
export default class Player extends Drawable {
static startx = 250;
static starty = 100;
width = IMGS.playerUp.width;
height = IMGS.playerUp.height;
sprite = IMGS.empty;
xvel = 0;
yvel = 0;
lastShoot = 0;
stopAtBorder = true;
squares: Rectangle[] = [new Rectangle(this)]
lifes = 3;
power = 2;
constructor() {
super(Player.startx, Player.starty)
}
shoot(bullet: Bullet[]) {
if (!keys.fire || keys.repeat) return;
if (Date.now() - this.lastShoot < FIRE_DELAY) return;
this.lastShoot = Date.now()
bullet.push(new Bullet(this.x + (this.width / 2) - Bullet.width / 2, this.y - Bullet.height, 0, -BULLET_VEL, true))
}
move() {
if (+keys.right ^ +keys.left) {
keys.right ? this.xvel = PLAYER_VEL : this.xvel = -PLAYER_VEL;
} else {
this.xvel = 0
}
if (+keys.up ^ +keys.down) {
keys.down ? this.yvel = PLAYER_VEL : this.yvel = -PLAYER_VEL;
} else {
this.yvel = 0
}
if (keys.left && keys.right) this.sprite = IMGS.playerUp
else if (keys.right) this.sprite = IMGS.playerRight
else if (keys.left) this.sprite = IMGS.playerLeft
else this.sprite = IMGS.playerUp
if (this.x + this.xvel <= 35) this.sprite = IMGS.playerUp
else if (this.x + this.xvel + this.width >= canvas.width - 35) this._x = canvas.width - 35 - this.width;
this.width = this.sprite.width;
this.height = this.sprite.height;
this.squares = [new Rectangle(this)]
this.y += this.yvel;
this.x += this.xvel;
return true;
}
draw() {
this.sprite = this.sprite ?? IMGS.playerUp;
try {
ctx.drawImage(this.sprite, this.x, this.y)
} catch (error) {
console.log(error);
}
}
get deathAnimation() { return new Anime(this, ["red", "green", "blue"]) }
}
|