-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathqueen.rb
63 lines (49 loc) · 1.49 KB
/
queen.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
# frozen_string_literal: true
class Queen
attr_accessor :x, :y, :image, :last_move_time
POINT_VALUE = 100
SPEED = 1.25 # move after this many seconds
def initialize(x, y)
@x = x
@y = y
@last_move_time = Time.now
@image = Gosu::Image.new("images/queen.png", tileable: true)
RogueRooks.occupy_square(x, y)
end
def move_closer
# TODO this should actually target an available rook, not just the center
diff_x = 7.5 - @x
diff_y = 7.5 - @y
# I think this math might be backwards, but it's currently working
angle = Math.atan2(diff_x, diff_y) / (Math::PI / 180.0)
move = if angle >= -22.5 && angle < 22.5
{ x: 0, y: 1 } # s
elsif angle >= 22.5 && angle < 67.5
{ x: 1, y: 1 } # se
elsif angle >= 67.5 && angle < 112.5
{ x: 1, y: 0 } # e
elsif angle >= 112.5 && angle < 157.5
{ x: 1, y: -1 } # ne
elsif angle >= 157.5 || angle < -157.5
{ x: 0, y: -1 } # n
elsif angle >= -157.5 && angle < -112.5
{ x: -1, y: -1 } # nw
elsif angle >= -112.5 && angle < -67.5
{ x: -1, y: 0 } # w
elsif angle >= -67.5 && angle < -22.5
{ x: -1, y: 1 } # sw
end
# only move if the square is open
orig_x = @x
orig_y = @y
new_x = @x + move[:x]
new_y = @y + move[:y]
unless RogueRooks.occupied_square?(new_x, new_y)
RogueRooks.leave_square(orig_x, orig_y)
@x = new_x
@y = new_y
RogueRooks.occupy_square(new_x, new_y)
end
@last_move_time = Time.now
end
end