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
-
#camera_location ⇒ Object
Returns the value of attribute camera_location.
-
#grid ⇒ Object
Returns the value of attribute grid.
-
#redraw_tiles ⇒ Object
Returns the value of attribute redraw_tiles.
-
#scale ⇒ Object
Returns the value of attribute scale.
-
#sprite_map ⇒ Object
Returns the value of attribute sprite_map.
-
#tile_class ⇒ Object
Returns the value of attribute tile_class.
-
#tile_height ⇒ Object
Returns the value of attribute tile_height.
-
#tile_width ⇒ Object
Returns the value of attribute tile_width.
-
#tiles_path ⇒ Object
Returns the value of attribute tiles_path.
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
- #background_image ⇒ Object
- #camera_offset ⇒ 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, #record?, #tiny_font, #view, #window
Constructor Details
This class inherits a constructor from Dedalus::Element
Instance Attribute Details
#camera_location ⇒ Object
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 |
#grid ⇒ Object
Returns the value of attribute grid.
4 5 6 |
# File 'lib/dedalus/elements/sprite_field.rb', line 4 def grid @grid end |
#redraw_tiles ⇒ Object
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 |
#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 |
#tile_class ⇒ Object
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_height ⇒ Object
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_width ⇒ Object
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_path ⇒ Object
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
.description ⇒ Object
68 69 70 |
# File 'lib/dedalus/elements/sprite_field.rb', line 68 def self.description 'sprites overlaid on an image grid' end |
.example_data ⇒ Object
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_image ⇒ Object
64 65 66 |
# File 'lib/dedalus/elements/sprite_field.rb', line 64 def background_image @background_image ||= nil end |
#camera_offset ⇒ Object
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_layer ⇒ Object
29 30 31 |
# File 'lib/dedalus/elements/sprite_field.rb', line 29 def canvas_layer Dedalus::Layer.new(sprites, freeform: true) end |
#image_grid ⇒ Object
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 |
#layers ⇒ Object
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 |
#show ⇒ Object
8 9 10 |
# File 'lib/dedalus/elements/sprite_field.rb', line 8 def show layers end |
#sprites ⇒ Object
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 |