diff --git a/controls.config b/controls.config new file mode 100644 index 0000000..97bb816 --- /dev/null +++ b/controls.config @@ -0,0 +1,6 @@ +ACTION_LEFT:KEY_S +ACTION_RIGHT:KEY_F +ACTION_UP:KEY_E +ACTION_DOWN:KEY_D +ACTION_JUMP:KEY_K +ACTION_ATTACK:KEY_J diff --git a/fcore/game-type.sml b/fcore/game-type.sml index ab68004..ba67bd5 100644 --- a/fcore/game-type.sml +++ b/fcore/game-type.sml @@ -12,7 +12,7 @@ sig , userKeys: CoreKey.user_key } - val initial: game_type + val initial: CoreKey.user_key -> game_type end structure GameType :> GAME_TYPE = @@ -35,7 +35,7 @@ struct end | enemyMapFromList ([], map) = map - val initial: game_type = + fun initial userKeys = let val player = { yAxis = EntityType.JUMPING 0 @@ -55,16 +55,6 @@ struct , platID = ~1 } - (* todo: replace initialKeys with keys parsed from file *) - val initialKeys = - { left = CoreKey.KEY_S - , right = CoreKey.KEY_L - , up = CoreKey.KEY_E - , down = CoreKey.KEY_D - , jump = CoreKey.KEY_K - , attack = CoreKey.KEY_J - } - val wall1 = {id = 1, x = 0, y = 0, width = 100, height = 1080} val wall2 = {id = 2, x = 1820, y = 0, width = 100, height = 1080} val wall3 = {id = 3, x = 0, y = 980, width = 1920, height = 108} @@ -152,7 +142,7 @@ struct , enemies = enemies , graph = graph , fallingEnemies = FallingEnemyMap.empty - , userKeys = initialKeys + , userKeys = userKeys } end end diff --git a/oms.mlb b/oms.mlb index 6852636..3188452 100644 --- a/oms.mlb +++ b/oms.mlb @@ -70,7 +70,7 @@ in end shell/input-state.sml +shell/parse-controls.sml shell/gl-shaders.sml shell/gl-draw.sml -shell/parse-controls.sml shell/shell.sml diff --git a/shell/gl-draw.sml b/shell/gl-draw.sml index 8f632a7..c3e418b 100644 --- a/shell/gl-draw.sml +++ b/shell/gl-draw.sml @@ -265,7 +265,20 @@ struct | true => Glfw.terminate () fun loop window = - let val shellState = create window - in helpLoop (shellState, GameType.initial) + let + val shellState = create window + val controls = + case ParseControls.parse () of + SOME controls => controls + | NONE => + { left = CoreKey.KEY_LEFT + , right = CoreKey.KEY_RIGHT + , up = CoreKey.KEY_UP + , down = CoreKey.KEY_DOWN + , jump = CoreKey.KEY_Z + , attack = CoreKey.KEY_X + } + in + helpLoop (shellState, GameType.initial controls) end end diff --git a/shell/parse-controls.sml b/shell/parse-controls.sml index e36ae1f..3c2f26c 100644 --- a/shell/parse-controls.sml +++ b/shell/parse-controls.sml @@ -14,7 +14,7 @@ struct | "ACTION_RIGHT" => SOME ACTION_RIGHT | "ACTION_UP" => SOME ACTION_UP | "ACTION_DOWN" => SOME ACTION_DOWN - | "ACTTION_JUMP" => SOME ACTION_JUMP + | "ACTION_JUMP" => SOME ACTION_JUMP | "ACTION_ATTACK" => SOME ACTION_ATTACK | _ => NONE @@ -107,6 +107,17 @@ struct | _ => NONE end + (* We don't want to attempt to parse strings + * which have trailing spaces or newlines + * so get the length of the last non-space chr *) + fun getLastPos (pos, line) = + if pos = String.size line then + String.size line - 1 + else + let val chr = String.sub (line, pos) + in if Char.isSpace chr then pos - 1 else getLastPos (pos + 1, line) + end + fun helpParse (controls, io) = case TextIO.inputLine io of SOME line => @@ -117,13 +128,13 @@ struct helpParse (controls, io) else let - val actionStart = colon + 1 - val actionLength = String.size line - actionStart - val actionString = - String.substring (line, actionStart, actionLength) + val actionString = String.substring (line, 0, colon) val action = actionFromString actionString - val keyString = String.substring (line, 0, colon) + val keyStart = colon + 1 + val keyFinish = getLastPos (keyStart, line) + val keyLength = keyFinish - keyStart + 1 + val keyString = String.substring (line, keyStart, keyLength) val key = CoreKey.keyFromString keyString val controls = @@ -135,7 +146,7 @@ struct helpParse (controls, io) end end - | NONE => returnControls controls + | NONE => let val () = TextIO.closeIn io in returnControls controls end fun parse () = let