Fix position
This commit is contained in:
@@ -7,7 +7,7 @@
|
||||
|
||||
#pragma once
|
||||
#include "script/module/modulebase.h"
|
||||
#include "script/module/math/modulevec3.h"
|
||||
#include "script/module/math/modulevec3ref.h"
|
||||
#include "script/scriptproto.h"
|
||||
#include "entity/entity.h"
|
||||
#include "entity/component/physics/entityphysics.h"
|
||||
@@ -30,7 +30,7 @@ static entityphysics_t * moduleEntityPhysicsGet(
|
||||
moduleBaseFunction(moduleEntityPhysicsGetVelocity) {
|
||||
entityphysics_t *phys = moduleEntityPhysicsGet(callInfo);
|
||||
if(!phys) return jerry_undefined();
|
||||
return moduleVec3Push(phys->velocity);
|
||||
return moduleVec3RefPush(phys->velocity, NULL, NULL);
|
||||
}
|
||||
|
||||
moduleBaseFunction(moduleEntityPhysicsSetVelocity) {
|
||||
@@ -38,7 +38,9 @@ moduleBaseFunction(moduleEntityPhysicsSetVelocity) {
|
||||
entityphysics_t *phys = moduleEntityPhysicsGet(callInfo);
|
||||
if(!phys) return jerry_undefined();
|
||||
vec3 v;
|
||||
if(!moduleVec3Check(args[0], v)) return moduleBaseThrow("expected Vec3");
|
||||
if(!moduleVec3AnyCheck(args[0], v)) {
|
||||
return moduleBaseThrow("expected Vec3");
|
||||
}
|
||||
glm_vec3_copy(v, phys->velocity);
|
||||
return jerry_undefined();
|
||||
}
|
||||
|
||||
@@ -7,7 +7,7 @@
|
||||
|
||||
#pragma once
|
||||
#include "script/module/modulebase.h"
|
||||
#include "script/module/math/modulevec3.h"
|
||||
#include "script/module/math/modulevec3ref.h"
|
||||
#include "script/scriptproto.h"
|
||||
#include "entity/entity.h"
|
||||
#include "entity/component/display/entityposition.h"
|
||||
@@ -38,7 +38,9 @@ static entityposition_t * moduleEntityPositionGet(
|
||||
moduleBaseFunction(moduleEntityPositionGetPosition) {
|
||||
entityposition_t *pos = moduleEntityPositionGet(callInfo);
|
||||
if(!pos) return jerry_undefined();
|
||||
return moduleVec3Push(pos->position);
|
||||
return moduleVec3RefPush(
|
||||
pos->position, (void(*)(void*))entityPositionRebuild, pos
|
||||
);
|
||||
}
|
||||
|
||||
moduleBaseFunction(moduleEntityPositionSetPosition) {
|
||||
@@ -46,7 +48,9 @@ moduleBaseFunction(moduleEntityPositionSetPosition) {
|
||||
entityposition_t *pos = moduleEntityPositionGet(callInfo);
|
||||
if(!pos) return jerry_undefined();
|
||||
vec3 v;
|
||||
if(!moduleVec3Check(args[0], v)) return moduleBaseThrow("expected Vec3");
|
||||
if(!moduleVec3AnyCheck(args[0], v)) {
|
||||
return moduleBaseThrow("expected Vec3");
|
||||
}
|
||||
glm_vec3_copy(v, pos->position);
|
||||
entityPositionRebuild(pos);
|
||||
return jerry_undefined();
|
||||
@@ -57,7 +61,9 @@ moduleBaseFunction(moduleEntityPositionSetPosition) {
|
||||
moduleBaseFunction(moduleEntityPositionGetRotation) {
|
||||
entityposition_t *pos = moduleEntityPositionGet(callInfo);
|
||||
if(!pos) return jerry_undefined();
|
||||
return moduleVec3Push(pos->rotation);
|
||||
return moduleVec3RefPush(
|
||||
pos->rotation, (void(*)(void*))entityPositionRebuild, pos
|
||||
);
|
||||
}
|
||||
|
||||
moduleBaseFunction(moduleEntityPositionSetRotation) {
|
||||
@@ -65,7 +71,9 @@ moduleBaseFunction(moduleEntityPositionSetRotation) {
|
||||
entityposition_t *pos = moduleEntityPositionGet(callInfo);
|
||||
if(!pos) return jerry_undefined();
|
||||
vec3 v;
|
||||
if(!moduleVec3Check(args[0], v)) return moduleBaseThrow("expected Vec3");
|
||||
if(!moduleVec3AnyCheck(args[0], v)) {
|
||||
return moduleBaseThrow("expected Vec3");
|
||||
}
|
||||
glm_vec3_copy(v, pos->rotation);
|
||||
entityPositionRebuild(pos);
|
||||
return jerry_undefined();
|
||||
@@ -76,7 +84,9 @@ moduleBaseFunction(moduleEntityPositionSetRotation) {
|
||||
moduleBaseFunction(moduleEntityPositionGetScale) {
|
||||
entityposition_t *pos = moduleEntityPositionGet(callInfo);
|
||||
if(!pos) return jerry_undefined();
|
||||
return moduleVec3Push(pos->scale);
|
||||
return moduleVec3RefPush(
|
||||
pos->scale, (void(*)(void*))entityPositionRebuild, pos
|
||||
);
|
||||
}
|
||||
|
||||
moduleBaseFunction(moduleEntityPositionSetScale) {
|
||||
@@ -84,7 +94,9 @@ moduleBaseFunction(moduleEntityPositionSetScale) {
|
||||
entityposition_t *pos = moduleEntityPositionGet(callInfo);
|
||||
if(!pos) return jerry_undefined();
|
||||
vec3 v;
|
||||
if(!moduleVec3Check(args[0], v)) return moduleBaseThrow("expected Vec3");
|
||||
if(!moduleVec3AnyCheck(args[0], v)) {
|
||||
return moduleBaseThrow("expected Vec3");
|
||||
}
|
||||
glm_vec3_copy(v, pos->scale);
|
||||
entityPositionRebuild(pos);
|
||||
return jerry_undefined();
|
||||
@@ -97,11 +109,11 @@ moduleBaseFunction(moduleEntityPositionLookAt) {
|
||||
entityposition_t *pos = moduleEntityPositionGet(callInfo);
|
||||
if(!pos) return jerry_undefined();
|
||||
vec3 target;
|
||||
if(!moduleVec3Check(args[0], target)) {
|
||||
if(!moduleVec3AnyCheck(args[0], target)) {
|
||||
return moduleBaseThrow("expected Vec3 target");
|
||||
}
|
||||
vec3 up = { 0.0f, 1.0f, 0.0f };
|
||||
if(argc >= 2 && !moduleVec3Check(args[1], up)) {
|
||||
if(argc >= 2 && !moduleVec3AnyCheck(args[1], up)) {
|
||||
return moduleBaseThrow("expected Vec3 up");
|
||||
}
|
||||
glm_lookat(pos->position, target, up, pos->transform);
|
||||
|
||||
@@ -9,12 +9,14 @@
|
||||
#include "script/module/modulebase.h"
|
||||
#include "modulevec2.h"
|
||||
#include "modulevec3.h"
|
||||
#include "modulevec3ref.h"
|
||||
#include "modulevec4.h"
|
||||
#include "modulemat4.h"
|
||||
|
||||
static void moduleMath(void) {
|
||||
moduleVec2();
|
||||
moduleVec3();
|
||||
moduleVec3Ref();
|
||||
moduleVec4();
|
||||
moduleMat4();
|
||||
}
|
||||
|
||||
@@ -0,0 +1,113 @@
|
||||
/**
|
||||
* Copyright (c) 2026 Dominic Masters
|
||||
*
|
||||
* This software is released under the MIT License.
|
||||
* https://opensource.org/licenses/MIT
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
#include "script/module/modulebase.h"
|
||||
#include "script/scriptproto.h"
|
||||
#include "cglm/cglm.h"
|
||||
#include "modulevec3.h"
|
||||
|
||||
typedef struct {
|
||||
float_t *data;
|
||||
void (*onChange)(void *ctx);
|
||||
void *ctx;
|
||||
} vec3ref_t;
|
||||
|
||||
static scriptproto_t MODULE_VEC3_REF_PROTO;
|
||||
|
||||
static inline vec3ref_t * moduleVec3RefGet(
|
||||
const jerry_call_info_t *callInfo
|
||||
) {
|
||||
return (vec3ref_t*)scriptProtoGetValue(
|
||||
&MODULE_VEC3_REF_PROTO, callInfo->this_value
|
||||
);
|
||||
}
|
||||
|
||||
static inline vec3ref_t * moduleVec3RefFrom(jerry_value_t val) {
|
||||
return (vec3ref_t*)scriptProtoGetValue(&MODULE_VEC3_REF_PROTO, val);
|
||||
}
|
||||
|
||||
static inline void moduleVec3RefNotify(vec3ref_t *ref) {
|
||||
if(ref->onChange) ref->onChange(ref->ctx);
|
||||
}
|
||||
|
||||
moduleBaseFunction(moduleVec3RefGetX) {
|
||||
vec3ref_t *ref = moduleVec3RefGet(callInfo);
|
||||
return ref ? jerry_number(ref->data[0]) : jerry_undefined();
|
||||
}
|
||||
moduleBaseFunction(moduleVec3RefSetX) {
|
||||
vec3ref_t *ref = moduleVec3RefGet(callInfo);
|
||||
if(ref && argc > 0) {
|
||||
ref->data[0] = (float_t)jerry_value_as_number(args[0]);
|
||||
moduleVec3RefNotify(ref);
|
||||
}
|
||||
return jerry_undefined();
|
||||
}
|
||||
|
||||
moduleBaseFunction(moduleVec3RefGetY) {
|
||||
vec3ref_t *ref = moduleVec3RefGet(callInfo);
|
||||
return ref ? jerry_number(ref->data[1]) : jerry_undefined();
|
||||
}
|
||||
moduleBaseFunction(moduleVec3RefSetY) {
|
||||
vec3ref_t *ref = moduleVec3RefGet(callInfo);
|
||||
if(ref && argc > 0) {
|
||||
ref->data[1] = (float_t)jerry_value_as_number(args[0]);
|
||||
moduleVec3RefNotify(ref);
|
||||
}
|
||||
return jerry_undefined();
|
||||
}
|
||||
|
||||
moduleBaseFunction(moduleVec3RefGetZ) {
|
||||
vec3ref_t *ref = moduleVec3RefGet(callInfo);
|
||||
return ref ? jerry_number(ref->data[2]) : jerry_undefined();
|
||||
}
|
||||
moduleBaseFunction(moduleVec3RefSetZ) {
|
||||
vec3ref_t *ref = moduleVec3RefGet(callInfo);
|
||||
if(ref && argc > 0) {
|
||||
ref->data[2] = (float_t)jerry_value_as_number(args[0]);
|
||||
moduleVec3RefNotify(ref);
|
||||
}
|
||||
return jerry_undefined();
|
||||
}
|
||||
|
||||
static inline jerry_value_t moduleVec3RefPush(
|
||||
float_t *data, void (*onChange)(void *), void *ctx
|
||||
) {
|
||||
vec3ref_t ref = { .data = data, .onChange = onChange, .ctx = ctx };
|
||||
return scriptProtoCreateValue(&MODULE_VEC3_REF_PROTO, &ref);
|
||||
}
|
||||
|
||||
static inline bool_t moduleVec3RefCheck(jerry_value_t val, float_t *out) {
|
||||
vec3ref_t *ref = moduleVec3RefFrom(val);
|
||||
if(!ref) return false;
|
||||
out[0] = ref->data[0];
|
||||
out[1] = ref->data[1];
|
||||
out[2] = ref->data[2];
|
||||
return true;
|
||||
}
|
||||
|
||||
static inline bool_t moduleVec3AnyCheck(jerry_value_t val, float_t *out) {
|
||||
return moduleVec3Check(val, out) || moduleVec3RefCheck(val, out);
|
||||
}
|
||||
|
||||
static void moduleVec3Ref(void) {
|
||||
scriptProtoInit(
|
||||
&MODULE_VEC3_REF_PROTO, NULL, sizeof(vec3ref_t), NULL
|
||||
);
|
||||
scriptProtoDefineProp(
|
||||
&MODULE_VEC3_REF_PROTO, "x",
|
||||
moduleVec3RefGetX, moduleVec3RefSetX
|
||||
);
|
||||
scriptProtoDefineProp(
|
||||
&MODULE_VEC3_REF_PROTO, "y",
|
||||
moduleVec3RefGetY, moduleVec3RefSetY
|
||||
);
|
||||
scriptProtoDefineProp(
|
||||
&MODULE_VEC3_REF_PROTO, "z",
|
||||
moduleVec3RefGetZ, moduleVec3RefSetZ
|
||||
);
|
||||
}
|
||||
Reference in New Issue
Block a user