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

#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



66
67
68
# File 'lib/dedalus/elements/sprite_field.rb', line 66

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

.example_dataObject



70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/dedalus/elements/sprite_field.rb', line 70

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



62
63
64
# File 'lib/dedalus/elements/sprite_field.rb', line 62

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

#camera_offsetObject



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

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



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

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

#image_gridObject



42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/dedalus/elements/sprite_field.rb', line 42

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'
  )
end

#layersObject



20
21
22
23
24
25
26
# File 'lib/dedalus/elements/sprite_field.rb', line 20

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



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

def show
  layers
end

#spritesObject



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

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



55
56
57
58
59
60
# File 'lib/dedalus/elements/sprite_field.rb', line 55

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