diff options
| author | troido <troido@hotmail.com> | 2018-01-01 16:55:58 +0100 |
|---|---|---|
| committer | troido <troido@hotmail.com> | 2018-01-01 16:55:58 +0100 |
| commit | 123edfd17cd9aa38d6d5e511a8c686d5d71ab140 (patch) | |
| tree | 0272a145ad0200d1405c345947d0a80559f5d269 | |
| parent | 38f3a3807373a19cacc0171dd7d5fd11f44dbd35 (diff) | |
made keybindings readable from an actual file
| -rw-r--r-- | asciifarm/client/gameclient.py | 16 | ||||
| -rw-r--r-- | asciifarm/client/keybindings.hy | 42 | ||||
| -rwxr-xr-x | asciifarm/client/main.py | 14 | ||||
| -rw-r--r-- | asciifarm/common/utils.py | 7 | ||||
| -rw-r--r-- | asciifarm/keybindings/keybindings.hy | 37 | ||||
| -rw-r--r-- | asciifarm/keybindings/vimbindings.hy | 39 |
6 files changed, 97 insertions, 58 deletions
diff --git a/asciifarm/client/gameclient.py b/asciifarm/client/gameclient.py index 914d90c..0ab5fb5 100644 --- a/asciifarm/client/gameclient.py +++ b/asciifarm/client/gameclient.py @@ -13,12 +13,15 @@ import string from .display.display import Display import hy -from . import keybindings as defaultkeybindings -import importlib + +hywrapper = """\ +(do + (require [asciifarm.client.keymacros [*]]) + {{ {} }})""" class Client: - def __init__(self, stdscr, display, name, connection, keybindings=None, logFile=None): + def __init__(self, stdscr, display, name, connection, keybindings, logFile=None): self.stdscr = stdscr self.display = display self.name = name @@ -26,11 +29,8 @@ class Client: self.connection = connection self.logFile = logFile - keymodule = defaultkeybindings - if keybindings: - keymodule = importlib.import_module(keybindings) - self.commands = keymodule.commands - self.controlsString = keymodule.docs + self.commands = hy.eval(hy.read_str(hywrapper.format(keybindings))) + self.controlsString = self.commands.get("help", "") self.display.showInfo(self.controlsString) diff --git a/asciifarm/client/keybindings.hy b/asciifarm/client/keybindings.hy deleted file mode 100644 index a0f392d..0000000 --- a/asciifarm/client/keybindings.hy +++ /dev/null @@ -1,42 +0,0 @@ - - -(require [asciifarm.client.keymacros [*]]) - -(setv commands { - "w" (input ["move" "north"]) - "s" (input ["move" "south"]) - "d" (input ["move" "east"]) - "a" (input ["move" "west"]) - "KEY_UP" (input ["move" "north"]) - "KEY_DOWN" (input ["move" "south"]) - "KEY_RIGHT" (input ["move" "east"]) - "KEY_LEFT" (input ["move" "west"]) - "e" (input ["take" (selectorvalue "ground")]) - "q" (input ["drop" (selectorvalue "inventory")]) - "E" (input ["use" (selectorvalue "inventory")]) - "r" (input ["interact" (selectorvalue "ground")]) - "v" (fn [client] (.select (selector "inventory") 1 True True)) - "c" (fn [client] (.select (selector "ground") 1 True True)) - "x" (fn [client] (.select (selector "equipment") 1 True True)) - "z" (input ["unequip" (selectorvalue "equipment")]) - "f" (doall [ - (input ["attack"]) - (input ["attack" "north"]) - (input ["attack" "south"]) - (input ["attack" "east"]) - (input ["attack" "west"])]) - "t" (fn [client] (client.readString)) }) - -(setv docs "\ -Controls: - wasd or arrows: - Move around - e: Grab - q: Drop - E: Use - r: Interact - f: Attack - t: Chat - z: Unequip - xcv: scroll") - diff --git a/asciifarm/client/main.py b/asciifarm/client/main.py index 274a6df..fa8a074 100755 --- a/asciifarm/client/main.py +++ b/asciifarm/client/main.py @@ -15,7 +15,7 @@ charMapPath = os.path.join(farmsPath, "charmaps") keybindingsPath = os.path.join(farmsPath, "keybindings") standardCharFiles = [name[:-5] for name in os.listdir(charMapPath) if name[-5:] == ".json"] -standardKeyFiles = [name[:-5] for name in os.listdir(keybindingsPath) if name[-5:] == ".json"] +standardKeyFiles = [name[:-3] for name in os.listdir(keybindingsPath) if name[-3:] == ".hy"] defaultAdresses = { @@ -35,7 +35,7 @@ def main(argv=None): parser.add_argument('-n', '--name', help='Your player name (must be unique!). Defaults to username', default=getpass.getuser()) parser.add_argument("-a", "--address", help="The address of the socket. When the socket type is 'abstract' this is just a name. When it is 'unix' this is a filename. When it is 'inet' is should be in the format 'address:port', eg 'localhost:8080'. Defaults depends on the socket type") parser.add_argument("-s", "--socket", help="the socket type. 'unix' is unix domain sockets, 'abstract' is abstract unix domain sockets and 'inet' is inet sockets. ", choices=["abstract", "unix", "inet"], default="abstract") - parser.add_argument('-k', '--keybindings', help='The script with the keybindings. This can be either a python3 or a hy script.') + parser.add_argument('-k', '--keybindings', help='The script with the keybindings. This file is a snippet of hy script.', default="keybindings") parser.add_argument('-c', '--characters', help='The file with the character mappings for the graphics. If it is either of these names: {} it will be loaded from the charmaps directory.'.format(standardCharFiles), default="default") parser.add_argument('-o', '--logfile', help='All game messages will be written to this file.'.format(standardCharFiles), default=None) @@ -49,12 +49,12 @@ def main(argv=None): charFile = os.path.join(charMapPath, charFile + ".json") with open(charFile, 'r') as cf: charMap = json.load(cf) + keyFile = args.keybindings - if keyFile: - with open(keyFile, 'r') as kf: - keybindings = json.load(kf) - else: - keybindings = None + if keyFile in standardKeyFiles: + keyFile = os.path.join(keybindingsPath, keyFile + ".hy") + with open(keyFile, 'r') as kf: + keybindings = kf.read() address = args.address if address == None: diff --git a/asciifarm/common/utils.py b/asciifarm/common/utils.py index 3df020c..729c85f 100644 --- a/asciifarm/common/utils.py +++ b/asciifarm/common/utils.py @@ -32,4 +32,9 @@ def writeFileSafe(filename, data, tempname=None): with open(tempname, 'w') as f: f.write(data) os.rename(tempname, filename) - + + +def readFile(filepath): + with open(filepath, "r") as f: + text = f.read() + return text diff --git a/asciifarm/keybindings/keybindings.hy b/asciifarm/keybindings/keybindings.hy new file mode 100644 index 0000000..e6dac86 --- /dev/null +++ b/asciifarm/keybindings/keybindings.hy @@ -0,0 +1,37 @@ + +"w" (input ["move" "north"]) +"s" (input ["move" "south"]) +"d" (input ["move" "east"]) +"a" (input ["move" "west"]) +"KEY_UP" (input ["move" "north"]) +"KEY_DOWN" (input ["move" "south"]) +"KEY_RIGHT" (input ["move" "east"]) +"KEY_LEFT" (input ["move" "west"]) +"e" (input ["take" (selectorvalue "ground")]) +"q" (input ["drop" (selectorvalue "inventory")]) +"E" (input ["use" (selectorvalue "inventory")]) +"r" (input ["interact" (selectorvalue "ground")]) +"v" (fn [client] (.select (selector "inventory") 1 True True)) +"c" (fn [client] (.select (selector "ground") 1 True True)) +"x" (fn [client] (.select (selector "equipment") 1 True True)) +"z" (input ["unequip" (selectorvalue "equipment")]) +"f" (doall [ + (input ["attack"]) + (input ["attack" "north"]) + (input ["attack" "south"]) + (input ["attack" "east"]) + (input ["attack" "west"])]) +"t" (fn [client] (client.readString)) +"help" "\ +Controls: + wasd or arrows: + Move around + e: Grab + q: Drop + E: Use + r: Interact + f: Attack + t: Chat + z: Unequip + xcv: scroll" + diff --git a/asciifarm/keybindings/vimbindings.hy b/asciifarm/keybindings/vimbindings.hy new file mode 100644 index 0000000..00c1ae1 --- /dev/null +++ b/asciifarm/keybindings/vimbindings.hy @@ -0,0 +1,39 @@ + + +;; mainly intended as example for using different keybindings +"k" (input ["move" "north"]) +"j" (input ["move" "south"]) +"l" (input ["move" "east"]) +"h" (input ["move" "west"]) +"KEY_UP" (input ["move" "north"]) +"KEY_DOWN" (input ["move" "south"]) +"KEY_RIGHT" (input ["move" "east"]) +"KEY_LEFT" (input ["move" "west"]) +"g" (input ["take" (selectorvalue "ground")]) +"q" (input ["drop" (selectorvalue "inventory")]) +"E" (input ["use" (selectorvalue "inventory")]) +"r" (input ["interact" (selectorvalue "ground")]) +"v" (fn [client] (.select (selector "inventory") 1 True True)) +"c" (fn [client] (.select (selector "ground") 1 True True)) +"x" (fn [client] (.select (selector "equipment") 1 True True)) +"z" (input ["unequip" (selectorvalue "equipment")]) +"f" (doall [ + (input ["attack"]) + (input ["attack" "north"]) + (input ["attack" "south"]) + (input ["attack" "east"]) + (input ["attack" "west"])]) +"t" (fn [client] (client.readString)) +"help" "\ +Controls: + wasd or arrows: + Move around + e: Grab + q: Drop + E: Use + r: Interact + f: Attack + t: Chat + z: Unequip + xcv: scroll" + |
