Skip to content

Commit

Permalink
Add roverlap to check if two rectangles overlap
Browse files Browse the repository at this point in the history
It's usefull to check if entities collide
  • Loading branch information
dwursteisen committed Nov 20, 2023
1 parent f3e2e47 commit c685d91
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand All @@ -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(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down Expand Up @@ -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() {

Expand Down

0 comments on commit c685d91

Please sign in to comment.