Game Development Reference
In-Depth Information
# ...
end
AiVision#can_go_forward? tells if tank can move ahead, and
AiVision#closest_free_path finds a point where tank can move without
obstacles. You can also call AiVision#closest_free_path_away_from and
provide coordinates you are trying to get away from.
We will use closest_free_path methods in newly implemented tank motion states,
and can_go_forward? in TankMotionFSM , to make a decision when to jump into
navigating or stuck state.
Those new states are nothing fancy:
13-advanced-ai/entities/components/ai/tank_navigating_state.rb
1 class TankNavigatingState < TankMotionState
2 def initialize (object, vision)
3 @object = object
4 @vision = vision
5 end
6
7 def update
8 change_direction if should_change_direction?
9 drive
10 end
11
12 def change_direction
13 closest_free_path = @vision . closest_free_path
14 if closest_free_path
15
@object . physics . change_direction(
Utils . angle_between(
16
@object . x, @object . y, * closest_free_path))
17
18 end
19 @changed_direction_at = Gosu . milliseconds
20 @will_keep_direction_for = turn_time
21 end
22
23 def wait_time
24 rand ( 10. . 100 )
25 end
26
27 def drive_time
28 rand ( 1000. . 2000 )
29 end
Search WWH ::




Custom Search