pellest/src/map.ml

68 lines
1.3 KiB
OCaml
Raw Normal View History

2022-12-11 18:58:56 +01:00
type dir =
| Left
| Right
| Down
| Up
type background =
| Grass
| Water
| Black
let pp_dir fmt dir =
let s =
match dir with
| Left -> "Left"
| Right -> "Right"
| Down -> "Down"
| Up -> "Up"
in
Format.pp_print_string fmt s
let pp_background fmt b =
let s =
match b with Grass -> "Grass" | Water -> "Water" | Black -> "Black"
in
Format.pp_print_string fmt s
type position =
{ x : int
; y : int
; dir : dir
}
let pp_position fmt p =
Format.fprintf fmt "(x = %d; y = %d; dir = %a)" p.x p.y pp_dir p.dir
2022-12-26 02:06:13 +01:00
2022-12-15 19:59:42 +01:00
type t =
{ tiles : background array array
; width : int
; height : int
}
2022-12-11 18:58:56 +01:00
let init () =
let width = 100 in
let height = 90 in
2022-12-15 19:59:42 +01:00
let tiles =
Array.init width (fun _x ->
Array.init height (fun _y ->
if Random.int 1000 <= 42 then Water else Grass ) )
in
2022-12-26 02:06:13 +01:00
{ tiles; width; height }
2022-12-15 19:59:42 +01:00
let get_tile_kind ~x ~y map =
try map.tiles.(x).(y) with Invalid_argument _ -> Black
2023-01-15 00:48:47 +01:00
let check_move map ({ x; y; _ } as pos) movement_dir =
let x, y =
match movement_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) as bg ->
Error (Format.asprintf "can't move on %a" pp_background bg)
| Grass -> Ok { pos with x; y }