added properties for Vector2:

Vector2.ZERO
Vector2.ONE
Vector2.NEG_ZERO
Vector2.NEG_ONE
Vector2.NEG_X
Vector2.NEG_Y
This commit is contained in:
unittensor 2024-01-21 22:17:30 -05:00
parent d2782012ac
commit 6195f0d7ba
4 changed files with 122 additions and 10 deletions

View File

@ -2,8 +2,24 @@ local v2 = Vector2.new(1, 3)
local v3 = Vector3.new(-10, 30, 50) local v3 = Vector3.new(-10, 30, 50)
local v4 = Vector4.new(50, 30, 10, -1000) local v4 = Vector4.new(50, 30, 10, -1000)
local function InspectVectorTable(Vector: {[string]: number})
local aye: {string} = {"{"}
for k,v in Vector do
table.insert(aye, `{tostring(k)}={tostring(v)}`)
end
table.insert(aye, "}")
return table.concat(aye, " ")
end
print("----StrafeLua----") print("----StrafeLua----")
print(("\n Vector2= x=%d,y=%d \n"):format(v2.x, v2.y)) print(_VERSION)
print(("\n Vector3= x=%d,y=%d,z=%d \n"):format(v3.x, v3.y, v3.z)) print("Vector2=",InspectVectorTable(v2))
print(("\n Vector4= x=%d,y=%d,z=%d,w=%d \n"):format(v4.x, v4.y, v4.z, v4.w)) print("Vector3=",InspectVectorTable(v3))
print("Vector4=",InspectVectorTable(v4))
print("Vector2.ZERO=",InspectVectorTable(Vector2.ZERO))
print("Vector2.ONE=",InspectVectorTable(Vector2.ONE))
print("Vector2.NEG_ZERO=",InspectVectorTable(Vector2.NEG_ZERO))
print("Vector2.NEG_ONE=",InspectVectorTable(Vector2.NEG_ONE))
print("Vector2.NEG_X=",InspectVectorTable(Vector2.NEG_X))
print("Vector2.NEG_Y=",InspectVectorTable(Vector2.NEG_Y))
print("-----------------") print("-----------------")

View File

@ -6,13 +6,31 @@ type struct_Vector3 = struct_Vector2 & {z: f64}
type struct_Vector4 = struct_Vector3 & {w: f64} type struct_Vector4 = struct_Vector3 & {w: f64}
type Vector2 = { type Vector2 = {
new: (x: f64, y: f64) -> struct_Vector2 new: (x: f64, y: f64) -> struct_Vector2,
ONE: struct_Vector2,
ZERO: struct_Vector2,
NEG_ZERO: struct_Vector2,
NEG_ONE: struct_Vector2,
NEG_X: struct_Vector2,
NEG_Y: struct_Vector2,
} }
type Vector3 = { type Vector3 = {
new: (x: f64, y: f64, z: f64) -> struct_Vector3 new: (x: f64, y: f64, z: f64) -> struct_Vector3,
ONE: struct_Vector3,
ZERO: struct_Vector3,
NEG_ZERO: struct_Vector3,
NEG_ONE: struct_Vector3,
NEG_X: struct_Vector3,
NEG_Y: struct_Vector3,
} }
type Vector4 = { type Vector4 = {
new: (x: f64, y: f64, z: f64, w: f64) -> struct_Vector4 new: (x: f64, y: f64, z: f64, w: f64) -> struct_Vector4,
ONE: struct_Vector4,
ZERO: struct_Vector4,
NEG_ZERO: struct_Vector4,
NEG_ONE: struct_Vector4,
NEG_X: struct_Vector4,
NEG_Y: struct_Vector4,
} }
local Vector2: Vector2 = Vector2 local Vector2: Vector2 = Vector2

View File

@ -29,23 +29,64 @@ trait Luavm {
impl Luavm for StrafeluaGlobals { impl Luavm for StrafeluaGlobals {
fn vector2(&self) -> Table { fn vector2(&self) -> Table {
let zero = Vec2::ZERO;
let one = Vec2::ONE;
let neg_zero = Vec2::ZERO;
let neg_one = Vec2::NEG_ONE;
let neg_x = Vec2::NEG_X;
let neg_y = Vec2::NEG_Y;
let field_vector2 = self.vm.create_table().unwrap(); let field_vector2 = self.vm.create_table().unwrap();
let vector2_one = self.vm.create_table().unwrap();
vector2_one.set("x", one.x).unwrap();
vector2_one.set("y", one.y).unwrap();
let vector2_zero = self.vm.create_table().unwrap();
vector2_zero.set("x", zero.x).unwrap();
vector2_zero.set("y", zero.y).unwrap();
let vector2_neg_zero = self.vm.create_table().unwrap();
vector2_neg_zero.set("x", neg_zero.x).unwrap();
vector2_neg_zero.set("y", neg_zero.y).unwrap();
let vector2_neg_one = self.vm.create_table().unwrap();
vector2_neg_one.set("x", neg_one.x).unwrap();
vector2_neg_one.set("y", neg_one.y).unwrap();
let vector2_neg_x = self.vm.create_table().unwrap();
vector2_neg_x.set("x", neg_x.x).unwrap();
vector2_neg_x.set("y", neg_x.y).unwrap();
let vector2_neg_y = self.vm.create_table().unwrap();
vector2_neg_y.set("x", neg_y.x).unwrap();
vector2_neg_y.set("y", neg_y.y).unwrap();
field_vector2.set("ONE", vector2_one).unwrap();
field_vector2.set("ZERO", vector2_zero).unwrap();
field_vector2.set("NEG_ZERO", vector2_neg_zero).unwrap();
field_vector2.set("NEG_ONE", vector2_neg_one).unwrap();
field_vector2.set("NEG_X", vector2_neg_x).unwrap();
field_vector2.set("NEG_Y", vector2_neg_y).unwrap();
let new_vector2 = self.vm.create_function(|this: &Luau, (x,y): (f32,f32)| { let new_vector2 = self.vm.create_function(|this: &Luau, (x,y): (f32,f32)| {
let glam_vec = Vec2::new(x,y); let glam_vec = Vec2::new(x,y);
let vec2 = this.create_table().unwrap(); let vec2 = this.create_table().unwrap();
vec2.set("x", glam_vec.x).unwrap(); vec2.set("x", glam_vec.x).unwrap();
vec2.set("y", glam_vec.y).unwrap(); vec2.set("y", glam_vec.y).unwrap();
Ok(vec2) Ok(vec2)
}).unwrap(); }).unwrap();
field_vector2.set("new", new_vector2).unwrap(); field_vector2.set("new", new_vector2).unwrap();
return field_vector2 return field_vector2
} }
fn vector3(&self) -> Table { fn vector3(&self) -> Table {
let zero = Vec3::ZERO;
let one = Vec3::ONE;
let field_vector3 = self.vm.create_table().unwrap(); let field_vector3 = self.vm.create_table().unwrap();
let vector3_one = self.vm.create_table().unwrap();
let new_vector3 = self.vm.create_function(|this: &Luau, (x,y,z): (f32,f32,f32)| { let new_vector3 = self.vm.create_function(|this: &Luau, (x,y,z): (f32,f32,f32)| {
let glam_vec = Vec3::new(x,y,z); let glam_vec = Vec3::new(x,y,z);
@ -55,14 +96,21 @@ impl Luavm for StrafeluaGlobals {
vec3.set("z", glam_vec.z).unwrap(); vec3.set("z", glam_vec.z).unwrap();
Ok(vec3) Ok(vec3)
}).unwrap(); }).unwrap();
field_vector3.set("new", new_vector3).unwrap(); field_vector3.set("new", new_vector3).unwrap();
vector3_one.set("x", one.x).unwrap();
vector3_one.set("y", one.y).unwrap();
vector3_one.set("z", one.z).unwrap();
field_vector3.set("one", vector3_one).unwrap();
return field_vector3; return field_vector3;
} }
fn vector4(&self) -> Table { fn vector4(&self) -> Table {
let zero = Vec4::ZERO;
let one = Vec4::ONE;
let field_vector4 = self.vm.create_table().unwrap(); let field_vector4 = self.vm.create_table().unwrap();
let vector4_one = self.vm.create_table().unwrap();
let new_vector4 = self.vm.create_function(|this: &Luau, (x,y,z,w): (f32,f32,f32,f32)| { let new_vector4 = self.vm.create_function(|this: &Luau, (x,y,z,w): (f32,f32,f32,f32)| {
let glam_vec = Vec4::new(x,y,z,w); let glam_vec = Vec4::new(x,y,z,w);
@ -73,9 +121,14 @@ impl Luavm for StrafeluaGlobals {
vec4.set("w", glam_vec.w).unwrap(); vec4.set("w", glam_vec.w).unwrap();
Ok(vec4) Ok(vec4)
}).unwrap(); }).unwrap();
field_vector4.set("new", new_vector4).unwrap(); field_vector4.set("new", new_vector4).unwrap();
vector4_one.set("x", one.x).unwrap();
vector4_one.set("y", one.y).unwrap();
vector4_one.set("z", one.z).unwrap();
vector4_one.set("w", one.w).unwrap();
field_vector4.set("one", vector4_one).unwrap();
return field_vector4; return field_vector4;
} }

View File

@ -119,8 +119,33 @@ pub fn default_models()->model::IndexedModelInstances{
fn main(){ fn main(){
let strafelua_vm = luau::new_state(true).expect("Failed to load strafe lua"); let strafelua_vm = luau::new_state(true).expect("Failed to load strafe lua");
// luau/tests/Vector.lua
strafelua_vm.load(r#" strafelua_vm.load(r#"
local v2 = Vector2.new(1, 3)
local v3 = Vector3.new(-10, 30, 50)
local v4 = Vector4.new(50, 30, 10, -1000)
local function InspectVectorTable(Vector: {[string]: number})
local aye: {string} = {"{"}
for k,v in Vector do
table.insert(aye, `{tostring(k)}={tostring(v)}`)
end
table.insert(aye, "}")
return table.concat(aye, " ")
end
print("----StrafeLua----")
print(_VERSION) print(_VERSION)
print("Vector2=",InspectVectorTable(v2))
print("Vector3=",InspectVectorTable(v3))
print("Vector4=",InspectVectorTable(v4))
print("Vector2.ZERO=",InspectVectorTable(Vector2.ZERO))
print("Vector2.ONE=",InspectVectorTable(Vector2.ONE))
print("Vector2.NEG_ZERO=",InspectVectorTable(Vector2.NEG_ZERO))
print("Vector2.NEG_ONE=",InspectVectorTable(Vector2.NEG_ONE))
print("Vector2.NEG_X=",InspectVectorTable(Vector2.NEG_X))
print("Vector2.NEG_Y=",InspectVectorTable(Vector2.NEG_Y))
print("-----------------")
"#).exec().expect("Lua syntax error!"); "#).exec().expect("Lua syntax error!");
//Lua syntax error!: SyntaxError { message: "[string \"src/main.rs:122:18\"]:7: Expected ')' (to close '(' at column 7), got ','", incomplete_input: false } //Lua syntax error!: SyntaxError { message: "[string \"src/main.rs:122:18\"]:7: Expected ')' (to close '(' at column 7), got ','", incomplete_input: false }
//we got our first lua syntax error, todo: make error an error handler in luau.rs //we got our first lua syntax error, todo: make error an error handler in luau.rs