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, #padding, #position, #width, #width_percent

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Dedalus::Element

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

Constructor Details

This class inherits a constructor from Dedalus::Element

Instance Attribute Details

#gridObject

Returns the value of attribute grid.



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

def grid
  @grid
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

Class Method Details

.descriptionObject



51
52
53
# File 'lib/dedalus/elements/sprite_field.rb', line 51

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

.example_dataObject



55
56
57
58
59
60
61
62
63
64
65
# File 'lib/dedalus/elements/sprite_field.rb', line 55

def self.example_data
  {
    grid: [[0,0,0,0,0],
           [0,0,0,0,0],
           [1,1,1,1,1],
           [1,1,1,1,1]],
    scale: 0.3,
    player_location: [2,2],
    sprite_map: { [1.2,2.4] => [ Sprite.new(Sprite.example_data) ] }
  }
end

Instance Method Details

#background_imageObject



47
48
49
# File 'lib/dedalus/elements/sprite_field.rb', line 47

def background_image
  Image.new(path: "media/images/cosmos.jpg", z_order: -1, scale: self.scale)
end

#canvas_layerObject



19
20
21
# File 'lib/dedalus/elements/sprite_field.rb', line 19

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

#image_gridObject



33
34
35
36
37
38
39
40
# File 'lib/dedalus/elements/sprite_field.rb', line 33

def image_grid
  ImageGrid.new(
    tiles_path: 'media/images/tiles.png',
    grid: grid,
    tile_width: 64,
    tile_height: 64
  )
end

#layersObject



10
11
12
13
14
15
16
17
# File 'lib/dedalus/elements/sprite_field.rb', line 10

def layers
  layer_stack = Dedalus::LayerStack.new
  layer_stack.push(Dedalus::Layer.new(background_image))
  layer_stack.push(Dedalus::Layer.new(image_grid))

  layer_stack.push(canvas_layer)
  layer_stack
end

#showObject



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

def show
  layers
end

#spritesObject



23
24
25
26
27
28
29
30
31
# File 'lib/dedalus/elements/sprite_field.rb', line 23

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



42
43
44
45
# File 'lib/dedalus/elements/sprite_field.rb', line 42

def to_screen_coordinates(location:)
  x,y = *location
  [(x * image_grid.tile_width), (y * image_grid.tile_height)]
end