From fcc8fe74d9138505b8eb159b393482eee0086c04 Mon Sep 17 00:00:00 2001 From: mandaw2014 Date: Mon, 18 Jul 2022 18:13:32 +0100 Subject: [PATCH] Added New Camera Angles --- achievements.py | 2 +- car.py | 85 ++++++++++++++++++++++++++++------------------- main.py | 3 +- main_menu.py | 87 +++++++++++++++++++++++++++++++------------------ 4 files changed, 109 insertions(+), 68 deletions(-) diff --git a/achievements.py b/achievements.py index 70dc807..42c902c 100644 --- a/achievements.py +++ b/achievements.py @@ -145,7 +145,7 @@ def unlock_forest_track(self): if self.car.enabled and self.car.last_count != 0: if self.snow_track.enabled and self.snow_track.unlocked: if self.car.last_count <= 35: - # Unlock Plains Track + # Unlock Forest Track self.forest_track.unlocked = True self.car.save_unlocked() return self.car.last_count <= 35 diff --git a/car.py b/car.py index 2644a44..83df3b3 100644 --- a/car.py +++ b/car.py @@ -15,10 +15,6 @@ def __init__(self, position = (0, 0, 4), rotation = (0, 0, 0), topspeed = 30, ac position = position, rotation = rotation, ) - - # Camera's position - camera.position = self.position + (20, 40, -50) - camera.rotation = (35, -20, 0) # Rotation parent self.rotation_parent = Entity() @@ -43,10 +39,12 @@ def __init__(self, position = (0, 0, 4), rotation = (0, 0, 0), topspeed = 30, ac self.min_drift_speed = 20 self.pivot_rotation_distance = 1 - # Smoothfollow - self.camera_angle = (20, 40, -50) - self.camera_follow = SmoothFollow(target = self, offset = self.camera_angle, speed = self.camera_speed) - camera.add_script(self.camera_follow) + # Camera Follow + self.camera_angle = "side" + self.camera_offset = (0, 60, -70) + self.camera_follow = False + self.c_pivot = Entity() + self.camera_pivot = Entity(parent = self.c_pivot, position = self.camera_offset) # Pivot for drifting self.pivot = Entity() @@ -278,9 +276,27 @@ def update(self): self.username_text = username.read() self.pivot.position = self.position - - camera.rotation = (35, -20, 0) - self.camera_follow.offset = self.camera_angle + self.c_pivot.position = self.position + self.c_pivot.rotation_y = self.rotation_y + self.camera_pivot.position = self.camera_offset + + if self.camera_follow: + if self.camera_angle == "side": + camera.rotation = (35, -20, 0) + camera.world_position = lerp(camera.world_position, self.world_position + (20, 40, -50), time.dt * self.camera_speed) + elif self.camera_angle == "top": + self.camera_offset = (0, 60, -70) + camera.rotation_x = 35 + camera.world_position = lerp(camera.world_position, self.camera_pivot.world_position, time.dt * 4) + camera.world_rotation_y = lerp(camera.world_rotation_y, self.world_rotation_y, time.dt * 4) + elif self.camera_angle == "behind": + self.camera_offset = (0, 10, -30) + camera.rotation_x = 12 + camera.world_position = lerp(camera.world_position, self.camera_pivot.world_position, time.dt * self.camera_speed) + camera.world_rotation_y = lerp(camera.world_rotation_y, self.world_rotation_y, time.dt * self.camera_speed) + elif self.camera_angle == "first-person": + camera.world_position = lerp(camera.world_position, self.world_position + (0.5, 0, 0), time.dt * 30) + camera.world_rotation = lerp(camera.world_rotation, self.world_rotation, time.dt * 30) # The y rotation distance between the car and the pivot self.pivot_rotation_distance = (self.rotation_y - self.pivot.rotation_y) @@ -427,7 +443,7 @@ def update(self): self.shake_amount = 0.03 # If the camera can shake and camera shake is on, then shake the camera - if self.can_shake and self.camera_shake_option: + if self.can_shake and self.camera_shake_option and self.camera_angle != "first-person": self.shake_camera() # Rotation @@ -438,32 +454,33 @@ def update(self): self.rotation_z = lerp(self.rotation_z, self.rotation_parent.rotation_z, 20 * time.dt) # Check if car is hitting the ground - if y_ray.distance <= self.scale_y * 1.7 + abs(movementY): - self.velocity_y = 0 - # Check if hitting a wall or steep slope - if y_ray.world_normal.y > 0.7 and y_ray.world_point.y - self.world_y < 0.5: - # Set the y value to the ground's y value - self.y = y_ray.world_point.y + 1.4 - self.hitting_wall = False - else: - # Car is hitting a wall - self.hitting_wall = True + if self.visible: + if y_ray.distance <= self.scale_y * 1.7 + abs(movementY): + self.velocity_y = 0 + # Check if hitting a wall or steep slope + if y_ray.world_normal.y > 0.7 and y_ray.world_point.y - self.world_y < 0.5: + # Set the y value to the ground's y value + self.y = y_ray.world_point.y + 1.4 + self.hitting_wall = False + else: + # Car is hitting a wall + self.hitting_wall = True - if self.copy_normals: - self.ground_normal = self.position + y_ray.world_normal - else: - self.ground_normal = self.position + (0, 180, 0) + if self.copy_normals: + self.ground_normal = self.position + y_ray.world_normal + else: + self.ground_normal = self.position + (0, 180, 0) - # Rotates the car according to the grounds normals - if not self.hitting_wall: - self.rotation_parent.look_at(self.ground_normal, axis = "up") - self.rotation_parent.rotate((0, self.rotation_y + 180, 0)) + # Rotates the car according to the grounds normals + if not self.hitting_wall: + self.rotation_parent.look_at(self.ground_normal, axis = "up") + self.rotation_parent.rotate((0, self.rotation_y + 180, 0)) + else: + self.rotation_parent.rotation = self.rotation else: + self.y += movementY * 50 * time.dt + self.velocity_y -= 50 * time.dt self.rotation_parent.rotation = self.rotation - else: - self.y += movementY * 50 * time.dt - self.velocity_y -= 50 * time.dt - self.rotation_parent.rotation = self.rotation # Movement movementX = self.pivot.forward[0] * self.speed * time.dt diff --git a/main.py b/main.py index 82ac8e7..f380d7e 100644 --- a/main.py +++ b/main.py @@ -135,10 +135,11 @@ def load_assets(): achievements = RallyAchievements(car, main_menu, sand_track, grass_track, snow_track, forest_track, savannah_track, lake_track) # Lighting + shadows -render.setShaderAuto() sun = SunLight(direction = (-0.7, -0.9, 0.5), resolution = 2048, car = car) ambient = AmbientLight(color = Vec4(0.5, 0.55, 0.66, 0) * 0.75) +render.setShaderAuto() + # Sky Sky(texture = "sky") diff --git a/main_menu.py b/main_menu.py index da2014d..ea1b2ca 100644 --- a/main_menu.py +++ b/main_menu.py @@ -68,8 +68,7 @@ def animate_in_menu(menu = menu): # Start Menu self.car.position = (-80, -42, 18.8) - self.car.rotation = (0, 90, 0) - self.car.enable() + self.car.visible = True self.grass_track.enable() def singleplayer(): @@ -78,9 +77,7 @@ def singleplayer(): self.main_menu.enable() grass_track.enable() self.car.position = (0, 0, 4) - camera.rotation = (35, -20, 0) - self.car.camera_follow.offset = self.car.camera_angle - self.car.disable() + self.car.visible = False for track in self.tracks: for i in track.track: i.disable() @@ -90,7 +87,7 @@ def singleplayer(): def multiplayer(): self.start_menu.disable() self.host_menu.enable() - self.car.enable() + self.car.visible = True self.car.position = (-3, -44.5, 92) grass_track.disable() snow_track.enable() @@ -126,7 +123,7 @@ def create_server(): self.car.server.start_server = True self.host_menu.disable() self.created_server_menu.enable() - self.car.enable() + self.car.visible = True self.car.position = (-63, -40, -7) self.car.rotation = (0, 90, 0) snow_track.disable() @@ -141,7 +138,7 @@ def create_server(): def join_server_func(): self.host_menu.disable() self.server_menu.enable() - self.car.enable() + self.car.visible = True self.car.position = (-105, -50, -59) snow_track.disable() sand_track.enable() @@ -156,7 +153,7 @@ def back_host(): self.start_menu.enable() self.car.position = (-80, -42, 18.8) self.car.rotation = (0, 90, 0) - self.car.enable() + self.car.visible = True self.grass_track.enable() self.snow_track.disable() for track in self.tracks: @@ -185,9 +182,9 @@ def join_hosted_server_func(): self.created_server_menu.disable() self.main_menu.enable() self.car.position = (0, 0, 4) + self.car.camera_offset = (20, 40, -50) camera.rotation = (35, -20, 0) - self.car.camera_follow.offset = self.car.camera_angle - self.car.disable() + self.car.visible = False self.sand_track.disable() self.grass_track.enable() for track in self.tracks: @@ -218,9 +215,9 @@ def join_server(): grass_track.enable() sand_track.disable() self.car.position = (0, 0, 4) + self.car.camera_offset = (20, 40, -50) camera.rotation = (35, -20, 0) - self.car.camera_follow.offset = self.car.camera_angle - self.car.disable() + self.car.visible = False self.car.connected = False for track in self.tracks: for i in track.track: @@ -231,7 +228,7 @@ def join_server(): def back_server(): self.host_menu.enable() self.server_menu.disable() - self.car.enable() + self.car.visible = True self.car.position = (-3, -44.5, 92) sand_track.disable() snow_track.enable() @@ -255,7 +252,7 @@ def back_server(): def back_singleplayer(): self.car.position = (-80, -42, 18.8) self.car.rotation = (0, 90, 0) - self.car.enable() + self.car.visible = True self.grass_track.enable() self.start_menu.enable() self.main_menu.disable() @@ -312,7 +309,7 @@ def ai_func(): def sand_track_func(): if sand_track.unlocked: - self.car.enable() + self.car.visible = True mouse.locked = True self.maps_menu.disable() self.car.position = (-63, -30, -7) @@ -350,7 +347,7 @@ def sand_track_func(): def grass_track_func(): if grass_track.unlocked: - self.car.enable() + self.car.visible = True mouse.locked = True self.maps_menu.disable() self.car.position = (-80, -30, 15) @@ -388,7 +385,7 @@ def grass_track_func(): def snow_track_func(): if snow_track.unlocked: - self.car.enable() + self.car.visible = True mouse.locked = True self.maps_menu.disable() self.car.position = (-5, -35, 90) @@ -426,7 +423,7 @@ def snow_track_func(): def forest_track_func(): if forest_track.unlocked: - self.car.enable() + self.car.visible = True mouse.locked = True self.maps_menu.disable() self.car.position = (12, -35, 73) @@ -464,7 +461,7 @@ def forest_track_func(): def savannah_track_func(): if savannah_track.unlocked: - self.car.enable() + self.car.visible = True mouse.locked = True self.maps_menu.disable() self.car.position = (-12, -35, 40) @@ -502,7 +499,7 @@ def savannah_track_func(): def lake_track_func(): if lake_track.unlocked: - self.car.enable() + self.car.visible = True mouse.locked = True self.maps_menu.disable() self.car.position = (-121, -40, 158) @@ -794,6 +791,20 @@ def back_settings(): # Gameplay Menu + def camera_angle(): + if self.car.camera_angle == "side": + self.car.camera_angle = "top" + camera_angle_button.text = "Camera Angle: Top" + elif self.car.camera_angle == "top": + self.car.camera_angle = "behind" + camera_angle_button.text = "Camera Angle: Behind" + elif self.car.camera_angle == "behind": + self.car.camera_angle = "first-person" + camera_angle_button.text = "Camera Angle: First-Person" + elif self.car.camera_angle == "first-person": + self.car.camera_angle = "side" + camera_angle_button.text = "Camera Angle: Side" + def camera_shake(): self.car.camera_shake_option = not self.car.camera_shake_option if self.car.camera_shake_option: @@ -805,10 +816,12 @@ def back_gameplay(): self.gameplay_menu.disable() self.settings_menu.enable() - camera_shake_button = Button("Camera Shake: On", color = color.black, scale_y = 0.1, scale_x = 0.3, y = 0.12, parent = self.gameplay_menu) - reset_highsore_button = Button(text = "Reset Highscore", color = color.black, scale_y = 0.1, scale_x = 0.3, y = 0, parent = self.gameplay_menu) - back_button_gameplay = Button(text = "Back", color = color.black, scale_y = 0.1, scale_x = 0.3, y = -0.12, parent = self.gameplay_menu) + camera_angle_button = Button("Camera Angle: Side", color = color.black, scale_y = 0.1, scale_x = 0.3, y = 0.12, parent = self.gameplay_menu) + camera_shake_button = Button("Camera Shake: On", color = color.black, scale_y = 0.1, scale_x = 0.3, y = 0, parent = self.gameplay_menu) + reset_highsore_button = Button(text = "Reset Highscore", color = color.black, scale_y = 0.1, scale_x = 0.3, y = -0.12, parent = self.gameplay_menu) + back_button_gameplay = Button(text = "Back", color = color.black, scale_y = 0.1, scale_x = 0.3, y = -0.24, parent = self.gameplay_menu) + camera_angle_button.on_click = Func(camera_angle) camera_shake_button.on_click = Func(camera_shake) reset_highsore_button.on_click = Func(self.car.reset_highscore) back_button_gameplay.on_click = Func(back_gameplay) @@ -1000,7 +1013,7 @@ def respawn(): def main_menu(): self.car.position = (0, 0, 4) - self.car.disable() + self.car.visible = False self.car.rotation = (0, 65, 0) self.car.speed = 0 self.car.velocity_y = 0 @@ -1041,9 +1054,9 @@ def back_garage(): self.garage_menu.disable() self.main_menu.enable() self.car.position = (0, 0, 4) + self.car.camera_offset = (20, 40, -50) camera.rotation = (35, -20, 0) - self.car.camera_follow.offset = self.car.camera_angle - self.car.disable() + self.car.visible = False grass_track.enable() sand_track.disable() for track in self.tracks: @@ -1060,7 +1073,7 @@ def garage_button_func(): self.cars_menu.enable() self.cosmetics_menu.disable() self.colours_menu.disable() - self.car.enable() + self.car.visible = True self.car.position = (-105, -50, -59) grass_track.disable() sand_track.enable() @@ -1409,6 +1422,11 @@ def garage_locked_text(self, warning): invoke(self.garage_unlocked_text.disable, delay = 1) def update(self): + if not self.start_menu.enabled and not self.main_menu.enabled and not self.settings_menu.enabled and not self.race_menu.enabled and not self.maps_menu.enabled and not self.settings_menu.enabled and not self.garage_menu.enabled and not self.controls_menu.enabled and not self.host_menu.enabled and not self.server_menu.enabled and not self.created_server_menu.enabled and not self.video_menu.enabled and not self.gameplay_menu.enabled: + self.car.camera_follow = True + else: + self.car.camera_follow = False + # AI Slider if self.car.multiplayer_update == False: if self.ai_slider.value == 0: @@ -1435,15 +1453,21 @@ def update(self): else: self.car.rotation_y = mouse.x * 200 + camera.position = lerp(camera.position, self.car.position + self.car.camera_offset, time.dt * self.car.camera_speed) + if self.start_menu.enabled: - self.car.camera_follow.offset = (-25, 4, 0) + self.car.camera_offset = (-25, 4, 0) camera.rotation = (5, 90, 0) elif self.host_menu.enabled: - self.car.camera_follow.offset = (-25, 8, 0) + self.car.camera_offset = (-25, 8, 0) camera.rotation = (14, 90, 0) else: - self.car.camera_follow.offset = (-25, 6, 5) + self.car.camera_offset = (-25, 6, 5) camera.rotation = (10, 90, 0) + else: + if not self.car.camera_follow: + camera.rotation = (35, -20, 0) + camera.position = lerp(camera.position, self.car.position + (20, 40, -50), time.dt * self.car.camera_speed) # If the host menu or server menu is enabled, save username if self.host_menu.enabled or self.server_menu.enabled or self.created_server_menu.enabled: @@ -1489,7 +1513,6 @@ def input(self, key): self.car.timer.disable() self.car.highscore.enable() - else: self.car.timer.disable() self.car.reset_count_timer.disable()