Advertisement
armyoflemons

Untitled

Apr 19th, 2020
498
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4.     <meta charset="utf-8">
  5.     <meta lang="en-us">
  6.     <title>LD46</title>
  7.     <script src="js/phaser.min.js"></script>
  8. </head>
  9. <body>
  10.     <script>
  11.  
  12.     class Entity extends Phaser.GameObjects.Sprite {
  13.         constructor(scene, x, y, key) {
  14.             super(scene, x, y, key);
  15.  
  16.             this.scene = scene;
  17.             this.scene.add.existing(this);
  18.             this.scene.physics.world.enableBody(this, 0);
  19.  
  20.             this.dirEnum = {
  21.                 LEFT: 'left',
  22.                 RIGHT: 'right'
  23.             }
  24.             this.dir = this.dirEnum.RIGHT;
  25.         }
  26.     }
  27.  
  28.     class Bullet extends Entity {
  29.         constructor(scene, x, y, key) {
  30.             super(scene, x, y, key);
  31.         }
  32.  
  33.         initialize() {
  34.             this.dir = this.scene.player.dir;
  35.             if (this.dir == this.dirEnum.RIGHT)
  36.                 this.body.setVelocityX(1000);
  37.             else
  38.                 this.body.setVelocityX(-1000);
  39.             this.body.setAllowGravity(false);
  40.  
  41.             this.emitter = this.scene.sparkle.createEmitter({
  42.                 speed: 50,
  43.                 scale: { start: 0.4, end: 0.2 },
  44.                 alpha: { start: 1, end: 0 },
  45.                 blendMode: 'COLOR',
  46.                 lifespan: 200
  47.             });
  48.             this.emitter.startFollow(this);
  49.         }
  50.  
  51.         explode() {
  52.             this.emitter.explode(1);
  53.             this.destroy();
  54.         }
  55.     }
  56.  
  57.     class EnemyBullet extends Entity {
  58.         constructor(scene, x, y, key) {
  59.             super(scene, x, y, key);
  60.         }
  61.  
  62.         initialize(dir) {
  63.             this.dir = dir;
  64.             if (this.dir == this.dirEnum.RIGHT)
  65.                 this.body.setVelocityX(750);
  66.             else
  67.                 this.body.setVelocityX(-750);
  68.             this.body.setAllowGravity(false);
  69.         }
  70.  
  71.         update() {
  72.             //this.scene.enemyBulletParticleEmitter.explode(1, this.x, this.y);
  73.             let d = Phaser.Math.Distance.Between(this.x, this.y, this.scene.fairy.x, this.scene.fairy.y);
  74.             if (d < 22) {
  75.                 this.scene.fairy.die();
  76.                 this.scene.player.die(false, false);
  77.             }
  78.         }
  79.     }
  80.  
  81.     class Player extends Entity {
  82.         constructor(scene, x, y, key) {
  83.             super(scene, x, y, key);
  84.  
  85.             this.body.setCollideWorldBounds(true);
  86.             this.setDepth(10);
  87.             this.scene.cameras.main.startFollow(this);
  88.             //this.body.setFriction(1);
  89.             //this.body.setAllowDrag(true);
  90.             //this.body.setDrag(2000, 0);
  91.  
  92.             this.reloadTime = 10;
  93.             this.reload = 0;
  94.  
  95.             this.inTheAir = true;
  96.             this.dead = false;
  97.         }
  98.  
  99.         goUp() {
  100.             this.body.setVelocityY(-600);
  101.             this.scene.sound.play('jump');
  102.         }
  103.  
  104.         goLeft() {
  105.             this.body.setVelocityX(-400);
  106.             this.dir = this.dirEnum.LEFT;
  107.             this.play('stay_left', true);
  108.         }
  109.  
  110.         goRight() {
  111.             this.body.setVelocityX(400);
  112.             this.dir = this.dirEnum.RIGHT;
  113.             this.play('stay_right', true);
  114.         }
  115.  
  116.         stay() {
  117.             this.body.setVelocityX(0);
  118.             if (this.dir == this.dirEnum.LEFT)
  119.                 this.play('stay_left', true);
  120.             else
  121.                 this.play('stay_right', true);
  122.         }
  123.  
  124.         shoot() {
  125.             if (this.reload > 0) return;
  126.            
  127.             let bullet = new Bullet(this.scene, this.x, this.y, 'bullet');
  128.             this.scene.bullets.add(bullet);
  129.             bullet.initialize();
  130.  
  131.             this.scene.enemyBulletParticleEmitter.explode(2, this.x, this.y);
  132.             this.scene.sound.play('shoot');
  133.  
  134.             this.reload = this.reloadTime;
  135.         }
  136.  
  137.         update() {
  138.             if (this.dead) return;
  139.  
  140.             if (this.inTheAir && this.body.blocked.down) {
  141.                 this.scene.dustEmitter.explode(50, this.x, (Math.floor(this.y / 32 + 1) * 32));
  142.                 this.scene.sound.play('land');
  143.                 this.inTheAir = false;
  144.             }
  145.             else if (!this.inTheAir && !this.body.blocked.down) {
  146.                 this.inTheAir = true;
  147.             }
  148.  
  149.             if (this.reload > 0)
  150.                 this.reload--;
  151.            
  152.             let tile = this.scene.layer.getTileAtWorldXY(this.x, this.y, true);
  153.             if (tile.index == 12 && this.body.blocked.down) {
  154.                 this.die(true, false);
  155.             }
  156.  
  157.             let dToGate = Phaser.Math.Distance.Between(this.x, this.y, this.scene.gate.x, this.scene.gate.y);
  158.             if (dToGate < 32) {
  159.                 this.scene.level++;
  160.                 this.die(false, true);
  161.             }
  162.         }
  163.  
  164.         die(rag, next) {
  165.             if (this.dead) return;
  166.             this.dead = true;
  167.  
  168.             this.setActive(false);
  169.             this.setVisible(false);
  170.  
  171.             if (!next)
  172.                 this.scene.sound.play('kill');
  173.             else
  174.                 this.scene.sound.play('next_level');
  175.  
  176.             this.scene.cameras.main.shake(100, 0.02);
  177.             this.scene.cameras.main.stopFollow();
  178.             if (rag) {
  179.                 let ragdoll = new EnemyRagdoll(this, 'hero');
  180.                 this.scene.ragdolls.add(ragdoll); // DEAD
  181.                 ragdoll.initialize();
  182.             }
  183.  
  184.             this.scene.time.addEvent({
  185.                 delay: 200,
  186.                 callback: function () {
  187.                     this.scene.cameras.main.fadeOut(300);
  188.                 },
  189.                 callbackScope: this,
  190.                 loop: false
  191.             });
  192.  
  193.             this.scene.time.addEvent({
  194.                 delay: 500,
  195.                 callback: function () {
  196.                     this.scene.cameras.main.fadeIn(200);
  197.                     if (next) {
  198.                         this.scene.destroyLevel();
  199.                         this.scene.initializeLevel();
  200.                     }
  201.                     this.scene.restart();
  202.                 },
  203.                 callbackScope: this,
  204.                 loop: false
  205.             });
  206.         }
  207.  
  208.         restart() {
  209.             this.dir = this.dirEnum.RIGHT;
  210.             this.dead = false;
  211.             this.setActive(true);
  212.             this.setVisible(true);
  213.         }
  214.     }
  215.  
  216.     class Enemy extends Entity {
  217.         constructor(scene, x, y, key) {
  218.             super(scene, x, y, key);
  219.  
  220.             this.body.setCollideWorldBounds(true);
  221.             this.setDepth(9);
  222.  
  223.             this.reloadTime = 90;
  224.             this.reload = Phaser.Math.Between(0, this.reloadTime);
  225.         }
  226.  
  227.         update() {
  228.             if (this.scene.player.x < this.x) {
  229.                 this.dir = this.dirEnum.LEFT;
  230.                 this.play('stay_left', true);
  231.             }
  232.             else {
  233.                 this.dir = this.dirEnum.RIGHT;
  234.                 this.play('stay_right', true);
  235.             }
  236.  
  237.             if (this.reload <= 0) {
  238.                 let bullet = new EnemyBullet(this.scene, this.x, this.y, 'bullet');
  239.                 this.scene.enemyBullets.add(bullet);
  240.                 bullet.initialize(this.dir);
  241.  
  242.                 this.scene.enemyBulletParticleEmitter.explode(2, this.x, this.y);
  243.                 this.scene.sound.play('shoot_enemy');
  244.  
  245.                 this.reload = this.reloadTime;
  246.             }
  247.             else {
  248.                 this.reload--;
  249.             }
  250.         }
  251.     }
  252.  
  253.     class EnemyRagdoll extends Entity {
  254.         constructor(entity, key) {
  255.             super(entity.scene, entity.x, entity.y, key);
  256.  
  257.             this.setDepth(9);
  258.             this.dir = entity.dir;
  259.         }
  260.  
  261.         initialize() {
  262.             if (this.dir == this.dirEnum.LEFT) {
  263.                 this.body.setAngularVelocity(1000);
  264.                 this.body.setVelocity(250, -300);
  265.             }
  266.             else {
  267.                 this.body.setAngularVelocity(-1000);
  268.                 this.body.setVelocity(-250, -300);
  269.             }
  270.         }
  271.     }
  272.  
  273.     class Fairy extends Entity {
  274.         constructor(scene, x, y, key) {
  275.             super(scene, x, y, key);
  276.  
  277.             this.body.setCollideWorldBounds(true);
  278.             this.setDepth(100);
  279.  
  280.             this.body.setAllowGravity(false);
  281.            
  282.  
  283.             this.emitterFairy = this.scene.sparkle.createEmitter({
  284.                 speed: 50,
  285.                 scale: { start: 0.6, end: 0.1 },
  286.                 alpha: { start: 1, end: 0 },
  287.                 blendMode: 'ADD'
  288.             });
  289.             this.emitterFairy.startFollow(this);
  290.  
  291.             this.obj = scene.player;
  292.         }
  293.  
  294.         update() {
  295.             this.scene.physics.moveTo(this, this.obj.x + ((this.scene.player.dir == this.scene.player.dirEnum.LEFT) ? 50 : -50), this.obj.y - 80, null, 500);
  296.         }
  297.  
  298.         restart() {
  299.             this.setPosition(this.obj.x, this.obj.y);
  300.             this.emitterFairy.frequency = 0;
  301.         }
  302.  
  303.         die() {
  304.             this.emitterFairy.explode(50);
  305.         }
  306.     }
  307.  
  308.     class Gate extends Entity {
  309.         constructor(scene, x, y, key) {
  310.             super(scene, x, y, key);
  311.  
  312.             this.body.setAllowGravity(false);
  313.             this.setDepth(8);
  314.         }
  315.  
  316.         restart() {
  317.         }
  318.  
  319.         die() {
  320.         }
  321.     }
  322.  
  323.     class SceneGame extends Phaser.Scene {
  324.         constructor() {
  325.             super({ key: 'SceneGame' });
  326.         }
  327.  
  328.         preload() {
  329.             this.load.image('tiles', 'assets/tileset.png');
  330.             this.load.tilemapTiledJSON('map', 'maps/map_corridors.json');
  331.             this.load.tilemapTiledJSON('winning', 'maps/map_winning.json');
  332.             this.load.tilemapTiledJSON('horizontal', 'maps/map_horizontal.json');
  333.             this.load.tilemapTiledJSON('small', 'maps/map_small.json');
  334.             this.load.tilemapTiledJSON('tutorial', 'maps/map_tutorial.json');
  335.  
  336.             this.load.spritesheet('hero', 'assets/hero.png', { frameWidth: 16, frameHeight: 24 });
  337.  
  338.             this.load.image('sparkle', 'assets/sparkle.png');
  339.             this.load.image('bullet', 'assets/bullet.png');
  340.             this.load.image('fairy', 'assets/fairy.png');
  341.             this.load.image('dust', 'assets/dust.png');
  342.             this.load.image('gate', 'assets/gate.png');
  343.  
  344.             this.load.audio('shoot', 'assets/shoot.wav');
  345.             this.load.audio('shoot_enemy', 'assets/shoot_enemy.wav');
  346.             this.load.audio('jump', 'assets/jump.wav');
  347.             this.load.audio('kill', 'assets/kill.wav');
  348.             this.load.audio('land', 'assets/land.wav');
  349.             this.load.audio('next_level', 'assets/next_level.wav');
  350.  
  351.             this.load.audio('music', 'assets/music.mp3');
  352.         }
  353.  
  354.         create()
  355.         {
  356.             this.mus = this.sound.add('music');
  357.             this.mus.setLoop(true);
  358.             this.mus.setVolume(0.1);
  359.             this.mus.play();
  360.  
  361.             this.anims.create({
  362.                 key: 'stay_right',
  363.                 frames: this.anims.generateFrameNumbers('hero', { start: 0, end: 1 }),
  364.                 frameRate: 5,
  365.                 repeat: -1
  366.             });
  367.             this.anims.create({
  368.                 key: 'stay_left',
  369.                 frames: this.anims.generateFrameNumbers('hero', { start: 2, end: 3 }),
  370.                 frameRate: 5,
  371.                 repeat: -1
  372.             });
  373.  
  374.             this.maps = ['tutorial', 'small', 'horizontal', 'map', 'winning'];
  375.  
  376.             this.gate = new Gate(this, 0, 0, 'gate');
  377.  
  378.             this.enemies = this.physics.add.group({
  379.                 runChildUpdate: true
  380.             });
  381.  
  382.             this.player = new Player(this, 500, 800, 'hero');
  383.            
  384.             this.dust = this.add.particles('dust');
  385.             this.dust.setDepth(20);
  386.             this.dustEmitter = this.dust.createEmitter({
  387.                 speed: 8,
  388.                 scale: { start: 0.5, end: 1 },
  389.                 alpha: { start: 0.4, end: 0 },
  390.                 blendMode: 'COLOR',
  391.                
  392.             });
  393.             this.dustEmitter.setFrequency(-1);
  394.  
  395.             this.sparkle = this.add.particles('sparkle');
  396.             this.sparkle.setDepth(99);
  397.             this.fairy = new Fairy(this, 550, 750, 'fairy');
  398.            
  399.             this.enemyBulletParticleEmitter = this.sparkle.createEmitter({
  400.                 speed: 100,
  401.                 scale: { start: 0.4, end: 0 },
  402.                 alpha: { start: 1, end: 0 },
  403.                 blendMode: 'COLOR',
  404.                 lifespan: 200,
  405.                 on: false
  406.             });
  407.  
  408.            
  409.  
  410.             this.bullets = this.physics.add.group({
  411.                 runChildUpdate: true
  412.             });
  413.  
  414.             this.enemyBullets = this.physics.add.group({
  415.                 runChildUpdate: true
  416.             });
  417.             this.physics.add.collider(this.player, this.enemyBullets, function (player, bullet) {
  418.                 player.die(true, false);
  419.                 bullet.destroy();
  420.             }, null, this);
  421.  
  422.             this.ragdolls = this.physics.add.group();
  423.            
  424.             this.cursors = this.input.keyboard.createCursorKeys();
  425.             this.keySpace = this.input.keyboard.addKey(Phaser.Input.Keyboard.KeyCodes.SPACE);
  426.  
  427.             this.level = 0;
  428.             this.initializeLevel();
  429.             this.restart();
  430.         }
  431.  
  432.         update()
  433.         {
  434.             this.player.update();
  435.  
  436.             if (this.cursors.left.isDown && !this.cursors.right.isDown) {
  437.                 this.player.goLeft();
  438.                
  439.             }
  440.             else if (this.cursors.right.isDown && !this.cursors.left.isDown) {
  441.                 this.player.goRight();
  442.             }
  443.             else {
  444.                 this.player.stay();
  445.             }
  446.  
  447.             if (this.cursors.up.isDown && !this.cursors.down.isDown && this.player.body.blocked.down) {
  448.                 this.player.goUp();
  449.             }
  450.  
  451.             if (this.keySpace.isDown) {
  452.                 this.player.shoot();
  453.             }
  454.  
  455.             this.fairy.update();
  456.         }
  457.  
  458.         restart() {
  459.             this.enemies.children.each(x=>x.destroy());
  460.             this.ragdolls.children.each(x=>x.destroy());
  461.             this.bullets.children.each(x=>x.explode());
  462.             this.enemyBullets.children.each(x=>x.destroy());
  463.  
  464.             this.map.filterObjects('objects', (obj) => {
  465.                 if (obj.name == 'shooter') {
  466.                     this.enemies.add(new Enemy(this, obj.x, obj.y, 'hero'));
  467.                 }
  468.                
  469.                 if (obj.name == 'player') {
  470.                     this.player.setPosition(obj.x, obj.y);
  471.                 }
  472.  
  473.                 if (obj.name == 'gate') {
  474.                     this.gate.setPosition(obj.x, obj.y-32);
  475.                 }
  476.             });
  477.  
  478.             this.player.restart();
  479.             this.fairy.restart();
  480.             this.cameras.main.startFollow(this.player);
  481.         }
  482.  
  483.         destroyLevel() {
  484.             this.collider0.destroy();
  485.             this.collider1.destroy();
  486.             this.collider2.destroy();
  487.             this.collider3.destroy();
  488.             this.collider4.destroy();
  489.  
  490.             this.frgLayer.destroy();
  491.             this.bcgLayer.destroy();
  492.             this.layer.destroy();
  493.         }
  494.  
  495.         initializeLevel() {
  496.             this.map = this.make.tilemap({ key: this.maps[this.level] });
  497.             this.tiles = this.map.addTilesetImage('tileset', 'tiles');
  498.  
  499.             this.bcgLayer = this.map.createStaticLayer('background', this.tiles, 0, 0);
  500.             this.frgLayer = this.map.createStaticLayer('foreground', this.tiles, 0, 0);
  501.             this.frgLayer.setDepth(21);
  502.  
  503.             this.layer = this.map.createStaticLayer('main', this.tiles, 0, 0);
  504.             this.layer.setCollisionByProperty({ collides: true });
  505.             this.physics.world.setBounds(0, 0, this.map.widthInPixels, this.map.heightInPixels);
  506.             this.cameras.main.setBounds(0, 0, this.map.widthInPixels, this.map.heightInPixels);
  507.  
  508.             this.collider0 = this.physics.add.collider(this.player, this.layer);
  509.  
  510.             this.collider3 = this.physics.add.collider(this.bullets, this.layer, function (bullet, layer) {
  511.                 this.cameras.main.shake(40, 0.002);
  512.                 bullet.explode();
  513.             }, null, this);
  514.  
  515.             this.collider1 = this.physics.add.collider(this.enemyBullets, this.layer, function (bullet, layer) {
  516.                 this.enemyBulletParticleEmitter.explode(3, bullet.x, bullet.y);
  517.                 bullet.destroy();
  518.             }, null, this);
  519.  
  520.             this.collider2 = this.physics.add.collider(this.bullets, this.enemies, function (bullet, enemy) {
  521.                 bullet.explode();
  522.                 this.sound.play('kill');
  523.                 let ragdoll = new EnemyRagdoll(enemy, 'hero');
  524.                 this.ragdolls.add(ragdoll); // DEAD
  525.                 ragdoll.initialize();
  526.                 enemy.destroy();
  527.                 this.cameras.main.shake(50, 0.01);
  528.             }, null, this);
  529.  
  530.             this.collider4 = this.physics.add.collider(this.enemies, this.layer);
  531.         }
  532.     }
  533.  
  534.     var config = {
  535.         type: Phaser.AUTO,
  536.         width: 800,
  537.         height: 600,
  538.         backgroundColor: '#000',
  539.         physics: {
  540.             default: 'arcade',
  541.             arcade: {
  542.                 gravity: { y: 1200 }
  543.             }
  544.         },
  545.         scene: [
  546.             SceneGame
  547.         ],
  548.         pixelArt: true,
  549.         roundPixels: true
  550.     };
  551.  
  552.     var game = new Phaser.Game(config);
  553.  
  554.     </script>
  555. </body>
  556. </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement