pellest/src/island_client.ml

156 lines
3.7 KiB
OCaml
Raw Normal View History

open Brr
open Brr_canvas
module G = struct
include Brr.G
let request_animation_frame f =
(ignore : int -> unit) @@ Brr.G.request_animation_frame f
end
2022-12-06 03:30:16 +01:00
let () = Random.self_init ()
2022-12-08 04:08:27 +01:00
type dir =
| Left
| Right
| Down
| Up
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
2022-12-08 04:08:27 +01:00
let player_dir = ref Down
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
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 = 875
2022-12-06 02:31:33 +01:00
let height = 675
2022-12-06 02:31:33 +01:00
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 tiles_per_w =
let n = width / tile_size in
if n mod 2 = 0 then n - 1 else n
2022-12-06 02:31:33 +01:00
let tiles_per_h =
let n = height / tile_size in
if n mod 2 = 0 then n - 1 else n
2022-12-06 02:31:33 +01:00
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-08 04:08:27 +01:00
let papy_left = C2d.image_src_of_el (get_el "papy_left")
let papy_right = C2d.image_src_of_el (get_el "papy_right")
let papy_down = C2d.image_src_of_el (get_el "papy_down")
let papy_up = C2d.image_src_of_el (get_el "papy_up")
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
2022-12-08 04:08:27 +01:00
let draw_map =
let papy_x = float_of_int (width - tile_size) /. 2. in
let papy_y = (float_of_int height /. 2.) -. (float_of_int tile_size *. 1.5) in
fun () ->
let player_x, player_y = !Map.player_pos in
for x = 0 to tiles_per_w - 1 do
let map_x = x + player_x - (tiles_per_w / 2) in
let tile_x = float_of_int ((x * tile_size) + orig_x) in
for y = 0 to tiles_per_h - 1 do
let map_y = y + player_y - (tiles_per_h / 2) in
let tile_y = float_of_int ((y * tile_size) + orig_y) in
let tile_img =
match Map.get_tile_kind ~x:map_x ~y:map_y with
| Grass -> grass
| Water -> water
| Black -> water
in
C2d.draw_image context tile_img ~x:tile_x ~y:tile_y
done
done;
let papy =
match !Map.player_dir with
| Left -> papy_left
| Right -> papy_right
| Down -> papy_down
| Up -> papy_up
in
C2d.draw_image context papy ~x:papy_x ~y:papy_y
let move 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 Map.get_tile_kind ~x ~y with
| Black | Water -> ()
| Grass -> Map.player_pos := (x, y)
end
else Map.player_dir := dir
2022-12-06 03:08:30 +01:00
2022-12-07 00:10:41 +01:00
let kb_handler ev =
2022-12-08 04:08:27 +01:00
match ev |> Ev.as_type |> Ev.Keyboard.code |> Jstr.to_string with
| "KeyW" | "ArrowUp" -> move Up
| "KeyA" | "ArrowLeft" -> move Left
| "KeyS" | "ArrowDown" -> move Down
| "KeyD" | "ArrowRight" -> move Right
| _s -> ()
2022-12-07 23:14:11 +01:00
let rec game_loop state _timestamp =
2022-12-07 23:14:11 +01:00
draw_map ();
let new_state = state in
G.request_animation_frame (game_loop new_state)
2022-12-08 04:08:27 +01:00
(* type will change later ! *)
let initial_state = ()
2022-12-06 03:30:16 +01:00
let () =
2022-12-08 04:08:27 +01:00
(* init canvas *)
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);
(* bind keys *)
let _e : Ev.listener =
Ev.listen Ev.keydown kb_handler (Window.as_target G.window)
in
(* start game *)
G.request_animation_frame (game_loop initial_state)