forked from reasonml-community/Mareo
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Main.fs
52 lines (44 loc) · 1.39 KB
/
Main.fs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
module Main
open Actors
open Sprite
open Object
open Director
open Fable.Import.Browser
module Pg = ProceduralGenerator
(*Canvas is chosen from the index.html file. The context is obtained from
*the canvas. Listeners are added. A level is generated and the general
*update_loop method is called to make the level playable.*)
let private load() =
let level_width = 2400.
let level_height = 256.
(* Random.self_init(); *)
let canvas_id = "canvas"
let canvas = document.getElementById(canvas_id) :?> HTMLCanvasElement
let context = canvas.getContext_2d()
document.addEventListener_keydown (fun e -> Director.keydown e)
document.addEventListener_keyup (fun e -> Director.keyup e)
Pg.init()
Director.update_loop canvas (Pg.generate level_width level_height context) (level_width,level_height)
|> ignore
//printfn "asd";
(*Used for concurrency issues.*)
let private preload() =
let root_dir = "sprites/"
let loadCount = ref 0
let imgsToLoad = 4
let inc_counter() =
loadCount := !loadCount + 1;
if !loadCount = imgsToLoad then load() else ()
null
[ "blocks.png";"items.png";"enemies.png";"mario-small.png" ]
|> List.iter (fun img_src ->
let img_src = root_dir + img_src in
let img = document.createElement_img() in
img.src <- img_src
img.addEventListener_load(fun ev -> inc_counter())
)
()
window.addEventListener_load(fun _ ->
preload()
null
)