-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbugRepeller.js
151 lines (128 loc) · 5.43 KB
/
bugRepeller.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
143
144
145
146
147
148
149
150
151
function preload() {
this.load.image('2', '2.png');
this.load.image('4', '4.png');
this.load.image('platform', 'https://content.codecademy.com/courses/learn-phaser/physics/platform.png');
this.load.image('codey', 'https://content.codecademy.com/courses/learn-phaser/Bug%20Invaders/codey.png');
this.load.image('bugPellet', 'https://content.codecademy.com/courses/learn-phaser/Bug%20Invaders/bugPellet.png');
this.load.image('bugRepellent', 'https://content.codecademy.com/courses/learn-phaser/Bug%20Invaders/bugRepellent.png');
}
// Helper Methods below:
// sortedEnemies() returns an array of enemy sprites sorted by their x coordinate
function sortedEnemies(){
const orderedByXCoord = gameState.enemies.getChildren().sort((a, b) => a.x - b.x);
return orderedByXCoord;
}
// numOfTotalEnemies() returns the number of total enemies
function numOfTotalEnemies() {
const totalEnemies = gameState.enemies.getChildren().length;
return totalEnemies;
}
const gameState = {
enemyVelocity: 1
};
function create() {
// When gameState.active is true, the game is being played and not over. When gameState.active is false, then it's game over
gameState.active = true;
// When gameState.active is false, the game will listen for a pointerup event and restart when the event happens
this.input.on('pointerup', () => {
if (gameState.active === false) {
this.scene.restart();
}
})
// Creating static platforms
const platforms = this.physics.add.staticGroup();
platforms.create(225, 490, 'platform').setScale(1, .3).refreshBody();
// Displays the initial number of bugs, this value is initially hardcoded as 24
gameState.scoreText = this.add.text(175, 482, 'Bugs Left: 24', { fontSize: '15px', fill: '#000000' });
//create monsters
//this array will be full of more assets
let enemiesarray = ['2', '4'];
gameState.enemies = this.physics.add.group();
//hardcoded this for now, maybe someone else can make it better
for (let xVal = 1; xVal <9; xVal++){
for (let yVal = 1; yVal <=2; yVal++){
gameState.enemies.create(50 * xVal, 50 * yVal, '4').setScale(.6).setGravityY(-200);
}
}
for (let xVal = 1; xVal <9; xVal++){
for (let yVal = 3; yVal <=4; yVal++){
gameState.enemies.create(50 * xVal, 50 * yVal, '2').setScale(.6).setGravityY(-200);
}
}
//create pellets (ew)
const pellets = this.physics.add.group();
function genPellet(){
let randomBug = Phaser.Utils.Array.GetRandom(gameState.enemies.getChildren());
pellets.create(randomBug.x,randomBug.y,'bugPellet')
}
gameState.pelletsLoop = this.time.addEvent({
delay:300,
callback:genPellet,
callbackScope: this,
loop: true,
});
// Uses the physics plugin to create Codey
gameState.player = this.physics.add.sprite(225, 450, 'codey').setScale(.5);
// Create Collider objects
gameState.player.setCollideWorldBounds(true);
this.physics.add.collider(gameState.player, platforms);
this.physics.add.collider(pellets,platforms,function(pellet){pellet.destroy()})
this.physics.add.collider(pellets,gameState.player,() => {
gameState.active = false;
gameState.pelletsLoop.destroy();
this.physics.pause();
this.add.text(300,300,'Game Over',{fontSize:'15px', fill: '#000'});
});
// Creates cursor objects to be used in update()
gameState.cursors = this.input.keyboard.createCursorKeys();
// Add new code below:
gameState.bugRepellent = this.physics.add.group();
this.physics.add.collider(gameState.enemies,gameState.bugRepellent,(bug,repellent) => {
bug.destroy();
repellent.destroy();
gameState.scoreText.setText(`Bugs Left: ${numOfTotalEnemies()}`);
});
}
function update() {
if (gameState.active) {
// If the game is active, then players can control Codey
if (gameState.cursors.left.isDown) {
gameState.player.setVelocityX(-160);
} else if (gameState.cursors.right.isDown) {
gameState.player.setVelocityX(160);
} else {
gameState.player.setVelocityX(0);
}
// Execute code if the spacebar key is pressed
if (Phaser.Input.Keyboard.JustDown(gameState.cursors.space)) {
gameState.bugRepellent.create(gameState.player.x,gameState.player.y,'bugRepellent').setGravityY(-400);
}
// Add logic for winning condition and enemy movements below:
gameState.enemies.getChildren().forEach(bug => bug.x = bug.x+gameState.enemyVelocity);
gameState.leftMostBug = sortedEnemies()[0];
gameState.rightMostBug = sortedEnemies()[sortedEnemies().length-1];
if (gameState.leftMostBug.x<10 || gameState.rightMostBug.x>440){
gameState.enemyVelocity = gameState.enemyVelocity * (-1);
gameState.enemies.getChildren().forEach(bug => bug.y = bug.y+10);
}
}
}
const config = {
type: Phaser.AUTO,
width: 450,
height: 500,
backgroundColor: "b9eaff",
physics: {
default: 'arcade',
arcade: {
gravity: { y: 200 },
enableBody: true,
}
},
scene: {
preload,
create,
update
}
};
const game = new Phaser.Game(config);