gale.command module
This file contains Command, a stateless, reusable action executed against a receiver passed in on every call (so a single instance can be shared as a singleton across every entity that performs it) whose __call__ alias lets it double as a plain callable(receiver, dt) leaf for gale.ai’s behavior/decision trees; CommandBindings, mapping an InputHandler input_id to the pair of commands run on press/release; and CommandControlled, an InputListener mixin that registers itself and routes on_input straight into a CommandBindings.
Author: Alejandro Mujica (aledrums@gmail.com)
- class gale.command.Command[source]
Bases:
objectBase class for the Command pattern. A Command holds no state of its own: everything it acts upon is the receiver passed into execute, never the constructor, so the same instance can be reused (even kept as a module-level singleton) across a player and any number of autonomous creatures that perform the same action.
- execute(receiver, dt=0.0)[source]
Perform this command against receiver.
- Parameters:
receiver (Any) – The object this command acts upon.
dt (float) – Time elapsed (in seconds) since the last execution. Defaults to 0.0, which is fine for one-shot commands, such as those fired from a press/release, that don’t need it.
- Return type:
None
- class gale.command.CommandBindings[source]
Bases:
objectMaps an InputHandler input_id to the pair of commands that should run when that input is pressed and when it is released.
- bind(input_id, press=None, release=None)[source]
- Parameters:
input_id (str) – The InputHandler action id this binding reacts to.
press (Command | None) – Command executed when the dispatched input_data reports pressed. The default value is None, meaning a press is ignored.
release (Command | None) – Command executed when the dispatched input_data reports released. The default value is None, meaning a release is ignored.
- Return type:
None
- dispatch(receiver, input_id, input_data, dt=0.0)[source]
Look up input_id and execute whichever of its commands matches input_data, if any.
- Parameters:
receiver (Any) – The object the resolved command, if any, is executed against.
input_id (str) – The action id reported by InputHandler, typically forwarded straight from on_input.
input_data (InputData) – The data reported alongside input_id. Only pressed/released are inspected, read through getattr with a False default since some variants (GamepadAxisData, for instance) don’t carry either attribute at all.
dt (float) – Time elapsed (in seconds) since the last dispatch, forwarded to the resolved command’s execute.
- Return type:
None
- class gale.command.CommandControlled(command_bindings, *args, **kwargs)[source]
Bases:
InputListenerMixin that registers itself with InputHandler and routes every on_input notification into a CommandBindings. Combine it by multiple inheritance with any entity class (Entity, Kinematic, and so on) to make that entity respond to input purely through Command objects, the same way plain InputListener is combined today.
- Parameters:
command_bindings (CommandBindings)
args (Any)
kwargs (Any)