All Downloads are FREE. Search and download functionalities are using the official Maven repository.

view.entity-module.SpriteBasedEntity.js Maven / Gradle / Ivy

Go to download

Entity Manager module for the CodinGame engine toolkit. Simplify the management of shapes and drawings.

There is a newer version: 4.5.0
Show newest version
import { TextureBasedEntity } from './TextureBasedEntity.js'
import { ErrorLog } from '../core/ErrorLog.js'
import { MissingImageError } from './errors/MissingImageError.js'

/* global PIXI */

export class SpriteBasedEntity extends TextureBasedEntity {
  constructor () {
    super()
    Object.assign(this.defaultState, {
      image: null,
      baseWidth: null,
      baseHeight: null,
      scaleMode: 'LINEAR'
    })
    this.missingTextures = {}
  }

  updateDisplay (state, changed, globalData) {
    super.updateDisplay(state, changed, globalData)
    if (changed.image) {
      try {
        if (state.image !== null) {
          this.graphics.texture = PIXI.Texture.from(state.image)
        } else {
          this.graphics.texture = PIXI.Texture.EMPTY
        }
        this.graphics.texture.baseTexture.scaleMode = PIXI.SCALE_MODES[state.scaleMode]
      } catch (error) {
        if (!this.missingTextures[state.image]) {
          this.missingTextures[state.image] = true
          ErrorLog.push(new MissingImageError(state.image, error))
        }
      }
    }
    if (changed.baseWidth) {
      this.graphics.width = state.baseWidth
    }
    if (changed.baseHeight) {
      this.graphics.height = state.baseHeight
    }
  }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy