Advertisement
Guest User

Untitled

a guest
Feb 11th, 2018
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /* HunterIntro();
  2.  
  3. function HunterIntro() {
  4.     // First we create a DOM object for the video inside the html document
  5.     var videoCanvas = document.createElement("video");
  6.     $(videoCanvas).attr(
  7.         {"src":"Video/HunterIntro.mp4",
  8.          "width":1280,
  9.          "height":720,
  10.          "autoplay":1,
  11.          "controls":0,})
  12.     document.body.appendChild(videoCanvas);
  13.     // When the video has loaded, we play it
  14.     document.onload = function playVid() { vid.play(videoCanvas); }
  15.     // Next we check if the video has ended using addEventListener
  16.     videoCanvas.addEventListener('ended',HunterIntroEnded,false);
  17.     // When the video ends, we remove the <video> element and start the game
  18.     function HunterIntroEnded(e) {
  19.         document.body.removeChild(videoCanvas);
  20.         console.log("Video finished. Loading game.");
  21.         // Music
  22.         // Create a seamless music loop with howler.js.
  23.        
  24.             HuntGame(); ACTUALLY BELONGS HERE } } */
  25.  
  26. HuntGame();
  27.  
  28. function HuntGame() {
  29.     var preyOfBirds = new Howl({
  30.         src: ['Music/PreyOfBirds.mp3'],
  31.         autoplay: true,
  32.         loop: true,
  33.         volume:0.5,
  34.         sprite: {
  35.             main: [0, 1008400, true]
  36.             }
  37.             });
  38.             preyOfBirds.play('main');
  39.    
  40.     /* The game */
  41.     var huntGame = new Phaser.Game(1280, 720, Phaser.AUTO, 'huntGame',
  42.                                    { preload: preload, create: create, update: update, render: render });
  43.    
  44.     function preload() {
  45.         huntGame.load.atlasJSONHash('crow', 'Graphic/Crow_animation.png', 'Graphic/Crow_animation.json');
  46.         huntGame.load.physics('Crow_physics', 'Graphic/Crow_physics.json')
  47.         huntGame.load.atlasJSONHash('crowRight', 'Graphic/CrowRight.png', 'Graphic/CrowRight.json');
  48.         huntGame.load.physics('Arrow_physics', 'Graphic/Arrow_physics.json');
  49.         huntGame.load.image('background', 'Graphic/HuntBG.png');
  50.         huntGame.load.image('arrow','Graphic/Arrow.png');
  51.         huntGame.load.image('archerPlaceholder','Graphic/ArcherPlaceholder.png');
  52.         huntGame.load.image('crosshair','Graphic/Crosshair.png')
  53.         huntGame.load.audio('bowDraw','Sound/BowDraw.ogg')
  54.         huntGame.load.audio('bowFire','Sound/BowFire.ogg')
  55.         huntGame.load.image('birdHit','Graphic/BirdHit.png')
  56.         huntGame.load.audio('birdHitSFX','Sound/BirdHit.ogg')
  57.     }
  58.    
  59.     var arrow;
  60.     var firePower = 501;
  61.     var mouseYInitial = 0;
  62.     var settingPower = false;
  63.     var powerBar;
  64.     var powerBarBG;
  65.     var drawBowSFX;
  66.     var fireBowSFX;
  67.     var birdHitSFX;
  68.     var arrows;
  69.     var quiver = 20;
  70.     var birdsToKill = 10;
  71.    
  72.     function create() {
  73.         // Physics
  74.         huntGame.physics.startSystem(Phaser.Physics.P2JS);
  75.         huntGame.physics.p2.setImpactEvents(true);
  76.         huntGame.physics.p2.gravity.y = 1000;
  77.        
  78.         birdCollisionGroup = huntGame.physics.p2.createCollisionGroup();
  79.         arrowCollisionGroup = huntGame.physics.p2.createCollisionGroup();
  80.         huntGame.physics.p2.updateBoundsCollisionGroup();
  81.        
  82.         // Power adjust bar
  83.         powerBarBG = new Phaser.Rectangle(795, 495, 30, 210);
  84.         powerBar = new Phaser.Rectangle(800, 500, 20, 200);
  85.        
  86.         // Sound effects
  87.         drawBoxSFX = huntGame.add.audio('bowDraw')
  88.         fireBowSFX = huntGame.add.audio('bowFire')
  89.         birdHitSFX = huntGame.add.audio('birdHitSFX')
  90.        
  91.         // Sprites etc
  92.         var huntBG = huntGame.add.sprite(0, 0, 'background'); // Lisätään ensin taustakuva pelin pohjalle.
  93.         var archer = huntGame.add.sprite(550, 450, 'archerPlaceholder');
  94.         archer.scale.x = 0.5;
  95.         archer.scale.y = 0.5;
  96.        
  97.         /* Birds' spawning and defining */
  98.         birdsLeft = huntGame.add.group();
  99.         birdsRight = huntGame.add.group();
  100.        
  101.         var j = 0;
  102.         for (var i = 0; i < 10; i++) // Change to increase / decrease birds.
  103.             {
  104.                 j++; // To make every 2nd bird spawn from a different side.
  105.                 if (j % 2 == 0) {
  106.                     var bird = birdsLeft.create(huntGame.rnd.between(-1000, -150), huntGame.rnd.between(80, 200), 'crow');
  107.                     huntGame.physics.p2.enable(bird,false);
  108.                     bird.body.kinematic = true;
  109.                     bird.body.clearShapes();
  110.                     bird.body.loadPolygon('Crow_physics','Crow_animation');
  111.                     bird.body.setCollisionGroup(birdCollisionGroup);
  112.                     bird.body.collides(arrowCollisionGroup);
  113.                     bird.animations.add('flap', [0, 1], 5, true);
  114.                     bird.animations.play('flap');
  115.                     bird.body.velocity.x = huntGame.rnd.between(120, 300);
  116.                 }
  117.                 // To be implemented later in P2 physics
  118.                 else {
  119.                     var birdRight = birdsRight.create(huntGame.rnd.between(2280, 1430), huntGame.rnd.between(80, 180), 'crowRight');
  120.                     huntGame.physics.p2.enable(birdRight,true);
  121.                     birdRight.body.clearShapes();
  122.                     birdRight.body.damping = 0;
  123.                     birdRight.scale.x = "0.5";
  124.                     birdRight.scale.y = "0.5";
  125.                     birdRight.animations.add('flapRight', [0, 1], 5, true);
  126.                     birdRight.animations.play('flapRight');
  127.                     birdRight.body.velocity.x = huntGame.rnd.between(-120, -300);
  128.                 }
  129.             }
  130.        
  131.         arrows = huntGame.add.group();
  132.         /* Function calls for weapon */
  133.         huntGame.input.onDown.add(draw);
  134.         huntGame.input.onUp.add(release);
  135.     }
  136.    
  137.     /* First action when mouse is pressed down */
  138.     function draw() {
  139.         powerBar.height = 0;
  140.         drawBoxSFX.play();
  141.         mouseYInitial = huntGame.input.mousePointer.y;
  142.         settingPower = true;
  143.     }
  144.    
  145.     /* Action when mouse is released */
  146.     function release() {
  147.         settingPower = false;
  148.         powerBar.height = 0;
  149.         if (huntGame.input.activePointer.withinGame) {
  150.             fireBowSFX.play();
  151.            
  152.             /* Arrow */
  153.             var arrow = arrows.create(640, 640, 'arrow');
  154.             huntGame.physics.p2.enable(arrow, false);
  155.             arrow.body.clearShapes();
  156.             arrow.body.loadPolygon('Arrow_physics','Arrow');
  157.             arrow.body.setCollisionGroup(arrowCollisionGroup);
  158.             arrow.body.collideWorldBounds = false;
  159.             arrow.body.collides(birdCollisionGroup, hitBird, this);
  160.             arrow.body.velocity.x = -(huntGame.input.activePointer.worldX - arrow.x)
  161.             arrow.body.velocity.y = -firePower;
  162.         }
  163.     }
  164.    
  165.     function hitBird (body1, body2) {
  166.         console.log(body1.velocity.y);
  167.             body1.clearCollision(true);
  168.             birdHitSFX.play();
  169.             body1.sprite.loadTexture('birdHit');
  170.             body2.sprite.kill();
  171.             body2.removeFromWorld();
  172.             console.log("Hit!");
  173.     }
  174.    
  175.     function update() {
  176.        
  177.         arrows.forEach(rotate, this);
  178.        
  179.         function rotate(arrow) {
  180.             console.log("Arrow angle: " + arrow.body.angle);
  181.         }
  182.        
  183.         /* Firepower adjust */
  184.         if (settingPower = true && huntGame.input.activePointer.withinGame)
  185.         {
  186.             if (((huntGame.input.activePointer.y - mouseYInitial) * 3) < 500) {
  187.                 firePower = 500;
  188.             }
  189.             else if (((huntGame.input.activePointer.y - mouseYInitial) * 3) > 1100) {
  190.                 firePower = 1100;
  191.             }
  192.             else {
  193.                 firePower = ((huntGame.input.activePointer.y - mouseYInitial) * 3)
  194.             }
  195.         }
  196.        
  197.         /* Power bar height setting */
  198.         if (firePower <= 1100) {
  199.             powerBar.height = ( firePower - 500 ) / 3;
  200.             $("powerBar").css({"color":"black"})
  201.         }
  202.         else if (firePower < 500) {
  203.             poweBar.height = 0;
  204.         }
  205.         else { powerBar.height = 200 }
  206.        
  207.         /* Bird position checks */
  208.         birdsLeft.forEach(checkPos, this);
  209.         function checkPos (bird) {
  210.             if (bird.body.x > 1430) {
  211.                 bird.body.x = huntGame.rnd.between(-350, -150);
  212.                 bird.body.y = huntGame.rnd.between(80, 200)
  213.                 bird.body.velocity.x = huntGame.rnd.between(150, 300);
  214.             }
  215.         }
  216.         birdsRight.forEach(checkPos2, this);
  217.         function checkPos2 (birdRight) {
  218.             if (birdRight.x < -150) {
  219.                 birdRight.x = 1430;
  220.                 birdRight.body.velocity.x = huntGame.rnd.between(-120, -300);
  221.             }
  222.         }
  223.     }
  224.    
  225.    
  226.    
  227.     function render()
  228.     {
  229.         /* Power bar changes color depending on its height */
  230.         huntGame.debug.geom(powerBarBG, '#000')
  231.         if (huntGame.input.mousePointer.isDown) {
  232.             if (powerBar.height < 150 && powerBar.height > 100) {
  233.                 huntGame.debug.geom(powerBar,'#e8924d');
  234.             }
  235.             else if (powerBar.height >= 150) {
  236.                 huntGame.debug.geom(powerBar,'#7a2400');
  237.             }
  238.             else if (powerBar.height <= 100) {
  239.                 huntGame.debug.geom(powerBar,'#ead29f')
  240.             }
  241.         }
  242.         /* Other debug stuff */
  243.         // huntGame.debug.inputInfo(32, 32);
  244.     }
  245. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement