forked from zapashcanon/pellest
Brrrr
This commit is contained in:
parent
9833eb520e
commit
82dcc24eed
@ -20,30 +20,32 @@ module Map = struct
|
||||
let get_tile_kind ~x ~y = try m.(x).(y) with Invalid_argument _ -> Black
|
||||
end
|
||||
|
||||
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
|
||||
|
||||
let tile_size = 40
|
||||
|
||||
let width = 835
|
||||
|
||||
let height = 635
|
||||
|
||||
let canvas = Jv.get Jv.global "canvas"
|
||||
let canvas =
|
||||
let el = get_el "canvas" in
|
||||
Canvas.of_el el
|
||||
|
||||
let context = Jv.call canvas "getContext" [| Jv.of_string "2d" |]
|
||||
let context = C2d.get_context canvas
|
||||
|
||||
let init_bg () =
|
||||
Jv.set canvas "width" (Jv.of_int width);
|
||||
Jv.set canvas "height" (Jv.of_int height);
|
||||
Jv.set context "fillStyle" (Jv.of_string "#FF1188");
|
||||
Jv.call context "fillRect"
|
||||
[| Jv.of_int 0; Jv.of_int 0; Jv.of_int width; Jv.of_int height |]
|
||||
|
||||
let window = Jv.get Jv.global "window"
|
||||
|
||||
let () =
|
||||
let (_ : Jv.t) =
|
||||
Jv.call window "addEventListener" [| Jv.of_string "load"; Jv.repr init_bg |]
|
||||
in
|
||||
()
|
||||
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)
|
||||
|
||||
let tiles_per_w = width / tile_size
|
||||
|
||||
@ -53,11 +55,11 @@ let orig_x = (width - (tiles_per_w * tile_size)) / 2
|
||||
|
||||
let orig_y = (height - (tiles_per_h * tile_size)) / 2
|
||||
|
||||
let grass = Jv.get Jv.global "grass"
|
||||
let grass = C2d.image_src_of_el (get_el "grass")
|
||||
|
||||
let papy_bottom = Jv.get Jv.global "papy_bottom"
|
||||
let papy_bottom = C2d.image_src_of_el (get_el "papy_bottom")
|
||||
|
||||
let water = Jv.get Jv.global "water"
|
||||
let water = C2d.image_src_of_el (get_el "water")
|
||||
|
||||
let draw_map () =
|
||||
let player_x, player_y = !Map.player_pos in
|
||||
@ -66,39 +68,25 @@ let draw_map () =
|
||||
let mapx = x + player_x - (tiles_per_w / 2) in
|
||||
for y = 0 to tiles_per_h - 1 do
|
||||
let mapy = y + player_y - (tiles_per_h / 2) in
|
||||
let img =
|
||||
let tile_img =
|
||||
match Map.get_tile_kind ~x:mapx ~y:mapy with
|
||||
| Grass -> grass
|
||||
| Water -> water
|
||||
| Black -> water
|
||||
in
|
||||
let (_ : Jv.t) =
|
||||
Jv.call context "drawImage"
|
||||
[| img
|
||||
; Jv.of_int (orig_x + (x * tile_size))
|
||||
; Jv.of_int (orig_y + (y * tile_size))
|
||||
|]
|
||||
in
|
||||
()
|
||||
C2d.draw_image context tile_img
|
||||
~x:(float_of_int (orig_x + (x * tile_size)))
|
||||
~y:(float_of_int (orig_y + (y * tile_size)))
|
||||
done
|
||||
done;
|
||||
let (_ : Jv.t) =
|
||||
Jv.call context "drawImage"
|
||||
[| papy_bottom; Jv.of_int (width / 2); Jv.of_int (height / 2) |]
|
||||
in
|
||||
()
|
||||
C2d.draw_image context papy_bottom
|
||||
~x:(float_of_int (width / 2))
|
||||
~y:(float_of_int (height / 2))
|
||||
|
||||
let () =
|
||||
let (_ : Jv.t) =
|
||||
Jv.call window "addEventListener"
|
||||
[| Jv.of_string "load"; Jv.repr draw_map |]
|
||||
in
|
||||
()
|
||||
|
||||
let kb_handler e =
|
||||
let kb_handler ev =
|
||||
let x, y = !Map.player_pos in
|
||||
let x, y =
|
||||
match Jv.to_string @@ Jv.get e "key" with
|
||||
match ev |> Ev.as_type |> Ev.Keyboard.key |> Jstr.to_string with
|
||||
| "z" -> (x, y - 1)
|
||||
| "q" -> (x - 1, y)
|
||||
| "s" -> (x, y + 1)
|
||||
@ -112,16 +100,19 @@ let kb_handler e =
|
||||
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 |]
|
||||
let on_window_load f =
|
||||
ignore @@ Ev.listen Ev.load (fun _ev -> f ()) (Window.as_target G.window)
|
||||
in
|
||||
()
|
||||
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
|
||||
|
||||
(*
|
||||
let draw_background () =
|
||||
|
@ -7,7 +7,9 @@ let get request =
|
||||
let title = "Pellest|Login" in
|
||||
let login =
|
||||
let submit = button ~a:[ a_id "submit_login" ] [ txt "submit" ] in
|
||||
let login = input ~a:[ a_id "login"; a_name "login"; a_input_type `Text ] () in
|
||||
let login =
|
||||
input ~a:[ a_id "login"; a_name "login"; a_input_type `Text ] ()
|
||||
in
|
||||
let password =
|
||||
input ~a:[ a_id "password"; a_name "password"; a_input_type `Password ] ()
|
||||
in
|
||||
|
Loading…
x
Reference in New Issue
Block a user