add basic movements

This commit is contained in:
zapashcanon 2022-12-06 03:30:16 +01:00
parent dddcf9b488
commit 9833eb520e
No known key found for this signature in database
GPG key ID: 8981C3C62D1D28F1

View file

@ -1,3 +1,5 @@
let () = Random.self_init ()
module Map = struct module Map = struct
type background = type background =
| Grass | Grass
@ -8,12 +10,12 @@ module Map = struct
let height = 1000 let height = 1000
let player_pos = ref (500, 500) let player_pos = ref (20, 3)
let m = let m =
Array.init width (fun _x -> Array.init width (fun _x ->
Array.init height (fun _y -> Array.init height (fun _y ->
if Random.int 1000 = 42 then Water else Grass ) ) if Random.int 1000 <= 42 then Water else Grass ) )
let get_tile_kind ~x ~y = try m.(x).(y) with Invalid_argument _ -> Black let get_tile_kind ~x ~y = try m.(x).(y) with Invalid_argument _ -> Black
end end
@ -82,10 +84,7 @@ let draw_map () =
done; done;
let (_ : Jv.t) = let (_ : Jv.t) =
Jv.call context "drawImage" Jv.call context "drawImage"
[| papy_bottom [| papy_bottom; Jv.of_int (width / 2); Jv.of_int (height / 2) |]
; Jv.of_int ((width / 2) - (tile_size / 2))
; Jv.of_int ((height / 2) - (tile_size / 2))
|]
in in
() ()
@ -96,6 +95,34 @@ let () =
in in
() ()
let kb_handler e =
let x, y = !Map.player_pos in
let x, y =
match Jv.to_string @@ Jv.get e "key" with
| "z" -> (x, y - 1)
| "q" -> (x - 1, y)
| "s" -> (x, y + 1)
| "d" -> (x + 1, y)
| _s -> (x, y)
in
let x = max 0 x in
let x = min (Map.width - 1) x in
let y = max 0 y in
let y = min (Map.height - 1) y in
Map.player_pos := (x, y);
draw_map ()
let bind_keys () =
Jv.call window "addEventListener"
[| Jv.of_string "keydown"; Jv.repr kb_handler |]
let () =
let (_ : Jv.t) =
Jv.call window "addEventListener"
[| Jv.of_string "load"; Jv.repr bind_keys |]
in
()
(* (*
let draw_background () = let draw_background () =
for x = 0 to tiles_per_w - 1 do for x = 0 to tiles_per_w - 1 do