141 lines
4.4 KiB
C
141 lines
4.4 KiB
C
/**
|
|
* 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 "script/module/math/modulevec3.h"
|
|
#include "script/module/entity/modulecomponent.h"
|
|
#include "entity/component/trigger/entitytrigger.h"
|
|
|
|
static scriptproto_t MODULE_TRIGGER_PROTO;
|
|
|
|
moduleBaseFunction(moduleTriggerCtor) {
|
|
(void)callInfo; (void)args; (void)argc;
|
|
return moduleBaseThrow("Trigger cannot be instantiated with new");
|
|
}
|
|
|
|
static inline jscomponent_t *moduleTriggerSelf(
|
|
const jerry_call_info_t *callInfo
|
|
) {
|
|
return (jscomponent_t *)scriptProtoGetValue(
|
|
&MODULE_TRIGGER_PROTO, callInfo->this_value
|
|
);
|
|
}
|
|
|
|
moduleBaseFunction(moduleTriggerGetEntity) {
|
|
jscomponent_t *c = moduleTriggerSelf(callInfo);
|
|
if(!c) return jerry_undefined();
|
|
return jerry_number((double)c->entityId);
|
|
}
|
|
|
|
moduleBaseFunction(moduleTriggerGetId) {
|
|
jscomponent_t *c = moduleTriggerSelf(callInfo);
|
|
if(!c) return jerry_undefined();
|
|
return jerry_number((double)c->componentId);
|
|
}
|
|
|
|
moduleBaseFunction(moduleTriggerGetMin) {
|
|
jscomponent_t *c = moduleTriggerSelf(callInfo);
|
|
if(!c) return jerry_undefined();
|
|
entitytrigger_t *t = entityTriggerGet(c->entityId, c->componentId);
|
|
if(!t) return jerry_undefined();
|
|
return moduleVec3Push(t->min);
|
|
}
|
|
|
|
moduleBaseFunction(moduleTriggerSetMin) {
|
|
moduleBaseRequireArgs(1);
|
|
jscomponent_t *c = moduleTriggerSelf(callInfo);
|
|
if(!c) return jerry_undefined();
|
|
float_t *v = moduleVec3From(args[0]);
|
|
if(!v) return moduleBaseThrow("Trigger.min: expected Vec3");
|
|
entitytrigger_t *t = entityTriggerGet(c->entityId, c->componentId);
|
|
if(!t) return jerry_undefined();
|
|
glm_vec3_copy(v, t->min);
|
|
return jerry_undefined();
|
|
}
|
|
|
|
moduleBaseFunction(moduleTriggerGetMax) {
|
|
jscomponent_t *c = moduleTriggerSelf(callInfo);
|
|
if(!c) return jerry_undefined();
|
|
entitytrigger_t *t = entityTriggerGet(c->entityId, c->componentId);
|
|
if(!t) return jerry_undefined();
|
|
return moduleVec3Push(t->max);
|
|
}
|
|
|
|
moduleBaseFunction(moduleTriggerSetMax) {
|
|
moduleBaseRequireArgs(1);
|
|
jscomponent_t *c = moduleTriggerSelf(callInfo);
|
|
if(!c) return jerry_undefined();
|
|
float_t *v = moduleVec3From(args[0]);
|
|
if(!v) return moduleBaseThrow("Trigger.max: expected Vec3");
|
|
entitytrigger_t *t = entityTriggerGet(c->entityId, c->componentId);
|
|
if(!t) return jerry_undefined();
|
|
glm_vec3_copy(v, t->max);
|
|
return jerry_undefined();
|
|
}
|
|
|
|
moduleBaseFunction(moduleTriggerSetBounds) {
|
|
moduleBaseRequireArgs(2);
|
|
jscomponent_t *c = moduleTriggerSelf(callInfo);
|
|
if(!c) return jerry_undefined();
|
|
float_t *minV = moduleVec3From(args[0]);
|
|
float_t *maxV = moduleVec3From(args[1]);
|
|
if(!minV) return moduleBaseThrow("Trigger.setBounds: expected Vec3 for min");
|
|
if(!maxV) return moduleBaseThrow("Trigger.setBounds: expected Vec3 for max");
|
|
entityTriggerSetBounds(c->entityId, c->componentId, minV, maxV);
|
|
return jerry_undefined();
|
|
}
|
|
|
|
moduleBaseFunction(moduleTriggerContains) {
|
|
moduleBaseRequireArgs(1);
|
|
jscomponent_t *c = moduleTriggerSelf(callInfo);
|
|
if(!c) return jerry_undefined();
|
|
float_t *v = moduleVec3From(args[0]);
|
|
if(!v) return moduleBaseThrow("Trigger.contains: expected Vec3");
|
|
return jerry_boolean(entityTriggerContains(c->entityId, c->componentId, v));
|
|
}
|
|
|
|
moduleBaseFunction(moduleTriggerToString) {
|
|
jscomponent_t *c = moduleTriggerSelf(callInfo);
|
|
if(!c) return jerry_string_sz("Trigger:invalid");
|
|
char_t buf[32];
|
|
snprintf(buf, sizeof(buf), "Trigger(%u)", (unsigned)c->componentId);
|
|
return jerry_string_sz(buf);
|
|
}
|
|
|
|
static void moduleTriggerInit(void) {
|
|
scriptProtoInit(
|
|
&MODULE_TRIGGER_PROTO, "Trigger",
|
|
sizeof(jscomponent_t), moduleTriggerCtor
|
|
);
|
|
|
|
scriptProtoDefineProp(
|
|
&MODULE_TRIGGER_PROTO, "entity", moduleTriggerGetEntity, NULL
|
|
);
|
|
scriptProtoDefineProp(
|
|
&MODULE_TRIGGER_PROTO, "id", moduleTriggerGetId, NULL
|
|
);
|
|
scriptProtoDefineProp(
|
|
&MODULE_TRIGGER_PROTO, "min", moduleTriggerGetMin, moduleTriggerSetMin
|
|
);
|
|
scriptProtoDefineProp(
|
|
&MODULE_TRIGGER_PROTO, "max", moduleTriggerGetMax, moduleTriggerSetMax
|
|
);
|
|
scriptProtoDefineFunc(
|
|
&MODULE_TRIGGER_PROTO, "setBounds", moduleTriggerSetBounds
|
|
);
|
|
scriptProtoDefineFunc(
|
|
&MODULE_TRIGGER_PROTO, "contains", moduleTriggerContains
|
|
);
|
|
scriptProtoDefineToString(&MODULE_TRIGGER_PROTO, moduleTriggerToString);
|
|
}
|
|
|
|
static void moduleTriggerDispose(void) {
|
|
scriptProtoDispose(&MODULE_TRIGGER_PROTO);
|
|
}
|