Advertisement
Guest User

Untitled

a guest
Mar 27th, 2016
177
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. <!doctype html>
  2. <html lang="en">
  3. <head>
  4.     <meta charset="UTF-8" />
  5.     <title>Phaser - Condensation</title>
  6.     <script type="text/javascript" src="js/phaser.min.js"></script>
  7.     <style type="text/css">
  8.         body {
  9.             margin: 0;
  10.         }
  11.     </style>
  12. </head>
  13. <body>
  14. <script src='https://code.responsivevoice.org/responsivevoice.js'>
  15. </script>
  16. <script type="text/javascript">
  17.  
  18. Dust = function (index, game, player) {
  19.  
  20.     var x = game.world.randomX;
  21.     var y = game.world.randomY;
  22.  
  23.     this.game = game;
  24.     this.health = 1;
  25.     this.player = player;
  26.     this.alive = true;
  27.     this.minSpeed = -50;
  28.     this.maxSpeed = 50;
  29.     this.vx = Math.random()*(this.maxSpeed-this.minSpeed+1)-this.minSpeed;
  30.     this.vy = Math.random()*(this.maxSpeed-this.minSpeed+1)-this.minSpeed;
  31.  
  32.  
  33.     this.dust = game.add.sprite(x, y, 'dust')
  34.     this.dust.anchor.set(0.5);
  35.  
  36.     this.dust.name = index.toString();
  37.     game.physics.enable(this.dust, Phaser.Physics.ARCADE);
  38.     this.dust.body.immovable = false;
  39.     this.dust.body.collideWorldBounds = true;
  40.     this.dust.body.bounce.setTo(1, 1);
  41.     this.dust.body.velocity.x = this.vx;
  42.     this.dust.body.velocity.y = this.vy;
  43. }
  44.  
  45. Dust.prototype.update = function() {
  46.  
  47. }
  48.  
  49. Water = function (index, game, player) {
  50.  
  51.     var x = game.world.randomX;
  52.     var y = game.world.randomY;
  53.  
  54.     this.game = game;
  55.     this.health = 1;
  56.     this.player = player;
  57.     this.alive = true;
  58.  
  59.     this.water = game.add.sprite(x, y, 'water')
  60.     this.water.anchor.set(0.5);
  61.  
  62.     this.water.name = index.toString();
  63.     game.physics.enable(this.water, Phaser.Physics.ARCADE);
  64.     this.water.body.immovable = false;
  65.     this.water.body.collideWorldBounds = true;
  66.     this.water.body.bounce.setTo(1, 1);
  67.    
  68. }
  69.  
  70. Water.prototype.update = function() {
  71.  
  72. }
  73.  
  74. Sky = function (index, game, player) {
  75.  
  76.     var x = game.world.randomX;
  77.     var y = game.world.randomY;
  78.  
  79.     this.game = game;
  80.     this.health = 1;
  81.     this.player = player;
  82.     this.alive = true;
  83.  
  84.     this.sky = game.add.sprite(x, y, 'sky')
  85.     this.sky.anchor.set(0.5);
  86.  
  87.     this.sky.name = index.toString();
  88.     game.physics.enable(this.sky, Phaser.Physics.ARCADE);
  89.     this.sky.body.immovable = true;
  90.     this.sky.body.collideWorldBounds = true;
  91.  
  92. }
  93.  
  94. Sky.prototype.update = function() {
  95.  
  96. }
  97.  
  98. var game = new Phaser.Game(1024, 768, Phaser.AUTO, 'condensation', { preload: preload, create: create, update: update, render: render });
  99.  
  100. function preload () {
  101.  
  102.     game.load.image('logo', 'assets/logo.png');
  103.     game.load.image('cloud', 'assets/cloud.png');
  104.     game.load.image('waterguy', 'assets/waterguy.png', 64, 64, 64);
  105.     game.load.image('dust', 'assets/dust.png', 64, 64, 64);
  106.     game.load.image('water', 'assets/water.png', 64, 64, 64);
  107.     game.load.image('sky', 'assets/sky.png', 64, 64, 64);
  108.    
  109. }
  110.  
  111. var land;
  112. var sky
  113.  
  114. var player;
  115. var dust;
  116. var water;
  117.  
  118. var enemies;
  119. var enemiesAlive = 0;
  120. var enemiesTotal = 0;
  121.  
  122. var friends;
  123. var friendsAlive = 0;
  124. var friendsTotal = 0;
  125.  
  126. var skyObjects;
  127. var skyTotal = 0;
  128. var skyAlive = 0;
  129.  
  130. var logo;
  131.  
  132. var currentSpeed = 0;
  133. var cursors;
  134.  
  135. var score = 0;
  136. var waterScore = 0;
  137. var dustScore = 0;
  138.  
  139. function create () {
  140.  
  141.     //  Resize our game world to be a 2000 x 2000 square
  142.     game.world.setBounds(-2000, -2000, 2000, 2000);
  143.  
  144.     //  Our tiled scrolling background
  145.     land = game.add.tileSprite(0, 0, 1024, 768, 'cloud');
  146.     land.fixedToCamera = true;
  147.  
  148.     // Loading player character
  149.     player = game.add.sprite(0, 0, 'waterguy');
  150.     player.anchor.setTo(0.5, 0.5);
  151.  
  152.     // This will control the speed of the player character
  153.     game.physics.enable(player, Phaser.Physics.ARCADE);
  154.     player.body.drag.set(0.2);
  155.     player.body.maxVelocity.setTo(400,400);
  156.     player.body.collideWorldBounds = true;
  157.  
  158.     // Create some sky obstacles
  159.     skyObjects = [];
  160.  
  161.     skyTotal = 0;
  162.  
  163.     for (var i = 0; i < skyTotal; i++)
  164.     {
  165.         skyObjects.push(new Sky(i, game, sky));
  166.     }
  167.  
  168.     // Create some baddies to avoid
  169.     enemies = [];
  170.  
  171.     enemiesTotal = 25;
  172.     enemiesAlive = 45;
  173.  
  174.     for (var i = 0; i < enemiesTotal; i++)
  175.     {
  176.         enemies.push(new Dust(i, game, dust));
  177.     }
  178.  
  179.     // Create friends to collect
  180.     friends = [];
  181.  
  182.     friendsTotal = 45;
  183.     friendsAlive = 45;
  184.  
  185.     for (var i = 0; i < friendsTotal; i++)
  186.     {
  187.         friends.push(new Water(i, game, water));
  188.     }
  189.  
  190.     player.bringToTop();
  191.  
  192.     // Displays the logo of the level
  193.     //logo = game.add.sprite(0, 200, 'logo');
  194.     //logo.fixedToCamera = true;
  195.  
  196.     game.input.onDown.add(removeLogo, this);
  197.  
  198.     game.camera.follow(player);
  199.     game.camera.deadzone = new Phaser.Rectangle(150, 150, 500, 300);
  200.     game.camera.focusOnXY(0, 0);
  201.  
  202.     // Controls
  203.     cursors = game.input.keyboard.createCursorKeys();
  204.  
  205. }
  206.  
  207. function removeLogo () {
  208.  
  209.     game.input.onDown.remove(removeLogo, this);
  210.     logo.kill();
  211.  
  212. }
  213.  
  214. function update () {
  215.  
  216.     game.physics.arcade.overlap(player, dust, water, null, this);
  217.  
  218.     enemiesAlive = 0;
  219.  
  220.     for (var i = 0; i < enemies.length; i++)
  221.     {
  222.         if (enemies[i].alive)
  223.         {
  224.             enemiesAlive++;
  225.             game.physics.arcade.overlap(player, enemies[i].dust, collectDust, null, this);
  226.             enemies[i].update();
  227.         }
  228.     }
  229.  
  230.     friendsAlive = 0;
  231.  
  232.     for (var i = 0; i < friends.length; i++)
  233.     {
  234.         if (friends[i].alive)
  235.         {
  236.             friendsAlive++;
  237.             game.physics.arcade.overlap(player, friends[i].water, collectWater, null, this);
  238.             friends[i].update();
  239.         }
  240.     }
  241.  
  242.     skyAlive = 0;
  243.  
  244.     for (var i = 0; i < skyObjects.length; i++)
  245.     {
  246.         if (skyObjects[i].alive)
  247.         {
  248.             skyAlive++;
  249.             game.physics.arcade.collide(player, skyObjects[i].sky);
  250.             skyObjects[i].update();
  251.         }
  252.     }
  253.  
  254.  
  255.     // Move on click
  256.     if (game.input.activePointer.isDown) {
  257.         game.physics.arcade.moveToPointer(player, 300);
  258.  
  259.         if (Phaser.Rectangle.contains(player.body, game.input.x, game.input.y)) {
  260.             player.body.velocity.setTo(0, 0);
  261.         }
  262.     } else {
  263.         player.body.velocity.setTo(0, 0);
  264.     }
  265.  
  266.     // End game
  267.     if (score >= 20)
  268.         gameOver();
  269.  
  270.  
  271.  
  272.     land.tilePosition.x = -game.camera.x;
  273.     land.tilePosition.y = -game.camera.y;
  274.  
  275. }
  276.  
  277. function collectDust (player, dust) {
  278.     dust.kill();
  279.     dustScore += 1;
  280.     score -= 1;
  281. }
  282.  
  283. function collectWater (player, water) {
  284.     water.kill();
  285.     waterScore += 1;
  286.     score += 1;
  287. }
  288.  
  289. function gameOver() {
  290.     score = ((waterScore - dustScore)/(waterScore + dustScore)*100)
  291.     window.alert("Game Over!\nScore: " + score + "%");
  292.     game.destroy();
  293. }
  294.  
  295. function render () {
  296.     game.debug.text('Collected ' + score + '/20 droplets', 32, 32);
  297. }
  298. </script>
  299.  
  300. </body>
  301. </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement