feat: add rollback button

This commit is contained in:
2025-10-24 19:27:58 +02:00
parent 8b7927a3c5
commit f1fadd123f
5 changed files with 70 additions and 12 deletions

View File

@@ -8,7 +8,8 @@ from src.remote_controller import RemoteController
from src.utils import get_segments_intersection, segments_intersect
from src.vec import Vec
sign = lambda x: 0 if x == 0 else (-1 if x < 0 else 1)
def sign(x): return 0 if x == 0 else (-1 if x < 0 else 1)
class Car:
@@ -27,6 +28,8 @@ class Car:
RAYS_MAX_DIST = 100
def __init__(self, pos: Vec, direction: Vec) -> None:
self.initial_pos: Vec = pos.copy()
self.initial_dir: Vec = direction.copy()
self.pos: Vec = pos
self.direction: Vec = direction
self.speed: float = 0
@@ -77,7 +80,8 @@ class Car:
if show_raycasts:
pos: Vec = camera.world2screen(self.pos)
for p in self.rays_end:
pygame.draw.line(surf, (255, 0, 0), pos, camera.world2screen(p), 2)
pygame.draw.line(surf, (255, 0, 0), pos,
camera.world2screen(p), 2)
pts: list[Vec] = self.get_corners()
pts = [camera.world2screen(p) for p in pts]
@@ -127,14 +131,17 @@ class Car:
n *= -1
dist = -dist
self.speed = 0
self.pos = self.pos + n * (self.COLLISION_MARGIN - dist)
self.pos = self.pos + n * \
(self.COLLISION_MARGIN - dist)
return
def cast_rays(self, polygons: list[list[Vec]]):
for i in range(self.N_RAYS):
angle: float = radians((i / (self.N_RAYS - 1) - 0.5) * self.RAYS_FOV)
angle: float = radians(
(i / (self.N_RAYS - 1) - 0.5) * self.RAYS_FOV)
p: Optional[Vec] = self.cast_ray(angle, polygons)
self.rays[i] = self.RAYS_MAX_DIST if p is None else (p - self.pos).mag()
self.rays[i] = self.RAYS_MAX_DIST if p is None else (
p - self.pos).mag()
self.rays_end[i] = self.pos if p is None else p
def cast_ray(self, angle: float, polygons: list[list[Vec]]) -> Optional[Vec]:
@@ -161,3 +168,8 @@ class Car:
dist = d
closest = p
return closest
def reset(self):
self.pos = self.initial_pos.copy()
self.direction = self.initial_dir.copy()
self.speed = 0