This repository has been archived by the owner on Jun 9, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 9
/
robot.rb
71 lines (57 loc) · 1.64 KB
/
robot.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
module Strategy::Model
class Robot
include Base
include Target
include Strategy::Constants
attr_accessor :last_turn, :username, :x, :y, :armor, :ammo, :rotation, :direction, :abilities, :power_ups, :board
def initialize(parent, args)
super
@power_ups = @power_ups.map { |p| PowerUp.new self, p }
@board = parent
end
def ammo_full?
ammo == MAX_AMMO
end
def alive?
armor > 0
end
def dead?
! alive?
end
def direction_to(target)
board.direction_to self, target
end
def distance_to(target)
board.distance_to self, target
end
def line_of_sight(skew = 0)
board.line_of_sight(self, rotation + skew).map {|p| Pixel.new p}
end
def line_of_sight_to(target)
board.line_of_sight(self, direction_to(target)).map {|p| Pixel.new p}
end
def can_fire_at?(target)
(rotation - direction_to(target)).abs <= MAX_SKEW
end
def can_see?(target)
los = line_of_sight_to target
hit = los.detect { |p| board.obstruction? p }
val = target.located_at? hit
val
end
def target_for(direction)
direction_x, direction_y = x, y
direction_y += 1 if direction == NORTH
direction_y -= 1 if direction == SOUTH
direction_x += 1 if direction == EAST
direction_x -= 1 if direction == WEST
Pixel.new Struct.new(:x, :y).new(direction_x, direction_y)
end
def can_move?(move)
board.available? target_for(move)
end
def to_s
"Robot[#{username}, x=#{x}, y=#{y}, ammo=#{ammo}, armor=#{armor}, rotation=#{rotation}, direction=#{direction}]"
end
end
end