diff --git a/CMakeLists.txt b/CMakeLists.txt index cb2370f..1a592c9 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -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" ) diff --git a/docs/tasks/editor.todo b/docs/tasks/editor.todo index 3c62f8f..76887d4 100644 --- a/docs/tasks/editor.todo +++ b/docs/tasks/editor.todo @@ -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) diff --git a/src/run_modes/editor/component_guis.cpp b/src/run_modes/editor/component_guis.cpp index eaddb71..0255830 100644 --- a/src/run_modes/editor/component_guis.cpp +++ b/src/run_modes/editor/component_guis.cpp @@ -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) diff --git a/src/scripting/component_api.cpp b/src/scripting/component_api.cpp new file mode 100644 index 0000000..61aee8c --- /dev/null +++ b/src/scripting/component_api.cpp @@ -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 +#include +#include + +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()) + { + Logger::Error(LogCategory::SCRIPTING, "Cannot allocate foreign class VelocityComponent - the entity does not have the component"); + (*vcp) = nullptr; + } + + (*vcp) = &pEnt->GetComponent(); + } + + 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()) + { + Logger::Error(LogCategory::SCRIPTING, "Cannot allocate foreign class BlockOutComponent - the entity does not have the component"); + (*vcp) = nullptr; + } + + (*vcp) = &pEnt->GetComponent(); + } + + 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); + } + +} \ No newline at end of file diff --git a/src/scripting/component_api.h b/src/scripting/component_api.h new file mode 100644 index 0000000..5336d84 --- /dev/null +++ b/src/scripting/component_api.h @@ -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 +#include +#include +#include + +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_ \ No newline at end of file diff --git a/src/scripting/internal_scripts/components.wren b/src/scripting/internal_scripts/components.wren index 6e3a25d..bc1ff9a 100644 --- a/src/scripting/internal_scripts/components.wren +++ b/src/scripting/internal_scripts/components.wren @@ -19,4 +19,13 @@ foreign class VelocityComponent { foreign SetVelocity(x, y) +} + +foreign class BlockOutComponent { + construct new(entity_id) {} + + foreign GetColor() + foreign SetColor(r, g, b, a) + foreign GetSize() + foreign SetSize(w, h) } \ No newline at end of file diff --git a/src/scripting/internal_scripts/world_interface.wren b/src/scripting/internal_scripts/world_interface.wren index fc7c505..d9b55aa 100644 --- a/src/scripting/internal_scripts/world_interface.wren +++ b/src/scripting/internal_scripts/world_interface.wren @@ -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) + } } diff --git a/src/world/world_api.cpp b/src/scripting/world_api.cpp similarity index 62% rename from src/world/world_api.cpp rename to src/scripting/world_api.cpp index d21be3c..f5abd27 100644 --- a/src/world/world_api.cpp +++ b/src/scripting/world_api.cpp @@ -7,8 +7,9 @@ ******************************************************************************/ #include "world_api.h" -#include "world.h" -#include "entity.h" +#include "component_api.h" +#include +#include #include #include @@ -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()) - { - Logger::Error(LogCategory::SCRIPTING, "Cannot allocate foreign class VelocityComponent - the entity does not have the component"); - (*vcp) = nullptr; - } - - (*vcp) = &pEnt->GetComponent(); - } - - 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; - } + } \ No newline at end of file diff --git a/src/world/world_api.h b/src/scripting/world_api.h similarity index 80% rename from src/world/world_api.h rename to src/scripting/world_api.h index e73ab4b..87799b9 100644 --- a/src/world/world_api.h +++ b/src/scripting/world_api.h @@ -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; diff --git a/src/world/world.cpp b/src/world/world.cpp index e98e751..e8d9912 100644 --- a/src/world/world.cpp +++ b/src/world/world.cpp @@ -10,7 +10,7 @@ ******************************************************************************/ #include "world.h" -#include "world_api.h" +#include #include #include #include