Class: Demiurge::Tmx::TmxCache
- Inherits:
-
Object
- Object
- Demiurge::Tmx::TmxCache
- Defined in:
- lib/demiurge/tmx.rb
Overview
A TmxCache loads and remembers TMX file maps from the tmx gem. For a variety of reasons, it's not great to reload TMX files every time we need to know about them, but it can also be a problem to store a copy of the parsed version every time and place we use it. Caching is, of course, a time-honored solution to this problem.
Instance Attribute Summary collapse
-
#root_dir ⇒ String
Root directory the cache was created relative to.
Instance Method Summary collapse
-
#clear_cache ⇒ void
Clear the TMX cache.
-
#initialize(options = {}) ⇒ void
constructor
Create the TmxCache.
-
#tmx_entry(layout_type, layout_filename) ⇒ Hash
private
Return a TMX entry for the given layout type and filename.
Constructor Details
#initialize(options = {}) ⇒ void
Create the TmxCache
272 273 274 |
# File 'lib/demiurge/tmx.rb', line 272 def initialize( = {}) @root_dir = [:root_dir] || Dir.pwd end |
Instance Attribute Details
#root_dir ⇒ String
Returns Root directory the cache was created relative to
264 265 266 |
# File 'lib/demiurge/tmx.rb', line 264 def root_dir @root_dir end |
Instance Method Details
#clear_cache ⇒ void
This method returns an undefined value.
Clear the TMX cache. Remove all existing entries. This can potentially break existing TMX-based objects. Objects often refer repeatedly back into the cache to avoid duplicating structures, and to pick up new changes to those structures.
283 284 285 286 |
# File 'lib/demiurge/tmx.rb', line 283 def clear_cache @tile_cache = {} nil end |
#tmx_entry(layout_type, layout_filename) ⇒ Hash
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Return a TMX entry for the given layout type and filename. A "layout type" is something like normal TMX (called "tmx") or Manasource-style (called "manasource".) But additional types are likely to be added in future versions of Pixiurge.
311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 |
# File 'lib/demiurge/tmx.rb', line 311 def tmx_entry(layout_type, layout_filename) @tile_cache ||= {} @tile_cache[layout_type] ||= {} if @tile_cache[layout_type][layout_filename] return @tile_cache[layout_type][layout_filename] end if layout_type == "manasource" @tile_cache[layout_type][layout_filename] = sprites_from_manasource_tmx(layout_filename) elsif layout_type == "tmx" @tile_cache[layout_type][layout_filename] = sprites_from_tmx(layout_filename) else raise "A TMX location must have a known type of layout (tmx or manasource), not #{layout_type.inspect}!" end @tile_cache[layout_type][layout_filename] end |