Game Development Reference
In-Depth Information
10 def initialize (object_pool, target)
11 @object_pool = object_pool
12 @target = target
13 @last_update = 0
14 end
15
16 def update
17 if Gosu . milliseconds - @last_update > UPDATE_FREQUENCY
18 @nearby = nil
19 end
20 @nearby ||= @object_pool . nearby( @target , 2000 ) . select do | o |
21 o . class == Tank && ! o . health . dead?
22 end
23 end
24
25 def draw
26 x1, x2, y1, y2 = radar_coords
27 $window . draw_quad(
28 x1, y1, BACKGROUND ,
29 x2, y1, BACKGROUND ,
30 x2, y2, BACKGROUND ,
31 x1, y2, BACKGROUND ,
32 200 )
33 draw_tank( @target , Gosu :: Color :: GREEN )
34 @nearby && @nearby . each do | t |
35 draw_tank(t, Gosu :: Color :: RED )
36 end
37 end
38
39
private
40
41 def draw_tank (tank, color)
42 x1, x2, y1, y2 = radar_coords
43 tx = x1 + WIDTH / 2 + (tank . x - @target . x) / 20
44 ty = y1 + HEIGHT / 2 + (tank . y - @target . y) / 20
45 if (x1 . .x2) . include?(tx) && (y1 . .y2) . include?(ty)
46
$window . draw_quad(
tx - 2 , ty - 2 , color,
47
tx + 2 , ty - 2 , color,
48
tx + 2 , ty + 2 , color,
49
tx - 2 , ty + 2 , color,
50
300 )
51
52 end
53 end
54
55 def radar_coords
Search WWH ::




Custom Search