Class: Dedalus::Elements::SpriteField
- Inherits:
-
Organism
- Object
- Dedalus::Element
- Organism
- Dedalus::Elements::SpriteField
- Defined in:
- lib/dedalus/elements/sprite_field.rb
Instance Attribute Summary collapse
-
#grid ⇒ Object
Returns the value of attribute grid.
-
#scale ⇒ Object
Returns the value of attribute scale.
-
#sprite_map ⇒ Object
Returns the value of attribute sprite_map.
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
- #background_image ⇒ Object
- #canvas_layer ⇒ Object
- #image_grid ⇒ Object
- #layers ⇒ Object
- #show ⇒ Object
- #sprites ⇒ Object
- #to_screen_coordinates(location:) ⇒ Object
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
#grid ⇒ Object
Returns the value of attribute grid.
4 5 6 |
# File 'lib/dedalus/elements/sprite_field.rb', line 4 def grid @grid end |
#scale ⇒ Object
Returns the value of attribute scale.
4 5 6 |
# File 'lib/dedalus/elements/sprite_field.rb', line 4 def scale @scale end |
#sprite_map ⇒ Object
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
.description ⇒ Object
51 52 53 |
# File 'lib/dedalus/elements/sprite_field.rb', line 51 def self.description 'sprites overlaid on an image grid' end |
.example_data ⇒ Object
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_image ⇒ Object
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_layer ⇒ Object
19 20 21 |
# File 'lib/dedalus/elements/sprite_field.rb', line 19 def canvas_layer Dedalus::Layer.new(sprites, freeform: true) end |
#image_grid ⇒ Object
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 |
#layers ⇒ Object
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 |
#show ⇒ Object
6 7 8 |
# File 'lib/dedalus/elements/sprite_field.rb', line 6 def show layers end |
#sprites ⇒ Object
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 |