-
Notifications
You must be signed in to change notification settings - Fork 0
/
game.js
135 lines (94 loc) · 3.13 KB
/
game.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
// Initialize Phaser, and creates a 400x490px game
var game = new Phaser.Game(800, 600, Phaser.AUTO, 'game_div', { preload: preload, create: create, update: update, render: render });
function preload() {
game.load.image('stars', 'assets/starfield.jpg');
game.load.image('mike', 'assets/mike.png');
game.load.image('ship', 'assets/boat.png');
game.load.image('baddie', 'assets/reaver.png');
}
var starfield;
var mikeFrog;
var serenity;
var cursors;
var reaver;
var score = 0;
var scoreText;
function create() {
game.world.setBounds(0, 0, 1920, 1200);
game.physics.startSystem(Phaser.Physics.P2JS);
game.physics.p2.setImpactEvents(true);
game.physics.p2.defaultRestitution = 0.8;
var playerCollisionGroup = game.physics.p2.createCollisionGroup();
var frogCollisionGroup = game.physics.p2.createCollisionGroup();
game.physics.p2.updateBoundsCollisionGroup();
starfield = game.add.tileSprite(0, 0, 800, 600, 'stars');
starfield.fixedToCamera = true;
mikeFrog = game.add.group();
mikeFrog.enableBody = true;
mikeFrog.physicsBodyType = Phaser.Physics.P2JS;
for (var i = 0; i < 25; i++)
{
var frogs = mikeFrog.create(game.world.randomX, game.world.randomY, 'mike');
frogs.body.setCircle(16);
frogs.body.setCollisionGroup(frogCollisionGroup);
frogs.body.collides([frogCollisionGroup, playerCollisionGroup]);
}
serenity = game.add.sprite(200, 200, 'ship')
game.physics.p2.enable(serenity, false);
serenity.body.setCircle(28);
game.camera.follow(serenity);
serenity.body.setCollisionGroup(playerCollisionGroup);
serenity.body.collides(frogCollisionGroup, hitMikeFrog, this);
scoreText = game.add.text(20, 20, score + ' of 25 Mikes Collected', { fontSize: '12px', fill: '#fff' });
scoreText.fixedToCamera = true;
cursors = game.input.keyboard.createCursorKeys();
}
function hitMikeFrog (body1, body2) {
body2.sprite.kill();
score += 1;
scoreText.setText(score + ' of 25 Mikes Collected');
if (score == 25) {
body1.sprite.kill();
scoreText.setText("You saved all the Mikes!! \nClick to Replay");
game.input.onTap.addOnce(restart,this);
}
}
function restart () {
score = 0;
create();
}
function update() {
if (cursors.left.isDown)
{
serenity.body.rotateLeft(100);
}
else if (cursors.right.isDown)
{
serenity.body.rotateRight(100);
}
else
{
serenity.body.setZeroRotation();
}
if (cursors.up.isDown)
{
serenity.body.thrust(400);
}
else if (cursors.down.isDown)
{
serenity.body.reverse(400);
}
if (!game.camera.atLimit.x)
{
starfield.tilePosition.x += (serenity.body.velocity.x * 16) * game.time.physicsElapsed;
}
if (!game.camera.atLimit.y)
{
starfield.tilePosition.y += (serenity.body.velocity.y * 16) * game.time.physicsElapsed;
}
}
function render() {
/*
game.debug.text('Catch the Mikes!!', 32, 32);
*/
}