Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <!doctype html>
- <html lang="en">
- <head>
- <meta charset="UTF-8" />
- <title>Phaser - Condensation</title>
- <script type="text/javascript" src="js/phaser.min.js"></script>
- <style type="text/css">
- body {
- margin: 0;
- }
- </style>
- </head>
- <body>
- <script src='https://code.responsivevoice.org/responsivevoice.js'>
- </script>
- <script type="text/javascript">
- Dust = function (index, game, player) {
- var x = game.world.randomX;
- var y = game.world.randomY;
- this.game = game;
- this.health = 1;
- this.player = player;
- this.alive = true;
- this.minSpeed = -50;
- this.maxSpeed = 50;
- this.vx = Math.random()*(this.maxSpeed-this.minSpeed+1)-this.minSpeed;
- this.vy = Math.random()*(this.maxSpeed-this.minSpeed+1)-this.minSpeed;
- this.dust = game.add.sprite(x, y, 'dust')
- this.dust.anchor.set(0.5);
- this.dust.name = index.toString();
- game.physics.enable(this.dust, Phaser.Physics.ARCADE);
- this.dust.body.immovable = false;
- this.dust.body.collideWorldBounds = true;
- this.dust.body.bounce.setTo(1, 1);
- this.dust.body.velocity.x = this.vx;
- this.dust.body.velocity.y = this.vy;
- }
- Dust.prototype.update = function() {
- }
- Water = function (index, game, player) {
- var x = game.world.randomX;
- var y = game.world.randomY;
- this.game = game;
- this.health = 1;
- this.player = player;
- this.alive = true;
- this.water = game.add.sprite(x, y, 'water')
- this.water.anchor.set(0.5);
- this.water.name = index.toString();
- game.physics.enable(this.water, Phaser.Physics.ARCADE);
- this.water.body.immovable = false;
- this.water.body.collideWorldBounds = true;
- this.water.body.bounce.setTo(1, 1);
- }
- Water.prototype.update = function() {
- }
- Sky = function (index, game, player) {
- var x = game.world.randomX;
- var y = game.world.randomY;
- this.game = game;
- this.health = 1;
- this.player = player;
- this.alive = true;
- this.sky = game.add.sprite(x, y, 'sky')
- this.sky.anchor.set(0.5);
- this.sky.name = index.toString();
- game.physics.enable(this.sky, Phaser.Physics.ARCADE);
- this.sky.body.immovable = true;
- this.sky.body.collideWorldBounds = true;
- }
- Sky.prototype.update = function() {
- }
- var game = new Phaser.Game(1024, 768, Phaser.AUTO, 'condensation', { preload: preload, create: create, update: update, render: render });
- function preload () {
- game.load.image('logo', 'assets/logo.png');
- game.load.image('cloud', 'assets/cloud.png');
- game.load.image('waterguy', 'assets/waterguy.png', 64, 64, 64);
- game.load.image('dust', 'assets/dust.png', 64, 64, 64);
- game.load.image('water', 'assets/water.png', 64, 64, 64);
- game.load.image('sky', 'assets/sky.png', 64, 64, 64);
- }
- var land;
- var sky
- var player;
- var dust;
- var water;
- var enemies;
- var enemiesAlive = 0;
- var enemiesTotal = 0;
- var friends;
- var friendsAlive = 0;
- var friendsTotal = 0;
- var skyObjects;
- var skyTotal = 0;
- var skyAlive = 0;
- var logo;
- var currentSpeed = 0;
- var cursors;
- var score = 0;
- var waterScore = 0;
- var dustScore = 0;
- function create () {
- // Resize our game world to be a 2000 x 2000 square
- game.world.setBounds(-2000, -2000, 2000, 2000);
- // Our tiled scrolling background
- land = game.add.tileSprite(0, 0, 1024, 768, 'cloud');
- land.fixedToCamera = true;
- // Loading player character
- player = game.add.sprite(0, 0, 'waterguy');
- player.anchor.setTo(0.5, 0.5);
- // This will control the speed of the player character
- game.physics.enable(player, Phaser.Physics.ARCADE);
- player.body.drag.set(0.2);
- player.body.maxVelocity.setTo(400,400);
- player.body.collideWorldBounds = true;
- // Create some sky obstacles
- skyObjects = [];
- skyTotal = 0;
- for (var i = 0; i < skyTotal; i++)
- {
- skyObjects.push(new Sky(i, game, sky));
- }
- // Create some baddies to avoid
- enemies = [];
- enemiesTotal = 25;
- enemiesAlive = 45;
- for (var i = 0; i < enemiesTotal; i++)
- {
- enemies.push(new Dust(i, game, dust));
- }
- // Create friends to collect
- friends = [];
- friendsTotal = 45;
- friendsAlive = 45;
- for (var i = 0; i < friendsTotal; i++)
- {
- friends.push(new Water(i, game, water));
- }
- player.bringToTop();
- // Displays the logo of the level
- //logo = game.add.sprite(0, 200, 'logo');
- //logo.fixedToCamera = true;
- game.input.onDown.add(removeLogo, this);
- game.camera.follow(player);
- game.camera.deadzone = new Phaser.Rectangle(150, 150, 500, 300);
- game.camera.focusOnXY(0, 0);
- // Controls
- cursors = game.input.keyboard.createCursorKeys();
- }
- function removeLogo () {
- game.input.onDown.remove(removeLogo, this);
- logo.kill();
- }
- function update () {
- game.physics.arcade.overlap(player, dust, water, null, this);
- enemiesAlive = 0;
- for (var i = 0; i < enemies.length; i++)
- {
- if (enemies[i].alive)
- {
- enemiesAlive++;
- game.physics.arcade.overlap(player, enemies[i].dust, collectDust, null, this);
- enemies[i].update();
- }
- }
- friendsAlive = 0;
- for (var i = 0; i < friends.length; i++)
- {
- if (friends[i].alive)
- {
- friendsAlive++;
- game.physics.arcade.overlap(player, friends[i].water, collectWater, null, this);
- friends[i].update();
- }
- }
- skyAlive = 0;
- for (var i = 0; i < skyObjects.length; i++)
- {
- if (skyObjects[i].alive)
- {
- skyAlive++;
- game.physics.arcade.collide(player, skyObjects[i].sky);
- skyObjects[i].update();
- }
- }
- // Move on click
- if (game.input.activePointer.isDown) {
- game.physics.arcade.moveToPointer(player, 300);
- if (Phaser.Rectangle.contains(player.body, game.input.x, game.input.y)) {
- player.body.velocity.setTo(0, 0);
- }
- } else {
- player.body.velocity.setTo(0, 0);
- }
- // End game
- if (score >= 20)
- gameOver();
- land.tilePosition.x = -game.camera.x;
- land.tilePosition.y = -game.camera.y;
- }
- function collectDust (player, dust) {
- dust.kill();
- dustScore += 1;
- score -= 1;
- }
- function collectWater (player, water) {
- water.kill();
- waterScore += 1;
- score += 1;
- }
- function gameOver() {
- score = ((waterScore - dustScore)/(waterScore + dustScore)*100)
- window.alert("Game Over!\nScore: " + score + "%");
- game.destroy();
- }
- function render () {
- game.debug.text('Collected ' + score + '/20 droplets', 32, 32);
- }
- </script>
- </body>
- </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement