-
Notifications
You must be signed in to change notification settings - Fork 0
/
laser.ts
99 lines (83 loc) · 2.92 KB
/
laser.ts
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
import GM from "./gm";
import DefaultScene from "./scenes/default";
import Entity from "./entity";
import Blast from "./blast";
import Ship from "./ship";
import Planet from "./planet";
export default class Laser implements Entity{
private image: Phaser.Physics.Arcade.Image;
private speed: number = 800;
private line: Phaser.Geom.Line;
private graphics: Phaser.GameObjects.Graphics;
private startX: number;
private startY: number;
constructor(
private scene: DefaultScene,
private owner: any,
private dx: number,
private dy: number,
private direction: number,
) {
this.image = this.scene.physics.add.image(owner.x, owner.y, 'laser');
this.startX = owner.x;
this.startY = owner.y;
this.graphics = this.scene.add.graphics({
lineStyle: {
width: 1,
color: 0x00ff00,
}
});
this.graphics.depth = 300;
this.line = new Phaser.Geom.Line(this.x, this.y, this.owner.x, this.owner.y);
// audio
this.scene.soundManager.play("laser");
}
update() {
let vx = GM.lengthDirX(this.speed, this.direction);
let vy = GM.lengthDirY(this.speed, this.direction);
this.image.angle = this.direction;
this.image.setVelocityX(vx);
this.image.setVelocityY(vy);
this.line.x1 = this.x;
this.line.y1 = this.y;
this.line.x2 = this.owner.x;
this.line.y2 = this.owner.y;
this.graphics.clear();
this.graphics.strokeLineShape(this.line);
const distance = GM.pointDistance(this.owner.x, this.owner.y, this.x, this.y);
const maxDistance = GM.pointDistance(this.startX, this.startY, this.dx, this.dy);
if (distance >= maxDistance) {
this.blast();
}
for (const entity of this.scene.entities) {
if (entity instanceof Ship && entity != this.owner) {
const distance = GM.pointDistance(this.x, this.y, entity.x, entity.y);
if (distance < 25) {
this.blast();
}
}
if (entity instanceof Planet && entity.species != this.owner.species) {
const distance = GM.pointDistance(this.x, this.y, entity.x, entity.y);
if (distance < 25) {
this.blast();
return;
}
}
}
}
private blast() {
this.graphics.destroy();
this.scene.removeEntity(this);
const blast = new Blast(this.scene, this.x, this.y, this.direction, 0.19, "explosion1", "explosion1-animation", 10);
this.scene.addEntity(blast);
this.scene.soundManager.playFromLocation('laser-hit', this.x, this.y);
}
slowUpdate() {
}
get x() {
return this.image.x;
}
get y() {
return this.image.y;
}
};