Skip to content

Commit 6413a36

Browse files
committed
moved random functions into an object. for example random_int(0,10) -> random.int(0,10).
1 parent 91481a4 commit 6413a36

File tree

4 files changed

+53
-47
lines changed

4 files changed

+53
-47
lines changed

docs/documentation.html

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -107,9 +107,9 @@
107107
Math
108108
<div class="code_block">lerp()
109109
clamp()
110-
random_int()
111-
random_choice()
112-
random_color()
110+
random.int()
111+
random.choice()
112+
color.random_color()
113113
lists_are_equal()
114114
</div>
115115
</div>

docs/documentation_clear_text.txt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,7 @@ math
128128
```
129129
lerp()
130130
clamp()
131-
random_int()
132-
random_choice()
131+
random.int()
132+
random.choice()
133133
lists_are_equal()
134134
```

sunsnake_compiler.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -453,6 +453,7 @@ math = Math
453453
sqrt = Math.sqrt
454454
sin = Math.sin
455455
pow = Math.pow
456+
math.pi = Math.PI
456457

457458
function round(value, digits=0) {
458459
return Number(Math.round(value+'e'+digits)+'e-'+digits);

taptapir.js

Lines changed: 47 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -226,26 +226,27 @@ function rgb_to_hsv(_rgb_color) {
226226
}
227227

228228
color = {
229-
white: hsv(0, 0, 1),
230-
smoke: hsv(0, 0, 0.96),
231-
light_gray: hsv(0, 0, 0.75),
232-
gray: hsv(0, 0, 0.5),
233-
dark_gray: hsv(0, 0, 0.25),
234-
black: hsv(0, 0, 0),
235-
red: hsv(0, 1, 1),
236-
orange: hsv(30, 1, 1),
237-
yellow: hsv(60, 1, 1),
238-
lime: hsv(90, 1, 1),
239-
green: hsv(120, 1, 1),
240-
turquoise: hsv(150, 1, 1),
241-
cyan: hsv(180, 1, 1),
242-
azure: hsv(210, 1, 1),
243-
blue: hsv(240, 1, 1),
244-
violet: hsv(270, 1, 1),
245-
magenta: hsv(300, 1, 1),
246-
pink: hsv(330, 1, 1),
247-
clear: [0, 0, 0, 0],
248-
text_color: hsv(0,0,.05),
229+
white: hsv(0, 0, 1),
230+
smoke: hsv(0, 0, 0.96),
231+
light_gray: hsv(0, 0, 0.75),
232+
gray: hsv(0, 0, 0.5),
233+
dark_gray: hsv(0, 0, 0.25),
234+
black: hsv(0, 0, 0),
235+
red: hsv(0, 1, 1),
236+
orange: hsv(30, 1, 1),
237+
yellow: hsv(60, 1, 1),
238+
lime: hsv(90, 1, 1),
239+
green: hsv(120, 1, 1),
240+
turquoise: hsv(150, 1, 1),
241+
cyan: hsv(180, 1, 1),
242+
azure: hsv(210, 1, 1),
243+
blue: hsv(240, 1, 1),
244+
violet: hsv(270, 1, 1),
245+
magenta: hsv(300, 1, 1),
246+
pink: hsv(330, 1, 1),
247+
clear: [0, 0, 0, 0],
248+
text_color: hsv(0,0,.05),
249+
random_color: function () {return rgb(Math.random(), Math.random(), Math.random())}
249250
}
250251

251252
function set_window_color(value) {_game_window.style.backgroundColor = value}
@@ -557,7 +558,7 @@ class Entity {
557558
this.model.style.backgroundImage = `url("${TAPTAPIR_TEXTURES[name]}")`
558559
}
559560
else {
560-
this.model.style.backgroundImage = `url("${TAPTAPIR_TEXTURES[name]}?${random_int(0,999)}")` // add random number so the gif restarts when setting .texture again
561+
this.model.style.backgroundImage = `url("${TAPTAPIR_TEXTURES[name]}?${random.int(0,999)}")` // add random number so the gif restarts when setting .texture again
561562
}
562563
this.color = color.clear
563564
return
@@ -582,7 +583,7 @@ class Entity {
582583
}
583584

584585
else if (name.endsWith('.gif')) { // .gif (ensure animation replays on reuse)
585-
this.model.style.backgroundImage = `url("${ASSETS_FOLDER}${name}?${random_int(0,999)}")` // add random number so the gif restarts when setting .texture again
586+
this.model.style.backgroundImage = `url("${ASSETS_FOLDER}${name}?${random.int(0,999)}")` // add random number so the gif restarts when setting .texture again
586587
this.color = color.clear
587588
return
588589
}
@@ -858,29 +859,33 @@ function lerp_angle(start_angle, end_angle, t) {
858859
}
859860

860861
function clamp(num, min, max) {
861-
return num <= min ? min : num >= max ? max : num;
862+
return num <= min ? min : num >= max ? max : num;
862863
}
863-
random_value = Math.random;
864864

865-
function random_int(min, max) {
866-
min = Math.ceil(min);
867-
max = Math.floor(max);
868-
return Math.floor(Math.random() * (max - min + 1)) + min;
869-
}
870-
function random_choice(list) {
871-
return list[random_int(0, len(list)-1)]
872-
}
873-
function random_shuffle(array) {
874-
for (let i = array.length - 1; i > 0; i--) {
875-
const j = Math.floor(Math.random() * (i + 1));
876-
[array[i], array[j]] = [array[j], array[i]]; // Swap elements
865+
random = {
866+
value: Math.random,
867+
random: Math.random,
868+
int: function (min, max) {
869+
min = Math.ceil(min);
870+
max = Math.floor(max);
871+
return Math.floor(Math.random() * (max - min + 1)) + min;
877872
}
878-
return array;
879-
}
880-
function random_color() {
881-
return rgb(Math.random(), Math.random(), Math.random())
873+
,
874+
choice: function (list) {
875+
return list[random.int(0, len(list)-1)]
876+
}
877+
,
878+
shuffle: function (array) {
879+
for (let i = array.length - 1; i > 0; i--) {
880+
const j = Math.floor(Math.random() * (i + 1));
881+
[array[i], array[j]] = [array[j], array[i]]; // Swap elements
882+
}
883+
return array;
884+
}
885+
,
882886
}
883887

888+
884889
function lists_are_equal(array_a, array_b) {
885890
for (let i=0; i<array_a.length; i++) {
886891
if (array_a[i] != array_b[i]) {
@@ -966,8 +971,8 @@ class Camera {
966971

967972
after(i*speed, () => {
968973
this.xy = [
969-
original_xy[0] + (random_int(-1, 1)*.1 * magnitude * direction[0]),
970-
original_xy[1] + (random_int(-1, 1)*.1 * magnitude * direction[1])
974+
original_xy[0] + (random.int(-1, 1)*.1 * magnitude * direction[0]),
975+
original_xy[1] + (random.int(-1, 1)*.1 * magnitude * direction[1])
971976
]
972977
})
973978
after(duration, () => {

0 commit comments

Comments
 (0)