pellest/src/map.ml

48 lines
988 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-15 19:59:42 +01:00
type t =
{ tiles : background array array
; mutable player_pos : int * int
; mutable player_dir : dir
; 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
{ tiles; player_pos = (20, 3); player_dir = Down; 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 =
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