Giter Club home page Giter Club logo

pixi-tilemap-tutorial's Introduction

About

Pixi-tilemap allows a developer to create tilemaps based on PIXI.js and https://github.com/pixijs/pixi-tilemap

Example Usage

Completed demo: https://blurymind.github.io/pixi-tilemap-tutorial/

Creating a basic tile map

<html>
  <head>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/pixi.js/4.6.2/pixi.min.js"></script>
    <script src="./lib/pixi-tilemap.js"></script>
    <script src="//rawgit.com/mrdoob/stats.js/master/build/stats.min.js"></script>
  </head>
  <body>
    <script>
      // We create a class where to store the sliced tiles from the tileset image resource
      class TileSet {
        constructor({
          tilewidth,
          tileheight,
          texture,
          offset,
          count,
          scaleMode
        }) {
          this.tilewidth = tilewidth;
          this.tileheight = tileheight;
          this.offset = offset || 0;
          this.texture = texture;
          this.textureCache = [];
          this.scaleMode = scaleMode || PIXI.SCALE_MODES.NEAREST;
          this.prepareTextures(count);
        }
        get width() {
          return this.texture.width;
        }
        get height() {
          return this.texture.height;
        }
        prepareTextures(count) {
          var size =
            count ||
            (this.width / this.tilewidth) * (this.height / this.tileheight);

          this.textureCache = new Array(size)
            .fill(0)
            .map((_, frame) => this.prepareTexture(frame));
        }
        prepareTexture(frame) {
          var cols = Math.floor(this.width / this.tilewidth);
          var x = ((frame - this.offset) % cols) * this.tilewidth;
          var y = Math.floor((frame - this.offset) / cols) * this.tileheight;
          var rect = new PIXI.Rectangle(x, y, this.tilewidth, this.tileheight);
          var texture = new PIXI.Texture(this.texture, rect);

          texture.baseTexture.scaleMode = this.scaleMode;
          texture.cacheAsBitmap = true;

          return texture;
        }
        getFrame(frame) {
          if (!this.textureCache[frame]) {
            this.prepareTexture(frame);
          }

          return this.textureCache[frame];
        }
      }
      var TIME = 0;
      var resolutionX = 1024;
      var resolutionY = 720;

      var stats = new Stats();
      stats.showPanel(0);
      document.body.appendChild(stats.dom);

      var app = new PIXI.Application(resolutionX, resolutionY);
      document.body.appendChild(app.view);

      // We need to load the tileset image resource and the exported json file from tiled that stores the tilemap and tileset data
      PIXI.loader.add(["imgs/Viking3.png", "imgs/island.json"]).load(setup);

      function setup(loader, resources) {
        PIXI.tilemap.Constant.boundSize = 2048;
        PIXI.tilemap.Constant.bufferSize = 4096;
        console.log(loader, resources);
        var island = resources["imgs/island.json"].data;

        // Here we take the first tileset in the tiled file. If you have multiple, you might need to do something different
        var tileset = island.tilesets[0];
        var { tileheight, tilewidth, tilecount } = tileset;

        var TILESET = new TileSet({
          tilewidth,
          tileheight,
          texture: PIXI.utils.TextureCache["imgs/Viking3.png"],
          offset: 1,
          count: tilecount,
          tileset,
          scaleMode: PIXI.SCALE_MODES.NEAREST
        });

        var TILEMAP = new PIXI.tilemap.CompositeRectTileLayer(0);
        app.stage.addChild(TILEMAP);

        island.layers.forEach(layer => {
          if (!layer.visible) return;
          if (layer.type === "objectgroup") {
            layer.objects.forEach(object => {
              var { gid, id, width, height, x, y, visible } = object;
              if (visible === false) return;
              if (TILESET.getFrame(gid)) {
                TILEMAP.addFrame(TILESET.getFrame(gid), x, y - tileheight);
              }
            });
          } else if (layer.type === "tilelayer") {
            var ind = 0;
            for (var i = 0; i < layer.height; i++) {
              for (var j = 0; j < layer.width; j++) {
                var xPos = tilewidth * j;
                var yPos = tileheight * i;

                var tileUid = layer.data[ind];

                if (tileUid !== 0) {
                  var tileData = tileset.tiles.find(
                    tile => tile.id === tileUid - 1
                  );

                  // Animated tiles have a limitation with only being able to use frames arranged one to each other on the image resource
                  if (tileData && tileData.animation) {
                    TILEMAP.addFrame(
                      TILESET.getFrame(tileUid),
                      xPos,
                      yPos
                    ).tileAnimX(tilewidth, tileData.animation.length);
                  } else {
                    // Non animated props dont require tileAnimX or Y
                    TILEMAP.addFrame(TILESET.getFrame(tileUid), xPos, yPos);
                  }
                }

                ind += 1;
              }
            }
            app.start();
          }
        });
        gameLoop();
      }

      // For tracking stats
      function gameLoop() {
        stats.begin();
        stats.end();
        requestAnimationFrame(gameLoop);
      }

      // We increment time by one whenever we want to progress all tile animations
      setInterval(() => {
        TIME += 1;
        // tileAnim[0] will move time on the X axis, while tileAnim[1] on the Y
        app.renderer.plugins.tilemap.tileAnim[0] = TIME;
        app.renderer.render(app.stage);
      }, 100);
    </script>
  </body>
</html>

pixi-tilemap-tutorial's People

Contributors

alan01252 avatar blurymind avatar curleighbraces avatar ivanpopelyshev avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.