Advertisement
Guest User

TB Mini Game part 2

a guest
Oct 18th, 2016
2,216
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /*
  2. Source:
  3.  
  4. http://phaser.io/examples/v2/games/invaders
  5.  
  6.  
  7. */
  8.  
  9.  
  10.  
  11.  
  12.  
  13. var game = new Phaser.Game(800, 600, Phaser.AUTO, 'game', { preload: preload, create: create, update: update, render: render });
  14.  
  15. //Tyrano Builder folder
  16. var myassetspath = 'data/others/minigame/';
  17.  
  18. /*
  19. =========================================================
  20. ========== Loading ======================================
  21. =========================================================
  22. */
  23.  
  24.  
  25. function preload() {
  26.  
  27.     //Image
  28.     game.load.image('bullet', myassetspath+'assets/imgs/game/bullet.png');
  29.     game.load.image('enemyBullet', myassetspath+'assets/imgs/game/enemy-bullet.png');
  30.     game.load.spritesheet('invader', myassetspath+'assets/imgs/game/invader32x32x4.png', 32, 32);
  31.     game.load.image('ship', myassetspath+'assets/imgs/game/player.png');
  32.     game.load.spritesheet('kaboom', myassetspath+'assets/imgs/game/explode.png', 128, 128);
  33.     game.load.image('starfield', myassetspath+'assets/imgs/game/starfield.png');
  34.     game.load.image('background', myassetspath+'assets/imgs/game/background2.png');
  35.    
  36.     //Audio
  37.     //Firefox doesn't support mp3 files, so use ogg
  38.     game.load.audio('boden', [myassetspath+'assets/audio/bodenstaendig_2000_in_rock_4bit.ogg']);
  39.  
  40.     //game.load.audio('sfx', [ 'assets/audio/SoundEffects/fx_mixdown.mp3', 'assets/audio/SoundEffects/fx_mixdown.ogg' ]);
  41.     game.load.audio('sfx', myassetspath+'assets/audio/fx_mixdown.ogg');
  42.  
  43. }
  44.  
  45.  
  46.  
  47.  
  48.  
  49. /*
  50. =========================================================
  51. ========== GAME =========================================
  52. =========================================================
  53. */
  54.  
  55.  
  56. var player;
  57. var aliens;
  58. var bullets;
  59. var bulletTime = 0;
  60. var cursors;
  61. var fireButton;
  62. var explosions;
  63. var starfield;
  64. var score = 0;
  65. var scoreString = '';
  66. var scoreText;
  67. var lives;
  68. var enemyBullet;
  69. var firingTimer = 0;
  70. var stateText;
  71. var livingEnemies = [];
  72.  
  73. //Audio
  74. var music;
  75. var fx;
  76.  
  77. function create() {
  78.  
  79.     game.physics.startSystem(Phaser.Physics.ARCADE);
  80.  
  81.     //  The scrolling starfield background
  82.     starfield = game.add.tileSprite(0, 0, 800, 600, 'starfield');
  83.  
  84.     //  Our bullet group
  85.     bullets = game.add.group();
  86.     bullets.enableBody = true;
  87.     bullets.physicsBodyType = Phaser.Physics.ARCADE;
  88.     bullets.createMultiple(30, 'bullet');
  89.     bullets.setAll('anchor.x', 0.5);
  90.     bullets.setAll('anchor.y', 1);
  91.     bullets.setAll('outOfBoundsKill', true);
  92.     bullets.setAll('checkWorldBounds', true);
  93.  
  94.     // The enemy's bullets
  95.     enemyBullets = game.add.group();
  96.     enemyBullets.enableBody = true;
  97.     enemyBullets.physicsBodyType = Phaser.Physics.ARCADE;
  98.     enemyBullets.createMultiple(30, 'enemyBullet');
  99.     enemyBullets.setAll('anchor.x', 0.5);
  100.     enemyBullets.setAll('anchor.y', 1);
  101.     enemyBullets.setAll('outOfBoundsKill', true);
  102.     enemyBullets.setAll('checkWorldBounds', true);
  103.  
  104.     //  The hero!
  105.     player = game.add.sprite(400, 500, 'ship');
  106.     player.anchor.setTo(0.5, 0.5);
  107.     game.physics.enable(player, Phaser.Physics.ARCADE);
  108.  
  109.     //  The baddies!
  110.     aliens = game.add.group();
  111.     aliens.enableBody = true;
  112.     aliens.physicsBodyType = Phaser.Physics.ARCADE;
  113.  
  114.     createAliens();
  115.  
  116.     //  The score
  117.     scoreString = 'Score : ';
  118.     scoreText = game.add.text(10, 10, scoreString + score, { font: '34px Arial', fill: '#fff' });
  119.  
  120.     //  Lives
  121.     lives = game.add.group();
  122.     game.add.text(game.world.width - 100, 10, 'Lives : ', { font: '34px Arial', fill: '#fff' });
  123.  
  124.     //  Text
  125.     stateText = game.add.text(game.world.centerX,game.world.centerY,' ', { font: '84px Arial', fill: '#fff' });
  126.     stateText.anchor.setTo(0.5, 0.5);
  127.     stateText.visible = false;
  128.  
  129.     for (var i = 0; i < 3; i++)
  130.     {
  131.         var ship = lives.create(game.world.width - 100 + (30 * i), 60, 'ship');
  132.         ship.anchor.setTo(0.5, 0.5);
  133.         ship.angle = 90;
  134.         ship.alpha = 0.4;
  135.     }
  136.  
  137.     //  An explosion pool
  138.     explosions = game.add.group();
  139.     explosions.createMultiple(30, 'kaboom');
  140.     explosions.forEach(setupInvader, this);
  141.  
  142.     //  And some controls to play the game with
  143.     cursors = game.input.keyboard.createCursorKeys();
  144.     fireButton = game.input.keyboard.addKey(Phaser.Keyboard.SPACEBAR);
  145.    
  146.     //Music
  147.     music = game.add.audio('boden');  
  148.     music.play();  
  149.     music.volume = 0.25;
  150.  
  151.  
  152.     //  Here we set-up our audio sprite
  153.     fx = game.add.audio('sfx');
  154.     fx.allowMultiple = true;
  155.  
  156.     //  And this defines the markers.
  157.  
  158.     //  They consist of a key (for replaying), the time the sound starts and the duration, both given in seconds.
  159.     //  You can also set the volume and loop state, although we don't use them in this example (see the docs)
  160.  
  161.     fx.addMarker('alien death', 1, 1.0);
  162.     fx.addMarker('boss hit', 3, 0.5);
  163.     fx.addMarker('escape', 4, 3.2);
  164.     fx.addMarker('meow', 8, 0.5);
  165.     fx.addMarker('numkey', 9, 0.1);
  166.     fx.addMarker('ping', 10, 1.0);
  167.     fx.addMarker('death', 12, 4.2);
  168.     fx.addMarker('shot', 17, 1.0);
  169.     fx.addMarker('squit', 19, 0.3);
  170.  
  171. }
  172.  
  173. function update() {
  174.  
  175.     //  Scroll the background
  176.     starfield.tilePosition.y += 2;
  177.  
  178.     if (player.alive)
  179.     {
  180.         //  Reset the player, then check for movement keys
  181.         player.body.velocity.setTo(0, 0);
  182.  
  183.         if (cursors.left.isDown)
  184.         {
  185.             player.body.velocity.x = -200;
  186.         }
  187.         else if (cursors.right.isDown)
  188.         {
  189.             player.body.velocity.x = 200;
  190.         }
  191.  
  192.         //  Firing?
  193.         if (fireButton.isDown)
  194.         {
  195.             fireBullet();
  196.         }
  197.  
  198.         if (game.time.now > firingTimer)
  199.         {
  200.             enemyFires();
  201.         }
  202.  
  203.         //  Run collision
  204.         game.physics.arcade.overlap(bullets, aliens, collisionHandler, null, this);
  205.         game.physics.arcade.overlap(enemyBullets, player, enemyHitsPlayer, null, this);
  206.     }
  207.  
  208. }
  209.  
  210. function render() {
  211.  
  212.     // for (var i = 0; i < aliens.length; i++)
  213.     // {
  214.     //     game.debug.body(aliens.children[i]);
  215.     // }
  216.  
  217. }
  218.  
  219. function collisionHandler (bullet, alien) {
  220.  
  221.     //  When a bullet hits an alien we kill them both
  222.     bullet.kill();
  223.     alien.kill();
  224.      PlayAudioFX('alien death');
  225.  
  226.     //  Increase the score
  227.     score += 20;
  228.     scoreText.text = scoreString + score;
  229.  
  230.     //  And create an explosion :)
  231.     var explosion = explosions.getFirstExists(false);
  232.     explosion.reset(alien.body.x, alien.body.y);
  233.     explosion.play('kaboom', 30, false, true);
  234.  
  235.     if (aliens.countLiving() == 0)
  236.     {
  237.         score += 1000;
  238.         scoreText.text = scoreString + score;
  239.  
  240.         enemyBullets.callAll('kill',this);
  241.         stateText.text = " You Won, \n Click to end.";
  242.         stateText.visible = true;
  243.  
  244.         //the "click to end minigame" handler
  245.         game.input.onTap.addOnce(EndMinigame,this);
  246.     }
  247.  
  248. }
  249.  
  250. //--------------------------------------------------
  251.  
  252. function createAliens () {
  253.  
  254.     for (var y = 0; y < 4; y++)
  255.     {
  256.         for (var x = 0; x < 10; x++)
  257.         {
  258.             var alien = aliens.create(x * 48, y * 50, 'invader');
  259.             alien.anchor.setTo(0.5, 0.5);
  260.             alien.animations.add('fly', [ 0, 1, 2, 3 ], 20, true);
  261.             alien.play('fly');
  262.             alien.body.moves = false;
  263.         }
  264.     }
  265.  
  266.     aliens.x = 100;
  267.     aliens.y = 50;
  268.  
  269.     //  All this does is basically start the invaders moving. Notice we're moving the Group they belong to, rather than the invaders directly.
  270.     var tween = game.add.tween(aliens).to( { x: 200 }, 2000, Phaser.Easing.Linear.None, true, 0, 1000, true);
  271.  
  272.     //  When the tween loops it calls descend
  273.     tween.onLoop.add(descend, this);
  274. }
  275.  
  276. function setupInvader (invader) {
  277.  
  278.     invader.anchor.x = 0.5;
  279.     invader.anchor.y = 0.5;
  280.     invader.animations.add('kaboom');
  281.  
  282. }
  283.  
  284. function descend() {
  285.  
  286.     aliens.y += 10;
  287.  
  288. }
  289.  
  290. function enemyHitsPlayer (player,bullet) {
  291.    
  292.     bullet.kill();
  293.  
  294.     live = lives.getFirstAlive();
  295.  
  296.     if (live)
  297.     {
  298.         live.kill();
  299.     }
  300.  
  301.     //  And create an explosion :)
  302.     var explosion = explosions.getFirstExists(false);
  303.     explosion.reset(player.body.x, player.body.y);
  304.     explosion.play('kaboom', 30, false, true);
  305.  
  306.     // When the player dies
  307.     if (lives.countLiving() < 1)
  308.     {
  309.         PlayAudioFX('death');
  310.  
  311.         player.kill();
  312.         enemyBullets.callAll('kill');
  313.  
  314.         stateText.text=" GAME OVER \n Click to restart";
  315.         stateText.visible = true;
  316.  
  317.         //the "click to restart" handler
  318.         game.input.onTap.addOnce(restart,this);
  319.     }
  320.  
  321. }
  322.  
  323. function enemyFires () {
  324.  
  325.     //  Grab the first bullet we can from the pool
  326.     enemyBullet = enemyBullets.getFirstExists(false);
  327.  
  328.     livingEnemies.length=0;
  329.  
  330.     aliens.forEachAlive(function(alien){
  331.  
  332.         // put every living enemy in an array
  333.         livingEnemies.push(alien);
  334.     });
  335.  
  336.  
  337.        
  338.    
  339.  
  340.     if (enemyBullet && livingEnemies.length > 0)
  341.     {
  342.          PlayAudioFX('meow');  
  343.  
  344.         var random=game.rnd.integerInRange(0,livingEnemies.length-1);
  345.  
  346.         // randomly select one of them
  347.         var shooter=livingEnemies[random];
  348.         // And fire the bullet from this enemy
  349.         enemyBullet.reset(shooter.body.x, shooter.body.y);
  350.  
  351.         game.physics.arcade.moveToObject(enemyBullet,player,120);
  352.         firingTimer = game.time.now + 2000;
  353.     }
  354.  
  355. }
  356.  
  357. function fireBullet () {
  358.  
  359.     //  To avoid them being allowed to fire too fast we set a time limit
  360.     if (game.time.now > bulletTime)
  361.     {
  362.         //  Grab the first bullet we can from the pool
  363.         bullet = bullets.getFirstExists(false);
  364.  
  365.         if (bullet)
  366.         {
  367.             //  And fire it
  368.             bullet.reset(player.x, player.y + 8);
  369.             bullet.body.velocity.y = -400;
  370.             bulletTime = game.time.now + 200;
  371.         }
  372.  
  373.             PlayAudioFX('shot');
  374.     }
  375.  
  376. }
  377.  
  378. function resetBullet (bullet) {
  379.  
  380.     //  Called if the bullet goes out of the screen
  381.     bullet.kill();
  382.  
  383. }
  384.  
  385. function restart () {
  386.  
  387.     //  A new level starts
  388.    
  389.     //resets the life count
  390.     lives.callAll('revive');
  391.     //  And brings the aliens back from the dead :)
  392.     aliens.removeAll();
  393.     createAliens();
  394.  
  395.     //revives the player
  396.     player.revive();
  397.     //hides the text
  398.     stateText.visible = false;
  399.  
  400. }
  401.  
  402. function PlayAudioFX(name) {
  403.         //Audio
  404.         fx.play(name);
  405.         fx.volume = 0.1;
  406. }
  407.  
  408. /*
  409. =========================================================
  410. ================ TYRANO =================================
  411. =========================================================
  412. */
  413.  
  414.  
  415. function DestroyMiniGame() {
  416.     fx.destroy();
  417.     music.destroy();
  418.     game.cache.removeSound('boden');
  419.     //Destroy miniga
  420.     game.destroy();
  421. }
  422.  
  423.  
  424. function EndMinigame()
  425. {
  426.     DestroyMiniGame();
  427.     //Variable created in Tyranno Script
  428.     f.endMinigame = true;
  429.     //Clears the HTML DIV mini game
  430.     $( ".layer.layer_free" ).remove();
  431.  
  432. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement