-
Notifications
You must be signed in to change notification settings - Fork 0
/
matter.html
220 lines (165 loc) · 5.67 KB
/
matter.html
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
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
<!DOCTYPE HTML>
<html>
<head>
<title>Matter</title>
<meta charset="utf-8">
<script type="text/javascript" src="js/phaser.min.js"></script>
</head>
<body>
<script>
var game = new Phaser.Game(800, 600, Phaser.CANVAS, '', { preload: preload, create: create, update: update });
////////////////////////////////////////////BULLETS/////////////////////////////////////////
// Our core Bullet class
// This is a simple Sprite object that we set a few properties on
// It is fired by all of the Weapon classes
var Bullet = function (game, key) {
Phaser.Sprite.call(this, game, 0, 0, key);
this.texture.baseTexture.scaleMode = PIXI.scaleModes.NEAREST;
this.anchor.set(0.5);
this.checkWorldBounds = true;
this.outOfBoundsKill = true;
this.exists = false;
this.tracking = false;
this.scaleSpeed = 0;
};
Bullet.prototype = Object.create(Phaser.Sprite.prototype);
Bullet.prototype.constructor = Bullet;
Bullet.prototype.fire = function (x, y, angle, speed, gx, gy) {
gx = gx || 0;
gy = gy || 0;
this.reset(x, y);
this.scale.set(1);
this.game.physics.arcade.velocityFromAngle(angle, speed, this.body.velocity);
this.angle = angle;
this.body.gravity.set(gx, gy);
};
Bullet.prototype.update = function () {
if (this.tracking)
{
this.rotation = Math.atan2(this.body.velocity.y, this.body.velocity.x);
}
if (this.scaleSpeed > 0)
{
this.scale.x += this.scaleSpeed;
this.scale.y += this.scaleSpeed;
}
};
var Weapon = {};
Weapon.Beam = function (game) {
Phaser.Group.call(this, game, game.world, 'Beam', false, true, Phaser.Physics.ARCADE);
this.nextFire = 0;
this.bulletSpeed = 500;
this.fireRate = 10;
for (var i = 0; i < 64; i++)
{
this.add(new Bullet(game, 'bullet'), true);
}
return this;
};
Weapon.Beam.prototype = Object.create(Phaser.Group.prototype);
Weapon.Beam.prototype.constructor = Weapon.Beam;
Weapon.Beam.prototype.fire = function (source) {
if (this.game.time.time < this.nextFire) { return; }
// Now work out where the END of the turret is
var p = new Phaser.Point(source.x, source.y);
p.rotate(p.x, p.y, source.rotation, false, 34);
var x = source.x;
var y = source.y;
var b = this.getFirstExists(false);
if(b) b.fire(p.x, p.y, source.angle, this.bulletSpeed, 0, 0);
this.nextFire = this.game.time.time + this.fireRate;
};
////////////////////////////////////////////////////////////////////////////////////////////
function preload() {
game.load.image('sky', 'assets/sky.png');
game.load.image('ground', 'assets/platform.png');
game.load.image('tank', 'assets/tank.png');
game.load.image('turret', 'assets/turret.png');
game.load.image('bullet', 'assets/bullet.png');
}
var player;
var platforms;
var cursors;
var turret;
var weapons = [];
var currentWeapon = 0;
function create() {
game.renderer.renderSession.roundPixels = true;
//enable Arcade Physics
game.physics.startSystem(Phaser.Physics.ARCADE);
//add the beam to the weapon arsenal
weapons.push(new Weapon.Beam(game));
currentWeapon = 0;
for (var i = 1; i < weapons.length; i++)
{
weapons[i].visible = false;
}
//simple background
//game.add.sprite(0, 0, 'sky');
// The platforms group contains the ground and the 2 ledges we can jump on
platforms = game.add.group();
// We will enable physics for any object that is created in this group
platforms.enableBody = true;
//create the ground
var ground = platforms.create(0, game.world.height - 64, 'ground');
// Scale it to fit the width of the game (the original sprite is 400x32 in size)
ground.scale.setTo(2, 2);
// This stops it from falling away when you jump on it
ground.body.immovable = true;
//add tank
player = game.add.sprite(32, game.world.height - 100, 'tank');
// The turret which we rotate
turret = game.add.sprite(player.x + 55, player.y + 20, 'turret');
// We need to enable physics on the player
game.physics.arcade.enable(player);
game.physics.arcade.enable(turret);
//player physics properties
//player.body.bounce.y = 0.2;
//player.body.gravity.y = 300;
player.body.collideWorldBounds = true;
//turret.body.bounce.y = 0.2;
//turret.body.gravity.y = 300;
turret.body.collideWorldBounds = true;
// Our controls.
cursors = game.input.keyboard.createCursorKeys();
game.input.keyboard.addKeyCapture([ Phaser.Keyboard.SPACEBAR ]);
}
function update() {
// Collide the player with the platforms
game.physics.arcade.collide(player, platforms);
game.physics.arcade.collide(turret, platforms);
// Reset the players velocity (movement)
player.body.velocity.x = 0;
turret.body.velocity.x = 0;
if (cursors.left.isDown)
{
// Move to the left
player.body.velocity.x = -150;
}
else if (cursors.right.isDown)
{
// Move to the right
player.body.velocity.x = 150;
}
if (cursors.up.isDown && turret.angle > -90)
{
turret.angle--;
}
else if (cursors.down.isDown && turret.angle < 30)
{
turret.angle++;
}
else
{
// Stand still
player.animations.stop();
}
if (game.input.keyboard.isDown(Phaser.Keyboard.SPACEBAR))
{
weapons[currentWeapon].fire(turret);
}
turret.body.x=player.body.x+55;
}
</script>
</body>
</html>