Class: UT::Engine
- Inherits:
-
Object
- Object
- UT::Engine
- Defined in:
- lib/ut/engine.rb
Overview
The Engine is responsible for fetching the relevant *Tile*s from the ‘source` and checking its visibility with the `mask`. The *Tile*s are also passed to the `shader` before being finally passed to the Viewport. `Enigne.new` takes an `options` hash as its only parameter. The `options` hash respects three members:
* `:source`: The source used to fetch tiles. _Defaults to `DEFAULT_SOURCE`_.
* `:mask`: The mask used to determine a tile's visiblity. _Defaults to
`DEFAULT_MASK`_.
* `:shader`: The shader to preprocess tiles. _Defaults to
`DEFAULT_SOURCE`_.
* `:viewport`: The *Viewport* used for rendering.
* `:world_width`: The width of the world. _Default to `nil`_.
* `:world_height`: The height of the world. _Defaults to `nil`_.
Constant Summary collapse
- DEFAULT_SHADER =
The default shader. Just returns the tile it gets.
lambda {|tile,x,y| tile}
- DEFAULT_SOURCE =
The default tile source. Always return ‘Tile::NULL`
lambda {|x,y| Tile::NULL}
- DEFAULT_MASK =
The default mask. Always returns ‘true`.
lambda {|x,y| true}
Instance Attribute Summary collapse
-
#viewport ⇒ Object
The Viewport used for rendering.
-
#world_height ⇒ Object
The width and height of the world.
-
#world_width ⇒ Object
The width and height of the world.
Instance Method Summary collapse
-
#apply_shader(tile, x, y) ⇒ Object
Applies the shader to the ‘tile` at `[x,y]`.
-
#cache_enabled=(value) ⇒ Object
Enables or disables the cache.
-
#cache_enabled? ⇒ Boolean
Checks if the cache is enabled.
-
#clear_cache ⇒ Object
Clears the cache.
-
#fetch(x, y) ⇒ Object
Fetches the Tile at location ‘[x,y]` from the source.
-
#in_world?(x, y) ⇒ Boolean
Checks if a location is in the world.
-
#initialize(options = {}) ⇒ Engine
constructor
A new instance of Engine.
-
#is_visible?(x, y) ⇒ Boolean
Determines the visibility of the Tile at ‘[x,y]`.
-
#set_mask(&blk) ⇒ Object
Takes a block that accepts ‘x` and `y` and stores it as the mask function.
-
#set_mask=(mask) ⇒ Object
Takes a Proc or lambda which accepts a ‘x` and `y` and stores it as the mask function.
-
#set_shader(&blk) ⇒ Object
Takes a block that accepts a ‘tile`, `x` and `y` and stores it as the shader function.
-
#set_shader=(shader) ⇒ Object
Takes a Proc or lambda which accepts a ‘tile`, `x` and `y` and stores it as the shader function.
-
#set_source(&blk) ⇒ Object
Takes a block that accepts ‘x` and `y` and stores it as the source function.
-
#set_source=(source) ⇒ Object
Takes a Proc or lambda which accepts a ‘x` and `y` and stores it as the source function.
-
#update(world_x, world_y) ⇒ Object
Updates the Engine.
Constructor Details
#initialize(options = {}) ⇒ Engine
Returns a new instance of Engine.
38 39 40 41 42 43 44 45 46 |
# File 'lib/ut/engine.rb', line 38 def initialize = {} @source = [:source] || DEFAULT_SOURCE @mask = [:mask] || DEFAULT_MASK @shader = [:shader] || DEFAULT_SHADER self. = [:viewport] self.world_width = [:world_width] self.world_height = [:world_height] @cache = {} end |
Instance Attribute Details
#viewport ⇒ Object
The Viewport used for rendering
31 32 33 |
# File 'lib/ut/engine.rb', line 31 def @viewport end |
#world_height ⇒ Object
The width and height of the world. *Tile*s ‘outside’ of the world are not passed to the Viewport for rendering. If the ‘world_width` and `world_height` are `nil`, the world is infinte and all tiles are rendered.
36 37 38 |
# File 'lib/ut/engine.rb', line 36 def world_height @world_height end |
#world_width ⇒ Object
The width and height of the world. *Tile*s ‘outside’ of the world are not passed to the Viewport for rendering. If the ‘world_width` and `world_height` are `nil`, the world is infinte and all tiles are rendered.
36 37 38 |
# File 'lib/ut/engine.rb', line 36 def world_width @world_width end |
Instance Method Details
#apply_shader(tile, x, y) ⇒ Object
Applies the shader to the ‘tile` at `[x,y]`
96 97 98 |
# File 'lib/ut/engine.rb', line 96 def apply_shader tile, x, y @shader.call tile, x, y end |
#cache_enabled=(value) ⇒ Object
Enables or disables the cache.
125 126 127 |
# File 'lib/ut/engine.rb', line 125 def cache_enabled= value @cache_enabled = value end |
#cache_enabled? ⇒ Boolean
Checks if the cache is enabled.
120 121 122 |
# File 'lib/ut/engine.rb', line 120 def cache_enabled? @cache_enabled end |
#clear_cache ⇒ Object
Clears the cache.
130 131 132 |
# File 'lib/ut/engine.rb', line 130 def clear_cache @cache = {} end |
#fetch(x, y) ⇒ Object
Fetches the Tile at location ‘[x,y]` from the source
85 86 87 88 89 90 91 92 93 |
# File 'lib/ut/engine.rb', line 85 def fetch x, y return nil unless in_world? x, y return nil unless is_visible? x, y tile = @cache[[x, y]] if cache_enabled? tile ||= @source.call x, y @cache[[x, y]] = tile if cache_enabled? apply_shader tile, x ,y end |
#in_world?(x, y) ⇒ Boolean
Checks if a location is in the world.
135 136 137 138 139 140 141 142 143 144 |
# File 'lib/ut/engine.rb', line 135 def in_world? x, y result = true unless world_width.nil? result &&= (x >= 0 && x < world_width) end unless world_height.nil? result &&= (y >= 0 && y< world_height) end result end |
#is_visible?(x, y) ⇒ Boolean
Determines the visibility of the Tile at ‘[x,y]`
101 102 103 |
# File 'lib/ut/engine.rb', line 101 def is_visible? x, y @mask.call x, y end |
#set_mask(&blk) ⇒ Object
Takes a block that accepts ‘x` and `y` and stores it as the mask function.
74 75 76 |
# File 'lib/ut/engine.rb', line 74 def set_mask &blk @mask = blk || DEFAULT_MASK end |
#set_mask=(mask) ⇒ Object
Takes a Proc or lambda which accepts a ‘x` and `y` and stores it as the mask function.
80 81 82 |
# File 'lib/ut/engine.rb', line 80 def set_mask= mask @mask = mask || DEFAULT_MASK end |
#set_shader(&blk) ⇒ Object
Takes a block that accepts a ‘tile`, `x` and `y` and stores it as the shader function.
50 51 52 |
# File 'lib/ut/engine.rb', line 50 def set_shader &blk @shader = blk || DEFAULT_SHADER end |
#set_shader=(shader) ⇒ Object
Takes a Proc or lambda which accepts a ‘tile`, `x` and `y` and stores it as the shader function.
56 57 58 |
# File 'lib/ut/engine.rb', line 56 def set_shader= shader @shader = shader || DEFAULT_SHADER end |
#set_source(&blk) ⇒ Object
Takes a block that accepts ‘x` and `y` and stores it as the source function.
62 63 64 |
# File 'lib/ut/engine.rb', line 62 def set_source &blk @source = blk || DEFAULT_SOURCE end |
#set_source=(source) ⇒ Object
Takes a Proc or lambda which accepts a ‘x` and `y` and stores it as the source function.
68 69 70 |
# File 'lib/ut/engine.rb', line 68 def set_source= source @source = source || DEFAULT_SOURCE end |
#update(world_x, world_y) ⇒ Object
Updates the Engine. ‘world_x` and `world_y` represents the center of the world that should be rendered in _tile coordinates_.
107 108 109 110 111 112 113 114 115 116 117 |
# File 'lib/ut/engine.rb', line 107 def update world_x, world_y x = world_x - @viewport.center_x y = world_y - @viewport.center_y @viewport.width.times do |xi| @viewport.height.times do |yi| tx, ty = x+xi, y+yi tile = fetch tx, ty @viewport.put_tile xi, yi, tile end end end |