Class: Metro::UI::TileMap

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

Overview

Draws a TileMap within the scene.

The viewport, or camera, of the TileMap is static but can be set to follow a particular actor within the scene. The viewport will continue to move according to the position of the specified actor.

Examples:

Creating a tile map with a Tiled TMX file


class MainScene < GameScene
  draw :tile_map, model: "metro::ui::tile_map",
    file: "first_level.tmx"
end

Creating a tile map that will follow the hero


class MainScene < GameScene
  draw :hero
  draw :tile_map, model: "metro::ui::tile_map",
    file: "first_level.tmx", follow: :hero
end

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, #create, #draw_completed?, hierarchy, inherited, #initialize, metro_name, #model, model_name, models, #name, #notification, #saveable_to_view, #show, #to_hash, #update_completed?

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

#fileObject

The Tiled File (*.tmx) that will be parsed and loaded for the tile map



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

property :file, type: :text

#followObject

The actor that the viewport of the scene will follow.



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

property :follow, type: :text

#rotationObject

The rotation of each tile within the scene. This is by default 0 and should likely remain 0.



40
# File 'lib/metro/models/ui/tile_map.rb', line 40

property :rotation

#viewportObject

Set the viewport of the map, by default ths viewport will be the dimensions of the current Game, so this may not be necessary to set.



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

def viewport
  @viewport ||= Game.bounds
end

Instance Method Details

#drawObject



87
88
89
# File 'lib/metro/models/ui/tile_map.rb', line 87

def draw
  layers.each {|layer| layer.draw }
end

#mapTMX::Map

Returns the map object found in the specified TMX file.

Returns:

  • (TMX::Map)

    the map object found in the specified TMX file.



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

def map
  @map ||= begin
    map = ::Tmx.load asset_path(file)
    map.tilesets.each {|tileset| tileset.window = window }
    map
  end
end

#objects(params) ⇒ Object

Find objects that match the specified type or by the specified parameters.

Examples:

Finding all the objects with the type :floor


objects(:floor)

Finding all the objects with the name ‘tree’


objects(name: 'tree')


79
80
81
# File 'lib/metro/models/ui/tile_map.rb', line 79

def objects(params)
  map.objects.find(params)
end

#updateObject



83
84
85
# File 'lib/metro/models/ui/tile_map.rb', line 83

def update
  shift_viewport_to_center_who_we_are_following
end