1
+ info.call("Rhai: the game_of_life.rhai script just got loaded");
2
+
3
+
4
+ fn fetch_life_state() {
5
+ let LifeState = world.get_type_by_name.call("LifeState");
6
+ let Settings = world.get_type_by_name.call("Settings");
7
+ for (v,i) in world.query.call().component.call(LifeState).build.call(){
8
+ return v.components.call()[0]
9
+ }
10
+ }
11
+
12
+
1
13
fn on_script_loaded() {
2
- world.info("Game of Life script loaded");
3
- // let LifeState = world.get_type_by_name("LifeState");
4
- // let life_state = world.get_component(entity,LifeState);
5
- // let cells = life_state.cells;
6
-
7
- // // set some cells alive
8
- // for x in 1..10000 {
9
- // let index = rand(0..cells.len());
10
- // cells[index] = 255;
11
- // }
14
+ let LifeState = world.get_type_by_name.call("LifeState");
15
+ let Settings = world.get_type_by_name.call("Settings");
16
+
17
+ info.call("Rhai: Hello! I am initiating the game of life simulation state with randomness!");
18
+ info.call("Rhai: Click on the screen to set cells alive after running the `gol start` command");
19
+
20
+ let life_state = fetch_life_state!();
21
+ let cells = life_state.cells;
22
+ let cells_len = cells.len.call();
23
+ let x = 0;
24
+ while x < 1000 {
25
+ let index = to_int(floor(rand.call()*cells_len)) ;
26
+ cells[index] = 255;
27
+ x += 1;
28
+ }
12
29
}
13
30
14
- fn on_update() {
31
+ fn on_click(x,y) {
32
+ let Settings = world.get_type_by_name.call("Settings");
33
+ let LifeState = world.get_type_by_name.call("LifeState");
34
+
35
+ info.call("Rhai: Clicked at x: "+ x + ", y: " + y );
36
+ let life_state = fetch_life_state!();
37
+ let cells = life_state.cells;
38
+
39
+ let settings = world.get_resource.call(Settings);
40
+ let dimensions = settings.physical_grid_dimensions;
41
+ let screen = settings.display_grid_dimensions;
42
+
43
+ let dimension_x = dimensions["_0"];
44
+ let dimension_y = dimensions["_1"];
15
45
16
- let LifeState = world.get_type_by_name("LifeState") ;
17
- let Settings = world.get_type_by_name("Settings") ;
46
+ let screen_x = screen["_0"] ;
47
+ let screen_y = screen["_1"] ;
18
48
19
- let life_state = world.get_component(entity,LifeState);
49
+ let cell_width = screen_x / dimension_x;
50
+ let cell_height = screen_y / dimension_y;
51
+
52
+ let cell_x = to_int(x / cell_width);
53
+ let cell_y = to_int(y / cell_height);
54
+
55
+ let index = cell_y * dimension_x + cell_x;
56
+ let cell_offsets_x = [0, 1, 0, 1, -1, 0, -1, 1, -1];
57
+ let cell_offsets_y = [0, 0, 1, 1, 0, -1, -1, -1, 1];
58
+ for (v,i) in cell_offsets_x {
59
+ let offset_x = cell_offsets_x[i];
60
+ let offset_y = cell_offsets_y[i];
61
+ let new_index = index + offset_x + offset_y * dimension_x;
62
+ if new_index >= 0 && new_index < (dimension_x * dimension_y) {
63
+ cells[new_index] = 255;
64
+ }
65
+ }
66
+
67
+ }
68
+
69
+ fn on_update() {
70
+ let LifeState = world.get_type_by_name.call("LifeState");
71
+ let Settings = world.get_type_by_name.call("Settings");
72
+
73
+ let life_state = fetch_life_state!();
20
74
let cells = life_state.cells;
21
75
22
76
23
77
// note that here we do not make use of RhaiProxyable and just go off pure reflection
24
- let settings = world.get_resource(Settings);
78
+ let settings = world.get_resource.call (Settings);
25
79
let dimensions = settings.physical_grid_dimensions;
26
-
80
+ let dimension_x = dimensions["_0"];
81
+ let dimension_y = dimensions["_1"];
27
82
28
83
// primitives are passed by value to rhai, keep a hold of old state but turn 255's into 1's
29
84
let prev_state = [];
30
85
for (v,k) in life_state.cells {
31
86
prev_state.push(life_state.cells[k] != 0);
32
87
}
33
88
34
- for i in 0..(dimensions[0] * dimensions[1] ) {
35
- let north = prev_state.get(i - dimensions[0] );
36
- let south = prev_state.get(i + dimensions[0] );
89
+ for i in 0..(dimension_x * dimension_y ) {
90
+ let north = prev_state.get(i - dimension_x );
91
+ let south = prev_state.get(i + dimension_x );
37
92
let east = prev_state.get(i + 1);
38
93
let west = prev_state.get(i - 1);
39
- let northeast = prev_state.get(i - dimensions[0] + 1);
40
- let southeast = prev_state.get(i + dimensions[0] + 1);
41
- let northwest = prev_state.get(i - dimensions[0] - 1);
42
- let southwest = prev_state.get(i + dimensions[0] - 1);
94
+ let northeast = prev_state.get(i - dimension_x + 1);
95
+ let southeast = prev_state.get(i + dimension_x + 1);
96
+ let northwest = prev_state.get(i - dimension_x - 1);
97
+ let southwest = prev_state.get(i + dimension_x - 1);
43
98
44
99
let neighbours = 0;
45
100
if north == () || north {neighbours+=1}
@@ -60,5 +115,18 @@ fn on_update() {
60
115
cells[i] = 0;
61
116
}
62
117
}
118
+ }
119
+
120
+ fn on_script_unloaded() {
121
+ let LifeState = world.get_type_by_name.call("LifeState");
122
+ let Settings = world.get_type_by_name.call("Settings");
63
123
124
+ info.call("Rhai: I am being unloaded, goodbye!");
125
+
126
+ // set state to 0's
127
+ let life_state = fetch_life_state!();
128
+ let cells = life_state.cells;
129
+ for i in 0..cells.len.call() {
130
+ cells[i] = 0;
131
+ }
64
132
}
0 commit comments