-
Notifications
You must be signed in to change notification settings - Fork 0
/
game.js
203 lines (156 loc) · 6.23 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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
function coopGame(){
$(document).ready(function () {
$('.home').hide();
var game = new Phaser.Game(window.innerWidth, window.innerHeight, Phaser.CANVAS, 'phaser-example', { preload: preload, create: create, update: update, render: render });
//window.screen.lockOrientationUniversal('portrait');
/*var rotate = 0 - window.orientation;
setAttribute("transform:rotate("+rotate+"deg);-ms-transform:rotate("+rotate+"deg);-webkit-transform:rotate("+rotate+"deg)", "style");*/
function preload() {
game.load.image('yball', 'assets/images/yball.png');
game.load.image('rball', 'assets/images/rball.png');
game.load.image('bball', 'assets/images/bball.png');
game.load.image('arrow', 'assets/images/arrow.png');
game.load.audio('big', 'assets/audio/big.mp3');
game.load.audio('medium', 'assets/audio/medium.mp3');
game.load.audio('small', 'assets/audio/small.mp3');
game.load.audio('pop', 'assets/audio/pop.mp3');
}
function create() {
if (window.DeviceOrientationEvent){
window.addEventListener("deviceorientation", function(event){
console.log (Math.round(event.beta))
y = (Math.round(event.beta))
game.physics.p2.gravity.y = 10 * y
}, true);
}
if (window.DeviceOrientationEvent) {
// Our browser supports DeviceOrientation
console.log("browser does support Device Orientation");
} else {
console.log("Sorry, your browser doesn't support Device Orientation");
}
window.addEventListener("deviceorientation", this.handleOrientation, true);
big = game.add.audio('big');
medium = game.add.audio('medium');
small = game.add.audio('small');
pop = game.add.audio('pop', 0.5);
big.allowMultiple = true;
medium.allowMultiple = true;
small.allowMultiple = true;
game.physics.startSystem(Phaser.Physics.P2JS);
//console.log (game.physics.p2.gravity.x)
game.physics.p2.setImpactEvents(true);
game.physics.p2.restitution = 0.8;
var yballCollisionGroup = game.physics.p2.createCollisionGroup();
var rballCollisionGroup = game.physics.p2.createCollisionGroup();
var bballCollisionGroup = game.physics.p2.createCollisionGroup();
var arrowCollisionGroup = game.physics.p2.createCollisionGroup();
// Some balls to collide with
yballs = game.add.physicsGroup(Phaser.Physics.P2JS);
rballs = game.add.physicsGroup(Phaser.Physics.P2JS);
bballs = game.add.physicsGroup(Phaser.Physics.P2JS);
arrows = game.add.physicsGroup(Phaser.Physics.P2JS);
game.physics.p2.updateBoundsCollisionGroup();
for (var i = 0; i < 2; i++)
{
var yball = yballs.create(game.world.randomX, game.world.randomY, 'yball');
yball.body.setCircle(203);
yball.scale.setTo(0.8, 0.8);
yball.body.setCollisionGroup(yballCollisionGroup);
yball.body.collides([yballCollisionGroup, rballCollisionGroup, bballCollisionGroup]);
yball.inputEnabled = true;
yball.events.onInputDown.add(popball, this);
}
for (var i = 0; i < 4; i++)
{
var rball = rballs.create(game.world.randomX, game.world.randomY, 'rball');
rball.body.setCircle(128);
rball.scale.setTo(0.5, 0.5);
rball.body.setCollisionGroup(rballCollisionGroup);
rball.body.collides([rballCollisionGroup, yballCollisionGroup, bballCollisionGroup]);
rball.inputEnabled = true;
rball.events.onInputDown.add(popball, this);
}
for (var i = 0; i < 6; i++)
{
var bball = bballs.create(game.world.randomX, game.world.randomY, 'bball');
bball.body.setCircle(50);
bball.scale.setTo(0.2, 0.2);
bball.body.setCollisionGroup(bballCollisionGroup);
bball.body.collides([bballCollisionGroup, yballCollisionGroup, rballCollisionGroup]);
bball.inputEnabled = true;
bball.events.onInputDown.add(popball, this);
}
bball.body.onBeginContact.add(bhit, this);
function bhit (body, bodyB) {
if (body){
if (bball.body.velocity.x > 15 || bball.body.velocity.y > 15){
small.play();
}
}
}
rball.body.onBeginContact.add(rhit, this);
function rhit (body, bodyB) {
if (body){
if (rball.body.velocity.x > 15 || rball.body.velocity.y > 15){
medium.play();
}
}
}
yball.body.onBeginContact.add(yhit, this);
function yhit (body, bodyB) {
if (body){
if (yball.body.velocity.x > 15 || yball.body.velocity.y > 15){
big.play();
}
}
}
function popball (balls) {
balls.destroy(balls);
pop.play();
popCheck();
}
function popCheck(){
living = yballs.countLiving() + rballs.countLiving() + bballs.countLiving();
if(living < 1 && y >= 0){
var arrow = arrows.create(game.world.centerX, game.world.centerY, 'arrow');
arrow.body.setCollisionGroup(arrowCollisionGroup);
game.physics.p2.restitution = 1.1;
arrow.body.fixedRotation = true;
var t=setInterval(upCheck,1000);
} else if (living < 1 && y <= 0){
var arrow = arrows.create(game.world.centerX, game.world.centerY, 'arrow');
arrow.body.setCollisionGroup(arrowCollisionGroup);
arrow.angle = 180
game.physics.p2.restitution = 1.1;
arrow.body.fixedRotation = true;
var t=setInterval(downCheck,1000);
}
function upCheck(){
console.log("Have I been flipped yet?")
if (y < 0 ){
clearInterval(t);
arrow.destroy();
create();
console.log("Yes I have been flipped");
}
}
function downCheck(){
console.log("Have I been flipped yet?")
if (y > 0 ){
clearInterval(t);
arrow.destroy();
create();
console.log("Yes I have been flipped");
}
}
}
}
function render () {
game.debug.body(yballs);
game.debug.body(bballs);
game.debug.body(rballs);
}
function update() {}
});
}