feat: add track classes
This commit is contained in:
		
							
								
								
									
										61
									
								
								src/track.py
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										61
									
								
								src/track.py
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,61 @@ | |||||||
|  | from __future__ import annotations | ||||||
|  |  | ||||||
|  | import json | ||||||
|  |  | ||||||
|  | from src.track_object import TrackObject, TrackObjectType | ||||||
|  | from src.utils import ROOT | ||||||
|  | from src.vec import Vec | ||||||
|  |  | ||||||
|  |  | ||||||
|  | class Track: | ||||||
|  |     TRACKS_DIRECTORY = ROOT / "assets" / "tracks" | ||||||
|  |  | ||||||
|  |     def __init__(self, id: str, name: str, start_pos: Vec, start_dir: Vec) -> None: | ||||||
|  |         self.id: str = id | ||||||
|  |         self.name: str = name | ||||||
|  |         self.start_pos: Vec = start_pos | ||||||
|  |         self.start_dir: Vec = start_dir | ||||||
|  |         self.objects: list[TrackObject] = [] | ||||||
|  |         self.load_objects() | ||||||
|  |  | ||||||
|  |     @staticmethod | ||||||
|  |     def load(name: str) -> Track: | ||||||
|  |         with open(Track.TRACKS_DIRECTORY / name / "meta.json", "r") as f: | ||||||
|  |             meta: dict = json.load(f) | ||||||
|  |  | ||||||
|  |         return Track( | ||||||
|  |             name, | ||||||
|  |             meta["name"], | ||||||
|  |             Vec(*meta["start"]["pos"]), | ||||||
|  |             Vec(*meta["start"]["direction"]), | ||||||
|  |         ) | ||||||
|  |  | ||||||
|  |     def load_objects(self): | ||||||
|  |         with open(Track.TRACKS_DIRECTORY / self.id / "track.json", "r") as f: | ||||||
|  |             data: list = json.load(f) | ||||||
|  |  | ||||||
|  |         self.objects = [] | ||||||
|  |         for obj_data in data: | ||||||
|  |             if obj_data["type"] == "road": | ||||||
|  |                 self.objects.append(Road.load(obj_data)) | ||||||
|  |  | ||||||
|  |  | ||||||
|  | class RoadPoint: | ||||||
|  |     def __init__(self, pos: Vec, direction: Vec, width: float) -> None: | ||||||
|  |         self.pos: Vec = pos | ||||||
|  |         self.direction: Vec = direction | ||||||
|  |         self.width: float = width | ||||||
|  |  | ||||||
|  |     @staticmethod | ||||||
|  |     def load(data: list[float]) -> RoadPoint: | ||||||
|  |         return RoadPoint(Vec(data[0], data[1]), Vec(data[2], data[3]), data[4]) | ||||||
|  |  | ||||||
|  |  | ||||||
|  | class Road(TrackObject): | ||||||
|  |     def __init__(self, pts: list[RoadPoint]) -> None: | ||||||
|  |         super().__init__(TrackObjectType.Road) | ||||||
|  |         self.pts: list[RoadPoint] = pts | ||||||
|  |  | ||||||
|  |     @staticmethod | ||||||
|  |     def load(data: dict) -> Road: | ||||||
|  |         return Road([RoadPoint.load(pt) for pt in data["pts"]]) | ||||||
							
								
								
									
										13
									
								
								src/track_object.py
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										13
									
								
								src/track_object.py
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,13 @@ | |||||||
|  | from enum import StrEnum | ||||||
|  |  | ||||||
|  |  | ||||||
|  | class TrackObjectType(StrEnum): | ||||||
|  |     Road = "road" | ||||||
|  |  | ||||||
|  |  | ||||||
|  | class TrackObject: | ||||||
|  |     def __init__( | ||||||
|  |         self, | ||||||
|  |         type: TrackObjectType, | ||||||
|  |     ) -> None: | ||||||
|  |         self.type: TrackObjectType = type | ||||||
							
								
								
									
										5
									
								
								src/utils.py
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										5
									
								
								src/utils.py
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,5 @@ | |||||||
|  | import os | ||||||
|  | from pathlib import Path | ||||||
|  |  | ||||||
|  |  | ||||||
|  | ROOT = Path(os.path.abspath(os.path.join(os.path.dirname(__file__), os.pardir))) | ||||||
		Reference in New Issue
	
	Block a user