-
Notifications
You must be signed in to change notification settings - Fork 0
/
bullet.ts
124 lines (106 loc) · 3.93 KB
/
bullet.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
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
import GM from "./gm";
import DefaultScene from "./scenes/default";
import Entity from "./entity";
import Blast from "./blast";
import Ship from "./ship";
import { BulletType } from "./bulletTypeEnum";
import Planet from "./planet";
export default class Bullet implements Entity {
private image: Phaser.Physics.Arcade.Image;
private speed: number = 600;
private startX: number;
private startY: number;
constructor(
private scene: DefaultScene,
private owner: any,
private dx: number,
private dy: number,
public direction: number,
public damage: number,
public bulletType: BulletType
) {
this.image = this.getSprite(bulletType, owner.x, owner.y);
this.startX = owner.x;
this.startY = owner.y;
}
getSprite(bulletType: BulletType, x: number, y: number): Phaser.Physics.Arcade.Image {
switch (bulletType) {
case BulletType.hotTorpedo: {
let image = this.scene.physics.add.image(x, y, 'hot-torpedo');
return image;
}
case BulletType.torpedo: {
return this.scene.physics.add.image(x, y, 'torpedo').setScale(0.6, 0.6);
}
case BulletType.nuke: {
return this.scene.physics.add.image(x, y, 'nuke').setScale(0.3, 0.3);
}
case BulletType.mine: {
let image = this.scene.physics.add.image(x, y, 'mine').setScale(0.2, 0.2);
return image;
}
default: {
return this.scene.physics.add.image(x, y, 'bullet');
}
}
}
update() {
let vx = GM.lengthDirX(this.speed, this.direction);
let vy = GM.lengthDirY(this.speed, this.direction);
if (this.bulletType == BulletType.mine) {
this.image.angle = this.direction += 45;
} else {
this.image.angle = this.direction;
}
this.image.setVelocityX(vx);
this.image.setVelocityY(vy);
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();
return;
}
}
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.image.destroy();
this.scene.removeEntity(this);
let blast;
switch (this.bulletType) {
case BulletType.nuke:
this.scene.soundManager.playFromLocation("nuke-exp", this.x, this.y);
blast = new Blast(this.scene, this.x, this.y, this.direction, this.damage, "nuclear-explosion", 'nuclear-explosion-animation', 15);
break;
case BulletType.laser:
default:
this.scene.soundManager.playFromLocation("explosion", this.x, this.y);
const spriteName = "explosion" + (Math.floor(Math.random() * 4) + 1) + "";
const animationName = spriteName + "-animation";
blast = new Blast(this.scene, this.x, this.y, this.direction, this.damage, spriteName, animationName, 64);
break;
}
this.scene.addEntity(blast);
}
slowUpdate() {
}
get x() {
return this.image.x;
}
get y() {
return this.image.y;
}
};