pellest/src/map.ml

47 lines
916 B
OCaml
Raw Normal View History

2022-12-11 18:58:56 +01:00
type dir =
| Left
| Right
| Down
| Up
type background =
| Grass
| Water
| Black
2022-12-26 02:06:13 +01:00
type position = int * int * dir
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 () =
2022-12-15 19:59:42 +01:00
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
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
2022-12-26 02:06:13 +01:00
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
2022-12-15 19:59:42 +01:00
match dir with
| Left -> (x - 1, y)
| Right -> (x + 1, y)
| Down -> (x, y + 1)
| Up -> (x, y - 1)
2022-12-26 02:06:13 +01:00
in
match get_tile_kind ~x ~y map with
| Black | Water -> Error "invalid terrain"
| Grass -> Ok (x, y, dir)