successfully parse controls from file (but todo: save controls imperative shell on load)

This commit is contained in:
2025-02-17 03:48:31 +00:00
parent 46c713a9b5
commit 18495a0cca
5 changed files with 43 additions and 23 deletions

6
controls.config Normal file
View File

@@ -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

View File

@@ -12,7 +12,7 @@ sig
, userKeys: CoreKey.user_key , userKeys: CoreKey.user_key
} }
val initial: game_type val initial: CoreKey.user_key -> game_type
end end
structure GameType :> GAME_TYPE = structure GameType :> GAME_TYPE =
@@ -35,7 +35,7 @@ struct
end end
| enemyMapFromList ([], map) = map | enemyMapFromList ([], map) = map
val initial: game_type = fun initial userKeys =
let let
val player = val player =
{ yAxis = EntityType.JUMPING 0 { yAxis = EntityType.JUMPING 0
@@ -55,16 +55,6 @@ struct
, platID = ~1 , 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 wall1 = {id = 1, x = 0, y = 0, width = 100, height = 1080}
val wall2 = {id = 2, x = 1820, 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} val wall3 = {id = 3, x = 0, y = 980, width = 1920, height = 108}
@@ -152,7 +142,7 @@ struct
, enemies = enemies , enemies = enemies
, graph = graph , graph = graph
, fallingEnemies = FallingEnemyMap.empty , fallingEnemies = FallingEnemyMap.empty
, userKeys = initialKeys , userKeys = userKeys
} }
end end
end end

View File

@@ -70,7 +70,7 @@ in
end end
shell/input-state.sml shell/input-state.sml
shell/parse-controls.sml
shell/gl-shaders.sml shell/gl-shaders.sml
shell/gl-draw.sml shell/gl-draw.sml
shell/parse-controls.sml
shell/shell.sml shell/shell.sml

View File

@@ -265,7 +265,20 @@ struct
| true => Glfw.terminate () | true => Glfw.terminate ()
fun loop window = fun loop window =
let val shellState = create window let
in helpLoop (shellState, GameType.initial) 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
end end

View File

@@ -14,7 +14,7 @@ struct
| "ACTION_RIGHT" => SOME ACTION_RIGHT | "ACTION_RIGHT" => SOME ACTION_RIGHT
| "ACTION_UP" => SOME ACTION_UP | "ACTION_UP" => SOME ACTION_UP
| "ACTION_DOWN" => SOME ACTION_DOWN | "ACTION_DOWN" => SOME ACTION_DOWN
| "ACTTION_JUMP" => SOME ACTION_JUMP | "ACTION_JUMP" => SOME ACTION_JUMP
| "ACTION_ATTACK" => SOME ACTION_ATTACK | "ACTION_ATTACK" => SOME ACTION_ATTACK
| _ => NONE | _ => NONE
@@ -107,6 +107,17 @@ struct
| _ => NONE | _ => NONE
end 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) = fun helpParse (controls, io) =
case TextIO.inputLine io of case TextIO.inputLine io of
SOME line => SOME line =>
@@ -117,13 +128,13 @@ struct
helpParse (controls, io) helpParse (controls, io)
else else
let let
val actionStart = colon + 1 val actionString = String.substring (line, 0, colon)
val actionLength = String.size line - actionStart
val actionString =
String.substring (line, actionStart, actionLength)
val action = actionFromString actionString 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 key = CoreKey.keyFromString keyString
val controls = val controls =
@@ -135,7 +146,7 @@ struct
helpParse (controls, io) helpParse (controls, io)
end end
end end
| NONE => returnControls controls | NONE => let val () = TextIO.closeIn io in returnControls controls end
fun parse () = fun parse () =
let let