Class: AiVision
- Inherits:
-
Object
- Object
- AiVision
- Defined in:
- lib/entities/components/ai/vision.rb
Constant Summary collapse
- CACHE_TIMEOUT =
500
- POWERUP_CACHE_TIMEOUT =
50
Instance Attribute Summary collapse
-
#in_sight ⇒ Object
readonly
Returns the value of attribute in_sight.
Instance Method Summary collapse
- #can_go_forward? ⇒ Boolean
- #closest_free_path(away_from = nil) ⇒ Object (also: #closest_free_path_away_from)
- #closest_powerup(*suitable) ⇒ Object
- #closest_tank ⇒ Object
-
#initialize(viewer, object_pool, distance) ⇒ AiVision
constructor
A new instance of AiVision.
- #update ⇒ Object
Constructor Details
#initialize(viewer, object_pool, distance) ⇒ AiVision
Returns a new instance of AiVision.
6 7 8 9 10 |
# File 'lib/entities/components/ai/vision.rb', line 6 def initialize(viewer, object_pool, distance) @viewer = viewer @object_pool = object_pool @distance = distance end |
Instance Attribute Details
#in_sight ⇒ Object (readonly)
Returns the value of attribute in_sight.
4 5 6 |
# File 'lib/entities/components/ai/vision.rb', line 4 def in_sight @in_sight end |
Instance Method Details
#can_go_forward? ⇒ Boolean
12 13 14 15 16 17 18 |
# File 'lib/entities/components/ai/vision.rb', line 12 def can_go_forward? in_front = Utils.point_at_distance( *@viewer.location, @viewer.direction, 40) @object_pool.map.can_move_to?(*in_front) && @object_pool.nearby_point(*in_front, 40, @viewer) .reject { |o| o.is_a? Powerup }.empty? end |
#closest_free_path(away_from = nil) ⇒ Object Also known as: closest_free_path_away_from
24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 |
# File 'lib/entities/components/ai/vision.rb', line 24 def closest_free_path(away_from = nil) paths = [] 5.times do |i| if paths.any? return farthest_from(paths, away_from) end radius = 55 - i * 5 range_x = range_y = [-radius, 0, radius] range_x.shuffle.each do |x| range_y.shuffle.each do |y| x = @viewer.x + x y = @viewer.y + y if @object_pool.map.can_move_to?(x, y) && @object_pool.nearby_point(x, y, radius, @viewer) .reject { |o| o.is_a? Powerup }.empty? if away_from paths << [x, y] else return [x, y] end end end end end false end |
#closest_powerup(*suitable) ⇒ Object
63 64 65 66 67 68 69 70 71 |
# File 'lib/entities/components/ai/vision.rb', line 63 def closest_powerup(*suitable) now = Gosu.milliseconds @closest_powerup = nil if now - (@powerup_cache_updated_at ||= 0) > POWERUP_CACHE_TIMEOUT @closest_powerup = nil @powerup_cache_updated_at = now end @closest_powerup ||= find_closest_powerup(*suitable) end |
#closest_tank ⇒ Object
53 54 55 56 57 58 59 60 61 |
# File 'lib/entities/components/ai/vision.rb', line 53 def closest_tank now = Gosu.milliseconds @closest_tank = nil if now - (@cache_updated_at ||= 0) > CACHE_TIMEOUT @closest_tank = nil @cache_updated_at = now end @closest_tank ||= find_closest_tank end |
#update ⇒ Object
20 21 22 |
# File 'lib/entities/components/ai/vision.rb', line 20 def update @in_sight = @object_pool.nearby(@viewer, @distance) end |