Advertisement
Guest User

Untitled

a guest
Apr 8th, 2020
201
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. var game;
  2. var gameOptions = {
  3.     bigCircleRadius: 250,
  4.     playerRadius: 25,
  5.     playerSpeed: 6000
  6. }
  7. window.onload = function() {
  8.     var gameConfig = {
  9.         thpe: Phaser.CANVAS,
  10.         width: 800,
  11.         height: 800,
  12.         scene: [playGame]
  13.     }
  14.     game = new Phaser.Game(gameConfig);
  15.     window.focus()
  16.     resize();
  17.     window.addEventListener("resize", resize, false);
  18. }
  19. class playGame extends Phaser.Scene{
  20.     constructor(){
  21.         super("PlayGame");
  22.     }
  23.     preload(){
  24.         this.load.image("bigcircle", "bigcircle.png");
  25.         this.load.image("player", "player.png");
  26.     }
  27.     create(){
  28.         this.bigCircle = this.add.sprite(game.config.width / 2, game.config.height / 2, "bigcircle");
  29.         this.bigCircle.displayWidth = gameOptions.bigCircleRadius * 2;
  30.         this.bigCircle.displayHeight = gameOptions.bigCircleRadius * 2;
  31.         this.player = this.add.sprite(game.config.width / 2, game.config.height / 2 - gameOptions.bigCircleRadius - gameOptions.playerRadius, "player");
  32.         this.player.displayWidth = gameOptions.playerRadius * 2;
  33.         this.player.displayHeight = gameOptions.playerRadius * 2;
  34.         this.player.currentAngle = -90;
  35.     }
  36.     update(t, dt){
  37.         var deltaAngle = 360 * (dt / gameOptions.playerSpeed);
  38.         this.player.currentAngle = Phaser.Math.Angle.WrapDegrees(this.player.currentAngle + deltaAngle);
  39.         var radians = Phaser.Math.DegToRad(this.player.currentAngle);
  40.         var distanceFromCenter = gameOptions.bigCircleRadius + gameOptions.playerRadius;
  41.         this.player.x = this.bigCircle.x + distanceFromCenter * Math.cos(radians);
  42.         this.player.y = this.bigCircle.y + distanceFromCenter * Math.sin(radians);
  43.         var revolutions = gameOptions.bigCircleRadius / gameOptions.playerRadius + 1;
  44.         this.player.angle = this.player.currentAngle * revolutions;
  45.     }
  46. }
  47. // pure javascript to scale the game
  48. function resize() {
  49.     var canvas = document.querySelector("canvas");
  50.     var windowWidth = window.innerWidth;
  51.     var windowHeight = window.innerHeight;
  52.     var windowRatio = windowWidth / windowHeight;
  53.     var gameRatio = game.config.width / game.config.height;
  54.     if(windowRatio < gameRatio){
  55.         canvas.style.width = windowWidth + "px";
  56.         canvas.style.height = (windowWidth / gameRatio) + "px";
  57.     }
  58.     else{
  59.         canvas.style.width = (windowHeight * gameRatio) + "px";
  60.         canvas.style.height = windowHeight + "px";
  61.     }
  62. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement