From 2cbea1068280bd3ae1d662cd9ff0a637717ac924 Mon Sep 17 00:00:00 2001 From: Matt Mower Date: Thu, 9 May 2024 12:55:23 +0100 Subject: [PATCH] =?UTF-8?q?v1.2.9=20Added:=20$global=20attribute=20to=20au?= =?UTF-8?q?tomatically=20create=20a=20window.$=20global=20reference=20?= =?UTF-8?q?for=20an=20element=20Fixes:=20bug=20in=20resume=20code=20Fixes:?= =?UTF-8?q?=20when=20doing=20a=20resume,=20merge=20any=20new=20params=20in?= =?UTF-8?q?to=20old=20params=20Removes:=20^v{=E2=80=A6}=20has=20been=20sup?= =?UTF-8?q?planted=20by=20^p{=E2=80=A6}=20Changed:=20Renamed=20and=20reorg?= =?UTF-8?q?anised=20the=20JS=20random=20functions.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../runtime/init_game_objects.js.eex | 7 +- assets/templates/runtime/rez_game.js | 5 +- assets/templates/stdlib.rez.eex | 100 ++++++++++-------- docs/language_reference.adoc | 23 ++-- lib/parser/utility_parsers.ex | 2 + lib/parser/value_parsers.ex | 16 --- mix.exs | 2 +- 7 files changed, 74 insertions(+), 81 deletions(-) diff --git a/assets/templates/runtime/init_game_objects.js.eex b/assets/templates/runtime/init_game_objects.js.eex index 4ebf53f..515441f 100644 --- a/assets/templates/runtime/init_game_objects.js.eex +++ b/assets/templates/runtime/init_game_objects.js.eex @@ -9,9 +9,14 @@ const game = <%= Node.js_initializer(@game) %>; basic_object.game = game; /* Encode game objects */ +let el; <%= for collection <- Game.js_classes_to_init(), {_id, obj} <- Map.get(@game, collection) do %> - game.addGameObject(<%= Node.js_initializer(obj) %>); + el = <%= Node.js_initializer(obj) %>; + game.addGameObject(el); + <%= if NodeHelper.get_attr_value(obj, "$global", false) do %> + window.$<%= obj.id %> = el; + <% end %> <% end %> <% diff --git a/assets/templates/runtime/rez_game.js b/assets/templates/runtime/rez_game.js index e539aa0..6470c4a 100644 --- a/assets/templates/runtime/rez_game.js +++ b/assets/templates/runtime/rez_game.js @@ -570,7 +570,10 @@ RezGame.prototype = { // Let the interlude know we're done this.current_scene.finish(); this.popScene(params); - this.current_scene.getViewLayout().assignParams(params); + + const layout = this.current_scene.getViewLayout(); + // Merge any new params into the existing params + layout.params = {...layout.params, ...params}; this.updateView(); } }, diff --git a/assets/templates/stdlib.rez.eex b/assets/templates/stdlib.rez.eex index 5c4a42e..f0a096f 100644 --- a/assets/templates/stdlib.rez.eex +++ b/assets/templates/stdlib.rez.eex @@ -381,45 +381,6 @@ } } -@patch PATCH_NUMBER_RANGE { - patch: "Number" - function: "range" - impl: function(from, to, step) { - step ??= 1; - - if(from>to) { - [from, to] = [to, from]; - } - - return Array.from( - {length: (to-from)/step+1}, - (_, i) => from + (i * step)); - } -} - -@patch PATCH_NUMBER_RND_BETWEEN { - patch: "Number" - function: "rand_between" - impl: function(min, max) { - if(min > max) { - [min, max] = [max, min] - } - return Math.floor(min + Math.random() * (max - min + 1)); - } -} - -@patch PATCH_NUMBER_RNDF_BETWEEN { - patch: "Number" - function: "randf_between" - impl: function(min, max) { - if(min > max) { - [min, max] = [max, min] - } - - return min + (Math.random() * (max - min)); - } -} - @patch PATCH_NUMBER_ROUNDP { %% https://stackoverflow.com/questions/7342957/how-do-you-round-to-1-decimal-place-in-javascript patch: "Number" @@ -461,7 +422,7 @@ patch: "Number" method: "chance" impl: function() { - return Number.rand_between(1, 100) < this; + return Math.rand_int_between(1, 100) < this; } } @@ -475,6 +436,22 @@ } } +@patch PATCH_MATH_RANGE { + patch: "Math" + function: "range" + impl: function(from, to, step) { + step ??= 1; + + if(from>to) { + [from, to] = [to, from]; + } + + return Array.from( + {length: (to-from)/step+1}, + (_, i) => from + (i * step)); + } +} + @patch PATCH_MATH_DIST_ROUND { %% Because JS only gives us random numbers as floating point we sometimes %% end up having to round when we want an. The question is whether we round @@ -499,9 +476,40 @@ } } -@patch PATCH_MATH_CLRAND_INT { +@patch PATCH_MATH_RND_INT_BETWEEN { + patch: "Math" + function: "rand_int_between" + impl: function(min, max) { + if(min > max) { + [min, max] = [max, min] + } + return Math.floor(min + Math.random() * (max - min + 1)); + } +} + +@patch PATCH_MATH_RAND_F_BETWEEN { + patch: "Math" + function: "rand_f_between" + impl: function(min, max) { + if(min > max) { + [min, max] = [max, min] + } + + return min + (Math.random() * (max - min)); + } +} + +@patch PATCH_MATH_CL_RAND_F_BETWEEN { + patch: "Math" + function: "cl_rand_f_between" + impl: function(min, max) { + return (Math.randf_between(min, max) + Math.randf_between(min, max))/2; + } +} + +@patch PATCH_MATH_CL_RAND_INT { patch: "Math" - function: "clrand_int" + function: "cl_rand_int" impl: function(lim) { const v1 = Math.rand_int(lim); const v2 = Math.rand_int(lim); @@ -509,12 +517,12 @@ } } -@patch PATCH_MATH_CLRANDR_INT { +@patch PATCH_MATH_CLRAND_INT_BETWEEN { patch: "Math" - function: "clrandr_int" + function: "cl_rand_int_between" impl: function(lo, hi) { - const v1 = Number.rnd_between(lo, hi); - const v2 = Number.rnd_between(lo, hi); + const v1 = Math.rand_int_between(lo, hi); + const v2 = Math.rand_int_between(lo, hi); return Math.dist_round((v1+v2)/2); } } diff --git a/docs/language_reference.adoc b/docs/language_reference.adoc index 06dbf86..44642d4 100644 --- a/docs/language_reference.adoc +++ b/docs/language_reference.adoc @@ -316,10 +316,6 @@ Rez defines many attribute types, some simple and some more complicated. The mor |A Javascript expression that is evaluated when the game starts |`^i{Math.rand_int(1,10)}` -|Dynamic Value -|A Javascript expression that is evaluated each time the attribute is referenced -|`^v{this.uses * this.item_value * 25}` - |Dynamic Property |A Javascript function expression that is converted into an object property |`^p{return this.first_name + " " + this.last_name}` @@ -458,6 +454,13 @@ It's not elegant but it's feasible. This will likely get cleaned up in a future === Tracery Grammar +* TODO + +=== Code Block + +A code block uses the form `^{...}` and can contain a legal Javascript expression. + +The code block is implicitly transformed into a function of 1 argument `obj` and that returns the value of the expression. === Dynamic Initializer @@ -477,18 +480,6 @@ Note that this is not a function, the initializer uses the last expression as th } .... -=== Dynamic Value - -A dynamic value uses the form `^v{...}` to create an expression that gets evaluated each time it is referenced. This should be mostly superceded by the use of `^p{...}` to create properties. - -.... -@actor random_npc { - class_name: ^v{class == "g" ? "Gunslinger" : class =="s" ? "Sleuth" : "Crook"} -} -.... - -Note that there is an implicit `return` statement to which this value code is appended. - === Dynamic Property A dynamic property is a property generated from an expression in the form `^p{}` for example: diff --git a/lib/parser/utility_parsers.ex b/lib/parser/utility_parsers.ex index 56678e0..55b1874 100644 --- a/lib/parser/utility_parsers.ex +++ b/lib/parser/utility_parsers.ex @@ -30,6 +30,8 @@ defmodule Rez.Parser.UtilityParsers do def hash(), do: ParserCache.get_parser("hash", fn -> char(?#) end) + def bang(), do: ParserCache.get_parser("bang", fn -> char(?!) end) + def dollar(), do: ParserCache.get_parser("dollar", fn -> char(?$) end) def pipe(), do: ParserCache.get_parser("pipe", fn -> char(?|) end) diff --git a/lib/parser/value_parsers.ex b/lib/parser/value_parsers.ex index 405aaac..e838ef0 100644 --- a/lib/parser/value_parsers.ex +++ b/lib/parser/value_parsers.ex @@ -236,21 +236,6 @@ defmodule Rez.Parser.ValueParsers do end) end - # Dynamic Value - # ^v{...} - def dynamic_value() do - ParserCache.get_parser("dynamic_value", fn -> - sequence( - [ - ignore(caret()), - ignore(char(?v)), - text_delimited_by_nested_parsers(open_brace(), close_brace()) - ], - ast: fn [f] -> {:dynamic_value, f} end - ) - end) - end - # Dynamic Initializer # ^i{...} def dynamic_initializer_value() do @@ -543,7 +528,6 @@ defmodule Rez.Parser.ValueParsers do code_block_value(), function_value(), dynamic_initializer_value(), - dynamic_value(), property_value(), attr_ref_value(), file_value(), diff --git a/mix.exs b/mix.exs index cd81b91..73e897c 100644 --- a/mix.exs +++ b/mix.exs @@ -1,7 +1,7 @@ defmodule Rez.MixProject do use Mix.Project - @version "1.2.8" + @version "1.2.9" def project do [