Game Development Reference
In-Depth Information
19 @closest_tank = nil
20 @cache_updated_at = now
21 end
22 @closest_tank ||= find_closest_tank
23 end
24
25
private
26
27 def find_closest_tank
28 @in_sight . select do | o |
29 o . class == Tank && ! o . health . dead?
30 end . sort do | a, b |
31 x, y = @viewer . x, @viewer . y
32 d1 = Utils . distance_between(x, y, a . x, a . y)
33 d2 = Utils . distance_between(x, y, b . x, b . y)
34 d1 <=> d2
35 end . first
36 end
37 end
It uses ObjectPool to put nearby objects in sight, and gets a short term focus on one
closest tank. Closest tank is cached for 500 milliseconds for two reasons:
1.
Performance. Uncached version would do Array#select and Array#sort
60 times per second, now it will do 2 times.
2.
Focus. When you choose a target, you should keep it a little longer. This should
also avoid “jitters”, when tank would shake between two nearby targets that are
within same distance.
Controlling Tank Gun
After we made AiVision , we can now use it to automatically aim and shoot at closest
tank. It should work like this:
1.
Every instance of the gun has it's own unique combination of speed, accuracy and
aggressiveness.
2.
Gun will automatically target closest tank in sight.
3.
If no other tank is in sight, gun will target in same direction as tank's body.
4.
If other tank is aimed at and within shooting distance, gun will make a decision
once in a while whether it should shoot or not, based on aggressiveness level.
Aggressive tanks will be trigger happy all the time, while less aggressive ones will
make small random pauses between shots.
Search WWH ::




Custom Search