wip: state server side; websocket
This commit is contained in:
parent
3c6a373dc9
commit
5f1d29bda3
11 changed files with 169 additions and 44 deletions
|
|
@ -1,5 +1,7 @@
|
|||
open Brr
|
||||
open Brr_io
|
||||
open Brr_canvas
|
||||
open Shared
|
||||
|
||||
module G = struct
|
||||
include Brr.G
|
||||
|
|
@ -8,36 +10,6 @@ module G = struct
|
|||
(ignore : int -> unit) @@ Brr.G.request_animation_frame f
|
||||
end
|
||||
|
||||
let () = Random.self_init ()
|
||||
|
||||
type dir =
|
||||
| Left
|
||||
| Right
|
||||
| Down
|
||||
| Up
|
||||
|
||||
module Map = struct
|
||||
type background =
|
||||
| Grass
|
||||
| Water
|
||||
| Black
|
||||
|
||||
let width = 1000
|
||||
|
||||
let height = 1000
|
||||
|
||||
let player_pos = ref (20, 3)
|
||||
|
||||
let player_dir = ref Down
|
||||
|
||||
let m =
|
||||
Array.init width (fun _x ->
|
||||
Array.init height (fun _y ->
|
||||
if Random.int 1000 <= 42 then Water else Grass ) )
|
||||
|
||||
let get_tile_kind ~x ~y = try m.(x).(y) with Invalid_argument _ -> Black
|
||||
end
|
||||
|
||||
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)
|
||||
|
|
@ -79,6 +51,11 @@ let papy_up = C2d.image_src_of_el (get_el "papy_up")
|
|||
|
||||
let water = C2d.image_src_of_el (get_el "water")
|
||||
|
||||
let map =
|
||||
(* TODO receive map / state *)
|
||||
(* dummy map; should ask for map to server *)
|
||||
ref (Map.init ())
|
||||
|
||||
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
|
||||
|
|
@ -91,7 +68,7 @@ let draw_map =
|
|||
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
|
||||
match Map.get_tile_kind ~x:map_x ~y:map_y !map with
|
||||
| Grass -> grass
|
||||
| Water -> water
|
||||
| Black -> water
|
||||
|
|
@ -118,7 +95,7 @@ let move dir =
|
|||
| Down -> (x, y + 1)
|
||||
| Up -> (x, y - 1)
|
||||
in
|
||||
match Map.get_tile_kind ~x ~y with
|
||||
match Map.get_tile_kind ~x ~y !map with
|
||||
| Black | Water -> ()
|
||||
| Grass -> Map.player_pos := (x, y)
|
||||
end
|
||||
|
|
@ -130,6 +107,7 @@ let kb_handler ev =
|
|||
| "KeyA" | "ArrowLeft" -> move Left
|
||||
| "KeyS" | "ArrowDown" -> move Down
|
||||
| "KeyD" | "ArrowRight" -> move Right
|
||||
| "KeyM" -> Ws_client.send State.Meditate
|
||||
| _s -> ()
|
||||
|
||||
let rec game_loop state _timestamp =
|
||||
|
|
@ -137,9 +115,6 @@ let rec game_loop state _timestamp =
|
|||
let new_state = state in
|
||||
G.request_animation_frame (game_loop new_state)
|
||||
|
||||
(* type will change later ! *)
|
||||
let initial_state = ()
|
||||
|
||||
let () =
|
||||
(* init canvas *)
|
||||
Canvas.set_w canvas width;
|
||||
|
|
@ -151,5 +126,16 @@ let () =
|
|||
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)
|
||||
|
||||
(* get state from server*)
|
||||
let initial_state_fut = Ev.next Message.Ev.message Ws_client.ws_target in
|
||||
|
||||
Fut.await initial_state_fut (fun msg ->
|
||||
let initial_state = Ws_client.to_server_msg msg in
|
||||
let state_ref = ref initial_state in
|
||||
(* attach message listener to update state *)
|
||||
Ws_client.on_update_state_message (fun received ->
|
||||
state_ref := received;
|
||||
Format.printf "YOUR MANA IS: %d@." !state_ref.mana );
|
||||
(* start game *)
|
||||
G.request_animation_frame (game_loop state_ref) )
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue