Skip to content

Commit

Permalink
Add examples on the math lib.
Browse files Browse the repository at this point in the history
  • Loading branch information
dwursteisen committed Jan 16, 2024
1 parent a8c6c14 commit c453418
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 6 deletions.
1 change: 1 addition & 0 deletions tiny-doc/src/docs/asciidoc/sandbox.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -38,3 +38,4 @@ end
- https://minigdx.github.io/tiny/sandbox.html?game=LS0gRnVuY3Rpb24gdG8gY2hlY2sgY29sbGlzaW9uIGJldHdlZW4gdHdvIHJlY3RhbmdsZXMKZnVuY3Rpb24gY2hlY2tDb2xsaXNpb24ocmVjdDEsIHJlY3QyKQogICAgcmV0dXJuIHJlY3QxLnggPCByZWN0Mi54ICsgcmVjdDIud2lkdGggYW5kCiAgICAgICAgICAgcmVjdDEueCArIHJlY3QxLndpZHRoID4gcmVjdDIueCBhbmQKICAgICAgICAgICByZWN0MS55IDwgcmVjdDIueSArIHJlY3QyLmhlaWdodCBhbmQKICAgICAgICAgICByZWN0MS55ICsgcmVjdDEuaGVpZ2h0ID4gcmVjdDIueQplbmQKCi0tIEZ1bmN0aW9uIHRvIHJlc29sdmUgY29sbGlzaW9uIGFuZCBzbGlkZQpmdW5jdGlvbiBjb2xsaWRlQW5kU2xpZGUob2JqZWN0LCBvYnN0YWNsZSkKICAgIGlmIGNoZWNrQ29sbGlzaW9uKG9iamVjdCwgb2JzdGFjbGUpIHRoZW4KICAgICAgICBsb2NhbCBvdmVybGFwWCA9IG1hdGgubWluKG9iamVjdC54ICsgb2JqZWN0LndpZHRoLCBvYnN0YWNsZS54ICsgb2JzdGFjbGUud2lkdGgpIC0gbWF0aC5tYXgob2JqZWN0LngsIG9ic3RhY2xlLngpCiAgICAgICAgbG9jYWwgb3ZlcmxhcFkgPSBtYXRoLm1pbihvYmplY3QueSArIG9iamVjdC5oZWlnaHQsIG9ic3RhY2xlLnkgKyBvYnN0YWNsZS5oZWlnaHQpIC0gbWF0aC5tYXgob2JqZWN0LnksIG9ic3RhY2xlLnkpCgogICAgICAgIGlmIG92ZXJsYXBYIDwgb3ZlcmxhcFkgdGhlbgogICAgICAgICAgICBpZiBvYmplY3QueCA8IG9ic3RhY2xlLnggdGhlbgogICAgICAgICAgICAgICAgb2JqZWN0LnggPSBvYmplY3QueCAtIG92ZXJsYXBYCiAgICAgICAgICAgIGVsc2UKICAgICAgICAgICAgICAgIG9iamVjdC54ID0gb2JqZWN0LnggKyBvdmVybGFwWAogICAgICAgICAgICBlbmQKICAgICAgICBlbHNlCiAgICAgICAgICAgIGlmIG9iamVjdC55IDwgb2JzdGFjbGUueSB0aGVuCiAgICAgICAgICAgICAgICBvYmplY3QueSA9IG9iamVjdC55IC0gb3ZlcmxhcFkKICAgICAgICAgICAgZWxzZQogICAgICAgICAgICAgICAgb2JqZWN0LnkgPSBvYmplY3QueSArIG92ZXJsYXBZCiAgICAgICAgICAgIGVuZAogICAgICAgIGVuZAogICAgZW5kCmVuZAoKcGxheWVyID0ge3ggPSA1MCwgeSA9IDUwLCB3aWR0aCA9IDIwLCBoZWlnaHQgPSAyMH0Kb2JzdGFjbGVzID0gewogICB7eCA9IDYwLCB5ID0gNDAsIHdpZHRoID0gMzAsIGhlaWdodCA9IDMwfSwKICB7eCA9IDY1LCB5ID0gNjAsIHdpZHRoID0gNDAsIGhlaWdodCA9IDMwfQp9CgpmdW5jdGlvbiBfdXBkYXRlKCkKICAgIGlmKGN0cmwucHJlc3Npbmcoa2V5cy5sZWZ0KSkgdGhlbgogICAgICAgICBwbGF5ZXIueCA9IHBsYXllci54IC0gMQogICAgZWxzZWlmIChjdHJsLnByZXNzaW5nKGtleXMucmlnaHQpKSB0aGVuCiAgICAgICAgIHBsYXllci54ID0gcGxheWVyLnggKyAxCiAgICBlbmQKCiAgICBpZihjdHJsLnByZXNzaW5nKGtleXMudXApKSB0aGVuCiAgICAgICAgIHBsYXllci55ID0gcGxheWVyLnkgLSAxCiAgICBlbHNlaWYgKGN0cmwucHJlc3Npbmcoa2V5cy5kb3duKSkgdGhlbgogICAgICAgICBwbGF5ZXIueSA9IHBsYXllci55ICsgMQogICAgZW5kCgogICAgZm9yIG9ic3RhY2xlIGluIGFsbChvYnN0YWNsZXMpIGRvCiAgICAgICBjb2xsaWRlQW5kU2xpZGUocGxheWVyLCBvYnN0YWNsZSkKICAgIGVuZAplbmQKCgpmdW5jdGlvbiBfZHJhdygpCiAgICBnZnguY2xzKCkKICAgIGZvciBvYnN0YWNsZSBpbiBhbGwob2JzdGFjbGVzKSBkbwogICAgICAgIHNoYXBlLnJlY3Qob2JzdGFjbGUueCwgb2JzdGFjbGUueSwgb2JzdGFjbGUud2lkdGgsIG9ic3RhY2xlLmhlaWdodCwgOSkKICAgIGVuZAoKICAgIHNoYXBlLnJlY3QocGxheWVyLngsIHBsYXllci55LCBwbGF5ZXIud2lkdGgsIHBsYXllci5oZWlnaHQsIDgpCmVuZA==[AABB Collision]
- https://minigdx.github.io/tiny/sandbox.html?game=LS0gVXBkYXRlIHRoZSBjb2RlIHRvIHVwZGF0ZSB0aGUgZ2FtZSEKbG9jYWwgUGxheWVyID0gewogICB4ID0gMTI4IC0gMTYsCiAgIHkgPSAxMjggLSAxNiwKfQpmdW5jdGlvbiBfaW5pdCgpCiAgcGxheWVyID0gbmV3KFBsYXllcikKZW5kCgpmdW5jdGlvbiBfdXBkYXRlKCkKICBpZiBjdHJsLnByZXNzaW5nKGtleXMubGVmdCkgdGhlbgogICAgIHBsYXllci54ID0gbWF0aC5tYXgoMCwgcGxheWVyLnggLSAxKQogIGVsc2VpZiBjdHJsLnByZXNzaW5nKGtleXMucmlnaHQpIHRoZW4KICAgICAgcGxheWVyLnggPSBtYXRoLm1pbigyNDAsIHBsYXllci54ICsgMSkKICBlbmQKCmlmIGN0cmwucHJlc3Npbmcoa2V5cy51cCkgdGhlbgogICAgIHBsYXllci55ID0gbWF0aC5tYXgoMCwgcGxheWVyLnkgLSAxKQogIGVsc2VpZiBjdHJsLnByZXNzaW5nKGtleXMuZG93bikgdGhlbgogICAgICBwbGF5ZXIueSA9IG1hdGgubWluKDI0MCwgcGxheWVyLnkgKyAxKQogIGVuZAplbmQKCmZ1bmN0aW9uIF9kcmF3KCkKICAgZ2Z4LmNscygpCgogICBzaGFwZS5ncmFkaWVudCgwLCAwLCAyNTYsIDI1NiwgMywgNCkKCiAgIHNwci5kcmF3KDk2LCBwbGF5ZXIueCwgcGxheWVyLnkpCiAgIAplbmQK[Control sprite move]
- https://minigdx.github.io/tiny/sandbox.html?game=bG9jYWwgUGxheWVyID0gewogIHggPSAxMjggLSAxNiwKICB5ID0gMTI4IC0gMTYsCiAgZHggPSAwLAogIGR5ID0gMAp9CiAgCmZ1bmN0aW9uIF9pbml0KCkKICAgbG9jYWwgc2VlZCA9IG1hdGgucm5kKCkKICAgcGxheWVyID0gbmV3KFBsYXllciwgewogICAgICAgZHggPSBtYXRoLmNvcyhzZWVkKSAqIDMsIAogICAgICAgZHkgPSBtYXRoLnNpbihzZWVkKSAqIDMKICAgfSkKZW5kCgpmdW5jdGlvbiBfdXBkYXRlKCkKICAgaWYgY3RybC5wcmVzc2VkKGtleXMuc3BhY2UpIHRoZW4KICAgICBfaW5pdCgpCiAgIGVuZAogICBwbGF5ZXIueCA9IHBsYXllci54ICsgcGxheWVyLmR4CiAgIHBsYXllci55ID0gcGxheWVyLnkgKyBwbGF5ZXIuZHkKICAgaWYgcGxheWVyLnggPiAyNDAgb3IgcGxheWVyLnggPCAwIHRoZW4KICAgICAgcGxheWVyLmR4ID0gcGxheWVyLmR4ICogLTEKICAgZW5kCiAgIGlmIHBsYXllci55ID4gMjQwIG9yIHBsYXllci55IDwgMCB0aGVuCiAgICAgIHBsYXllci5keSA9IHBsYXllci5keSAqIC0xCiAgIGVuZAogICBwbGF5ZXIueCA9IG1hdGgubWF4KDAsIG1hdGgubWluKHBsYXllci54LCAyNDApKQogICBwbGF5ZXIueSA9IG1hdGgubWF4KDAsIG1hdGgubWluKHBsYXllci55LCAyNDApKQplbmQ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xKQoKICAgICAgICBsZXR0ZXIgPSBtYXRoLm1heCgxLCAobGV0dGVyICsgMSkgJSAoI3R4dCArIDEpKQogICAgZW5kCmVuZAoK[Perlin and dither]
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package com.github.minigdx.tiny.lua

//language=Lua
const val MATH_PERLIN_EXAMPLE = """
function _draw()
gfx.cls(8)
gfx.dither(0x0001)
local x = math.perlin(0.1, 0.2, tiny.frame / 100)
local y = math.perlin(0.4, 0.5, tiny.frame / 100)
shape.circlef(x * 256, y * 256, 64, 7)
end
"""

//language=Lua
const val MATH_RND_EXAMPLE = """
local value = {}
function _update()
table.insert(value, math.rnd(126))
if(#value > 50) then
table.remove(value, #value)
end
end
function _draw()
gfx.cls()
local y = 0
for v in all(value) do
print("rnd: "..v, 4, y)
y = y + 6
end
end
"""

//language=Lua
const val MATH_SIGN_EXAMPLE = """
function _draw()
gfx.cls()
local cos = math.cos(tiny.t)
print("cos: "..cos)
print("sign: "..math.sign(cos), 0, 8)
shape.line(128, 128, 128 + cos * 128, 128, 9)
end
"""
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ class MathLib : org.luaj.vm2.lib.MathLib() {
return math
}

@TinyFunction("Return the sign of the number: -1 if negative. 1 otherwise.")
@TinyFunction("Return the sign of the number: -1 if negative. 1 otherwise.", example = MATH_SIGN_EXAMPLE)
internal inner class sign : OneArgFunction() {

@TinyCall("Return the sign of the number.")
Expand Down Expand Up @@ -112,7 +112,7 @@ class MathLib : org.luaj.vm2.lib.MathLib() {
}
}

@TinyFunction("Generate random values")
@TinyFunction("Generate random values", example = MATH_RND_EXAMPLE)
internal inner class rnd : TwoArgFunction() {
@TinyCall("Generate a random int (negative or positive value)")
override fun call(): LuaValue {
Expand Down Expand Up @@ -172,7 +172,7 @@ class MathLib : org.luaj.vm2.lib.MathLib() {
}
}

@TinyFunction("Perlin noise. The random generated value is between 0 and 1.")
@TinyFunction("Perlin noise. The random generated value is between 0.0 and 1.0.", example = MATH_PERLIN_EXAMPLE)
inner class perlin(seed: Long) : ThreeArgFunction() {

private val permutation: MutableList<Int>
Expand Down Expand Up @@ -240,9 +240,9 @@ class MathLib : org.luaj.vm2.lib.MathLib() {

@TinyCall("Generate a random value regarding the parameters x,y and z.")
override fun call(
@TinyArg("x") arg1: LuaValue,
@TinyArg("y") arg2: LuaValue,
@TinyArg("z") arg3: LuaValue,
@TinyArg("x", description = "A value between 0.0 and 1.0.") arg1: LuaValue,
@TinyArg("y", description = "A value between 0.0 and 1.0.") arg2: LuaValue,
@TinyArg("z", description = "A value between 0.0 and 1.0.") arg3: LuaValue,
): LuaValue {
return valueOf(noise(arg1.todouble(), arg2.todouble(), arg3.todouble()))
}
Expand Down

0 comments on commit c453418

Please sign in to comment.