pellest/src/map.ml

47 lines
916 B
OCaml

type dir =
| Left
| Right
| Down
| Up
type background =
| Grass
| Water
| Black
type position = int * int * dir
type t =
{ tiles : background array array
; width : int
; height : int
}
let init () =
let width = 1000 in
let height = 1000 in
let tiles =
Array.init width (fun _x ->
Array.init height (fun _y ->
if Random.int 1000 <= 42 then Water else Grass ) )
in
{ tiles; width; height }
let get_tile_kind ~x ~y map =
try map.tiles.(x).(y) with Invalid_argument _ -> Black
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 -> Error "invalid terrain"
| Grass -> Ok (x, y, dir)