fix: minor issues in Snapshot class
This commit is contained in:
		| @@ -6,7 +6,7 @@ from typing import Optional | ||||
|  | ||||
| import numpy as np | ||||
|  | ||||
| from vec import Vec | ||||
| from src.vec import Vec | ||||
|  | ||||
|  | ||||
| def iter_unpack(format, data): | ||||
| @@ -20,7 +20,7 @@ class Snapshot: | ||||
|     position: Vec = field(default_factory=Vec) | ||||
|     direction: Vec = field(default_factory=Vec) | ||||
|     speed: float = 0 | ||||
|     raycast_distances: list[float] | tuple[float, ...] = [0] | ||||
|     raycast_distances: list[float] | tuple[float, ...] = field(default_factory=list) | ||||
|     image: Optional[np.ndarray] = None | ||||
|  | ||||
|     def pack(self): | ||||
| @@ -46,19 +46,29 @@ class Snapshot: | ||||
|  | ||||
|         return data | ||||
|  | ||||
|     def unpack(self, data): | ||||
|         self.controls, data = iter_unpack(">BBBB", data) | ||||
|     @staticmethod | ||||
|     def unpack(data: bytes) -> Snapshot: | ||||
|         controls, data = iter_unpack(">BBBB", data) | ||||
|         (x, y, dx, dy, s), data = iter_unpack(">fffff", data) | ||||
|         self.position = Vec(x, y) | ||||
|         self.direction = Vec(dx, dy) | ||||
|         self.speed = s | ||||
|         position = Vec(x, y) | ||||
|         direction = Vec(dx, dy) | ||||
|         speed = s | ||||
|  | ||||
|         (nbr_raycasts,), data = iter_unpack(">B", data) | ||||
|         self.raycast_distances, data = iter_unpack(f">{nbr_raycasts}f", data) | ||||
|         raycast_distances, data = iter_unpack(f">{nbr_raycasts}f", data) | ||||
|  | ||||
|         (h, w), data = iter_unpack(">ii", data) | ||||
|  | ||||
|         if h * w > 0: | ||||
|             self.image = np.frombuffer(data, np.uint8).reshape(h, w, 3) | ||||
|             image = np.frombuffer(data, np.uint8).reshape(h, w, 3) | ||||
|         else: | ||||
|             self.image = None | ||||
|             image = None | ||||
|  | ||||
|         return Snapshot( | ||||
|             controls=controls, | ||||
|             position=position, | ||||
|             direction=direction, | ||||
|             speed=speed, | ||||
|             raycast_distances=raycast_distances, | ||||
|             image=image, | ||||
|         ) | ||||
|   | ||||
		Reference in New Issue
	
	Block a user