Coverage for topdownengine/mobile_object/__init__.py: 94%
32 statements
« prev ^ index » next coverage.py v7.15.2, created at 2026-07-24 02:27 +0000
« prev ^ index » next coverage.py v7.15.2, created at 2026-07-24 02:27 +0000
1# Copyright (c) 2026 Shaurya Sharma
2# SPDX-License-Identifier: MIT
4from topdownengine.game_object import GameObject
5from typing import Any
6from topdownengine.game import Game
8class MobileObject(GameObject):
9 """Subclass of GameObject that serves as a wrapper around Controllers with some
10 extra movement functionality.
12 Attributes:
13 animation_paths (dict[str,str]|None): The paths animations were loaded from.
14 frame_size (tuple[int]|None): The frame size used to load/generate animations.
15 directional_anims (bool): Whether or not to load and use directional animations.
16 current_dir (str): Current direction (only set if directional_anims is True).
17 controller (BaseController): The controller the MobileObject should use.
18 jump_vel (float): The z-velocity that should be used while jumping.
19 """
20 def __init__(
21 self,
22 controller: Any,
23 animation_paths: dict[str,str]|None=None,
24 frame_size: tuple[int]|None=None,
25 directional_anims: bool=False
26 ) -> None:
27 """Initialize the MobileObject.
29 Args:
30 controller (BaseController): The controller the MobileObject should use.
31 animation_paths (dict[str,str], optional): The animation paths to load animations from. Defaults to None.
32 frame_size (tuple[int]|None, optional): The frame size to use to load/generate animations. Defaults to None.
33 directional_anims (bool, optional): Whether or not to load and use directional animations. Defaults to False.
34 """
35 # Set animation paths dict and frame size before calling super().__init__()
36 # This make it automatically load in the animations without
37 # having to call it a second time.
38 self.animation_paths = animation_paths
39 if frame_size is not None:
40 self.frame_size = frame_size
41 self.directional_anims = directional_anims
42 if self.directional_anims:
43 self.current_dir = "d"
45 super().__init__()
46 self.controller = controller
47 self.jump_vel = 0.75
49 def update(self, dt: float, game: Game) -> None:
50 """This method updates the MobileObject instance.
52 Args:
53 dt (float): The deltatime.
54 game (Game): The Game object.
55 """
56 self.controller.update(self, dt)
57 super().update(dt, game)
58 if self.velocity.length():
59 self.current_animation = "walk"
60 angle = self.velocity.as_polar()[1]
61 if -67.5 <= angle <= 67.5: # Right
62 self.current_dir = "r"
64 elif (-180 <= angle <= -112.5) or (112.5 <= angle <= 180): # Left
65 self.current_dir = "l"
67 elif 67.5 <= angle <= 112.5: # Down
68 self.current_dir = "d"
70 elif -112.5 <= angle <= -67.5: # Up
71 self.current_dir = "u"
72 else:
73 self.current_animation = "idle"
75 def jump(self) -> None:
76 """This method sets the MobileObject's z velocity to the jump velocity
77 if the MobileObject is grounded (MobileObject.elevation == MobileObject.z).
78 """
79 if self.elevation == self.z:
80 self.z_vel = self.jump_vel