Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Custom sound #11

Merged
merged 4 commits into from
Mar 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file modified tiny-cli/src/jvmMain/resources/sfx/tiny-export.zip
Binary file not shown.
Original file line number Diff line number Diff line change
Expand Up @@ -120,15 +120,15 @@ class AsciidocLibSection(val title: String?) {
)
}

fun example(lua: String?, spritePath: String? = null, levelPath: String? = null) {
fun example(functionName: String, lua: String?, spritePath: String? = null, levelPath: String? = null) {
if (lua == null) return
val spr = spritePath?.let { """sprite="$it"""" } ?: ""
val lvl = levelPath?.let { """level="$it"""" } ?: ""
paragraph(
"""
>++++
><tiny-editor style="display: none;" $spr $lvl>
>$lua
>${lua.replace("##function##", functionName)}
></tiny-editor>
>++++
""".trimMargin(">"),
Expand Down Expand Up @@ -365,6 +365,7 @@ class KspProcessor(
lib(prefix) {
paragraph(variable.description)
example(
prefix,
"""
function _update()
gfx.cls()
Expand Down Expand Up @@ -403,7 +404,7 @@ class KspProcessor(
)
}

example(func.example, func.spritePath, func.levelPath)
example(func.name, func.example, func.spritePath, func.levelPath)
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion tiny-doc/src/docs/asciidoc/sample/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<meta charset="UTF-8">
<title>Tiny - Platform game</title>
</head>
<body>
<body style="margin: 0">
<div style="text-align:center;">
<tiny-game name="Platform game" mouse="false" width="256" height="256" zoom="2" spritew="16" spriteh="16">
<!-- GAME_SCRIPT -->
Expand Down
2 changes: 1 addition & 1 deletion tiny-doc/src/docs/asciidoc/sample/tiny-engine.js

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -208,16 +208,12 @@ class TouchManager(lastKeyCode: KeyCode) {
private fun processTouchEvent(event: InternalTouchEvent) {
when (event.way) {
InternalTouchEventWay.DOWN -> {
justTouch[event.touchSignal.ordinal] = event.position
touch[event.touchSignal.ordinal] = event.position
justTouch[event.touchSignal.ordinal] = event.position.copy()
touch[event.touchSignal.ordinal] = event.position.copy()
lastTouch.set(event.position)
}

InternalTouchEventWay.MOVE -> {
touch[event.touchSignal.ordinal]?.run {
x = event.position.x
y = event.position.y
}
lastTouch.set(event.position)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ class CtrlLib(
@TinyFunction(
"Return true if the key was pressed during the last frame. " +
"If you need to check that the key is still pressed, see `ctrl.pressing` instead.",
example = CTRL_PRESSING_EXAMPLE,
)
inner class pressed : OneArgFunction() {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,61 +12,67 @@ end

//language=Lua
const val CTRL_PRESSING_EXAMPLE = """
function _init()
circle = {
x = 256 * 0.5,
y = 256 * 0.5,
radius = 10
}
end
local percent_a = 1
local percent_b = 1

function _update()
-- check keys for horizontal move
if (ctrl.pressing(keys.left)) then
circle.x = math.max(circle.x - 1, 0)
elseif (ctrl.pressing(keys.right)) then
circle.x = math.min(circle.x + 1, 256)
end
percent_a = math.min(percent_a + 0.05, 1)
percent_b = math.min(percent_b + 0.05, 1)

-- check keys for vertical move
if (ctrl.pressing(keys.up)) then
circle.y = math.max(circle.y - 1, 0)
elseif (ctrl.pressing(keys.down)) then
circle.y = math.min(circle.y + 1, 256)
if ctrl.pressed(keys.space) then
percent_a = 0
end

-- check keys for update circle size
if (ctrl.pressing(keys.space)) then
circle.radius = math.min(circle.radius + 1, 256)
elseif (ctrl.pressing(keys.enter)) then
circle.radius = math.max(circle.radius - 1, 0)

if ctrl.pressing(keys.space) then
percent_b = 0
end
end

function _draw()
gfx.cls(1)
shape.circlef(circle.x, circle.y, circle.radius, 8)
shape.circle(circle.x, circle.y, 2, 9)
local offset_a = juice.powIn2(0, 8, percent_a)
local offset_b = juice.powIn2(0, 8, percent_b)

gfx.cls()
shape.rectf(64, 128 - 16, 32, 32, 7)
shape.rectf(64, 128 - 32 + offset_a, 32, 32, 8)

shape.rectf(32 + 128, 128 - 16, 32, 32, 7)
shape.rectf(32 + 128, 128 - 32 + offset_b, 32, 32, 8)

print("pressed", 64, 128 + 32)
print("pressing", 32 + 128, 128 + 32)
end
"""

const val CTRL_TOUCHING_EXAMPLE = """
function _draw()
gfx.cls()
local p = ctrl.touch()
shape.circlef(p.x, p.y, 4, 8)

local start = ctrl.touching(0)
if start ~= nil then
local pos = ctrl.touch()
shape.line(start.x, start.y, pos.x, pos.y, 9)
shape.line(start.x, start.y, pos.x, pos.y, 1)
print("("..start.x .. ", "..start.y..")", start.x, start.y)
print("("..pos.x .. ", "..pos.y..")", pos.x, pos.y)
end
end
"""

const val CTRL_TOUCHED_EXAMPLE = """
function _draw()
local circles = {}

function _update()
local pos = ctrl.touched(0)
if pos ~= nil then
table.insert(circles, pos)
end
end

function _draw()
gfx.cls()
local p = ctrl.touch()
shape.circlef(p.x, p.y, 4, 8)
for pos in all(circles) do
shape.circlef(pos.x, pos.y, 4, 9)
print("("..pos.x .. ", "..pos.y..")", pos.x + 3, pos.y + 3)
end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,8 @@ function _draw()
local pos = ctrl.touching(0)
if pos ~= nil then
-- set the pixel with the color 9 when the mouse is pressed
gfx.pset(pos.x, pos.y, 9)
local p = ctrl.touch()
gfx.pset(p.x, p.y, 9)
end
end"""

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,39 +32,39 @@ import org.luaj.vm2.lib.TwoArgFunction
)
class JuiceLib : TwoArgFunction() {
@TinyFunction(name = "pow2", example = JUICE_EXAMPLE)
@TinyFunction(name = "pow3")
@TinyFunction(name = "pow4")
@TinyFunction(name = "pow5")
@TinyFunction(name = "powIn2")
@TinyFunction(name = "powIn3")
@TinyFunction(name = "powIn4")
@TinyFunction(name = "powIn5")
@TinyFunction(name = "powOut2")
@TinyFunction(name = "powOut3")
@TinyFunction(name = "powOut4")
@TinyFunction(name = "powOut5")
@TinyFunction(name = "sine")
@TinyFunction(name = "sineIn")
@TinyFunction(name = "sineOut")
@TinyFunction(name = "circle")
@TinyFunction(name = "circleIn")
@TinyFunction(name = "circleOut")
@TinyFunction(name = "elastic")
@TinyFunction(name = "elasticIn")
@TinyFunction(name = "elasticOut")
@TinyFunction(name = "swing")
@TinyFunction(name = "swingIn")
@TinyFunction(name = "swingOut")
@TinyFunction(name = "bounce")
@TinyFunction(name = "bounceIn")
@TinyFunction(name = "bounceOut")
@TinyFunction(name = "exp10")
@TinyFunction(name = "expIn10")
@TinyFunction(name = "expOut10")
@TinyFunction(name = "exp5")
@TinyFunction(name = "expIn5")
@TinyFunction(name = "expOut5")
@TinyFunction(name = "linear")
@TinyFunction(name = "pow3", example = JUICE_EXAMPLE)
@TinyFunction(name = "pow4", example = JUICE_EXAMPLE)
@TinyFunction(name = "pow5", example = JUICE_EXAMPLE)
@TinyFunction(name = "powIn2", example = JUICE_EXAMPLE)
@TinyFunction(name = "powIn3", example = JUICE_EXAMPLE)
@TinyFunction(name = "powIn4", example = JUICE_EXAMPLE)
@TinyFunction(name = "powIn5", example = JUICE_EXAMPLE)
@TinyFunction(name = "powOut2", example = JUICE_EXAMPLE)
@TinyFunction(name = "powOut3", example = JUICE_EXAMPLE)
@TinyFunction(name = "powOut4", example = JUICE_EXAMPLE)
@TinyFunction(name = "powOut5", example = JUICE_EXAMPLE)
@TinyFunction(name = "sine", example = JUICE_EXAMPLE)
@TinyFunction(name = "sineIn", example = JUICE_EXAMPLE)
@TinyFunction(name = "sineOut", example = JUICE_EXAMPLE)
@TinyFunction(name = "circle", example = JUICE_EXAMPLE)
@TinyFunction(name = "circleIn", example = JUICE_EXAMPLE)
@TinyFunction(name = "circleOut", example = JUICE_EXAMPLE)
@TinyFunction(name = "elastic", example = JUICE_EXAMPLE)
@TinyFunction(name = "elasticIn", example = JUICE_EXAMPLE)
@TinyFunction(name = "elasticOut", example = JUICE_EXAMPLE)
@TinyFunction(name = "swing", example = JUICE_EXAMPLE)
@TinyFunction(name = "swingIn", example = JUICE_EXAMPLE)
@TinyFunction(name = "swingOut", example = JUICE_EXAMPLE)
@TinyFunction(name = "bounce", example = JUICE_EXAMPLE)
@TinyFunction(name = "bounceIn", example = JUICE_EXAMPLE)
@TinyFunction(name = "bounceOut", example = JUICE_EXAMPLE)
@TinyFunction(name = "exp10", example = JUICE_EXAMPLE)
@TinyFunction(name = "expIn10", example = JUICE_EXAMPLE)
@TinyFunction(name = "expOut10", example = JUICE_EXAMPLE)
@TinyFunction(name = "exp5", example = JUICE_EXAMPLE)
@TinyFunction(name = "expIn5", example = JUICE_EXAMPLE)
@TinyFunction(name = "expOut5", example = JUICE_EXAMPLE)
@TinyFunction(name = "linear", example = JUICE_EXAMPLE)
inner class InterpolationLib(private val interpolation: Interpolation) : LibFunction() {

@TinyCall("Give a percentage (progress) of the interpolation")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,44 +2,31 @@ package com.github.minigdx.tiny.lua

//language=Lua
const val JUICE_EXAMPLE = """
function _init()
t = 0
end
local center_x = 256 * 0.5
local center_y = 256 * 0.5
local width = 128

function _update()
t = t + 1/60
gfx.cls()
shape.line(center_x - 64, center_y + 64, center_x + 64, center_y + 64, 2)
shape.line(center_x + 64, center_y - 64, center_x + 64, center_y + 64, 2)


for x = 0, width, 2 do
local y = juice.##function##(0, 128, x / width)
gfx.pset(
center_x - 64 + x, center_y + 64 - y, 3
)
end

local percent = (tiny.frame % 100) / 100
local x = width * percent
local y = juice.##function##(0, 128, percent)
shape.circlef(center_x - 64 + x, center_y + 64 - y, 4, 7)
shape.rectf(center_x - 64 + x - 2, center_y + 64 + 8, 4, 4, 7)
shape.rectf(center_x + 70, center_y + 64 - y, 4, 4, 7)
local name = "##function##"
print(name, center_x - #name * 4 * 0.5, center_y + 92)
end

function _draw()
gfx.cls()
progress = math.abs(math.cos(t))

print("pow", 0, 0)
shape.circlef(juice.pow2(20, 236, progress), 10, 10, 2)
shape.circlef(juice.pow3(20, 236, progress), 20, 10, 3)
shape.circlef(juice.pow4(20, 236, progress), 30, 10, 4)


print("bounce", 0, 50)
shape.circlef(juice.bounce(20, 236, progress), 60, 10, 5)

print("exp", 0, 80)
shape.circlef(juice.exp10(20, 236, progress), 90, 10, 6)


print("swing", 0, 110)
shape.circlef(juice.swing(20, 236, progress), 120, 10, 7)

print("sine", 0, 140)
shape.circlef(juice.sine(20, 236, progress), 150, 10, 8)

print("circle", 0, 170)
shape.circlef(juice.circle(20, 236, progress), 180, 10, 9)

print("elastic", 0, 200)
shape.circlef(juice.elastic(20, 236, progress), 210, 10, 10)

print("linear", 0, 230)
shape.circlef(juice.linear(20, 236, progress), 240, 10, 1)
end
"""
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,9 @@ function _draw()
gfx.cls()

spr.sdraw()

if touching ~= nil then
spr.pset(touching.x, touching.y, 9)
spr.pset(pos.x, pos.y, 9)
end
print("click to alter", 45, 96)
shape.circle(64 + 8, 128 + 8, 32, 1)
Expand All @@ -38,6 +38,7 @@ function _draw()

shape.circlef(pos.x, pos.y, 2, 3)
end

"""

//language=Lua
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.github.minigdx.tiny.input

import kotlin.test.Test
import kotlin.test.assertEquals
import kotlin.test.assertFalse
import kotlin.test.assertNotNull
import kotlin.test.assertNull
Expand Down Expand Up @@ -30,7 +31,7 @@ class TouchManagerTest {
}

@Test
fun key_just_presset() {
fun key_just_pressed() {
// push key
val touchManager = TouchManager(10)
touchManager.onKeyPressed(1)
Expand All @@ -54,7 +55,7 @@ class TouchManagerTest {
}

@Test
fun key_presset_without_released() {
fun key_pressed_without_released() {
// push key
val touchManager = TouchManager(10)
touchManager.onKeyPressed(1)
Expand All @@ -77,4 +78,17 @@ class TouchManagerTest {
assertFalse(touchManager.isAnyKeyPressed)
assertFalse(touchManager.isKeyPressed(1))
}

@Test
fun touching() {
// push key
val touchManager = TouchManager(10)
touchManager.onTouchDown(TouchSignal.TOUCH1, 0f, 0f)
touchManager.processReceivedEvent()
touchManager.onTouchMove(TouchSignal.TOUCH1, 10f, 0f)
touchManager.processReceivedEvent()

val pos = touchManager.isTouched(TouchSignal.TOUCH1)
assertEquals(0f, pos?.x)
}
}
Loading
Loading