-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdeefishy.html
More file actions
194 lines (150 loc) · 6.65 KB
/
deefishy.html
File metadata and controls
194 lines (150 loc) · 6.65 KB
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
<!doctype html>
<html>
<head>
<meta charset="UTF-8" />
<title>hello phaser!</title>
<script src="phaser.js"></script>
</head>
<body>
<script type="text/javascript">
window.onload = function() {
var game = new Phaser.Game(800, 600, Phaser.CANVAS, 'phaser-example', { preload: preload, create: create, update: update, render: render })
function preload() {
// Here we load the Starling Texture Atlas and XML file
//game.load.atlasXML('seacreatures', 'assets/sprites/seacreatures.png', 'assets/sprites/seacreatures.xml');
// Here is the exact same set of animations but as a JSON file instead
game.load.atlas('seacreatures', 'assets/sprites/seacreatures_json.png', 'assets/sprites/seacreatures_json.json');
// Just a few images to use in our underwater scene
game.load.image('undersea', 'assets/pics/undersea.jpg');
game.load.image('coral', 'assets/pics/seabed.png');
game.load.spritesheet('kaboom', 'assets/pics/explode.png', 128, 128);
game.load.image('fishFood1', 'assets/sprites/fishMedium2.png');
game.load.image('fishFood2', 'assets/sprites/fishMedium1.png');
game.load.image('fishFood2', 'assets/sprites/fishMedium4.png');
game.load.image('fishFood3', 'assets/sprites/fishLarge3.png');
game.load.image('fishFood4', 'assets/sprites/fishSmall3.png');
}
var sprite;
var fish;
var explosions;
var MAX_FISH = 10;
var score = 0;
var newFishTimer = 0;
function create() {
game.physics.startSystem(Phaser.Physics.ARCADE);
// The background sea floor.
game.add.image(0, 0, 'undersea');
game.add.image(0, 466, 'coral');
// The fish food group
fish = game.add.group();
fish.enableBody = true;
fish.physicsBodyType = Phaser.Physics.ARCADE;
fish.createMultiple(MAX_FISH);
// for(var i = 0; i < MAX_FISH; i++) {
//
// fish.create(800,600,'fishFood'+fishType);
// }
fish.setAll('outOfBoundsKill',true);
fish.setAll('checkWorldBounds',true);
fish.setAll('anchor.x', 0);
fish.setAll('anchor.y', 0.5);
// The player
sprite = game.add.sprite(400, 300, 'fishFood1');
// sprite = game.add.sprite(400, 300, 'seacreatures');
// sprite.animations.add('swim', Phaser.Animation.generateFrameNames('purpleFish', 0, 20, '', 4), 30, true);
// sprite.animations.play('swim');
sprite.scale.x = -1 * calculateFishScale();
sprite.scale.y *= calculateFishScale();
sprite.anchor.setTo(0.5, 0.5);
// Enable Arcade Physics for the sprite
game.physics.enable(sprite, Phaser.Physics.ARCADE);
// Tell it we don't want physics to manage the rotation
sprite.body.allowRotation = true;
// Collision Counter
scoreString = ' Score : ';
scoreText = game.add.text(10, 10, scoreString + score);
// Text
stateText = game.add.text(game.world.centerX,game.world.centerY,' ', { font: '84px Arial', fill: '#fff' });
stateText.anchor.setTo(0.5, 0.5);
stateText.visible = false;
// An explosion pool
explosions = game.add.group();
explosions.createMultiple(30, 'kaboom');
}
function update() {
sprite.rotation = game.physics.arcade.moveToPointer(sprite, 200, game.input.activePointer, 1500);
var spriteDir = sprite.deltaX;
if (spriteDir < 0) {
sprite.scale.y = -1 * calculateFishScale();
} else {
sprite.scale.y = 1 * calculateFishScale();
}
sprite.scale.x = (sprite.scale.x > 0 ? 1 : -1) * calculateFishScale();
if (game.time.now > newFishTimer) {
sendFish();
}
game.physics.arcade.collide(sprite, fish, collisionHandler, null, this);
}
function calculateFishScale() {
return Math.log(score + 2);
}
function collisionHandler (player, target) {
target.kill();
if (Math.abs(target.height) > Math.abs(player.height) || Math.abs(target.width) > Math.abs(player.width)) {
var explosion = explosions.getFirstExists(false);
explosion.reset(player.body.x, player.body.y);
explosion.play('kaboom', 30, false, true);
player.kill();
explosions.callAll('kill');
stateText.text=" GAME OVER \n Click to restart";
stateText.visible = true;
//the "click to restart" handler
game.input.onTap.addOnce(restart,this);
} else {
score += 1;
}
scoreText.text = scoreString + score;
}
function sendFish() {
newFish = fish.getFirstExists(false);
if (newFish == null) {
return;
}
var fishType = game.rnd.integerInRange(2,4);
newFish.loadTexture('fishFood'+fishType);
var direction = -1;
var startX = 0;
var endX = 800;
if(game.rnd.integer() % 2 == 0) {
direction = 1;
startX = 800;
endX = 0;
}
var startY = game.rnd.integer() % 600;
var speed = game.rnd.integerInRange(10,60);
var scale = game.rnd.realInRange(0.3, 1);
newFish.reset(startX, startY);
newFish.scale.x = direction * scale;
newFish.scale.y = scale;
game.physics.arcade.accelerateToXY(newFish, endX, startY, speed);
newFishTimer = game.time.now + 1000;
}
function restart () {
//revives the player
sprite.revive();
fish.callAll('kill');
//hides the text
stateText.visible = false;
score = 0;
scoreText.text = scoreString + score;
}
function render() {
// game.debug.spriteInfo(sprite, 32, 32);
// game.debug.renderPhysicsBody(sprite.body);
}
};
</script>
<div id="phaser-example">
</div>
</body>
</html>