-
Notifications
You must be signed in to change notification settings - Fork 0
/
checkpoint.rb
126 lines (98 loc) · 2.87 KB
/
checkpoint.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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
Point = Struct.new(:x, :y)
Line = Struct.new(:p1, :p2)
def draw_checkpoints
i = 0
while (i < @track.checkpoints.size)
checkpoint = @track.checkpoints[i]
draw_checkpoint(checkpoint, i)
i += 1
end
end
def draw_checkpoint(checkpoint, index = -1)
color = Gosu::Color::RED
p1 = checkpoint.p1
p2 = checkpoint.p2
draw_line(
screen_position_x(p1.x * TILE_SIZE), screen_position_y(p1.y * TILE_SIZE), color,
screen_position_x(p2.x * TILE_SIZE), screen_position_y(p2.y * TILE_SIZE), color
)
if (index > -1)
x = (p1.x + (p2.x - p1.x) / 2) * TILE_SIZE
y = (p1.y + (p2.y - p1.y) / 2) * TILE_SIZE
x = screen_position_x(x)
y = screen_position_y(y)
draw_aligned_text(index + 1, @body_font, x, y, 0, Gosu::Color::BLACK)
end
end
def check_checkpoints(car)
i = 0
while (i < @track.checkpoints.size)
checkpoint = @track.checkpoints[i]
if (car_crossed_checkpoint?(car, checkpoint))
apply_checkpoint(car, checkpoint)
end
i += 1
end
end
def apply_checkpoint(car, checkpoint)
index = get_checkpoint_index(checkpoint)
crossing_finish = index == 0 && car.checkpoint != 0
crossing_new = index > car.checkpoint
crossing_next = index == car.checkpoint + 1
if (crossing_new)
car.missed_checkpoints += index - car.checkpoint - 1
car.checkpoint = index
end
if (crossing_finish)
car.missed_checkpoints += @track.checkpoints.size - car.checkpoint - 1
car.checkpoint = index
if (car.lap < LAPS - 1)
car.lap += 1
elsif (car != @ghost)
set_game_state(GameState::PostGame)
end
end
end
def car_crossed_checkpoint?(car, checkpoint)
p1 = Point.new(car.old_pos_x, car.old_pos_y)
p2 = Point.new(car.pos_x, car.pos_y)
car_line = Line.new(p1, p2)
p1 = Point.new(checkpoint.p1.x * TILE_SIZE, checkpoint.p1.y * TILE_SIZE)
p2 = Point.new(checkpoint.p2.x * TILE_SIZE, checkpoint.p2.y * TILE_SIZE)
checkpoint_line = Line.new(p1, p2)
line_intersect?(checkpoint_line, car_line)
end
def valid_checkpoint?(checkpoint)
checkpoint && !same_point?(checkpoint.p1, checkpoint.p2)
end
def same_point?(p1, p2)
p1.x == p2.x && p1.y == p2.y
end
def get_checkpoint_index(checkpoint)
found_index = -1
i = 0
while (i < @track.checkpoints.size && found_index == -1)
cp = @track.checkpoints[i]
if (cp == checkpoint)
found_index = i
end
i += 1
end
found_index
end
def same_checkpoint?(checkpoint1, checkpoint2)
(same_point?(checkpoint1.p1, checkpoint2.p1) && same_point?(checkpoint1.p2, checkpoint2.p2)) ||
(same_point?(checkpoint1.p1, checkpoint2.p2) && same_point?(checkpoint1.p2, checkpoint2.p1))
end
def find_same_checkpoint(checkpoint)
found_checkpoint = nil
i = 0
while (i < @track.checkpoints.size && !found_checkpoint)
cp = @track.checkpoints[i]
if (same_checkpoint?(checkpoint, cp))
found_checkpoint = cp
end
i += 1
end
found_checkpoint
end