Class: Dedalus::Elements::SpriteField

Inherits:
Organism show all
Defined in:
lib/dedalus/elements/sprite_field.rb

Instance Attribute Summary collapse

Attributes inherited from Dedalus::Element

#background_color, #color, #height, #height_percent, #margin, #offset, #padding, #position, #shown, #width, #width_percent, #z_order

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Dedalus::Element

#big_font, #code_font, #draw_bounding_box, #font, #huge_font, #initialize, #record?, #tiny_font, #view, #window

Constructor Details

This class inherits a constructor from Dedalus::Element

Instance Attribute Details

#camera_locationObject

Returns the value of attribute camera_location.



4
5
6
# File 'lib/dedalus/elements/sprite_field.rb', line 4

def camera_location
  @camera_location
end

#gridObject

Returns the value of attribute grid.



4
5
6
# File 'lib/dedalus/elements/sprite_field.rb', line 4

def grid
  @grid
end

#redraw_tilesObject

Returns the value of attribute redraw_tiles.



6
7
8
# File 'lib/dedalus/elements/sprite_field.rb', line 6

def redraw_tiles
  @redraw_tiles
end

#scaleObject

Returns the value of attribute scale.



4
5
6
# File 'lib/dedalus/elements/sprite_field.rb', line 4

def scale
  @scale
end

#sprite_mapObject

Returns the value of attribute sprite_map.



4
5
6
# File 'lib/dedalus/elements/sprite_field.rb', line 4

def sprite_map
  @sprite_map
end

#tile_classObject

Returns the value of attribute tile_class.



5
6
7
# File 'lib/dedalus/elements/sprite_field.rb', line 5

def tile_class
  @tile_class
end

#tile_heightObject

Returns the value of attribute tile_height.



5
6
7
# File 'lib/dedalus/elements/sprite_field.rb', line 5

def tile_height
  @tile_height
end

#tile_widthObject

Returns the value of attribute tile_width.



5
6
7
# File 'lib/dedalus/elements/sprite_field.rb', line 5

def tile_width
  @tile_width
end

#tiles_pathObject

Returns the value of attribute tiles_path.



5
6
7
# File 'lib/dedalus/elements/sprite_field.rb', line 5

def tiles_path
  @tiles_path
end

Class Method Details

.descriptionObject



68
69
70
# File 'lib/dedalus/elements/sprite_field.rb', line 68

def self.description
  'sprites overlaid on an image grid'
end

.example_dataObject



72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
# 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

Instance Method Details

#background_imageObject



64
65
66
# File 'lib/dedalus/elements/sprite_field.rb', line 64

def background_image
  @background_image ||= nil
end

#camera_offsetObject



12
13
14
15
16
17
18
19
# 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_layerObject



29
30
31
# File 'lib/dedalus/elements/sprite_field.rb', line 29

def canvas_layer
  Dedalus::Layer.new(sprites, freeform: true)
end

#image_gridObject



43
44
45
46
47
48
49
50
51
52
53
54
55
# 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

#layersObject



21
22
23
24
25
26
27
# 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

#showObject



8
9
10
# File 'lib/dedalus/elements/sprite_field.rb', line 8

def show
  layers
end

#spritesObject



33
34
35
36
37
38
39
40
41
# 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:) ⇒ Object



57
58
59
60
61
62
# 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