Motion
Module: terminaltexteffects.engine.motion
Motion class for managing the movement of a character.
Attributes:
Name | Type | Description |
---|---|---|
paths |
dict[str, Path]
|
dictionary of paths |
character |
EffectCharacter
|
The EffectCharacter to move. |
current_coord |
Coord
|
current coordinate |
previous_coord |
Coord
|
previous coordinate |
active_path |
Path | None
|
active path |
Methods:
Name | Description |
---|---|
set_coordinate |
Coord) -> None: Sets the current coordinate to the given coordinate. |
new_path |
float = 1, ease: easing.EasingFunction | None = None, layer: int | None = None, hold_time: int = 0, loop: bool = False, id: str = "") -> Path: Creates a new Path and adds it to the Motion.paths dictionary with the path_id as key. |
query_path |
str) -> Path: Returns the path with the given path_id. |
movement_is_complete |
Returns whether the character has an active path. |
chain_paths |
list[Path], loop=False): Creates a chain of paths by registering activation events for each path such that paths[n] activates paths[n+1] when reached. If loop is True, paths[-1] activates paths[0] when reached. |
activate_path |
Path) -> None: Activates the first waypoint in the path. |
deactivate_path |
Path) -> None: Unsets the current path if the current path is path. |
move |
Moves the character one step closer to the target position based on an easing function if present, otherwise linearly. |
Source code in terminaltexteffects/engine/motion.py
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 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 |
|
__init__(character)
Initializes the Motion object with the given EffectCharacter.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
character |
EffectCharacter
|
The EffectCharacter to move. |
required |
Source code in terminaltexteffects/engine/motion.py
activate_path(path)
Activates the first waypoint in the given path and updates the path's properties accordingly.
This method sets the active path to the given path, calculates the distance to the first waypoint, and updates the total distance of the path. If the path has an origin segment, it removes it from the segments list and subtracts its distance from the total distance. Then, it creates a new origin segment from the current coordinate to the first waypoint and inserts it at the beginning of the segments list.
The method also resets the current step, hold time remaining, and max steps of the path based on the total distance and speed. It ensures that the enter and exit events for each segment are not triggered. If the path has a layer, it sets the character's layer to it. Finally, it triggers the PATH_ACTIVATED event for the character.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
path |
Path
|
The path to activate. |
required |
Source code in terminaltexteffects/engine/motion.py
chain_paths(paths, loop=False)
Creates a chain of paths by registering activation events for each path such that paths[n] activates paths[n+1] when reached. If loop is True, paths[-1] activates paths[0] when reached.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
paths |
list[Path]
|
list of paths to chain |
required |
loop |
bool
|
Whether the chain should loop. Defaults to False. |
False
|
Source code in terminaltexteffects/engine/motion.py
deactivate_path(path)
Set the active path to None if the active path is the given path.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
path |
Path
|
the Path to deactivate |
required |
Source code in terminaltexteffects/engine/motion.py
move()
Moves the character along the active path.
The character's current coordinate is updated to the next step in the active path. If the active path is completed, an event is triggered based on whether the path is set to loop or not. If the path is set to loop, the path is deactivated and then reactivated to start from the beginning. If not, the path is simply deactivated and a PATH_COMPLETE event is triggered.
If the path has a hold time, the character will pause at the end of the path for the specified duration. During this hold time, a PATH_HOLDING event is triggered on the first frame, and the hold time is decremented on each subsequent frame until it reaches zero.
If there is no active path or if the active path has no segments, the character does not move.
The character's previous coordinate is preserved before moving to allow for clearing the location in the terminal.
Source code in terminaltexteffects/engine/motion.py
movement_is_complete()
Returns whether the character has an active path.
Returns:
Name | Type | Description |
---|---|---|
bool |
bool
|
True if the character has no active path, False otherwise. |
Source code in terminaltexteffects/engine/motion.py
new_path(*, speed=1, ease=None, layer=None, hold_time=0, loop=False, id='')
Creates a new Path and adds it to the Motion.paths dictionary with the path_id as key.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
speed |
float
|
speed > 0. Defaults to 1. |
1
|
ease |
EasingFunction | None
|
easing function for character movement. Defaults to None. |
None
|
layer |
int | None
|
layer to move the character to, if None, layer is unchanged. Defaults to None. |
None
|
hold_time |
int
|
number of frames to hold the character at the end of the path. Defaults to 0. |
0
|
loop |
bool
|
Whether the path should loop back to the beginning. Default is False. |
False
|
id |
str
|
Unique identifier for the path. Used to query for the path. Defaults to "". |
''
|
Raises:
Type | Description |
---|---|
ValueError
|
If a path with the provided id already exists. |
Returns:
Name | Type | Description |
---|---|---|
Path |
Path
|
The new path. |
Source code in terminaltexteffects/engine/motion.py
query_path(path_id)
Returns the path with the given path_id.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
path_id |
str
|
path_id |
required |
Returns:
Name | Type | Description |
---|---|---|
Path |
Path
|
The path with the given path_id. |
Source code in terminaltexteffects/engine/motion.py
set_coordinate(coord)
Sets the current coordinate to the given coordinate.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
coord |
Coord
|
coordinate |
required |