do not send whole state on action

This commit is contained in:
Swrup 2022-12-26 02:06:13 +01:00
parent 86489c5394
commit caffcbb527
6 changed files with 135 additions and 66 deletions

View file

@ -9,10 +9,10 @@ type background =
| Water
| Black
type position = int * int * dir
type t =
{ tiles : background array array
; mutable player_pos : int * int
; mutable player_dir : dir
; width : int
; height : int
}
@ -25,23 +25,22 @@ let init () =
Array.init height (fun _y ->
if Random.int 1000 <= 42 then Water else Grass ) )
in
{ tiles; player_pos = (20, 3); player_dir = Down; width; height }
{ tiles; width; height }
let get_tile_kind ~x ~y map =
try map.tiles.(x).(y) with Invalid_argument _ -> Black
let move map dir =
if map.player_dir = dir then begin
let x, y = map.player_pos in
let x, y =
let check_move map entity_pos dir =
let x, y, current_dir = entity_pos in
let x, y =
if current_dir <> dir then (x, y)
else
match dir with
| Left -> (x - 1, y)
| Right -> (x + 1, y)
| Down -> (x, y + 1)
| Up -> (x, y - 1)
in
match get_tile_kind ~x ~y map with
| Black | Water -> ()
| Grass -> map.player_pos <- (x, y)
end
else map.player_dir <- dir
in
match get_tile_kind ~x ~y map with
| Black | Water -> Error "invalid terrain"
| Grass -> Ok (x, y, dir)