-
Notifications
You must be signed in to change notification settings - Fork 54
Expand file tree
/
Copy pathindex.html
More file actions
170 lines (143 loc) · 4.53 KB
/
Copy pathindex.html
File metadata and controls
170 lines (143 loc) · 4.53 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
<!DOCTYPE HTML>
<html>
<head>
<title>Shooter</title>
<meta charset="utf-8">
<style type="text/css">
body {
margin: 0;
padding: 0;
background-color: #000;
}
</style>
<script src="//cdn.jsdelivr.net/phaser/2.6.2/phaser.min.js"
type="text/javascript"></script>
</head>
<body>
<script type="text/javascript">
var game = new Phaser.Game(800, 600, Phaser.AUTO, 'phaser-demo', {
preload : preload,
create : create,
update : update,
render : render
});
var player;
var starfield;
var cursors;
var bank;
var shipTrail;
var bullets;
var fireButton;
var bulletTimer = 0;
var ACCLERATION = 600;
var DRAG = 400;
var MAXSPEED = 400;
function preload() {
// We need this because the assets are on github pages
// Remove the next 2 lines if running locally
game.load.baseURL = 'https://ioniodi.github.io/Shooter/';
game.load.crossOrigin = 'anonymous';
game.load.image('starfield', 'assets/starfield.png');
game.load.image('ship', 'assets/ship.png');
game.load.image('bullet', 'assets/bullets/bullet.png');
}
function create() {
game.scale.pageAlignHorizontally = true;
// The scrolling starfield background
starfield = game.add.tileSprite(0, 0, 800, 600, 'starfield');
// Our bullet group
bullets = game.add.group();
bullets.enableBody = true;
bullets.physicsBodyType = Phaser.Physics.ARCADE;
bullets.createMultiple(30, 'bullet');
bullets.setAll('anchor.x', 0.5);
bullets.setAll('anchor.y', 1);
bullets.setAll('outOfBoundsKill', true);
bullets.setAll('checkWorldBounds', true);
// The hero!
player = game.add.sprite(100, game.height / 2, 'ship');
player.anchor.setTo(0.5, 0.5);
game.physics.enable(player, Phaser.Physics.ARCADE);
player.body.maxVelocity.setTo(MAXSPEED, MAXSPEED);
player.body.drag.setTo(DRAG, DRAG);
// And some controls to play the game with
cursors = game.input.keyboard.createCursorKeys();
fireButton = game.input.keyboard.addKey(Phaser.Keyboard.SPACEBAR);
// Add an emitter for the ship's trail
shipTrail = game.add.emitter(player.x - 20, player.y, 400);
shipTrail.height = 10;
shipTrail.makeParticles('bullet');
shipTrail.setYSpeed(20, -20);
shipTrail.setXSpeed(-140, -120);
shipTrail.setRotation(50, -50);
shipTrail.setAlpha(1, 0.01, 800);
shipTrail.setScale(0.05, 0.4, 0.05, 0.4, 2000,
Phaser.Easing.Quintic.Out);
shipTrail.start(false, 5000, 10);
}
function update() {
// Scroll the background
starfield.tilePosition.x -= 2;
// Reset the player, then check for movement keys
player.body.acceleration.y = 0;
player.body.acceleration.x = 0;
if (cursors.up.isDown) {
player.body.acceleration.y = -ACCLERATION;
} else if (cursors.down.isDown) {
player.body.acceleration.y = ACCLERATION;
} else if (cursors.left.isDown) {
player.body.acceleration.x = -ACCLERATION;
} else if (cursors.right.isDown) {
player.body.acceleration.x = ACCLERATION;
}
// Stop at screen edges
if (player.x > game.width - 30) {
player.x = game.width - 30;
player.body.acceleration.x = 0;
}
if (player.x < 30) {
player.x = 30;
player.body.acceleration.x = 0;
}
if (player.y > game.height - 15) {
player.y = game.height - 15;
player.body.acceleration.y = 0;
}
if (player.y < 15) {
player.y = 15;
player.body.acceleration.y = 0;
}
// Fire bullet
if (player.alive && fireButton.isDown) {
fireBullet();
}
// Keep the shipTrail lined up with the ship
shipTrail.y = player.y;
shipTrail.x = player.x - 20;
}
function render() {
}
function fireBullet() {
// To avoid them being allowed to fire too fast we set a time limit
if (game.time.now > bulletTimer) {
var BULLET_SPEED = 400;
var BULLET_SPACING = 250;
// Grab the first bullet we can from the pool
var bullet = bullets.getFirstExists(false);
if (bullet) {
// And fire it
// Make bullet come out of tip of ship with right angle
var bulletOffset = 20 * Math.sin(game.math
.degToRad(player.angle));
bullet.reset(player.x + bulletOffset, player.y);
bullet.angle = player.angle;
game.physics.arcade.velocityFromAngle(bullet.angle,
BULLET_SPEED, bullet.body.velocity);
bullet.body.velocity.y += player.body.velocity.y;
bulletTimer = game.time.now + BULLET_SPACING;
}
}
}
</script>
</body>
</html>