Skip to content

Commit

Permalink
v1.2.9
Browse files Browse the repository at this point in the history
Added: $global attribute to automatically create a window.$<id> global reference for an element
Fixes: bug in resume code
Fixes: when doing a resume, merge any new params into old params
Removes: ^v{…} has been supplanted by ^p{…}
Changed: Renamed and reorganised the JS random functions.
  • Loading branch information
mmower committed May 9, 2024
1 parent d8d6b7a commit 2cbea10
Show file tree
Hide file tree
Showing 7 changed files with 74 additions and 81 deletions.
7 changes: 6 additions & 1 deletion assets/templates/runtime/init_game_objects.js.eex
Original file line number Diff line number Diff line change
Expand Up @@ -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 %>

<%
Expand Down
5 changes: 4 additions & 1 deletion assets/templates/runtime/rez_game.js
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
},
Expand Down
100 changes: 54 additions & 46 deletions assets/templates/stdlib.rez.eex
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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;
}
}

Expand All @@ -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
Expand All @@ -499,22 +476,53 @@
}
}

@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);
return (v1+v2)/2;
}
}

@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);
}
}
Expand Down
23 changes: 7 additions & 16 deletions docs/language_reference.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -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}`
Expand Down Expand Up @@ -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

Expand All @@ -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:
Expand Down
2 changes: 2 additions & 0 deletions lib/parser/utility_parsers.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
16 changes: 0 additions & 16 deletions lib/parser/value_parsers.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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(),
Expand Down
2 changes: 1 addition & 1 deletion mix.exs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
defmodule Rez.MixProject do
use Mix.Project

@version "1.2.8"
@version "1.2.9"

def project do
[
Expand Down

0 comments on commit 2cbea10

Please sign in to comment.