Unit
src.model.unit
¶
Unit domain model.
A unit represents a playable hero on the battlefield. It stores combat state, position, team ownership, available actions, learned competences, and rendering metadata required by the view layer.
The documentation follows Google-style docstrings so tools such as pdoc, Sphinx Napoleon, or MkDocs-based pipelines can expose parameters, return values, and side effects in a consistent HTML API reference.
Unit
¶
Bases: Sprite
Playable battlefield unit.
A unit combines gameplay state such as health, team, movement range, available actions, and competences with the position data needed by the animation layer. It also provides movement, attack dispatch, defense activation, and state synchronization helpers.
Attributes:
| Name | Type | Description |
|---|---|---|
state |
str
|
Current gameplay and animation state. |
actions |
dict[str, bool]
|
Per-turn action availability flags. |
old_position |
list[int]
|
Last saved position used for rollback. |
Notes
This class is documented as part of the public project API and is intended to be readable in generated HTML documentation.
Source code in src\model\unit.py
21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 | |
name
property
¶
Return the display name.
Returns:
| Name | Type | Description |
|---|---|---|
str |
Display name. |
x
property
writable
¶
Horizontal position in pixels.
Returns:
| Name | Type | Description |
|---|---|---|
int |
Current horizontal position in pixels. |
Side Effects
Updates the internal horizontal coordinate when it remains within bounds.
y
property
writable
¶
Vertical position in pixels.
Returns:
| Name | Type | Description |
|---|---|---|
int |
Current vertical position in pixels. |
Side Effects
Updates the internal vertical coordinate when it remains within bounds.
health
property
writable
¶
Current health points.
Returns:
| Name | Type | Description |
|---|---|---|
float |
Current health points. |
Side Effects
Reduces health and sets the unit state to dead when health reaches zero.
team
property
¶
Team or player identifier.
Returns:
| Name | Type | Description |
|---|---|---|
str |
Team or player identifier. |
speed
property
¶
Return the speed value.
Returns:
| Name | Type | Description |
|---|---|---|
int |
Configured speed value. |
movement_range
property
¶
Movement range limits.
Returns:
| Type | Description |
|---|---|
|
tuple[int, int, int]: Movement range constraints. |
is_selected
property
writable
¶
Whether the unit is currently selected.
Returns:
| Name | Type | Description |
|---|---|---|
bool |
Selection state. |
Side Effects
Updates the selection flag.
competences
property
¶
Competences grouped by category.
Returns:
| Type | Description |
|---|---|
|
dict[str, list]: Competences grouped by category. |
image
property
writable
¶
Current image associated with the object.
Returns:
| Type | Description |
|---|---|
|
pygame.Surface | None: Current image surface. |
Side Effects
Replaces the current image surface.
add_competence(competence, type)
¶
Attach an attack or defense competence to the unit.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
competence
|
Attack or defense object to attach to the unit. |
required | |
type
|
Action subtype, competence name, or category key used by the caller. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
None |
This method is executed for its side effects. |
Side Effects
Appends the competence to the attack or defense collection.
Source code in src\model\unit.py
activate_defense(damage, animation_manager)
¶
Apply all defensive competences to an incoming damage value.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
damage
|
int
|
Incoming damage value to transform or apply. |
required |
animation_manager
|
AnimationManager coordinating unit animations and effects. |
required |
Returns:
| Type | Description |
|---|---|
int
|
int | float: Damage remaining after every defense has been applied. |
Side Effects
Triggers each defense effect and logs damage reduction.
Source code in src\model\unit.py
attack(target, attack, animation_manager)
¶
Dispatch an attack competence against a target unit.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
target
|
Target unit affected by an attack or competence. |
required | |
attack
|
Attack competence selected for execution or range testing. |
required | |
animation_manager
|
AnimationManager coordinating unit animations and effects. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
None |
This method is executed for its side effects. |
Side Effects
May reduce target health and start the attack effect.
Notes
The method delegates damage computation to the selected Attack instance.
Source code in src\model\unit.py
move(dx, dy, rect, feet, map_obj)
¶
Move the unit by a pixel offset when the destination remains inside the map.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
dx
|
int
|
Horizontal displacement in pixels. |
required |
dy
|
int
|
Vertical displacement in pixels. |
required |
rect
|
Rect
|
Sprite rectangle synchronized with the unit position. |
required |
feet
|
Rect
|
Collision rectangle representing the unit feet. |
required |
map_obj
|
Map
|
Map object used to validate movement bounds. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
None |
This method is executed for its side effects. |
Side Effects
Saves the previous position, moves the unit, and synchronizes rectangles when valid.
Notes
The method validates map bounds but leaves collision resolution to the caller or surrounding game loop.
Source code in src\model\unit.py
save_location()
¶
Store the current coordinates so the unit can be restored later.
Returns:
| Name | Type | Description |
|---|---|---|
None |
This method is executed for its side effects. |
Side Effects
Stores the current coordinates for later rollback.
Source code in src\model\unit.py
move_back(rect, feet)
¶
Restore the last saved position and synchronize collision rectangles.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
rect
|
Rect
|
Sprite rectangle synchronized with the unit position. |
required |
feet
|
Rect
|
Collision rectangle representing the unit feet. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
None |
This method is executed for its side effects. |
Side Effects
Restores the saved coordinates and synchronizes rectangles.
Source code in src\model\unit.py
update(rect, feet)
¶
Synchronize the unit position with its sprite and feet rectangles.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
rect
|
Rect
|
Sprite rectangle synchronized with the unit position. |
required |
feet
|
Rect
|
Collision rectangle representing the unit feet. |
required |
Returns:
| Type | Description |
|---|---|
|
bool | None: For animations and effects, indicates completion or active playback; for rendering/update controllers, no explicit value is returned. |
Side Effects
Mutates animation, effect, terrain, and rendering state depending on the object.
Notes
The method is part of the frame loop and is expected to be called repeatedly.
Source code in src\model\unit.py
set_state(state, action_type, effect, target_pos=None)
¶
Update the unit state and optionally trigger a visual effect.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
state
|
Gameplay or animation state to apply. |
required | |
action_type
|
Specific action or competence type to associate with the state. |
required | |
effect
|
Effect instance associated with a unit or action. |
required | |
target_pos
|
Optional target position used to place or interpolate a visual effect. |
None
|
Returns:
| Name | Type | Description |
|---|---|---|
None |
This method is executed for its side effects. |
Side Effects
Changes the unit state and may start a visual effect.
Source code in src\model\unit.py
reset_movement_range()
¶
Reset the movement reference position to the current coordinates.
Returns:
| Name | Type | Description |
|---|---|---|
None |
This method is executed for its side effects. |
Side Effects
Updates the saved reference position.