Skip to content

Commit

Permalink
Introduce vec2 lib to manage vector 2
Browse files Browse the repository at this point in the history
  • Loading branch information
dwursteisen committed Dec 29, 2023
1 parent 3a090aa commit 0b35f2b
Show file tree
Hide file tree
Showing 3 changed files with 211 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,144 @@
package com.github.minigdx.tiny.lua

import com.github.mingdx.tiny.doc.TinyFunction
import com.github.mingdx.tiny.doc.TinyLib
import org.luaj.vm2.LuaTable
import org.luaj.vm2.LuaValue
import org.luaj.vm2.Varargs
import org.luaj.vm2.lib.LibFunction
import org.luaj.vm2.lib.TwoArgFunction

@TinyLib("vec2", "Vector2 manipulation library.")
class Vec2Lib : TwoArgFunction() {
override fun call(arg1: LuaValue, arg2: LuaValue): LuaValue {
val vector2 = LuaTable()
vector2["create"] = create()
vector2["add"] = add()
vector2["sub"] = sub()
vector2["dot"] = dot()
vector2["crs"] = crs()
vector2["mag"] = mag()
vector2["nor"] = nor()
vector2["scl"] = scl()
arg2.set("vec2", vector2)
arg2.get("package").get("loaded").set("vec2", vector2)
return vector2
}

@TinyFunction("Create a vector 2 as a table { x, y }")
class create : TwoArgFunction() {
override fun call(arg1: LuaValue, arg2: LuaValue): LuaValue {
val (x, y) = extract(arg1, arg2)
return v2(x, y)
}

override fun call(arg: LuaValue): LuaValue = super.call(arg)
}

@TinyFunction("Add vector2 to another vector2", example = VECTOR2_ADD)
class add : LibFunction() {
override fun invoke(args: Varargs): Varargs {
val (x1, y1, x2, y2) = extract(args)
return v2(
valueOf(x1.todouble() + x2.todouble()),
valueOf(y1.todouble() + y2.todouble()),
)
}
}

@TinyFunction("Subtract another vector to a vector", example = VECTOR2_SUB)
class sub : LibFunction() {
override fun invoke(args: Varargs): Varargs {
val (x1, y1, x2, y2) = extract(args)
return v2(
valueOf(x1.todouble() - x2.todouble()),
valueOf(y1.todouble() - y2.todouble()),
)
}
}

@TinyFunction("Dot product between two vectors")
class dot : LibFunction() {
override fun invoke(args: Varargs): Varargs {
val (x1, y1, x2, y2) = extract(args)
return valueOf(x1.todouble() * x2.todouble() + y1.todouble() * y2.todouble())
}
}

@TinyFunction("Calculate the magnitude (length) of a vector")
class mag : LibFunction() {
override fun invoke(args: Varargs): Varargs {
val (x1, y1) = extract(args)
return valueOf(kotlin.math.sqrt(x1.todouble() * x1.todouble() + y1.todouble() * y1.todouble()))
}
}

@TinyFunction("Normalize a vector")
class nor : LibFunction() {
override fun invoke(args: Varargs): Varargs {
val (x1, y1) = extract(args)
val len = kotlin.math.sqrt(x1.todouble() * x1.todouble() + y1.todouble() * y1.todouble())
if (len != 0.0) {
return v2(valueOf(x1.todouble() / len), valueOf(y1.todouble() / len))
} else {
return NIL
}
}
}

@TinyFunction("Cross product")
class crs : LibFunction() {
override fun invoke(args: Varargs): Varargs {
val (x1, y1, x2, y2) = extract(args)
return valueOf(
x1.todouble() * y2.todouble() - y1.todouble() * x2.todouble(),
)
}
}

@TinyFunction("Scale a vector", example = VECTOR2_SCL)
class scl : LibFunction() {
override fun invoke(args: Varargs): Varargs {
val (x1, y1, scale) = extract(args)
return v2(valueOf(x1.todouble() * scale.todouble()), valueOf(y1.todouble() * scale.todouble()))
}
}

companion object {
fun extract(arg1: LuaValue, arg2: LuaValue): List<LuaValue> {
return if (arg1.istable()) {
listOf(arg1.get("x"), arg2.get("y"))
} else {
listOf(arg1, arg2)
}
}

fun extract(args: Varargs): List<LuaValue> {
var index = 1
val result = mutableListOf<LuaValue>()
var current = args.arg(index)
while (index <= args.narg()) {
if (current.istable()) {
result.add(current.get("x"))
result.add(current.get("y"))
index++
} else {
if (args.narg() - index >= 0) {
result.add(current)
result.add(args.arg(++index))
index++
}
}
current = args.arg(index)
}
return result
}

fun v2(arg1: LuaValue, arg2: LuaValue): LuaTable {
return LuaTable().apply {
set("x", arg1)
set("y", arg2)
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
package com.github.minigdx.tiny.lua

//language=Lua
const val VECTOR2_ADD = """
function _init()
gfx.camera(-128, -128)
local v1 = vec2.create(32, 38)
local v2 = vec2.create(20, 2)
gfx.cls()
print("v1", 2, 15, 10)
shape.line(0, 0, v1.x, v1.y, 10)
print("v2", 23, 3, 9)
shape.line(0, 0, v2.x, v2.y, 9)
local v3 = vec2.add(v1, v2)
print("v1 + v2", 30, 15, 11)
gfx.dither(0xAAAA)
shape.line(0, 0, v3.x, v3.y, 11)
gfx.dither()
end
"""

//language=Lua
const val VECTOR2_SUB = """
function _init()
gfx.camera(-128, -128)
local v1 = vec2.create(32, 38)
local v2 = vec2.create(20, 2)
gfx.cls()
print("v1", 18, 15, 10)
shape.line(0, 0, v1.x, v1.y, 10)
print("v2", 23, 3, 9)
shape.line(0, 0, v2.x, v2.y, 9)
local v3 = vec2.sub(v1, v2)
print("v1 - v2", 0, 40, 11)
gfx.dither(0xAAAA)
shape.line(0, 0, v3.x, v3.y, 11)
gfx.dither()
end
"""

//language=Lua
const val VECTOR2_SCL = """
function _init()
gfx.camera(-128, -128)
local v1 = vec2.create(32, 38)
local v2 = vec2.scl(v1, 2)
gfx.cls()
print("v1 scaled", 8, 60, 11)
gfx.dither(0xAAAA)
shape.line(0, 0, v2.x, v2.y, 11)
gfx.dither()
print("v1", 18, 15, 10)
shape.line(0, 0, v1.x, v1.y, 10)
end
"""
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import com.github.minigdx.tiny.lua.SprLib
import com.github.minigdx.tiny.lua.StdLib
import com.github.minigdx.tiny.lua.TinyBaseLib
import com.github.minigdx.tiny.lua.TinyLib
import com.github.minigdx.tiny.lua.Vec2Lib
import org.luaj.vm2.Globals
import org.luaj.vm2.LoadState
import org.luaj.vm2.LuaError
Expand Down Expand Up @@ -83,6 +84,7 @@ class GameScript(
load(DebugLib(this@GameScript.resourceAccess))
load(KeysLib())
load(MathLib())
load(Vec2Lib())
load(tinyLib)
load(sprLib)
load(JuiceLib())
Expand Down

0 comments on commit 0b35f2b

Please sign in to comment.