Class: Zyps::TrailsView

Inherits:
Object
  • Object
show all
Defined in:
lib/zyps/views/trails.rb

Overview

A view of game objects.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ TrailsView

Takes a hash with these keys and defaults: :canvas => nil :width => 600 :height => 400 :trail_length => 5 :erase_flag => true



44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/zyps/views/trails.rb', line 44

def initialize (options = {})

	options = {
		:width => 600,
		:height => 400,
		:trail_length => 5,
		:erase_flag => true
	}.merge(options)
	@width = options[:width]
	@height = options[:height]
	@trail_length = options[:trail_length]
	@erase_flag = options[:erase_flag]
	@canvas = options[: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

#canvasObject

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_flagObject

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

#heightObject

Dimensions of the view.



32
33
34
# File 'lib/zyps/views/trails.rb', line 32

def height
  @height
end

#trail_lengthObject

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

#widthObject

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.



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
131
132
133
134
135
# File 'lib/zyps/views/trails.rb', line 84

def update(environment)

	#Clear the background on the buffer.
	if @erase_flag
		@canvas.draw_rectangle(
			:color => Color.new(0, 0, 0),
			:filled => true,
			:x => 0, :y => 0,
			:width => @width, :height => @height
		)
	end
	
	#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