diff --git a/tiny-engine/src/commonMain/kotlin/com/github/minigdx/tiny/lua/MapLib.kt b/tiny-engine/src/commonMain/kotlin/com/github/minigdx/tiny/lua/MapLib.kt index 592021c0..fdcfe715 100644 --- a/tiny-engine/src/commonMain/kotlin/com/github/minigdx/tiny/lua/MapLib.kt +++ b/tiny-engine/src/commonMain/kotlin/com/github/minigdx/tiny/lua/MapLib.kt @@ -127,7 +127,7 @@ class MapLib(private val resourceAccess: GameResourceAccess, private val spriteS } @TinyCall("Convert the cell coordinates from a table [cx,cy] into screen coordinates as a table [x,y].") - override fun call(arg: LuaValue): LuaValue = super.call(arg) + override fun call(@TinyArg("cell") arg: LuaValue): LuaValue = super.call(arg) } @TinyFunction( @@ -151,25 +151,31 @@ class MapLib(private val resourceAccess: GameResourceAccess, private val spriteS } @TinyCall("Convert the coordinates from a table [x,y] into cell coordinates as a table [cx,cy].") - override fun call(arg: LuaValue) = super.call(arg) + override fun call(@TinyArg("coordinates") arg: LuaValue) = super.call(arg) } @TinyFunction("Get the flag from a tile.") - inner class flag : LibFunction() { + inner class flag : TwoArgFunction() { @TinyCall("Get the flag from the tile at the coordinate cx,cy.") - override fun call(@TinyArg("cx") a: LuaValue, @TinyArg("cy") b: LuaValue): LuaValue { - val tileX = a.checkint() - val tileY = b.checkint() + override fun call(@TinyArg("cx") arg1: LuaValue, @TinyArg("cy") arg2: LuaValue): LuaValue { + val (tileX, tileY) = if (arg1.istable()) { + arg1["cx"].toint() to arg1["cy"].toint() + } else { + arg1.checkint() to arg2.checkint() + } - val layer = resourceAccess.level(currentLevel)?.intLayers?.first { l -> l != null } ?: return NONE + val layer = resourceAccess.level(currentLevel)?.intLayers?.first { l -> l != null } ?: return NIL return if (tileX in 0 until layer.width && tileY in 0 until layer.height) { valueOf(layer.ints.getOne(tileX, tileY)) } else { - NONE + NIL } } + + @TinyCall("Get the flag from the tile at the coordinate table [cx,cy].") + override fun call(@TinyArg("cell") arg: LuaValue): LuaValue = super.call(arg) } @TinyFunction( diff --git a/tiny-engine/src/commonMain/kotlin/com/github/minigdx/tiny/lua/MathLib.kt b/tiny-engine/src/commonMain/kotlin/com/github/minigdx/tiny/lua/MathLib.kt index bacbd239..9254fc2a 100644 --- a/tiny-engine/src/commonMain/kotlin/com/github/minigdx/tiny/lua/MathLib.kt +++ b/tiny-engine/src/commonMain/kotlin/com/github/minigdx/tiny/lua/MathLib.kt @@ -31,6 +31,7 @@ class MathLib : org.luaj.vm2.lib.MathLib() { math["dst"] = dst() math["dst2"] = dst2() math["sign"] = sign() + math["roverlap"] = roverlap() math["perlin"] = perlin(Random.nextLong()) return math } @@ -146,6 +147,27 @@ class MathLib : org.luaj.vm2.lib.MathLib() { } } + @TinyFunction("Check if two (r)ectangles overlaps.") + inner class roverlap() : TwoArgFunction() { + @TinyCall("Check if the rectangle rect1 overlaps with the rectangle rect2.") + override fun call( + @TinyArg("rect1", "Rectangle as a table {x, y, with, height}.") arg1: LuaValue, + @TinyArg("rect2", "Rectangle as a table {x, y, with, height}.") arg2: LuaValue, + ): LuaValue { + val ax = arg1["x"].toint() + val ay = arg1["y"].toint() + val awidth = arg1["width"].toint() + val aheight = arg1["height"].toint() + + val bx = arg2["x"].toint() + val by = arg2["y"].toint() + val bwidth = arg2["width"].toint() + val bheight = arg2["height"].toint() + + return valueOf(ax < bx + bwidth && ax + awidth > bx && ay < by + bheight && ay + aheight > by) + } + } + @TinyFunction("Perlin noise. The random generated value is between 0 and 1.") inner class perlin(seed: Long) : ThreeArgFunction() {