npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

pixi-ldtk-loader

v2.3.3

Published

LDtk loader for Pixi.js.

Downloads

454

Readme

pixi-ldtk-loader

Import LDtk files directly into your Pixi.js project.

Version BSD-3-Clause License Downloads Bundle size

Build status Dependencies Maintainability Code of Conduct

Watch on GitHub Star on GitHub

LDtk is an excellent tool for building expansive 2D worlds, which also makes it an amazing candidate for building your Pixi.js levels. This project makes it dead simple to import your raw .ldtk files directly into Pixi.js.

When loading your .ldtk files, the loader will automatically create spritesheets and textures for your LDtk tilesets. It'll also generate an easy-to-parse object representing the entire file, making it easy to access all of your levels, layers, enums, entities, and custom properties.

Quick Start

Requirements

  • pixi.js @ 8.0.0+
  • LDtk 1.0.0+

Installation

npm install pixi-ldtk-loader

# OR

yarn add pixi-ldtk-loader

Usage

The loader can be used directly:

import { LDTKLoader } from 'pixi-ldtk-loader'

const loader = new LDTKLoader()
loader.add('path/to/project.ldtk')
loader.load(() => {})

Or it can be used as an Application level extension:

import {
  Application,
  extensions,
} from 'pixi.js'
import { LDTKLoader } from 'pixi-ldtk-loader'

extensions.add(LDTKLoader)

const app = new Application()
app.loader.add('path/to/project.ldtk')
app.loader.load(() => {})

Rendering the map

The loader will create spritesheets and textures for your project, but due to the wide range of differences between rendering pipelines and personal preferences the loader will not render your levels for you. Rather, you'll need to write your own parser to render the project.

Assuming we've used the loader to load a project titled game-project.ldtk, here's an example of rendering all levels and layers at once. Keep in mind that for larger worlds you may want to render levels selectively based on the camera's current position.

import { Assets } from 'pixi.js'

const app = new PIXI.Application()

// We'll use this cache to use a single container for each layer, regardless of
// which level the layer's tiles belong to. This makes it simple to, for
// example, render your player on your Entities layer, moving its sprite
// smoothly across levels screen without having to reparent it to a new
// container.
const layerCache = {}

// Since we're only rendering the levels, we'll destructure only the `levels`
// array from the map's data.
const { levels } = Assets.get('game-project.ldtk')

levels.forEach(level => {
  // We'll destructure the `layers` array from the level since it's the only key
  // we need for rendering.
  const { layers } = level

  layers.forEach(layer => {
    // Skip all parsing if the layer has no tiles.
    if (layer.tiles?.length) {
      // Retrieve the layer's Pixi container from the cache if it exists...
      let layerContainer = layerCache[layer.name]

      // ...or create a new container and store it in the cache..
      if (!layerContainer) {
        layerContainer = new Container
        layerContainer.name = layer.name
        layerCache[layer.name] = layerContainer
      }

      layer.tiles.forEach(tile => {
        // We create a tile object for every tile, regardless of whether they
        // have a texture applied. If they have no texture, tho, then we have
        // nothing to render.
        if (!tile?.texture) return

        // Create a new sprite for every tile. If your tiles don't need to be
        // adjusted individually, it'd probably be better to use
        // `@pixi/tilemap`.
        const sprite = new Sprite(tile.texture)

        // Position the Sprite in screen space using its position in the level,
        // adjusted by the level's world offset.
        sprite.x = (tile.position.x * tile.width) + level.worldPosition.x
        sprite.y = (tile.position.y * tile.height) + level.worldPosition.y

        // Add the tile's Sprite to the layer's Container.
        layerContainer.addChild(sprite)
      })
    }

    // Add the layer's container to the application stage.
    app.stage.addChild(layerContainer)
  })

  return targetContainer
})