change scene sexp syntax for sprites

This commit is contained in:
tali 2024-01-20 15:36:43 -05:00
parent d7209eebec
commit 65dd5eb0b8
2 changed files with 38 additions and 42 deletions

View File

@ -3,44 +3,45 @@
(object "playfield" (object "playfield"
(transform (tx -160) (ty 320)) (transform (tx -160) (ty 320))
(sprite hud grid) (sprites hud grid field)
(sprite hud field)
(object blocks (object blocks
(sprite blocks L (pos 0 0)) (sprites blocks
(sprite blocks L (pos 32 0)) (L 0 0)
(sprite blocks L (pos 64 0)) (L 32 0)
(sprite blocks L (pos 64 -32)))) (L 64 0)
(L 64 -32))))
(object "hold" (object "hold"
(transform (tx -272) (ty -272)) (transform (tx -272) (ty -272))
(sprite hud hold)) (sprites hud hold))
(object "next" (object "next"
(transform (tx 272) (ty -272)) (transform (tx 272) (ty -272))
(sprite hud next)) (sprites hud next))
(object "left_labels" (object "left_labels"
(transform (tx -172) (ty 320)) (transform (tx -172) (ty 320))
(sprite hud pps (pos -12 -148)) (sprites hud
(sprite hud kpp (pos -12 -116)) (pps -12 -148)
(kpp -12 -116)
(num 0 -73)
(time 0 -25))
(sprite hud num (pos 0 -73))
(object "pcnt" (object "pcnt"
(label mono-sm "0")
(transform (ty -50) (sx 24) (sy 24) (transform (ty -50) (sx 24) (sy 24)
; TODO: text align right ; TODO: text align right
(tx -14))) (tx -14))
(label mono-sm "0"))
(sprite hud time (pos 0 -25))
(object "time" (object "time"
(label mono-sm "0:00.00")
(transform (ty -2) (sx 24) (sy 24) (transform (ty -2) (sx 24) (sy 24)
; TODO: text align right ; TODO: text align right
(tx -100)))) (tx -100))
(label mono-sm "0:00.00")))
(object "right_labels" (object "right_labels"
(transform (tx 272) (ty 320)) (transform (tx 272) (ty 320))
(sprite hud lines_rem (pos 0 -20)) (sprites hud (lines_rem 0 -20))
(object "goal" (object "goal"
(label roman-md "40") (label roman-md "40")
(transform (ty -56) (sx 80) (sy 80) (transform (ty -56) (sx 80) (sy 80)

View File

@ -191,7 +191,6 @@ module Scene = struct
and obj = { and obj = {
name : string option; name : string option;
transform : Transform_graph.node; transform : Transform_graph.node;
sprites : Sprite_graph.node list;
} }
let get t name = try Hashtbl.find t.objs name let get t name = try Hashtbl.find t.objs name
@ -212,25 +211,23 @@ module Scene = struct
| sexp -> | sexp ->
of_sexp_error "bad argument to transform" sexp of_sexp_error "bad argument to transform" sexp
let parse_sprite_arg ~(pos : vec2) = function let parse_sprite_arg ~sg ~tf ~map = function
| Sexp.List [Atom "pos"; x; y] -> | Sexp.List [Atom sprite; x; y] ->
Vec2.set pos let sprite = Sprite_graph.get_sprite sg map sprite in
~x:(float_of_sexp x) let pos = vec2 (float_of_sexp x) (float_of_sexp y) in
~y:(float_of_sexp y) Sprite_graph.add_sprite sg ~tf ~pos ~sprite |> ignore
(* | Sexp.List [Atom "tint"; rgb] -> *) | Sexp.List [Atom sprite] | Atom sprite ->
(* Color.set_rgb24 tint *)
(* (Sexplib.Conv.int_of_sexp rgb) *)
| sexp ->
of_sexp_error "bad argument to sprite" sexp
let parse_sprite ~sg ~tf = function
| Sexp.List (Atom "sprite" :: Atom map :: Atom sprite :: args) ->
let sprite = Sprite_graph.get_sprite sg map sprite in let sprite = Sprite_graph.get_sprite sg map sprite in
let pos = vec2 0.0 0.0 in let pos = vec2 0.0 0.0 in
List.iter (parse_sprite_arg ~pos) args; Sprite_graph.add_sprite sg ~tf ~pos ~sprite |> ignore
Sprite_graph.add_sprite sg ~tf ~pos ~sprite
| sexp -> | sexp ->
of_sexp_error "invalid sprite" sexp of_sexp_error "bad sprite argument" sexp
let parse_sprites ~sg ~tf = function
| Sexp.List (Atom "sprites" :: Atom map :: args) ->
List.iter (parse_sprite_arg ~sg ~tf ~map) args;
| sexp ->
of_sexp_error "invalid sprites" sexp
let parse_label_arg ~(fg : color) = function let parse_label_arg ~(fg : color) = function
| Sexp.List [Atom "fg"; rgb] -> | Sexp.List [Atom "fg"; rgb] ->
@ -243,7 +240,7 @@ module Scene = struct
let font = Sprite_graph.get_font sg font in let font = Sprite_graph.get_font sg font in
let fg = rgb24 0xffffff in let fg = rgb24 0xffffff in
List.iter (parse_label_arg ~fg) args; List.iter (parse_label_arg ~fg) args;
Sprite_graph.add_label sg ~tf ~font ~text ~fg Sprite_graph.add_label sg ~tf ~font ~text ~fg |> ignore
| sexp -> | sexp ->
of_sexp_error "invalid label" sexp of_sexp_error "invalid label" sexp
@ -255,7 +252,7 @@ module Scene = struct
| Some parent -> parent.transform | Some parent -> parent.transform
in in
let transform = Transform_graph.add tg ptgn in let transform = Transform_graph.add tg ptgn in
let obj = { name = None; transform; sprites = [] } in let obj = { name = None; transform } in
let obj = parse_obj_args t obj args ~tg ~sg in let obj = parse_obj_args t obj args ~tg ~sg in
Option.iter Option.iter
(fun name -> Hashtbl.replace t.objs name obj) (fun name -> Hashtbl.replace t.objs name obj)
@ -265,7 +262,7 @@ module Scene = struct
of_sexp_error "invalid object" sexp of_sexp_error "invalid object" sexp
and parse_obj_args t obj ~tg ~sg = function and parse_obj_args t obj ~tg ~sg = function
| [] -> { obj with sprites = List.rev obj.sprites } | [] -> obj
| arg :: args -> | arg :: args ->
match arg with match arg with
| Atom name -> | Atom name ->
@ -279,16 +276,14 @@ module Scene = struct
trace (fun m -> m "parse_obj_args: tf=%a" Mat2A.pp tf); trace (fun m -> m "parse_obj_args: tf=%a" Mat2A.pp tf);
parse_obj_args t obj args ~tg ~sg parse_obj_args t obj args ~tg ~sg
| Sexp.List (Atom "sprite" :: _) -> | Sexp.List (Atom "sprites" :: _) ->
let tf = Transform_graph.world obj.transform in let tf = Transform_graph.world obj.transform in
let spr = parse_sprite arg ~sg ~tf in parse_sprites arg ~sg ~tf;
let obj = { obj with sprites = spr :: obj.sprites } in
parse_obj_args t obj args ~tg ~sg parse_obj_args t obj args ~tg ~sg
| Sexp.List (Atom "label" :: _) -> | Sexp.List (Atom "label" :: _) ->
let tf = Transform_graph.world obj.transform in let tf = Transform_graph.world obj.transform in
let lab = parse_label arg ~sg ~tf in parse_label arg ~sg ~tf;
let obj = { obj with sprites = lab :: obj.sprites } in
parse_obj_args t obj args ~tg ~sg parse_obj_args t obj args ~tg ~sg
| Sexp.List (Atom "object" :: _) -> | Sexp.List (Atom "object" :: _) ->