-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsubmarine.js
126 lines (110 loc) · 3.9 KB
/
submarine.js
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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
import { Bullet } from './bullet.js';
export class Submarine {
constructor(app, screen, sprite, projectileTexture) {
this.sprite = sprite;
this.screen = screen;
this.speed = 5;
this.app = app;
this.projectileTexture = projectileTexture;
this.sprite.scale.x = 0.1;
this.sprite.scale.y = 0.1;
this.sprite.x = -this.screen.x + this.app.screen.width / 2;
this.sprite.y = -this.screen.y + this.app.screen.height / 2;
this.sprite.anchor.set(0.5);
this.dark = new PIXI.Graphics();
this.dark.beginFill(0x000000);
this.dark.alpha = 0.0;
this.dark.drawRect(0, 0, this.app.screen.width*2, this.app.screen.height);
this.dark.endFill();
this.dark.x = 0;
this.dark.y = 0;
this.lightMask = new PIXI.Graphics();
this.lightMask.beginFill(0xFFFFFF);
this.lightMask.drawRect(0, 0, this.app.screen.width, this.app.screen.height);
this.lightMask.endFill();
this.lightMask.beginFill(0);
this.lightMask.drawCircle(this.app.screen.width / 2, this.app.screen.height / 2, 200);
this.lightMask.cut();
this.lightMask.endFill();
// Apply the mask to the dark overlay
this.dark.mask = this.lightMask;
this.screen.addChild(this.dark);
app.ticker.add((delta) => {
this.screen.removeChild(this.dark);
this.screen.removeC
let s = new Set();
s.add("ArrowDown");
this.move(s, 1*delta.deltaTime, false);
this.screen.addChild(this.dark);
});
}
move(keys, magnitude=undefined, or=true) {
let dx = 0;
let dy = 0;
if (keys.has('ArrowUp')) {
dy = 1;
}
if (keys.has('ArrowDown')) {
dy = -1;
}
if (keys.has('ArrowLeft')) {
dx = 1;
}
if (keys.has('ArrowRight')) {
dx = -1;
}
// if (keys.has(' ')) {
// let x = this.sprite.x - this.screen.x - dx * 10;
// let y = this.sprite.y - this.screen.y - dy * 10;
// this.fire(x, y);
// console.log('fire');
// return;
// }
if (dx != 0 || dy != 0) {
let norm = Math.sqrt(dx * dx + dy * dy);
if (magnitude == undefined) {
magnitude = this.speed;
}
let multiplier = magnitude / norm;
dx *= multiplier;
dy *= multiplier;
// Check collisions
let x = this.sprite.x - dx;
let y = this.sprite.y - dy;
let indice = Math.floor(x/20);
if (heightmap[indice] == undefined || -heightmap[indice]*20 <= y) {
return;
}
// Water
if (y < 0 && dy > 0) {
dy = 0;
}
this.screen.x += dx;
this.screen.y += dy;
this.sprite.x -= dx;
this.sprite.y -= dy;
this.dark.x -= dx;
this.dark.y -= dy;
this.lightMask.x -= dx;
this.lightMask.y -= dy;
this.dark.alpha = Math.min(0.8, this.dark.y / 600);
if (!or) {
return;
}
if (dx < 0) {
this.sprite.scale.x = Math.abs(this.sprite.scale.x);
this.sprite.rotation = Math.atan2(dy, dx) + Math.PI;
} else {
this.sprite.scale.x = -Math.abs(this.sprite.scale.x);
this.sprite.rotation = Math.atan2(dy, dx);
}
}
}
fire(x, y) {
if (this.lastFire == undefined || Date.now() - this.lastFire > 500) {
let angle = Math.atan2(y, x);
new Bullet(this.screen, this.projectileTexture, this.sprite.x, this.sprite.y, angle);
this.lastFire = Date.now();
}
}
}