class Dedalus::Elements::SpriteField

Attributes

camera_location[RW]
grid[RW]
redraw_tiles[RW]
scale[RW]
sprite_map[RW]
tile_class[RW]
tile_height[RW]
tile_width[RW]
tiles_path[RW]

Public Class Methods

description() click to toggle source
# File lib/dedalus/elements/sprite_field.rb, line 68
def self.description
  'sprites overlaid on an image grid'
end
example_data() click to toggle source
# File lib/dedalus/elements/sprite_field.rb, line 72
def self.example_data
  {
    grid: [[0,2,0,0,0],
           [0,0,0,0,0],
           [1,1,3,1,1],
           [1,1,1,1,1]],
    scale: 2.0,
    camera_location: [1.2,2.4],
    tiles_path: "media/images/tiles.png",
    tile_width: 64,
    tile_height: 64,
    tile_class: "Dedalus::Elements::MapTile",
    sprite_map: {
      [1.2,2.4] => [ Sprite.new(Sprite.example_data) ]
    }
  }
end

Public Instance Methods

background_image() click to toggle source
# File lib/dedalus/elements/sprite_field.rb, line 64
def background_image
  @background_image ||= nil
end
camera_offset() click to toggle source
# File lib/dedalus/elements/sprite_field.rb, line 12
def camera_offset
  if camera_location
    cx,cy = *camera_location
    [-cx * (tile_width*scale), -cy * (tile_height*scale)]
  else
    [0,0]
  end
end
canvas_layer() click to toggle source
# File lib/dedalus/elements/sprite_field.rb, line 29
def canvas_layer
  Dedalus::Layer.new(sprites, freeform: true)
end
image_grid() click to toggle source
# File lib/dedalus/elements/sprite_field.rb, line 43
def image_grid
  ImageGrid.new(
    tiles_path: tiles_path,
    grid: grid,
    tile_width: tile_width,
    tile_height: tile_height,
    tile_class: tile_class,
    scale: scale,
    offset: camera_offset,
    name: 'sprite-field-tiles',
    redraw_tiles: redraw_tiles
  )
end
layers() click to toggle source
# File lib/dedalus/elements/sprite_field.rb, line 21
def layers
  layer_stack = Dedalus::LayerStack.new
  layer_stack.push(Dedalus::Layer.new(background_image)) if background_image
  layer_stack.push(Dedalus::Layer.new(image_grid))
  layer_stack.push(canvas_layer)
  layer_stack
end
show() click to toggle source
# File lib/dedalus/elements/sprite_field.rb, line 8
def show
  layers
end
sprites() click to toggle source
# File lib/dedalus/elements/sprite_field.rb, line 33
def sprites
  sprite_map.flat_map do |location, sprite_list|
    sprite_list.map do |sprite|
      position = to_screen_coordinates(location: location)
      sprite.position = position
      sprite
    end
  end
end
to_screen_coordinates(location:) click to toggle source
# File lib/dedalus/elements/sprite_field.rb, line 57
def to_screen_coordinates(location:)
  w,h = tile_width, tile_height
  x,y = *location
  cx,cy = *camera_offset
  [(x * w * scale) + cx, (y * h * scale) + cy]
end