Skip to content

Commit

Permalink
menu: allow to edit a level, add keyboard shortcuts
Browse files Browse the repository at this point in the history
  • Loading branch information
emersion committed May 12, 2017
1 parent 9cac9f9 commit 3c4ca2a
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 14 deletions.
2 changes: 1 addition & 1 deletion levels/0.csv
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ ball,390,380
ball,390,320
ball,440,320
ball,160,420
block,0xFF69B4,396,132,406,112,421,122,436,112,446,132,436,142,426,132,426,172,421,177,416,172,416,132,406,142
#block,0xFF69B4,396,132,406,112,421,122,436,112,446,132,436,142,426,132,426,172,421,177,416,172,416,132,406,142
star,120,150
goal,220,410
rope,120,300,100,150
Expand Down
44 changes: 31 additions & 13 deletions menu.ml
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,10 @@ open Mlgrope
open Frontend
open Level
open Player
open Editor

exception SelectLevel of game_state
exception PlayLevel of game_state
exception EditLevel of string

type menu = {
size : vec;
Expand Down Expand Up @@ -67,28 +69,38 @@ let step m =
draw_arrow m.size 1;
m

let level_path name =
levels_dir^"/"^name^levels_ext

let load_level name =
let path = levels_dir^"/"^name^levels_ext in
let ch = open_in path in
let ch = open_in (level_path name) in
Level.input ch

let switch_level m delta =
let selected_index = m.selected_index + delta in
let selected_index =
if selected_index < 0 then 0
else if selected_index >= Array.length m.levels then (Array.length m.levels) - 1
else selected_index
in
let selected_level = load_level m.levels.(selected_index) in
{m with selected_index; selected_level}

let handle_event m s s' =
match s' with
| {button = true} ->
let pos = mouse_of_status s' in
let delta =
if collide_arrow m.size (-1) pos then -1
else if collide_arrow m.size 1 pos then 1
else raise (SelectLevel m.selected_level)
in
let selected_index = m.selected_index + delta in
let selected_index =
if selected_index < 0 then 0
else if selected_index >= Array.length m.levels then (Array.length m.levels) - 1
else selected_index
else raise (PlayLevel m.selected_level)
in
let selected_level = load_level m.levels.(selected_index) in
{m with selected_index; selected_level}
switch_level m delta
| {keypressed = true; key = 'p'} -> raise (PlayLevel m.selected_level)
| {keypressed = true; key = 'e'} ->
raise (EditLevel (level_path m.levels.(m.selected_index)))
| {keypressed = true; key = '<'} -> switch_level m (-1)
| {keypressed = true; key = '>'} -> switch_level m 1
| {keypressed = true; key = '\027'} -> raise Exit
| _ -> m

Expand All @@ -98,9 +110,11 @@ let rec run size =
Array.sort String.compare levels;
if Array.length levels = 0 then raise (Failure "No level available") else

Printf.printf "Press < or > to select a level, p to play, e to edit\n%!";

let m = {size; levels; selected_index = 0; selected_level = load_level levels.(0)} in
try Frontend.run step handle_event size m with
| SelectLevel(state) -> (
| PlayLevel(state) -> (
try Player.run size state with
| Player.TouchedGoalException(state, pos) ->
let t0 = get_time () in
Expand All @@ -119,3 +133,7 @@ let rec run size =
Frontend.run step handle_event size ()
| Player.OutOfBoundsException(state) -> run size
)
| EditLevel(path) -> (
try Editor.run size path with
| Exit -> run size
)

0 comments on commit 3c4ca2a

Please sign in to comment.