Files
phaser/examples/games/tanks.php
T

171 lines
4.0 KiB
PHP

<?php
$title = "Tanks";
require('../head.php');
?>
<script type="text/javascript">
var game = new Phaser.Game(800, 600, Phaser.CANVAS, '', { preload: preload, create: create, update: update, render: render });
function preload() {
game.load.atlas('tank', 'assets/games/tanks/tanks.png', 'assets/games/tanks/tanks.json');
game.load.image('bullet', 'assets/games/tanks/bullet.png');
game.load.image('earth', 'assets/games/tanks/scorched_earth.png');
}
var land;
var shadow;
var tank;
var turret;
var enemy;
var currentSpeed = 0;
var cursors;
var bullets;
var fireRate = 100;
var nextFire = 0;
function create() {
// Resize our game world to be a 2000x2000 square
game.world.setBounds(-1000, -1000, 2000, 2000);
// Our tiled scrolling background
land = game.add.tileSprite(0, 0, 800, 600, 'earth');
land.fixedToCamera = true;
// A shadow below our tank
shadow = game.add.sprite(0, 0, 'tank', 'shadow');
shadow.anchor.setTo(0.5, 0.5);
// Our bullet group
bullets = game.add.group();
bullets.createMultiple(50, 'bullet');
bullets.setAll('anchor.x', 0.5);
bullets.setAll('anchor.y', 0.5);
bullets.setAll('outOfBoundsKill', true);
// The base of our tank
tank = game.add.sprite(0, 0, 'tank', 'tank1');
tank.anchor.setTo(0.5, 0.5);
tank.animations.add('move', ['tank1', 'tank2', 'tank3', 'tank4', 'tank5', 'tank6'], 20, true);
// tank.play('move');
// This will force it to decelerate and limit its speed
tank.body.drag.setTo(200, 200);
tank.body.maxVelocity.setTo(400, 400);
tank.body.collideWorldBounds = true;
// Finally the turret that we place on-top of the tank body
turret = game.add.sprite(0, 0, 'tank', 'turret');
turret.anchor.setTo(0.3, 0.5);
enemy = game.add.sprite(900, 400, 'tank', 'tank1');
enemy.anchor.setTo(0.5, 0.5);
enemy.body.immovable = true;
enemy.body.collideWorldBounds = true;
game.camera.follow(tank);
game.camera.deadzone = new Phaser.Rectangle(100, 100, 600, 400);
game.camera.focusOnXY(0, 0);
cursors = game.input.keyboard.createCursorKeys();
}
function update() {
game.physics.collide(tank, enemy);
if (cursors.left.isDown)
{
tank.angle -= 4;
}
else if (cursors.right.isDown)
{
tank.angle += 4;
}
if (cursors.up.isDown)
{
// The speed we'll travel at
currentSpeed = 300;
}
else
{
if (currentSpeed > 0)
{
currentSpeed -= 4;
}
}
if (currentSpeed > 0)
{
game.physics.velocityFromRotation(tank.rotation, currentSpeed, tank.body.velocity);
// Scroll the background (note the negative offset to ensure it moves the direction we're facing, not coming from)
// land.tilePosition.x -= (tank.body.velocity.x / 50);
// land.tilePosition.y -= (tank.body.velocity.y / 50);
}
land.tilePosition.x = -game.camera.x;
land.tilePosition.y = -game.camera.y;
// Position all the parts and align rotations
shadow.x = tank.x;
shadow.y = tank.y;
shadow.rotation = tank.rotation;
turret.x = tank.x;
turret.y = tank.y;
turret.rotation = game.physics.angleToPointer(turret);
if (game.input.activePointer.isDown)
{
// Boom!
fire();
}
}
function fire() {
if (game.time.now > nextFire && bullets.countDead() > 0)
{
nextFire = game.time.now + fireRate;
var bullet = bullets.getFirstDead();
bullet.reset(turret.x, turret.y);
bullet.rotation = game.physics.moveToPointer(bullet, 1000);
}
}
function render() {
// game.debug.renderText('Active Bullets: ' + bullets.countLiving() + ' / ' + bullets.total, 32, 32);
// game.debug.renderText('sr: ' + tank.body.right, 32, 100);
// game.debug.renderText('sb: ' + tank.body.bottom, 32, 132);
// game.debug.renderSpriteCorners(tank, true, true);
game.debug.renderCameraInfo(game.camera, 500, 32);
game.debug.renderSpriteInfo(tank, 32, 450);
}
</script>
<?php
require('../foot.php');
?>