armyoflemons

Ludum Dare 47 submission

Oct 4th, 2020
603
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
HTML 13.21 KB | None | 0 0
  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4.     <meta charset="utf-8">
  5.     <meta lang="en-us">
  6.     <title>LD47</title>
  7.     <script src="js/phaser.min.js"></script>
  8. </head>
  9. <body>
  10.     <script>
  11.  
  12.     class Planet extends Phaser.GameObjects.Sprite {
  13.         constructor(scene, x, y, R, a, r, vang, key) {
  14.           super(scene, x, y, key);
  15.           this.scene = scene;
  16.           this.vang = vang;
  17.           this.ang = a;
  18.           this.r = r;
  19.           this.scene.add.existing(this);
  20.           this.scene.planets.add(this);
  21.           this.green = false;
  22.           this.greenPath = new Phaser.Curves.Ellipse(0, 0, this.r + 8);
  23.           this.safe = true;
  24.  
  25.           this.path = new Phaser.Curves.Path();
  26.           this.ellipse = new Phaser.Curves.Ellipse(x,y,R)
  27.           this.path.add(this.ellipse);
  28.  
  29.           this.path.draw(this.scene.orbits);
  30.  
  31.           let pos = new Phaser.Math.Vector2();
  32.           this.ellipse.getPoint(a, pos);
  33.  
  34.           let scale = this.r/64.0;
  35.           this.body.setCircle(32);
  36.           this.setScale(scale);
  37.           this.setPosition(pos.x, pos.y);
  38.  
  39.           this.setPipeline('Light2D');
  40.         }
  41.  
  42.         update() {
  43.           this.ang += this.vang;
  44.           let pos = new Phaser.Math.Vector2();
  45.           this.ellipse.getPoint(this.ang, pos);
  46.           this.setPosition(pos.x, pos.y);
  47.  
  48.           if (this.green) {
  49.             // ?
  50.           }
  51.         }
  52.  
  53.         activateGreen() {
  54.           this.green = true;
  55.         }
  56.  
  57.         setDangerZone(active) {
  58.           this.dangerous = active;
  59.           if (active) {
  60.             this.setTint(0xff0000);
  61.           } else {
  62.             this.clearTint();
  63.           }
  64.         }
  65.     }
  66.  
  67.     class Rocket {
  68.       constructor(scene, x, y, planet) {
  69.         this.scene = scene;
  70.         this.planet = planet;
  71.         this.dPlanet = planet;
  72.         this.landed = false;
  73.         this.canStart = false;
  74.         this.dead = false;
  75.  
  76.         this.spr = this.scene.physics.add.sprite(320,320,'rocket');
  77.         this.spr.setPipeline('Light2D');
  78.         this.spr.body.setCircle(4);
  79.         this.spr.body.setMaxVelocity(200);
  80.         this.spr.body.setCollideWorldBounds(true);
  81.  
  82.         this.sparks = this.scene.add.particles('spark');
  83.         this.sparks.setDepth(-1);
  84.         this.emitter = this.sparks.createEmitter({
  85.           speed:8,
  86.           scale: {start:1, end: 0.5},
  87.           alpha: {start:1, end:0},
  88.           blendMode: 'COLOR',
  89.           frequency: -1
  90.         });
  91.         this.emitter.startFollow(this.spr);
  92.  
  93.         this.lighter = this.scene.lights.addLight(x,y,64,0xffffff,5);
  94.       }
  95.  
  96.       start() {
  97.         this.planet = this.scene.pls[0];
  98.         this.scene.dPlanet = this.planet;
  99.         this.spr.setVelocity(0);
  100.         this.landed = true;
  101.         this.canStart = false;
  102.         this.dead = false;
  103.         this.spr.setVisible(true);
  104.         this.emitter.on = true;
  105.         this.update();
  106.       }
  107.  
  108.       update() {
  109.         if (this.dead) return;
  110.  
  111.         this.lighter.x = this.spr.x
  112.         this.lighter.y = this.spr.y;
  113.         if (this.landed) {
  114.           this.spr.setPosition(this.planet.x, this.planet.y);
  115.         }
  116.       }
  117.  
  118.       die() {
  119.         this.dead = true;
  120.         this.spr.setVisible(false);
  121.         this.scene.red_explosion.explode(200, this.spr.x, this.spr.y);
  122.         this.emitter.on = false;
  123.         this.scene.sound.play('explode');
  124.         this.scene.death();
  125.       }
  126.  
  127.       land(p) {
  128.         if (this.landed || this.dead) {
  129.           return;
  130.         }
  131.  
  132.         if (p.dangerous) {
  133.           this.die();
  134.           return;
  135.         }
  136.  
  137.         if (this.planet != p) {
  138.           this.planet = p;
  139.           this.landed = true;
  140.           this.spr.setVelocity(0);
  141.           this.canStart = false;
  142.           if (p == this.dPlanet) {
  143.             this.scene.chooseNewDestination();
  144.           }
  145.           this.scene.point();
  146.           this.scene.cameras.main.shake(50, 0.007);
  147.         }
  148.       }
  149.  
  150.       up() {
  151.         if (this.landed && this.canStart) {
  152.          this.landed = false;
  153.         }
  154.         this.scene.physics.velocityFromRotation(this.spr.rotation, 100, this.spr.body.acceleration);
  155.         this.emitter.setFrequency(0);
  156.       }
  157.  
  158.       noup() {
  159.         this.spr.setAcceleration(0);
  160.         this.canStart = true;
  161.         this.emitter.setFrequency(-1);
  162.       }
  163.  
  164.       left() {
  165.         this.spr.setAngularVelocity(-300);
  166.       }
  167.  
  168.       right() {
  169.         this.spr.setAngularVelocity(300);
  170.       }
  171.  
  172.       straight() {
  173.         this.spr.setAngularVelocity(0);
  174.       }
  175.     }
  176.  
  177.     class ArrowAim {
  178.       constructor(scene, n) {
  179.         this.scene = scene;
  180.         this.n = n;
  181.         this.angle = 0;
  182.         this.r = 0;
  183.         this.planet = null;
  184.         this.arrows = [];
  185.         for (let i=0; i<n; i++) {
  186.          this.arrows.push(this.scene.add.sprite(0,0,'arrow'));
  187.        }
  188.      }
  189.  
  190.      update() {
  191.        this.angle -= 0.005;
  192.        let pos = new Phaser.Math.Vector2();
  193.        for (let i=0; i<this.n; i++) {
  194.          let a = 1.0/this.n*i + this.angle;
  195.          this.planet.greenPath.getPoint(a, pos);
  196.          this.arrows[i].setPosition(pos.x+this.planet.x, pos.y+this.planet.y);
  197.          this.arrows[i].setAngle(a*360+90);
  198.        }
  199.      }
  200.  
  201.      aimAtPlanet(p) {
  202.        this.planet = p;
  203.      }
  204.    }
  205.  
  206.    class Game extends Phaser.Scene {
  207.        constructor() {
  208.            super({ key: 'Game' });
  209.        }
  210.  
  211.        preload() {
  212.          this.load.setPath('img/');
  213.          this.load.image('planet', ['planet.png', 'planet_n.png']);
  214.          this.load.image('rocket', ['rocket.png', 'rocket_n.png']);
  215.          this.load.image('arrow', 'arrow.png');
  216.          this.load.image('spark', 'spark.png');
  217.          this.load.image('sun', 'sun.png');
  218.          this.load.image('red_spark', 'red_spark.png');
  219.  
  220.          this.load.image('sphere', ['sphere.png', 'sphere_n.png']);
  221.  
  222.          this.load.image('bcg1', 'bcg1.png');
  223.          this.load.image('bcg2', 'bcg2.png');
  224.          this.load.image('bcg3', 'bcg3.png');
  225.  
  226.          this.load.image('start', 'start.png');
  227.  
  228.          this.load.setPath('audio/')
  229.          this.load.audio('explode', 'explode.wav');
  230.          this.load.audio('point', 'point.wav');
  231.          this.load.audio('music', 'music.wav');
  232.        }
  233.  
  234.        create()
  235.        {
  236.          this.prestart = true;
  237.  
  238.          this.mus = this.sound.add('music');
  239.          this.mus.setLoop(true);
  240.          this.mus.setVolume(0.2);
  241.          this.mus.play();
  242.  
  243.          this.startImg = this.add.image(320,320,'start');
  244.          this.startImg.setDepth(1000);
  245.  
  246.          this.cursors = this.input.keyboard.createCursorKeys();
  247.          this.keySpace = this.input.keyboard.addKey(Phaser.Input.Keyboard.KeyCodes.SPACE);
  248.          this.bcgs = ['bcg1', 'bcg2', 'bcg3'];
  249.          this.bcgID = 0;
  250.  
  251.          this.background = this.add.image(320,320,this.bcgs[this.bcgID]);
  252.          this.background.setDepth(-1000);
  253.  
  254.          this.orbits = this.add.graphics({ lineStyle: { width: 1, color: 0xffffff }, fillStyle: { color: 0xffffff }});
  255.          this.orbits.setAlpha(0.25);
  256.          this.orbits.setDepth(-500);
  257.  
  258.          this.arrowAim = new ArrowAim(this, 5);
  259.          this.sun = this.physics.add.image(320,320,'sun');
  260.          this.sun.body.setCircle(50);
  261.  
  262.          this.dPlanet = null;
  263.          this.planets = this.physics.add.group();
  264.          this.pls = [
  265.            new Planet(this, 320, 320, 100, -0.1, 25, 0.0032, 'planet'),
  266.            new Planet(this, 320, 320, 150, 0.7, 42, -0.0015, 'planet'),
  267.            new Planet(this, 320, 320, 200, 0.5, 30, 0.0020, 'planet'),
  268.            new Planet(this, 320, 320, 215, 0, 27, 0.002, 'planet'),
  269.            new Planet(this, 320, 320, 250, 0.9, 64, -0.002, 'planet'),
  270.            new Planet(this, 320, 320, 300, 0.25, 20, 0.001, 'planet')
  271.          ];
  272.  
  273.          this.rocket = new Rocket(this, 320, 320, null);
  274.          this.physics.add.overlap(this.rocket.spr, this.planets, function(rocket, planet) {
  275.            this.rocket.land(planet);
  276.          }, null, this);
  277.          this.physics.add.overlap(this.rocket.spr, this.sun, function(rocket, sun) {
  278.            if (this.rocket.dead == false) {
  279.              this.rocket.die();
  280.            }
  281.          }, null, this);
  282.  
  283.          this.red_sparks = this.add.particles('red_spark');
  284.          this.red_explosion = this.red_sparks.createEmitter({
  285.            speed: {min: 16, max: 96},
  286.            scale: {start: 1, end: 0},
  287.            alpha: {start: 1, end: 0.5},
  288.            blendMode: 'COLOR',
  289.            frequency: -1
  290.          });
  291.          this.p50 = this.add.particles('spark');
  292.          this.p50e = this.p50.createEmitter({
  293.            speed: {min: 64, max: 512},
  294.            scale: {start: 1.5, end: 0},
  295.            alpha: {start: 1, end: 0},
  296.            lifespan: 2000,
  297.            blendMode: 'NORMAL',
  298.            frequency: -1
  299.          });
  300.          this.p50e.setPosition(320,320);
  301.  
  302.          this.score = 0;
  303.          let style = {
  304.            fontSize: '48px',
  305.            fill: '#000',
  306.          }
  307.          this.scoreText = this.add.text(320, 320, '0', style);
  308.          this.scoreText.setOrigin(0.5);
  309.          let rstyle = {
  310.            fontSize: '32px',
  311.            fill: '#fff'
  312.          }
  313.          this.restartText = this.add.text(320, 600, 'press space to restart', rstyle);
  314.          this.restartText.setOrigin(0.5);
  315.          this.apprText = this.add.text(320, 20, '', rstyle);
  316.          this.apprText.setOrigin(0.5);
  317.  
  318.          let light = this.lights.addLight(320, 320, 360, 0xffffff, 5);
  319.          this.lights.enable();
  320.  
  321.          this.start();
  322.        }
  323.  
  324.        update()
  325.        {
  326.          if (this.prestart) {
  327.            if (this.keySpace.isDown) {
  328.              this.startTheGame();
  329.            }
  330.            return;
  331.          }
  332.  
  333.          for (let p of this.pls) {
  334.            p.update();
  335.          }
  336.  
  337.          if (this.rocket.dead == false) {
  338.  
  339.            if (this.cursors.up.isDown) {
  340.              this.rocket.up();
  341.            } else {
  342.              this.rocket.noup();
  343.            }
  344.  
  345.            let l = this.cursors.left.isDown;
  346.            let r = this.cursors.right.isDown;
  347.            if (l) {
  348.              this.rocket.left();
  349.            }
  350.            if (r) {
  351.              this.rocket.right();
  352.            }
  353.            if ((l && r) || (!l && !r)) {
  354.              this.rocket.straight();
  355.            }
  356.  
  357.            this.rocket.update();
  358.          } else {
  359.            if (this.keySpace.isDown) {
  360.              this.start();
  361.            }
  362.          }
  363.  
  364.          this.arrowAim.update();
  365.        }
  366.  
  367.        chooseNewDestination() {
  368.          for (let p of this.pls) {
  369.            p.setDangerZone(true);
  370.          }
  371.          if (this.dPlanet != null) {
  372.            this.dPlanet.setDangerZone(false);
  373.            //this.dPlanet.setTint(0xaaaaaa);
  374.          }
  375.  
  376.          let newPlanet = this.dPlanet;
  377.          do {
  378.            newPlanet = this.pls[Math.floor(Math.random() * this.pls.length)];
  379.          } while (newPlanet == this.dPlanet);
  380.  
  381.          this.dPlanet = newPlanet;
  382.          this.rocket.dPlanet = newPlanet;
  383.          this.dPlanet.activateGreen();
  384.          this.arrowAim.aimAtPlanet(newPlanet);
  385.  
  386.          this.dPlanet.setDangerZone(false);
  387.        }
  388.  
  389.        point() {
  390.          this.score++;
  391.          this.scoreText.setText(this.score);
  392.          this.sound.play('point');
  393.          this.p50e.explode(500);
  394.  
  395.          if (this.score < 10) {
  396.            return;
  397.          }
  398.          else if (this.score < 20) {
  399.            this.apprText.setText('good pilot');
  400.          } else if (this.score < 50) {
  401.            this.apprText.setText('ace of the skies');
  402.          } else if (this.score < 100) {
  403.            this.apprText.setText('legend');
  404.          } else if (this.score < 200) {
  405.            this.apprText.setText('god-like');
  406.          } else {
  407.            this.apprText.setText("nobody's here");
  408.          }
  409.        }
  410.  
  411.        changeBackground() {
  412.          this.bcgID = (this.bcgID + 1) % this.bcgs.length;
  413.          this.background.setTexture(this.bcgs[this.bcgID]);
  414.        }
  415.  
  416.        start() {
  417.          this.changeBackground();
  418.          this.score = 0;
  419.          this.scoreText.setText(0);
  420.  
  421.          this.rocket.start();
  422.          this.chooseNewDestination();
  423.  
  424.          this.sun.setPosition(320, 320);
  425.  
  426.          this.restartText.setVisible(false);
  427.          this.scoreText.setSize(48);
  428.  
  429.          this.apprText.setText('');
  430.        }
  431.  
  432.        death() {
  433.          this.restartText.setVisible(true);
  434.          this.scoreText.setSize(64);
  435.          this.cameras.main.shake(150, 0.02);
  436.        }
  437.  
  438.        startTheGame() {
  439.          this.prestart = false;
  440.          this.startImg.destroy();
  441.          this.start();
  442.        }
  443.    }
  444.  
  445.    var config = {
  446.        type: Phaser.AUTO,
  447.        width: 640,
  448.        height: 640,
  449.        backgroundColor: '#4A4737',
  450.        physics: {
  451.            default: 'arcade',
  452.            arcade: {
  453.              //debug: true,
  454.              gravity: 0
  455.            }
  456.        },
  457.        scene: [
  458.            Game
  459.        ],
  460.        //pixelArt: true,
  461.        roundPixels: true
  462.    };
  463.  
  464.    var game = new Phaser.Game(config);
  465.  
  466.    </script>
  467. </body>
  468. </html>
  469.  
Advertisement
Add Comment
Please, Sign In to add comment