Class: Metro::UI::TileLayer

Inherits:
Model
  • Object
show all
Defined in:
lib/metro/models/ui/tile_map.rb

Constant Summary

Constants included from Metro::Units

Metro::Units::Bounds

Instance Attribute Summary collapse

Attributes inherited from Model

#scene, #window

Instance Method Summary collapse

Methods inherited from Model

#_load, #_save, #after_initialize, #bounds, #completed?, #create, hierarchy, inherited, #initialize, metro_name, #model, model_name, models, #name, #notification, #saveable_to_view, #show, #to_hash, #update

Methods included from HasEvents

included

Methods included from KeyValueCoding

#get, #set

Methods included from PropertyOwner

included, #properties

Constructor Details

This class inherits a constructor from Metro::Model

Instance Attribute Details

#layerObject

Returns the value of attribute layer.



8
9
10
# File 'lib/metro/models/ui/tile_map.rb', line 8

def layer
  @layer
end

#mapObject

Returns the value of attribute map.



7
8
9
# File 'lib/metro/models/ui/tile_map.rb', line 7

def map
  @map
end

#tilesetsObject

Returns the value of attribute tilesets.



9
10
11
# File 'lib/metro/models/ui/tile_map.rb', line 9

def tilesets
  @tilesets
end

#viewportObject

Returns the value of attribute viewport.



31
32
33
# File 'lib/metro/models/ui/tile_map.rb', line 31

def viewport
  @viewport
end

Instance Method Details

#build_tiles_indexObject



45
46
47
48
49
50
51
# File 'lib/metro/models/ui/tile_map.rb', line 45

def build_tiles_index
  data.each_with_index.map do |image_index,position|
    next if image_index == 0
    image = tileset_image(image_index)
    [ position_of_image(image,row(position),column(position)), image ]
  end.compact
end

#cached_imagesObject



71
72
73
# File 'lib/metro/models/ui/tile_map.rb', line 71

def cached_images
  @cached_images ||= {}
end

#column(position) ⇒ Object



37
38
39
# File 'lib/metro/models/ui/tile_map.rb', line 37

def column(position)
  position % layer.width
end

#dataObject



11
12
13
# File 'lib/metro/models/ui/tile_map.rb', line 11

def data
  layer.data
end

#drawObject



57
58
59
# File 'lib/metro/models/ui/tile_map.rb', line 57

def draw
  tiles_within_viewport.each { |bounds,image| image.draw_rot(bounds.left - x,bounds.top - y,z_order,rotation) }
end

#row(position) ⇒ Object



33
34
35
# File 'lib/metro/models/ui/tile_map.rb', line 33

def row(position)
  position / layer.width
end

#tile_boundsObject



41
42
43
# File 'lib/metro/models/ui/tile_map.rb', line 41

def tile_bounds
  @tile_bounds ||= build_tiles_index
end

#tiles_within_viewportObject



53
54
55
# File 'lib/metro/models/ui/tile_map.rb', line 53

def tiles_within_viewport
  tile_bounds.find_all {|bounds,images| viewport.intersect?(bounds) }
end

#tileset_image(image_index) ⇒ Object



61
62
63
64
65
66
67
68
69
# File 'lib/metro/models/ui/tile_map.rb', line 61

def tileset_image(image_index)
  unless cached_images[image_index]
    tileset = map.tilesets.find {|t| image_index >= t.firstgid && image_index < t.firstgid + t.images.count }
    tileset_image_index = image_index - tileset.firstgid
    cached_images[image_index] = tileset.images[tileset_image_index]
  end

  cached_images[image_index]
end

#xObject



15
16
17
# File 'lib/metro/models/ui/tile_map.rb', line 15

def x
  viewport.left
end

#yObject



19
20
21
# File 'lib/metro/models/ui/tile_map.rb', line 19

def y
  viewport.top
end

#z_orderObject



23
24
25
# File 'lib/metro/models/ui/tile_map.rb', line 23

def z_order
  -1
end