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.



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

def grid
  @grid
end

#scaleObject

Returns the value of attribute scale.



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

def scale
  @scale
end

#sprite_mapObject

Returns the value of attribute sprite_map.



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

def sprite_map
  @sprite_map
end

Class Method Details

.descriptionObject



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

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

.example_dataObject



50
51
52
53
54
55
56
57
58
59
60
# File 'lib/dedalus/elements/sprite_field.rb', line 50

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: { [0,0] => [ Sprite.example_data ] }
  }
end

Instance Method Details

#background_imageObject



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

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

#image_gridObject



28
29
30
31
32
33
34
35
# File 'lib/dedalus/elements/sprite_field.rb', line 28

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

#layersObject



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/dedalus/elements/sprite_field.rb', line 11

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

  # TODO one sprite *layer*
  sprite_map.each do |location, sprite_list|
    sprite_list.each do |sprite_attrs|
      position = to_screen_coordinates(location: location)
      sprite = Sprite.new(sprite_attrs.merge(position: position))
      layer_stack.push(Dedalus::Layer.new(sprite, freeform: true))
    end
  end

  layer_stack
end

#showObject



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

def show
  layers
end

#to_screen_coordinates(location:) ⇒ Object



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

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