Motion
Module: terminaltexteffects.engine.motion
Motion class for managing the movement of a character.
A character's position is measured in its parent's frame: (column, row, height) on the text plane, where it
starts. Waypoints take a Coord (height 0), a Vec3, or a node to home in on.
Attributes:
| Name | Type | Description |
|---|---|---|
paths |
dict[str, Path]
|
dictionary of paths |
character |
EffectCharacter
|
The EffectCharacter to move. |
position |
Vec3
|
The exact position in the parent's frame. Setting it moves the character now. |
current_coord |
Coord
|
The cell the character is drawn in. |
previous_coord |
Coord
|
previous coordinate |
active_path |
Path | None
|
active path |
Methods:
| Name | Description |
|---|---|
set_coordinate |
Moves the character to a cell in its parent's frame, keeping its height. |
new_path |
Creates a new Path and adds it to the Motion.paths dictionary with the path_id as key. |
query_path |
Returns the path with the given path_id. |
movement_is_complete |
Returns whether the character has an active path. |
chain_paths |
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 |
Activates the first waypoint in the path. |
deactivate_path |
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
591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 | |
position
property
writable
The character's position in its parent's frame: (column, row, height) on the text plane.
__init__(character)
Initialize 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.
If the provided path arg is not a Path object, it must be a path_id string.
This method sets the active path to the given path and mutates the Path to reflect the character's current starting position. It 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 | str
|
The path to activate. Must be the Path itself, or the Path's ID. |
required |
Source code in terminaltexteffects/engine/motion.py
880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 | |
chain_paths(paths, *, loop=False)
Create a chain of paths.
Paths are chained 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=None)
Deactivate a path if it is currently active.
If path is omitted, the current active path is deactivated if one exists.
If path is a string, it is resolved as a path ID before deactivation.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
path
|
Path | str | None
|
Path to deactivate, its ID, or None to deactivate the current active path. |
None
|
Raises:
| Type | Description |
|---|---|
PathNotFoundError
|
If |
Source code in terminaltexteffects/engine/motion.py
move()
Move 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. Looping paths are reactivated from the current coordinate when they contain more than one segment. Otherwise, the path is 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.
If a segment or holding callback deactivates, replaces, or restarts the path, processing of the old traversal stops. Callback coordinates are preserved and any newly activated traversal begins moving on the next tick.
Source code in terminaltexteffects/engine/motion.py
1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 | |
movement_is_complete()
Return whether movement is complete.
Returns:
| Name | Type | Description |
|---|---|---|
bool |
bool
|
True if the character has no active path, False otherwise. |
new_path(*, speed=1, ease=None, layer=None, hold_time=0, loop=False, path_id='')
Create a new Path and add 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
|
path_id
|
str
|
Unique identifier for the path. Used to query for the path. Defaults to "". |
''
|
Raises:
| Type | Description |
|---|---|
DuplicatePathIDError
|
If a path with the provided id already exists. |
PathInvalidSpeedError
|
If |
Returns:
| Name | Type | Description |
|---|---|---|
Path |
Path
|
The new path. |
Source code in terminaltexteffects/engine/motion.py
query_path(path_id, not_found_action='raise')
Return the path with the given path_id, or None if no path with the given ID exists.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
path_id
|
str
|
path_id |
required |
not_found_action
|
Literal['raise'] | None
|
Action to take if a path with the given
path_id is not found. If "raise", a PathNotFoundError will be raised. If |
'raise'
|
Returns:
| Type | Description |
|---|---|
Path | None
|
Path | None: The path with the given path_id, or None. |
Raises:
| Type | Description |
|---|---|
PathNotFoundError
|
If |
Source code in terminaltexteffects/engine/motion.py
set_coordinate(coord)
Move the character to a cell, now: column and row in its parent's frame, keeping its height.
Assigning current_coord does the same.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
coord
|
Coord
|
coordinate |
required |
Source code in terminaltexteffects/engine/motion.py
Bases: Motion
Moves a body along paths of Vec3 waypoints in its parent's local frame, in units.
The same paths, easing, holds, loops and events as a character's motion. A body has no cell or layer: a path
with a layer can't be activated on a body, and current_coord is only the rounded (x, y) it last took from
set_coordinate. The world moves bodies along their paths at the start of every frame.
Source code in terminaltexteffects/engine/motion.py
activate_path(path)
Start moving the body along a path. See Motion.activate_path.
Raises:
| Type | Description |
|---|---|
ValueError
|
For the world root, which never moves, or a path with a |