Reorganized script api files

Added access to BlockOutComponent to the scripting system
dev
Joey Pollack 3 years ago
parent f0dd9e4e4d
commit 0c2b5b2def

@ -98,7 +98,8 @@ set(LUNARIUM_SRC
"src/scripting/wren_script.cpp"
"src/scripting/coreAPI.cpp"
"src/world/world.cpp"
"src/world/world_api.cpp"
"src/scripting/world_api.cpp"
"src/scripting/component_api.cpp"
"src/world/entity.cpp"
)

@ -63,10 +63,11 @@ Editor:
☐ Toolbar with play/pause/stop buttons
World Hierarchy (Tree View):
☐ If control is held while dropping and entities, reorder the dropped entity instead of parenting
☐ If control is held while dropping entities, reorder the dropped entity instead of parenting
- This will allow for reorganizing the hierarchy
☐ Add "move up" and "move down" options to the entity context menu
- Another way to reorganize the hierarchy
✔ Do not show arrows on tree view items with no children @done(23-01-04 19:02)
✔ Handle showing Enities with children @high @done(22-10-14 18:29)
✔ Handle adding child entities @high @done(22-10-14 18:29)
If an entity was right clicked on the new entity should be a child of the clicked entity
@ -75,6 +76,7 @@ Editor:
Asset Viewer:
✔ Get references to the EditorAsset objects instead of the raw file locations @done(22-06-01 18:48)
☐ Put files into a table with columns for the file Properties
✔ Do not show arrows on tree view items with no children @done(23-01-04 19:02)
✔ Double click folders in content view to open @done(22-06-21 15:17)
✔ Drag and Drop asset files to move them to different folders @done(22-06-21 15:18)
✔ Add directory back button to content view @done(22-06-21 15:45)

@ -318,7 +318,10 @@ namespace lunarium { namespace editor
if (ImGui::IsItemHovered())
{
ImGui::SetTooltip("Is this a fast moving body that should be prevented from tunneling through other moving bodies? Note that all bodies are prevented from tunneling through kinematic and static bodies. This setting is only considered on dynamic bodies. warning: You should use this flag sparingly since it increases processing time.");
ImGui::SetTooltip("Is this a fast moving body that should be prevented from tunneling through other moving bodies?\n\
Note that all bodies are prevented from tunneling through kinematic and static bodies.\n\
This setting is only considered on dynamic bodies. \n\
warning: You should use this flag sparingly since it increases processing time.");
}
// TODO: Expose other body parameters (damping, gravity scale, sleeping, etc)

@ -0,0 +1,159 @@
/******************************************************************************
* File - component_api.cpp
* Author - Joey Pollack
* Date - 2023/01/05 (y/m/d)
* Mod Date - 2023/01/05 (y/m/d)
* Description - static methods for all component related foreign classes
******************************************************************************/
#include "component_api.h"
#include <world/components.h>
#include <world/world.h>
#include <world/entity.h>
namespace lunarium
{
World* ComponentAPI::mpWorld = nullptr;
void ComponentAPI::Initialize(World* pWorld, WrenState& script_state)
{
mpWorld = pWorld;
SetupForeignClassVelocityComp(script_state);
SetupForeignClassBlockOutComp(script_state);
}
/////////////////////////////////////////////////////////////////////
// VELOCITY COMPONENT
/////////////////////////////////////////////////////////////////////
void ComponentAPI::SetupForeignClassVelocityComp(WrenState& script_state)
{
script_state.RegisterForeignClass({"Components", "VelocityComponent", sizeof(VelocityComponent*),
{(WrenForeignMethodFn)&ComponentAPI::VelocityCompAllocate, &ComponentAPI::VelocityCompFinalise}});
script_state.RegisterForeignMethod({"Components", "VelocityComponent", false, "GetVelocityX()",&ComponentAPI::GetVelocityXFM});
script_state.RegisterForeignMethod({"Components", "VelocityComponent", false, "GetVelocityY()",(WrenForeignMethodFn)&ComponentAPI::GetVelocityYFM});
script_state.RegisterForeignMethod({"Components", "VelocityComponent", false, "SetVelocity(_,_)", (WrenForeignMethodFn)&ComponentAPI::SetVelocityFM});
}
void ComponentAPI::VelocityCompAllocate(WrenVM* vm)
{
VelocityComponent** vcp = (VelocityComponent**)wrenSetSlotNewForeign(vm, 0, 0, sizeof(VelocityComponent*));
std::string str_entity_id = wrenGetSlotString(vm, 1);
LUUID entity_id = std::stoull(str_entity_id);
Entity* pEnt = mpWorld->GetEntity(entity_id);
if (!pEnt->HasComponent<VelocityComponent>())
{
Logger::Error(LogCategory::SCRIPTING, "Cannot allocate foreign class VelocityComponent - the entity does not have the component");
(*vcp) = nullptr;
}
(*vcp) = &pEnt->GetComponent<VelocityComponent>();
}
void ComponentAPI::VelocityCompFinalise(void* data)
{
// Nothing to clean up
}
void ComponentAPI::GetVelocityXFM(WrenVM* vm)
{
VelocityComponent** vcp = (VelocityComponent**)wrenGetSlotForeign(vm, 0);
wrenEnsureSlots(vm, 1);
wrenSetSlotDouble(vm, 0, (*vcp)->Velocity.x);
}
void ComponentAPI::GetVelocityYFM(WrenVM* vm)
{
VelocityComponent** vcp = (VelocityComponent**)wrenGetSlotForeign(vm, 0);
wrenEnsureSlots(vm, 1);
wrenSetSlotDouble(vm, 0, (*vcp)->Velocity.y);
}
void ComponentAPI::SetVelocityFM(WrenVM* vm)
{
VelocityComponent** vcp = (VelocityComponent**)wrenGetSlotForeign(vm, 0);
double x = wrenGetSlotDouble(vm, 1);
double y = wrenGetSlotDouble(vm, 2);
(*vcp)->Velocity.x = x;
(*vcp)->Velocity.y = y;
}
/////////////////////////////////////////////////////////////////////
// BLOCKOUT COMPONENT
/////////////////////////////////////////////////////////////////////
void ComponentAPI::SetupForeignClassBlockOutComp(WrenState& script_state)
{
script_state.RegisterForeignClass({"Components", "BlockOutComponent", sizeof(BlockOutComponent*),
{(WrenForeignMethodFn)&ComponentAPI::BlockOutCompAllocate, &ComponentAPI::BlockOutCompFinalise}});
script_state.RegisterForeignMethod({"Components", "BlockOutComponent", false, "GetColor()",&ComponentAPI::GetColorFM});
script_state.RegisterForeignMethod({"Components", "BlockOutComponent", false, "GetSize()",(WrenForeignMethodFn)&ComponentAPI::GetSizeFM});
script_state.RegisterForeignMethod({"Components", "BlockOutComponent", false, "SetColor(_,_,_,_)", (WrenForeignMethodFn)&ComponentAPI::SetColorFM});
script_state.RegisterForeignMethod({"Components", "BlockOutComponent", false, "SetSize(_,_)", (WrenForeignMethodFn)&ComponentAPI::SetSizeFM});
}
void ComponentAPI::BlockOutCompAllocate(WrenVM* vm)
{
BlockOutComponent** vcp = (BlockOutComponent**)wrenSetSlotNewForeign(vm, 0, 0, sizeof(BlockOutComponent*));
std::string str_entity_id = wrenGetSlotString(vm, 1);
LUUID entity_id = std::stoull(str_entity_id);
Entity* pEnt = mpWorld->GetEntity(entity_id);
if (!pEnt->HasComponent<BlockOutComponent>())
{
Logger::Error(LogCategory::SCRIPTING, "Cannot allocate foreign class BlockOutComponent - the entity does not have the component");
(*vcp) = nullptr;
}
(*vcp) = &pEnt->GetComponent<BlockOutComponent>();
}
void ComponentAPI::BlockOutCompFinalise(void* data)
{
// Nothing to clean up
}
void ComponentAPI::GetColorFM(WrenVM* vm)
{
BlockOutComponent** vcp = (BlockOutComponent**)wrenGetSlotForeign(vm, 0);
wrenEnsureSlots(vm, 5);
wrenSetSlotDouble(vm, 1, (*vcp)->Color.r);
wrenSetSlotDouble(vm, 2, (*vcp)->Color.g);
wrenSetSlotDouble(vm, 3, (*vcp)->Color.b);
wrenSetSlotDouble(vm, 4, (*vcp)->Color.a);
wrenSetSlotNewList(vm, 0);
wrenInsertInList(vm, 0, 0, 4);
wrenInsertInList(vm, 0, 0, 3);
wrenInsertInList(vm, 0, 0, 2);
wrenInsertInList(vm, 0, 0, 1);
}
void ComponentAPI::GetSizeFM(WrenVM* vm)
{
BlockOutComponent** vcp = (BlockOutComponent**)wrenGetSlotForeign(vm, 0);
wrenEnsureSlots(vm, 3);
wrenSetSlotDouble(vm, 1, (*vcp)->Size.x);
wrenSetSlotDouble(vm, 2, (*vcp)->Size.y);
wrenSetSlotNewList(vm, 0);
wrenInsertInList(vm, 0, 0, 2);
wrenInsertInList(vm, 0, 0, 1);
}
void ComponentAPI::SetColorFM(WrenVM* vm)
{
BlockOutComponent** vcp = (BlockOutComponent**)wrenGetSlotForeign(vm, 0);
(*vcp)->Color.r = wrenGetSlotDouble(vm, 1);
(*vcp)->Color.g = wrenGetSlotDouble(vm, 2);
(*vcp)->Color.b = wrenGetSlotDouble(vm, 3);
(*vcp)->Color.a = wrenGetSlotDouble(vm, 4);
}
void ComponentAPI::SetSizeFM(WrenVM* vm)
{
BlockOutComponent** vcp = (BlockOutComponent**)wrenGetSlotForeign(vm, 0);
(*vcp)->Size.x = wrenGetSlotDouble(vm, 1);
(*vcp)->Size.y = wrenGetSlotDouble(vm, 2);
}
}

@ -0,0 +1,48 @@
/******************************************************************************
* File - component_api.h
* Author - Joey Pollack
* Date - 2023/01/05 (y/m/d)
* Mod Date - 2023/01/05 (y/m/d)
* Description - static methods for all component related foreign classes
******************************************************************************/
#ifndef LUNARIUM_COMPONENT_API_H_
#define LUNARIUM_COMPONENT_API_H_
#include <core/common_defs.h>
#include <scripting/wren_state.h>
#include <scripting/wren_script.h>
#include <utils/op_res.h>
namespace lunarium
{
class World;
class ComponentAPI
{
public:
static void Initialize(World* pWorld, WrenState& script_state);
// VelocityComponent
static void SetupForeignClassVelocityComp(WrenState& script_state);
static void VelocityCompAllocate(WrenVM* vm);
static void VelocityCompFinalise(void* data);
static void GetVelocityXFM(WrenVM* vm);
static void GetVelocityYFM(WrenVM* vm);
static void SetVelocityFM(WrenVM* vm);
// Blockout Component
static void SetupForeignClassBlockOutComp(WrenState& script_state);
static void BlockOutCompAllocate(WrenVM* vm);
static void BlockOutCompFinalise(void* data);
static void GetColorFM(WrenVM* vm);
static void GetSizeFM(WrenVM* vm);
static void SetColorFM(WrenVM* vm);
static void SetSizeFM(WrenVM* vm);
private:
static World* mpWorld;
};
}
#endif // LUNARIUM_COMPONENT_API_H_

@ -20,3 +20,12 @@ foreign class VelocityComponent {
}
foreign class BlockOutComponent {
construct new(entity_id) {}
foreign GetColor()
foreign SetColor(r, g, b, a)
foreign GetSize()
foreign SetSize(w, h)
}

@ -6,7 +6,7 @@
* Description - The main interface for scripts to interact with the game world
******************************************************************************/
import "Components" for VelocityComponent
import "Components" for VelocityComponent, BlockOutComponent
// Manages all of the EntityBehaviors
class WorldInterface {
@ -51,6 +51,10 @@ class WorldInterface {
static GetVelocityComponent(entity_id) {
return VelocityComponent.new(entity_id)
}
static GetBlockOutComponent(entity_id) {
return BlockOutComponent.new(entity_id)
}
}

@ -7,8 +7,9 @@
******************************************************************************/
#include "world_api.h"
#include "world.h"
#include "entity.h"
#include "component_api.h"
#include <world/world.h>
#include <world/entity.h>
#include <utils/logger.h>
#include <utils/helpers.h>
@ -29,8 +30,8 @@ namespace lunarium
// Register foreign methods
//mScriptState.RegisterForeignMethod({"WorldInterface", "WorldInterface", true, "GetComponent(_,_)", (WrenForeignMethodFn)&WorldAPI::GetVelocityComponent});
// Register foreign classes
SetupForeignClassVelocityComp();
// Register component foreign classes
ComponentAPI::Initialize(mpWorld, mScriptState);
// Load the component script
WrenScript components;
@ -111,57 +112,5 @@ namespace lunarium
// FOREIGN CLASSES
/////////////////////////////////////////////////////////////////////
void WorldAPI::SetupForeignClassVelocityComp()
{
mScriptState.RegisterForeignClass({"Components", "VelocityComponent", sizeof(VelocityComponent*),
{(WrenForeignMethodFn)&WorldAPI::VelocityCompAllocate, &WorldAPI::VelocityCompFinalise}});
mScriptState.RegisterForeignMethod({"Components", "VelocityComponent", false, "GetVelocityX()",&WorldAPI::GetVelocityXFM});
mScriptState.RegisterForeignMethod({"Components", "VelocityComponent", false, "GetVelocityY()",(WrenForeignMethodFn)&WorldAPI::GetVelocityYFM});
mScriptState.RegisterForeignMethod({"Components", "VelocityComponent", false, "SetVelocity(_,_)", (WrenForeignMethodFn)&WorldAPI::SetVelocityFM});
}
void WorldAPI::VelocityCompAllocate(WrenVM* vm)
{
VelocityComponent** vcp = (VelocityComponent**)wrenSetSlotNewForeign(vm, 0, 0, sizeof(VelocityComponent*));
std::string str_entity_id = wrenGetSlotString(vm, 1);
LUUID entity_id = std::stoull(str_entity_id);
Entity* pEnt = mpWorld->GetEntity(entity_id);
if (!pEnt->HasComponent<VelocityComponent>())
{
Logger::Error(LogCategory::SCRIPTING, "Cannot allocate foreign class VelocityComponent - the entity does not have the component");
(*vcp) = nullptr;
}
(*vcp) = &pEnt->GetComponent<VelocityComponent>();
}
void WorldAPI::VelocityCompFinalise(void* data)
{
// Nothing to clean up
}
void WorldAPI::GetVelocityXFM(WrenVM* vm)
{
VelocityComponent** vcp = (VelocityComponent**)wrenGetSlotForeign(vm, 0);
wrenEnsureSlots(vm, 1);
wrenSetSlotDouble(vm, 0, (*vcp)->Velocity.x);
}
void WorldAPI::GetVelocityYFM(WrenVM* vm)
{
VelocityComponent** vcp = (VelocityComponent**)wrenGetSlotForeign(vm, 0);
wrenEnsureSlots(vm, 1);
wrenSetSlotDouble(vm, 0, (*vcp)->Velocity.y);
}
void WorldAPI::SetVelocityFM(WrenVM* vm)
{
VelocityComponent** vcp = (VelocityComponent**)wrenGetSlotForeign(vm, 0);
double x = wrenGetSlotDouble(vm, 1);
double y = wrenGetSlotDouble(vm, 2);
(*vcp)->Velocity.x = x;
(*vcp)->Velocity.y = y;
}
}

@ -49,12 +49,7 @@ namespace lunarium
static void GetVelocityComponent(WrenVM* vm);
// Foreign classes
static void SetupForeignClassVelocityComp();
static void VelocityCompAllocate(WrenVM* vm);
static void VelocityCompFinalise(void* data);
static void GetVelocityXFM(WrenVM* vm);
static void GetVelocityYFM(WrenVM* vm);
static void SetVelocityFM(WrenVM* vm);
private:
static WrenState mScriptState;

@ -10,7 +10,7 @@
******************************************************************************/
#include "world.h"
#include "world_api.h"
#include <scripting/world_api.h>
#include <LunariumConfig.h>
#include <utils/logger.h>
#include <utils/helpers.h>

Loading…
Cancel
Save