How to use the phaser.Math function in phaser

To help you get started, we’ve selected a few phaser examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github mikewesthad / navmesh / packages / examples-phaser3 / src / performance / js / scenes / start.js View on Github external
getRandomEmptyPoint() {
    // Find random tile that is empty
    let tileX, tileY, tile;
    do {
      tileX = Phaser.Math.Between(0, this.tilemap.width);
      tileY = Phaser.Math.Between(0, this.tilemap.height);
      tile = this.wallLayer.hasTileAt(tileX, tileY);
    } while (tile && tile.collides);
    // Convert from tile location to pixel location (at center of tile)
    return new Phaser.Math.Vector2(
      tileX * this.tilemap.tileWidth + this.tilemap.tileWidth / 2,
      tileY * this.tilemap.tileHeight + this.tilemap.tileHeight / 2
    );
  }
github mikewesthad / navmesh / packages / examples-phaser3 / src / demo / js / scenes / start.js View on Github external
this.input.on("pointerdown", pointer => {
      const start = new Phaser.Math.Vector2(follower.x, follower.y);
      const end = new Phaser.Math.Vector2(pointer.x, pointer.y);

      // Tell the follower sprite to find its path to the target
      follower.goTo(end);

      // For demo purposes, let's recalculate the path here and draw it on the screen
      const startTime = performance.now();
      const path = navMesh.findPath(start, end);
      // -> path is now an array of points, or null if no valid path found
      const pathTime = performance.now() - startTime;

      navMesh.debugDrawClear();
      navMesh.debugDrawPath(path, 0xffd900);

      const formattedTime = pathTime.toFixed(3);
      uiTextLines[0] = path
        ? `Path found in: ${formattedTime}ms`
github samme / phaser-parcel / src / app / playScene.js View on Github external
nextRound: function () {
      //  A new batch of stars to collect
      stars.children.iterate(function (child) {
        child.enableBody(true, child.x, 0, true, true);
      });

      var x = (player.x < 400) ? Phaser.Math.Between(400, 800) : Phaser.Math.Between(0, 400);

      var bomb = bombs.create(x, 16, 'bomb');
      bomb.setBounce(1);
      bomb.setCollideWorldBounds(true);
      bomb.setVelocity(Phaser.Math.Between(-200, 200), 20);
      bomb.setAngularVelocity(360);
      bomb.allowGravity = false;
    }
github mipearson / dungeondash / src / entities / FOVLayer.ts View on Github external
function updateTileAlpha(
  desiredAlpha: number,
  delta: number,
  tile: Phaser.Tilemaps.Tile
) {
  // Update faster the further away we are from the desired value,
  // but restrict the lower bound so we don't get it slowing
  // down infinitley.
  const distance = Math.max(Math.abs(tile.alpha - desiredAlpha), 0.05);
  const updateFactor = alphaPerMs * delta * distance;
  if (tile.alpha > desiredAlpha) {
    tile.setAlpha(Phaser.Math.MinSub(tile.alpha, updateFactor, desiredAlpha));
  } else if (tile.alpha < desiredAlpha) {
    tile.setAlpha(Phaser.Math.MaxAdd(tile.alpha, updateFactor, desiredAlpha));
  }
}
github mikewesthad / navmesh / packages / examples-phaser3 / src / demo / js / scenes / start.js View on Github external
this.input.on("pointerdown", pointer => {
      const start = new Phaser.Math.Vector2(follower.x, follower.y);
      const end = new Phaser.Math.Vector2(pointer.x, pointer.y);

      // Tell the follower sprite to find its path to the target
      follower.goTo(end);

      // For demo purposes, let's recalculate the path here and draw it on the screen
      const startTime = performance.now();
      const path = navMesh.findPath(start, end);
      // -> path is now an array of points, or null if no valid path found
      const pathTime = performance.now() - startTime;

      navMesh.debugDrawClear();
      navMesh.debugDrawPath(path, 0xffd900);

      const formattedTime = pathTime.toFixed(3);
      uiTextLines[0] = path
github mikewesthad / navmesh / packages / examples-phaser3 / src / performance / js / scenes / start.js View on Github external
getRandomEmptyPoint() {
    // Find random tile that is empty
    let tileX, tileY, tile;
    do {
      tileX = Phaser.Math.Between(0, this.tilemap.width);
      tileY = Phaser.Math.Between(0, this.tilemap.height);
      tile = this.wallLayer.hasTileAt(tileX, tileY);
    } while (tile && tile.collides);
    // Convert from tile location to pixel location (at center of tile)
    return new Phaser.Math.Vector2(
      tileX * this.tilemap.tileWidth + this.tilemap.tileWidth / 2,
      tileY * this.tilemap.tileHeight + this.tilemap.tileHeight / 2
    );
  }
github mipearson / dungeondash / src / entities / FOVLayer.ts View on Github external
(x: number, y: number) => {
        const distance = Math.floor(
          new Phaser.Math.Vector2(x, y).distance(
            new Phaser.Math.Vector2(pos.x, pos.y)
          )
        );

        const rolloffIdx = distance <= radius ? radius - distance : 0;
        const alpha =
          rolloffIdx < lightDropoff.length ? lightDropoff[rolloffIdx] : 0;
        this.map.tiles[y][x].desiredAlpha = alpha;
        this.map.tiles[y][x].seen = true;
      }
    );
github samme / phaser-plugin-follow / src / index.js View on Github external
import Phaser from 'phaser';

const DATA_KEY = '_follow';
const GetFastValue = Phaser.Utils.Objects.GetFastValue;
const RotateAround = Phaser.Math.RotateAround;

export default class FollowPlugin extends Phaser.Plugins.ScenePlugin {

  boot () {
    this.gameObjects = new Phaser.Structs.Set();

    this.systems.events
      .on('postupdate', this.sceneUpdate, this)
      .on('shutdown', this.sceneShutdown, this)
      .once('destroy', this.sceneDestroy, this);
  }

  sceneUpdate () {
    this.gameObjects.iterate(this.updateObject, this);
  }
github Luchanso / bunny-funny-runner / src / game / game / actors / cloud.js View on Github external
addTween() {
    const R = this.game.rnd.between(50, 200);
    const time = this.game.rnd.between(4000, 8000);

    this.data.x = this.x;
    this.data.y = this.y;
    this.data.rotation = 0;

    const tween = this.game.add
      .tween(this.data)
      .to(
        {
          rotation: Phaser.Math.PI2
        },
        time
      )
      .loop(-1)
      .start();

    tween.onUpdateCallback(() => {
      this.x = this.data.x + R * Math.cos(this.data.rotation);
      this.y = this.data.y + R * Math.sin(this.data.rotation);
    }, this);

    return tween;
  }