-
Notifications
You must be signed in to change notification settings - Fork 75
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #27 from threedworld-mit/keyboard_controller
Keyboard controller
- Loading branch information
Showing
11 changed files
with
302 additions
and
124 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
# `keyboard_controller.py` | ||
|
||
## `KeyboardController(Controller)` | ||
|
||
`from tdw.keyboard_controller import KeyboardController` | ||
|
||
Listen for keyboard input to send commands. | ||
|
||
Usage: | ||
|
||
```python | ||
from tdw.keyboard_controller import KeyboardController | ||
from tdw.tdw_utils import TDWUtils | ||
|
||
def stop(): | ||
done = True | ||
|
||
done = False | ||
c = KeyboardController() | ||
c.start() | ||
|
||
# Quit. | ||
c.listen(key="esc", commands=None, function=stop) | ||
|
||
# Equivalent to c.start() | ||
c.listen(key="r", commands={"$type": "load_scene", "scene_name": "ProcGenScene"}, function=None) | ||
|
||
while not done: | ||
# Receive data. Load the scene when r is pressed. Quit when Esc is pressed. | ||
c.communicate([]) | ||
# Stop the build. | ||
c.communicate({"$type": "terminate"}) | ||
``` | ||
|
||
*** | ||
|
||
#### `stop()` | ||
|
||
def __init__(self, port: int = 1071, check_version: bool = True, launch_build: bool = True): | ||
|
||
*** | ||
|
||
#### `__init__(self, port: int = 1071, check_version: bool = True, launch_build: bool = True)` | ||
|
||
Create the network socket and bind the socket to the port. | ||
|
||
| Parameter | Description | | ||
| --- | --- | | ||
| port | The port number. | | ||
| check_version | If true, the controller will check the version of the build and print the result. | | ||
| launch_build | If True, automatically launch the build. If one doesn't exist, download and extract the correct version. Set this to False to use your own build, or (if you are a backend developer) to use Unity Editor. | | ||
|
||
*** | ||
|
||
#### `communicate(self, commands: Union[dict, List[dict]]) -> list` | ||
|
||
Listen for when a key is pressed and send commands. | ||
|
||
| Parameter | Description | | ||
| --- | --- | | ||
| key | The keyboard key. | | ||
| commands | Commands to be sent when the key is pressed. | | ||
| function | A function to be invoked when the key is pressed. | | ||
|
||
*** | ||
|
||
#### `listen(self, key: str, commands: Union[dict, List[dict]] = None, function=None) -> None` | ||
|
||
Listen for when a key is pressed and send commands. | ||
|
||
| Parameter | Description | | ||
| --- | --- | | ||
| key | The keyboard key. | | ||
| commands | Commands to be sent when the key is pressed. | | ||
| function | A function to be invoked when the key is pressed. | | ||
|
||
*** | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
from tdw.keyboard_controller import KeyboardController | ||
from tdw.tdw_utils import TDWUtils | ||
|
||
|
||
""" | ||
Use WASD or arrow keys to move an avatar. | ||
""" | ||
|
||
|
||
class KeyboardControls(KeyboardController): | ||
def __init__(self, port: int = 1071): | ||
super().__init__(port=port) | ||
self.done = False | ||
|
||
def run(self, force=80, torque=100): | ||
""" | ||
:param force: The force magnitude used to move the avatar. | ||
:param torque: The torque magnitude to turn the avatar by. | ||
""" | ||
|
||
print("W, up-arrow = Move forward") | ||
print("S, down-arrow = Move backward") | ||
print("A, left-arrow = Turn counterclockwise") | ||
print("D, right-arrow = Turn clockwise") | ||
print("Esc = Quit") | ||
|
||
# Listen for keyboard input for movement. | ||
self.listen("w", commands={"$type": "move_avatar_forward_by", | ||
"magnitude": force, | ||
"avatar_id": "a"}) | ||
self.listen("up", commands={"$type": "move_avatar_forward_by", | ||
"magnitude": force, | ||
"avatar_id": "a"}) | ||
self.listen("s", commands={"$type": "move_avatar_forward_by", | ||
"magnitude": -force, | ||
"avatar_id": "a"}) | ||
self.listen("down", commands={"$type": "move_avatar_forward_by", | ||
"magnitude": -force, | ||
"avatar_id": "a"}) | ||
self.listen("d", commands={"$type": "turn_avatar_by", | ||
"torque": torque, | ||
"avatar_id": "a"}) | ||
self.listen("right", commands={"$type": "turn_avatar_by", | ||
"torque": torque, | ||
"avatar_id": "a"}) | ||
self.listen("a", commands={"$type": "turn_avatar_by", | ||
"torque": -torque, | ||
"avatar_id": "a"}) | ||
self.listen("left", commands={"$type": "turn_avatar_by", | ||
"torque": -torque, | ||
"avatar_id": "a"}) | ||
# Listen for keyboard input to quit. | ||
self.listen("esc", function=self.stop) | ||
|
||
self.start() | ||
# Create the room. | ||
commands = [TDWUtils.create_empty_room(12, 12)] | ||
# Create the avatar. | ||
commands.extend(TDWUtils.create_avatar(avatar_type="A_Img_Caps", avatar_id="a")) | ||
# 1. Set high drag values so it doesn't feel like the avatar is sliding on ice. | ||
# 2. Set the room's floor material. | ||
commands.extend([{"$type": "set_avatar_drag", | ||
"drag": 10, | ||
"angular_drag": 20, | ||
"avatar_id": "a"}, | ||
self.get_add_material("parquet_alternating_orange", library="materials_high.json"), | ||
{"$type": "set_proc_gen_floor_material", | ||
"name": "parquet_alternating_orange"}, | ||
{"$type": "set_proc_gen_floor_texture_scale", | ||
"scale": {"x": 8, "y": 8}}]) | ||
self.communicate(commands) | ||
while not self.done: | ||
# Listen for keyboard input to add other commands. | ||
self.communicate([]) | ||
self.communicate({"$type": "terminate"}) | ||
|
||
def stop(self): | ||
""" | ||
Stop the controller and the build. | ||
""" | ||
|
||
self.done = True | ||
|
||
|
||
if __name__ == "__main__": | ||
KeyboardControls().run() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.