-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlevel1.js
142 lines (118 loc) · 4.46 KB
/
level1.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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
import Player from "../../prefabs/player";
import Obstacle from "../../prefabs/obstacle";
import EnemyNav from "../../prefabs/enemyNav";
import EnemyBasic from "../../prefabs/enemyBasic";
import Sword from "../../prefabs/sword";
import obstacles from "./obstacles";
import enemyNavs from "./enemyNavs";
import enemies from "./enemies";
//Documentation for Phaser's (2.5.0) states:: phaser.io/docs/2.5.0/Phaser.State.html
class Level1 extends Phaser.State {
//initialization code in the constructor
constructor(game, parent) {
super(game,parent);
}
//Load operations (uses Loader), method called first
preload() {
}
//Setup code, method called after preload
create() {
this.game.world.setBounds(0, 0, 19200, 1080);
this.world.width = 1920;
this.world.height = 1080;
this.game.physics.arcade.gravity.y = 1400;
this.bitmap = this.game.add.bitmapData(this.world.width, this.world.height);
this.game.add.image(0, 0, this.bitmap);
this.player = new Player(this.game, this.game.world.centerX, this.game.world.centerY);
this.game.add.existing(this.player);
this.game.camera.follow(this.player, Phaser.Camera.FOLLOW_TOPDOWN);
//const sword = new Sword(this.game, this.player.body.position.x + this.player.width, this.player.body.position.y);
const sword = new Sword(this.game, Math.abs(this.player.width / 2), this.player.height / 2);
sword.y -= sword.height / 2;
sword.kill();
this.player.sword = sword;
this.player.addChild(sword);
//this.game.add.existing(sword);
this.obstacles = [];
for (const obstacle of obstacles) {
const newObstacle = new Obstacle(this.game, obstacle.x, obstacle.y, obstacle.width, obstacle.height, "crosshairs");
this.game.add.existing(newObstacle);
this.obstacles.push(newObstacle);
}
this.enemyNavs = [];
for (const enemyNav of enemyNavs) {
const newEnemyNav = new EnemyNav(this.game, enemyNav.x, enemyNav.y, enemyNav.width, enemyNav.height);
this.game.add.existing(newEnemyNav);
this.enemyNavs.push(newEnemyNav);
}
this.enemies = [];
for (const enemy of enemies) {
const newEnemy = new EnemyBasic(this.game, enemy.x, enemy.y, enemy.vel);
newEnemy.cacheObstacles(this.obstacles);
newEnemy.cachePlayer(this.player);
this.game.add.existing(newEnemy);
this.enemies.push(newEnemy);
}
this.bullets = [];
}
//Code ran on each frame of game
update() {
this.handleBulletCollisions();
this.game.physics.arcade.collide(this.player, this.obstacles, this.player.grounded, null, this.player);
this.game.physics.arcade.collide(this.enemies, this.obstacles);
this.game.physics.arcade.collide(this.enemies, this.enemyNavs, (enemy, enemyNav) => {
enemy.reverseDirection(enemyNav);
});
const remainingEnemies = [];
for (const enemy of this.enemies) {
if (this.game.physics.arcade.overlap(this.player.sword, enemy, (sword, enemy) => {
enemy.eliminate();
}) === false) {
remainingEnemies.push(enemy);
}
}
//this.game.physics.arcade.collide(this.player, this.enemies);
const linesToPlayer = [];
for (const enemy of this.enemies) {
if (enemy.losToPlayer !== null && enemy.isShooting === false) {
linesToPlayer.push(enemy.losToPlayer);
}
}
this.drawLines(linesToPlayer);
this.enemies = remainingEnemies;
}
//Handles bullet collisions with obstacles and player
handleBulletCollisions() {
for (const enemy of this.enemies) {
this.game.physics.arcade.collide(enemy.weapon.bullets, this.player, (player, bullet) => {
bullet.kill();
this.game.state.start("gameover");
}, null, this);
this.game.physics.arcade.collide(enemy.weapon.bullets, this.obstacles, (obstacle, bullet) => {
bullet.kill();
});
}
}
//Draws LOS's to player
drawLines(linesToPlayer) {
this.bitmap.context.clearRect(0, 0, this.world.width, this.world.height);
for (const line of linesToPlayer) {
this.bitmap.context.strokeStyle = "red";
this.bitmap.context.beginPath();
this.bitmap.context.moveTo(line.start.x, line.start.y);
this.bitmap.context.lineTo(line.end.x, line.end.y);
this.bitmap.context.stroke();
}
this.bitmap.dirty = true;
}
//Called when game is paused
paused() {
}
//You're able to do any final post-processing style effects here.
render() {
}
//Called when switching to a new state
shutdown() {
}
}
export default Level1;