Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /* HunterIntro();
- function HunterIntro() {
- // First we create a DOM object for the video inside the html document
- var videoCanvas = document.createElement("video");
- $(videoCanvas).attr(
- {"src":"Video/HunterIntro.mp4",
- "width":1280,
- "height":720,
- "autoplay":1,
- "controls":0,})
- document.body.appendChild(videoCanvas);
- // When the video has loaded, we play it
- document.onload = function playVid() { vid.play(videoCanvas); }
- // Next we check if the video has ended using addEventListener
- videoCanvas.addEventListener('ended',HunterIntroEnded,false);
- // When the video ends, we remove the <video> element and start the game
- function HunterIntroEnded(e) {
- document.body.removeChild(videoCanvas);
- console.log("Video finished. Loading game.");
- // Music
- // Create a seamless music loop with howler.js.
- HuntGame(); ACTUALLY BELONGS HERE } } */
- HuntGame();
- function HuntGame() {
- var preyOfBirds = new Howl({
- src: ['Music/PreyOfBirds.mp3'],
- autoplay: true,
- loop: true,
- volume:0.5,
- sprite: {
- main: [0, 1008400, true]
- }
- });
- preyOfBirds.play('main');
- /* The game */
- var huntGame = new Phaser.Game(1280, 720, Phaser.AUTO, 'huntGame',
- { preload: preload, create: create, update: update, render: render });
- function preload() {
- huntGame.load.atlasJSONHash('crow', 'Graphic/Crow_animation.png', 'Graphic/Crow_animation.json');
- huntGame.load.physics('Crow_physics', 'Graphic/Crow_physics.json')
- huntGame.load.atlasJSONHash('crowRight', 'Graphic/CrowRight.png', 'Graphic/CrowRight.json');
- huntGame.load.physics('Arrow_physics', 'Graphic/Arrow_physics.json');
- huntGame.load.image('background', 'Graphic/HuntBG.png');
- huntGame.load.image('arrow','Graphic/Arrow.png');
- huntGame.load.image('archerPlaceholder','Graphic/ArcherPlaceholder.png');
- huntGame.load.image('crosshair','Graphic/Crosshair.png')
- huntGame.load.audio('bowDraw','Sound/BowDraw.ogg')
- huntGame.load.audio('bowFire','Sound/BowFire.ogg')
- huntGame.load.image('birdHit','Graphic/BirdHit.png')
- huntGame.load.audio('birdHitSFX','Sound/BirdHit.ogg')
- }
- var arrow;
- var firePower = 501;
- var mouseYInitial = 0;
- var settingPower = false;
- var powerBar;
- var powerBarBG;
- var drawBowSFX;
- var fireBowSFX;
- var birdHitSFX;
- var arrows;
- var quiver = 20;
- var birdsToKill = 10;
- function create() {
- // Physics
- huntGame.physics.startSystem(Phaser.Physics.P2JS);
- huntGame.physics.p2.setImpactEvents(true);
- huntGame.physics.p2.gravity.y = 1000;
- birdCollisionGroup = huntGame.physics.p2.createCollisionGroup();
- arrowCollisionGroup = huntGame.physics.p2.createCollisionGroup();
- huntGame.physics.p2.updateBoundsCollisionGroup();
- // Power adjust bar
- powerBarBG = new Phaser.Rectangle(795, 495, 30, 210);
- powerBar = new Phaser.Rectangle(800, 500, 20, 200);
- // Sound effects
- drawBoxSFX = huntGame.add.audio('bowDraw')
- fireBowSFX = huntGame.add.audio('bowFire')
- birdHitSFX = huntGame.add.audio('birdHitSFX')
- // Sprites etc
- var huntBG = huntGame.add.sprite(0, 0, 'background'); // Lisätään ensin taustakuva pelin pohjalle.
- var archer = huntGame.add.sprite(550, 450, 'archerPlaceholder');
- archer.scale.x = 0.5;
- archer.scale.y = 0.5;
- /* Birds' spawning and defining */
- birdsLeft = huntGame.add.group();
- birdsRight = huntGame.add.group();
- var j = 0;
- for (var i = 0; i < 10; i++) // Change to increase / decrease birds.
- {
- j++; // To make every 2nd bird spawn from a different side.
- if (j % 2 == 0) {
- var bird = birdsLeft.create(huntGame.rnd.between(-1000, -150), huntGame.rnd.between(80, 200), 'crow');
- huntGame.physics.p2.enable(bird,false);
- bird.body.kinematic = true;
- bird.body.clearShapes();
- bird.body.loadPolygon('Crow_physics','Crow_animation');
- bird.body.setCollisionGroup(birdCollisionGroup);
- bird.body.collides(arrowCollisionGroup);
- bird.animations.add('flap', [0, 1], 5, true);
- bird.animations.play('flap');
- bird.body.velocity.x = huntGame.rnd.between(120, 300);
- }
- // To be implemented later in P2 physics
- else {
- var birdRight = birdsRight.create(huntGame.rnd.between(2280, 1430), huntGame.rnd.between(80, 180), 'crowRight');
- huntGame.physics.p2.enable(birdRight,true);
- birdRight.body.clearShapes();
- birdRight.body.damping = 0;
- birdRight.scale.x = "0.5";
- birdRight.scale.y = "0.5";
- birdRight.animations.add('flapRight', [0, 1], 5, true);
- birdRight.animations.play('flapRight');
- birdRight.body.velocity.x = huntGame.rnd.between(-120, -300);
- }
- }
- arrows = huntGame.add.group();
- /* Function calls for weapon */
- huntGame.input.onDown.add(draw);
- huntGame.input.onUp.add(release);
- }
- /* First action when mouse is pressed down */
- function draw() {
- powerBar.height = 0;
- drawBoxSFX.play();
- mouseYInitial = huntGame.input.mousePointer.y;
- settingPower = true;
- }
- /* Action when mouse is released */
- function release() {
- settingPower = false;
- powerBar.height = 0;
- if (huntGame.input.activePointer.withinGame) {
- fireBowSFX.play();
- /* Arrow */
- var arrow = arrows.create(640, 640, 'arrow');
- huntGame.physics.p2.enable(arrow, false);
- arrow.body.clearShapes();
- arrow.body.loadPolygon('Arrow_physics','Arrow');
- arrow.body.setCollisionGroup(arrowCollisionGroup);
- arrow.body.collideWorldBounds = false;
- arrow.body.collides(birdCollisionGroup, hitBird, this);
- arrow.body.velocity.x = -(huntGame.input.activePointer.worldX - arrow.x)
- arrow.body.velocity.y = -firePower;
- }
- }
- function hitBird (body1, body2) {
- console.log(body1.velocity.y);
- body1.clearCollision(true);
- birdHitSFX.play();
- body1.sprite.loadTexture('birdHit');
- body2.sprite.kill();
- body2.removeFromWorld();
- console.log("Hit!");
- }
- function update() {
- arrows.forEach(rotate, this);
- function rotate(arrow) {
- console.log("Arrow angle: " + arrow.body.angle);
- }
- /* Firepower adjust */
- if (settingPower = true && huntGame.input.activePointer.withinGame)
- {
- if (((huntGame.input.activePointer.y - mouseYInitial) * 3) < 500) {
- firePower = 500;
- }
- else if (((huntGame.input.activePointer.y - mouseYInitial) * 3) > 1100) {
- firePower = 1100;
- }
- else {
- firePower = ((huntGame.input.activePointer.y - mouseYInitial) * 3)
- }
- }
- /* Power bar height setting */
- if (firePower <= 1100) {
- powerBar.height = ( firePower - 500 ) / 3;
- $("powerBar").css({"color":"black"})
- }
- else if (firePower < 500) {
- poweBar.height = 0;
- }
- else { powerBar.height = 200 }
- /* Bird position checks */
- birdsLeft.forEach(checkPos, this);
- function checkPos (bird) {
- if (bird.body.x > 1430) {
- bird.body.x = huntGame.rnd.between(-350, -150);
- bird.body.y = huntGame.rnd.between(80, 200)
- bird.body.velocity.x = huntGame.rnd.between(150, 300);
- }
- }
- birdsRight.forEach(checkPos2, this);
- function checkPos2 (birdRight) {
- if (birdRight.x < -150) {
- birdRight.x = 1430;
- birdRight.body.velocity.x = huntGame.rnd.between(-120, -300);
- }
- }
- }
- function render()
- {
- /* Power bar changes color depending on its height */
- huntGame.debug.geom(powerBarBG, '#000')
- if (huntGame.input.mousePointer.isDown) {
- if (powerBar.height < 150 && powerBar.height > 100) {
- huntGame.debug.geom(powerBar,'#e8924d');
- }
- else if (powerBar.height >= 150) {
- huntGame.debug.geom(powerBar,'#7a2400');
- }
- else if (powerBar.height <= 100) {
- huntGame.debug.geom(powerBar,'#ead29f')
- }
- }
- /* Other debug stuff */
- // huntGame.debug.inputInfo(32, 32);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement