Class: Pixiurge::Display::TileAnimatedSprite

Inherits:
Pixiurge::Displayable show all
Defined in:
lib/pixiurge/displayable/tile_animated_sprite.rb

Overview

TileAnimatedSprites are good for playing tile-based animations from one or more tile sheeets.

Since:

  • 0.1.0

Instance Attribute Summary

Attributes inherited from Pixiurge::Displayable

#block_height, #block_width, #displayable_type, #location_displayable, #location_name, #name, #position, #x, #y

Instance Method Summary collapse

Methods inherited from Pixiurge::Displayable

#destroy_for_player, #move_for_player, #show_to_player

Constructor Details

#initialize(parameters, name:, engine_connector:) ⇒ TileAnimatedSprite

Constructor - create the sprite

In addition to normal Pixiurge Displayable parameters, a TileAnimatedSprite takes a params hash in a very specific format.

For tilesets, the url property is where to load the tileset from, usually on the game's asset server(s). The tile_width, tile_height, spacing and margin define the size and location of the frames within a tileset. The first_frame_id defaults to 1 for the first tileset and gives the frame ID (number) for the very first frame number of that tileset. For later tilesets, the frame numbering begins with the next unused frame ID - so a tileset with room for 24 images followed by a tileset with room for 6 images will have frame IDs between 1 and 30 unless one of them sets first_frame_id.

Properties for animations: frames, after (opt). Frames is an array. each element may be an integer, in which case it's a frame ID

The default value for "after" is "stop". Special values for "after" are "loop" and "stop". It can also be set to the name of another animation in the same TileAnimatedSprite.

The "tileset" property of an animation gives the default tileset for any frame that doesn't specify. The default value is the first tileset.

The "frames" property of an animation is an array of frame specifications. A frame can be a number, which is a frame number as defined by the tilesets. A frame can also be a structure with a duration and frame_id. Or it can be a structure with a duration, an x and y coordinate for the upper left corner, a width and height, and a tileset name.

Examples:

``` javascript
{
  tilesets: [
    {
      name: "name1",
      url: "https://first_url.com/path",
      tile_width: 32,
      tile_height: 32,
    },
    {
      name: "name2",
      url: "https://second_url.com/path",
      tile_width: 32,
      tile_height: 32,
      first_frame_id: 37,   # Frame ID of the first tile in this tileset
      spacing: 3, # Optional field, default 0
      margin: 2,  # Optional field, default 0
      reg_x: 16,  # Optional field for anchor/registration x coord, default 0
      reg_y: 16,  # Optional field for anchor/registration y coord, default 0
    }
  ],
  animations: {
    funny_walk: { frames: [ 1, 2, 3, 4, 5 ], after: "loop" },
    second_anim: { frames: [ 7, 2, 4, 1, 6 ] },
    third_anim: { frames: [
      { tileset: "name1", x: 0, y: 0, width: 32, height: 32, duration: 100 },
      { frame_id: 7, duration: 150 },
      { tileset: "name2", x: 32, y: 96, width: 32, height: 32, duration: 50 },
      7,
      15
    ], after: "loop" }
  }
  animation: "funny_walk"
}
```

Parameters:

  • parameters (Hash)

    A JSON-serializable hash of options to pass to the front end

  • name (String)

    The Demiurge item name for this Displayable

  • engine_connector (Pixiurge::EngineConnector)

    The Pixiurge EngineConnector this Displayable belongs to

Since:

  • 0.1.0



81
82
83
84
85
86
# File 'lib/pixiurge/displayable/tile_animated_sprite.rb', line 81

def initialize parameters, name:, engine_connector:
  @parameters = parameters
  @displayable_type = "tile_animated_sprite"
  check_parameters
  super(name: name, engine_connector: engine_connector)
end

Instance Method Details

#messages_to_show_player(player) ⇒ Array

Show this Displayable to a player.

Parameters:

Returns:

  • (Array)

    Messages to show the TileAnimatedSprite via JSON

Since:

  • 0.1.0



93
94
95
96
97
# File 'lib/pixiurge/displayable/tile_animated_sprite.rb', line 93

def messages_to_show_player(player)
  messages = super
  messages[0].merge!({ "params" => @parameters })
  messages
end