-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathescape_the_gabo.js
150 lines (119 loc) · 3.81 KB
/
escape_the_gabo.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
var game = new Phaser.Game(800, 600, Phaser.AUTO, 'phaser', {
preload: preload,
create: create,
update: update,
render: render
});
var background;
var ian;
var gabos;
var cursors;
var timeCounter = 0;
var gaboSound;
var gameEnded;
var GABO_COUNT = 13;
function preload() {
game.load.image('bg', 'media/bg_small.jpg');
game.load.image('ian', 'media/ian_small_trans.png');
game.load.image('gabo', 'media/gabo_small_trans.png');
game.load.audio('gaboSound', 'media/gabo.wav');
};
function create() {
game.physics.startSystem(Phaser.Physics.P2JS);
game.physics.p2.setImpactEvents(true);
game.physics.p2.restitution = 1.05;
var playerCollisionGroup = game.physics.p2.createCollisionGroup();
var gaboCollisionGroup = game.physics.p2.createCollisionGroup();
game.physics.p2.updateBoundsCollisionGroup();
background = game.add.tileSprite(0, 0, 800, 600, 'bg');
background.fixedToCamera = true;
addGabos(playerCollisionGroup, gaboCollisionGroup);
addIan(playerCollisionGroup, gaboCollisionGroup);
addTimerText();
cursors = game.input.keyboard.createCursorKeys();
game.time.events.loop(Phaser.Timer.SECOND, updateTimeCounter, this);
gaboSound = game.add.audio('gaboSound');
gaboSound.addMarker('gaboSound', 0, 1.0);
}
function update() {
ian.body.setZeroVelocity();
if (!gameEnded) {
if (cursors.left.isDown) {
ian.body.moveLeft(200);
} else if (cursors.right.isDown) {
ian.body.moveRight(200);
}
if (cursors.up.isDown) {
ian.body.moveUp(200);
} else if (cursors.down.isDown) {
ian.body.moveDown(200);
}
// if (!game.camera.atLimit.x) {
// background.tilePosition.x += (ian.body.velocity.x * 16) * game.time.physicsElapsed;
// }
//
// if (!game.camera.atLimit.y) {
// background.tilePosition.y += (ian.body.velocity.y * 16) * game.time.physicsElapsed;
// }
}
}
function render() {}
function addGabos(playerCollisionGroup, gaboCollisionGroup) {
gabos = game.add.group();
gabos.enableBody = true;
gabos.physicsBodyType = Phaser.Physics.P2JS;
for (var iGabo = 0; iGabo < GABO_COUNT; iGabo++) {
var aGabo = gabos.create(game.world.randomX, gaboRandomY(), 'gabo');
aGabo.body.setRectangle(40, 40);
aGabo.body.setCollisionGroup(gaboCollisionGroup);
aGabo.body.collides([gaboCollisionGroup, playerCollisionGroup]);
aGabo.body.velocity.x = gaboRandomVelocity();
aGabo.body.velocity.y = gaboRandomVelocity();
aGabo.body.damping = 0;
aGabo.body.angularDamping = 0;
}
}
function addIan(playerCollisionGroup, gaboCollisionGroup) {
ian = game.add.sprite(game.world.centerX, 50, 'ian');
game.physics.p2.enable(ian, false);
ian.body.setCircle(28);
ian.body.fixedRotation = true;
ian.body.setCollisionGroup(playerCollisionGroup);
ian.body.collides(gaboCollisionGroup, hitGabo, this);
}
function addTimerText() {
timerText = game.add.text(100, 100, '0', {
font: "70px Arial",
fill: "#8a2be2",
align: "center"
});
timerText.anchor.setTo(0.5, 0.5);
}
function gaboRandomVelocity() {
var inital = 130;
var factor = 1;
return (inital + (Math.floor(Math.random() * factor))) * (Math.random() > 0.5 ? 1 : -1);
};
function gaboRandomY() {
var top = 100;
var bottom = game.world.getBounds().bottom;
return ((Math.floor(Math.random() * (bottom - top)) + top));
};
function hitGabo(body1, body2) {
gaboSound.play('gaboSound');
pauseAll();
}
function updateTimeCounter() {
if (!gameEnded) {
timeCounter++;
timerText.setText(timeCounter);
}
}
function pauseAll() {
for (var iGabo = 0; iGabo < gabos.countLiving(); iGabo++) {
var aGabo = gabos.getAt(iGabo);
aGabo.body.setZeroVelocity();
aGabo.body.setZeroRotation();
}
gameEnded = true;
}