Skip to content

Instantly share code, notes, and snippets.

@MrJaba
Created December 10, 2009 15:18
Show Gist options
  • Save MrJaba/253386 to your computer and use it in GitHub Desktop.
Save MrJaba/253386 to your computer and use it in GitHub Desktop.
module BoardsHelper
def render_board( board )
BoardPresenter.new( self, board ).render
end
def render_map( map )
MapPresenter.new( self, map ).render
end
end
class MapPresenter
include PresenterMethodMissing
attr_accessor :map, :renderers
def initialize( template, map, *renderers )
self.template = template
self.map = map
self.renderers = renderers
end
def render( opts={} )
current_x, current_y = 0,0
html = render_map
if opts.delete(:exclude_container)
html
else
content_tag( :div, :style => "height:#{map.pixel_height}px;width:#{map.pixel_width}px", :id => "board", :class => "game_board margin-top-10 left" ) do
html
end
end
end
def left_offset( x )
(x * TileFactory::TILE_WIDTH) + (TileFactory::TILE_WIDTH >> 2)
end
def top_offset( y )
(y * TileFactory::TILE_HEIGHT) + (TileFactory::TILE_HEIGHT >> 2)
end
private
def render_map
html = ""
(0...map.levels.size).each do |level|
(0...map.height).each do | current_y |
(0...map.width).each do | current_x |
tile = map.tile_at( current_x, current_y, level )
html << render_tile( tile, current_x, current_y )
renderers.each do | renderer |
content = renderer.render( current_x, current_y, level )
html << content if content
end
end
end
end
html
end
def render_tile( tile, x, y )
image_tag( tile.image, :id => "tile_#{x}-#{y}", :class => "map_tile" )
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment