pellest/src/island_client.ml

157 lines
3.6 KiB
OCaml
Raw Normal View History

2022-12-06 03:30:16 +01:00
let () = Random.self_init ()
2022-12-06 03:08:30 +01:00
module Map = struct
type background =
| Grass
| Water
| Black
let width = 1000
let height = 1000
2022-12-06 03:30:16 +01:00
let player_pos = ref (20, 3)
2022-12-06 03:08:30 +01:00
let m =
Array.init width (fun _x ->
Array.init height (fun _y ->
2022-12-06 03:30:16 +01:00
if Random.int 1000 <= 42 then Water else Grass ) )
2022-12-06 03:08:30 +01:00
let get_tile_kind ~x ~y = try m.(x).(y) with Invalid_argument _ -> Black
end
2022-12-07 00:10:41 +01:00
open Brr
open Brr_canvas
let get_el id =
match Document.find_el_by_id G.document (Jstr.of_string id) with
| None -> failwith (Format.sprintf {|Could not find element by id: "%s"|} id)
| Some el -> el
2022-12-06 02:31:33 +01:00
let tile_size = 40
let width = 835
let height = 635
2022-12-07 00:10:41 +01:00
let canvas =
let el = get_el "canvas" in
Canvas.of_el el
2022-12-06 02:31:33 +01:00
2022-12-07 00:10:41 +01:00
let context = C2d.get_context canvas
2022-12-06 02:31:33 +01:00
let init_bg () =
2022-12-07 00:10:41 +01:00
Canvas.set_w canvas width;
Canvas.set_h canvas height;
C2d.set_fill_style context (C2d.color (Jstr.v "#FF1188"));
C2d.fill_rect context ~x:0. ~y:0. ~w:(float_of_int width)
~h:(float_of_int height)
2022-12-06 02:31:33 +01:00
let tiles_per_w = width / tile_size
let tiles_per_h = height / tile_size
let orig_x = (width - (tiles_per_w * tile_size)) / 2
let orig_y = (height - (tiles_per_h * tile_size)) / 2
2022-12-07 00:10:41 +01:00
let grass = C2d.image_src_of_el (get_el "grass")
2022-12-06 02:31:33 +01:00
2022-12-07 00:10:41 +01:00
let papy_bottom = C2d.image_src_of_el (get_el "papy_bottom")
2022-12-06 03:08:30 +01:00
2022-12-07 00:10:41 +01:00
let water = C2d.image_src_of_el (get_el "water")
2022-12-06 03:08:30 +01:00
let draw_map () =
let player_x, player_y = !Map.player_pos in
Format.printf "player_x = %d@\nplayer_y = %d@\n" player_x player_y;
2022-12-06 02:31:33 +01:00
for x = 0 to tiles_per_w - 1 do
2022-12-06 03:08:30 +01:00
let mapx = x + player_x - (tiles_per_w / 2) in
2022-12-06 02:31:33 +01:00
for y = 0 to tiles_per_h - 1 do
2022-12-06 03:08:30 +01:00
let mapy = y + player_y - (tiles_per_h / 2) in
2022-12-07 00:10:41 +01:00
let tile_img =
2022-12-06 03:08:30 +01:00
match Map.get_tile_kind ~x:mapx ~y:mapy with
| Grass -> grass
| Water -> water
| Black -> water
in
2022-12-07 00:10:41 +01:00
C2d.draw_image context tile_img
~x:(float_of_int (orig_x + (x * tile_size)))
~y:(float_of_int (orig_y + (y * tile_size)))
2022-12-06 02:31:33 +01:00
done
2022-12-06 03:08:30 +01:00
done;
2022-12-07 00:10:41 +01:00
C2d.draw_image context papy_bottom
~x:(float_of_int (width / 2))
~y:(float_of_int (height / 2))
2022-12-06 03:08:30 +01:00
2022-12-07 00:10:41 +01:00
let kb_handler ev =
2022-12-06 03:30:16 +01:00
let x, y = !Map.player_pos in
let x, y =
2022-12-07 00:10:41 +01:00
match ev |> Ev.as_type |> Ev.Keyboard.key |> Jstr.to_string with
2022-12-06 03:30:16 +01:00
| "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 () =
2022-12-07 00:10:41 +01:00
let on_window_load f =
ignore @@ Ev.listen Ev.load (fun _ev -> f ()) (Window.as_target G.window)
2022-12-06 03:30:16 +01:00
in
2022-12-07 00:10:41 +01:00
let bind_keys () =
ignore
@@ Ev.listen Ev.keydown
(fun ev -> kb_handler ev)
(Window.as_target G.window)
in
on_window_load init_bg;
on_window_load draw_map;
on_window_load bind_keys
2022-12-06 03:30:16 +01:00
2022-12-06 03:08:30 +01:00
(*
let draw_background () =
for x = 0 to tiles_per_w - 1 do
for y = 0 to tiles_per_h - 1 do
let (_ : Jv.t) =
Jv.call context "drawImage"
[| grass
; Jv.of_int (orig_x + (x * tile_size))
; Jv.of_int (orig_y + (y * tile_size))
|]
in
()
done
done
2022-12-06 02:31:33 +01:00
let () =
let (_ : Jv.t) =
Jv.call window "addEventListener"
[| Jv.of_string "load"; Jv.repr draw_background |]
in
()
2022-12-06 03:08:30 +01:00
let draw_papy () =
let (_ : Jv.t) =
Jv.call context "drawImage"
[| papy_bottom
; Jv.of_int ((width / 2) - (tile_size / 2))
; Jv.of_int ((height / 2) - (tile_size / 2))
|]
in
()
let () =
let (_ : Jv.t) =
Jv.call window "addEventListener"
[| Jv.of_string "load"; Jv.repr draw_papy |]
in
()
*)