-
Notifications
You must be signed in to change notification settings - Fork 1
/
race.py
87 lines (69 loc) · 3.66 KB
/
race.py
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
# A data class holding all data that belongs to a race.
# This includes:
# - file paths to floor and background sprites
# - the function that creates the collision map for the track that is played in this race
# - the number of laps required to win the race
# - the mode of the race (grand-prix, time-attack, ...)
# - the file path of the music track that should play during the race
class Race:
def __init__(self, race_track_creator, floor_tex_path, bg_tex_path, required_laps,
init_player_pos_x, init_player_pos_y, init_player_angle, is_foggy, race_mode, music_track_path):
# create collision map for played track using the passed function
self.race_track = race_track_creator()
# environment textures
self.floor_texture_path = floor_tex_path
self.bg_texture_path = bg_tex_path
# lap counting
self.player_completed_laps = 0
self.required_laps = required_laps
# initial player position and rotation
self.init_player_pos_x = init_player_pos_x
self.init_player_pos_y = init_player_pos_y
self.init_player_angle = init_player_angle
# whether the renderer should apply a fog effect during this race
self.is_foggy = is_foggy
self.race_mode = race_mode
self.music_track_path = music_track_path
# Returns True if and only if the registered player
# has finished the race on this track
# (i.e. finished the required number of laps).
def player_finished_race(self):
return self.player_completed_laps >= self.required_laps
# API for the App class to poll whether player has completed at least one lap.
# Idea: player should only be able to boost after first lap completed
def player_completed_first_lap(self):
return self.player_completed_laps >= 1
# First updates the passed flags of the key checkpoints of the track that this race is played on.
# Then checks whether the player has crossed the finish line.
# If so all key checkpoints are reset after checking whether player has passed all of them
# (if so, the player is credited a completed lap).
def update_lap_count(self, player_coll):
self.race_track.update_key_checkpoints(player_coll)
# if player has crossed the finish line
if self.race_track.is_on_finish_line(player_coll):
# if player has honestly finished a lap
if self.race_track.all_key_checkpoints_passed():
# Increment completed laps.
# If player has completed enough laps, initialize the finish sequence.
self.player_completed_laps += 1
print(str(self.player_completed_laps) + " laps completed!")
if self.player_finished_race():
print("race finished!")
self.race_track.reset_key_checkpoints()
# Restarts the race and resets all race data to their initial value.
# E.g. player's completed laps, passed key checkpoints, ...
def reset_data(self):
self.player_completed_laps = 0
self.race_track.reset_key_checkpoints()
# ------------- exposure of RaceTrack API ---------------------
def is_on_track(self, other):
return self.race_track.is_on_track(other)
def is_on_dash_plate(self, other):
return self.race_track.is_on_dash_plate(other)
def is_on_recovery_zone(self, other):
return self.race_track.is_on_recovery_zone(other)
def is_on_ramp(self, other):
return self.race_track.is_on_ramp(other)
def guard_rails_active(self):
return self.race_track.guard_rails_active()
# ------------- end of exposure of RaceTrack API --------------