Class: Zyps::TrailsView
- Inherits:
-
Object
- Object
- Zyps::TrailsView
- Defined in:
- lib/zyps/views/trails.rb
Overview
A view of game objects.
Instance Attribute Summary collapse
-
#canvas ⇒ Object
A GUI toolkit-specific drawing area that will be used to render the view.
-
#erase_flag ⇒ Object
Whether view should be erased before re-drawing.
-
#height ⇒ Object
Dimensions of the view.
-
#trail_length ⇒ Object
Number of line segments to draw for each object.
-
#width ⇒ Object
Dimensions of the view.
Instance Method Summary collapse
-
#initialize(options = {}) ⇒ TrailsView
constructor
Takes a hash with these keys and defaults: :canvas => nil :width => 600 :height => 400 :trail_length => 5.
-
#update(environment) ⇒ Object
Takes an Environment, and draws it to the canvas.
Constructor Details
#initialize(options = {}) ⇒ TrailsView
Takes a hash with these keys and defaults: :canvas => nil :width => 600 :height => 400 :trail_length => 5
43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 |
# File 'lib/zyps/views/trails.rb', line 43 def initialize ( = {}) = { :width => 600, :height => 400, :trail_length => 5, }.merge() @width = [:width] @height = [:height] @trail_length = [:trail_length] @canvas = [:canvas] #Set canvas's size to match view's. resize if @canvas #Track a list of locations for each object. @locations = Hash.new {|h, k| h[k] = Array.new} end |
Instance Attribute Details
#canvas ⇒ Object
A GUI toolkit-specific drawing area that will be used to render the view. See WxCanvas and GTK2Canvas.
30 31 32 |
# File 'lib/zyps/views/trails.rb', line 30 def canvas @canvas end |
#erase_flag ⇒ Object
Whether view should be erased before re-drawing.
36 37 38 |
# File 'lib/zyps/views/trails.rb', line 36 def erase_flag @erase_flag end |
#height ⇒ Object
Dimensions of the view.
32 33 34 |
# File 'lib/zyps/views/trails.rb', line 32 def height @height end |
#trail_length ⇒ Object
Number of line segments to draw for each object.
34 35 36 |
# File 'lib/zyps/views/trails.rb', line 34 def trail_length @trail_length end |
#width ⇒ Object
Dimensions of the view.
32 33 34 |
# File 'lib/zyps/views/trails.rb', line 32 def width @width end |
Instance Method Details
#update(environment) ⇒ Object
Takes an Environment, and draws it to the canvas. Tracks the position of each GameObject over time so it can draw a trail behind it. The head will match the object’s Color exactly, fading to black at the tail. GameObject.size will be used as the line thickness at the object’s head, diminishing to 1 at the tail.
81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 |
# File 'lib/zyps/views/trails.rb', line 81 def update(environment) #Clear view. @canvas.draw_rectangle( :color => Color.new(0, 0, 0), :filled => true, :x => 0, :y => 0, :width => @width, :height => @height ) #For each GameObject in the environment: environment.objects.each do |object| object_radius = Math.sqrt(object.size / Math::PI) #Add the object's current location to the list. @locations[object.identifier] << [object.location.x, object.location.y] #If the list is larger than the number of tail segments, delete the first position. @locations[object.identifier].shift while @locations[object.identifier].length > @trail_length #For each location in this object's list: @locations[object.identifier].each_with_index do |location, index| #Skip first location. next if index == 0 #Divide the current segment number by trail segment count to get the multiplier to use for brightness and width. multiplier = index.to_f / @locations[object.identifier].length.to_f #Get previous location so we can draw a line from it. previous_location = @locations[object.identifier][index - 1] @canvas.draw_line( :color => Color.new( object.color.red * multiplier, object.color.green * multiplier, object.color.blue * multiplier ), :width => object_radius * 2 * multiplier, :x1 => previous_location[0], :y1 => previous_location[1], :x2 => location[0], :y2 => location[1] ) end end @canvas.render end |