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+
113fn 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+ }
1229}
1330
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"];
1545
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"] ;
1848
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!();
2074 let cells = life_state.cells;
2175
2276
2377 // 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);
2579 let dimensions = settings.physical_grid_dimensions;
26-
80+ let dimension_x = dimensions["_0"];
81+ let dimension_y = dimensions["_1"];
2782
2883 // primitives are passed by value to rhai, keep a hold of old state but turn 255's into 1's
2984 let prev_state = [];
3085 for (v,k) in life_state.cells {
3186 prev_state.push(life_state.cells[k] != 0);
3287 }
3388
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 );
3792 let east = prev_state.get(i + 1);
3893 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);
4398
4499 let neighbours = 0;
45100 if north == () || north {neighbours+=1}
@@ -60,5 +115,18 @@ fn on_update() {
60115 cells[i] = 0;
61116 }
62117 }
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");
63123
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+ }
64132}
0 commit comments