From 5491251ff6de46c9788e5e5460dd12ef0c47aa25 Mon Sep 17 00:00:00 2001 From: BlackMATov Date: Wed, 15 Jan 2020 19:32:41 +0700 Subject: [PATCH] rigid_body type --- .../enduro2d/high/components/rigid_body.hpp | 23 +++++++++ samples/bin/library/scenes/sample_08.json | 51 ++++++++++++++++++- .../bin/library/scripts/sample_08/ship.lua | 9 ---- .../enduro2d/high/components/rigid_body.cpp | 26 +++++++++- 4 files changed, 96 insertions(+), 13 deletions(-) diff --git a/headers/enduro2d/high/components/rigid_body.hpp b/headers/enduro2d/high/components/rigid_body.hpp index b010d659..23810e9f 100644 --- a/headers/enduro2d/high/components/rigid_body.hpp +++ b/headers/enduro2d/high/components/rigid_body.hpp @@ -15,11 +15,22 @@ namespace e2d { class rigid_body final { + public: + ENUM_HPP_CLASS_DECL(types, u8, + (dynamic) + (kinematic)) public: rigid_body() = default; + + rigid_body& type(types value) noexcept; + [[nodiscard]] types type() const noexcept; + private: + types type_ = types::dynamic; }; } +ENUM_HPP_REGISTER_TRAITS(e2d::rigid_body::types) + namespace e2d { template <> @@ -47,3 +58,15 @@ namespace e2d void operator()(gcomponent& c) const; }; } + +namespace e2d +{ + inline rigid_body& rigid_body::type(types value) noexcept { + type_ = value; + return *this; + } + + inline rigid_body::types rigid_body::type() const noexcept { + return type_; + } +} diff --git a/samples/bin/library/scenes/sample_08.json b/samples/bin/library/scenes/sample_08.json index b21f1514..0b4f55fd 100644 --- a/samples/bin/library/scenes/sample_08.json +++ b/samples/bin/library/scenes/sample_08.json @@ -6,21 +6,68 @@ } }, "children" : [{ - "prototype" : "../prefabs/camera_prefab.json" + "prototype" : "../prefabs/camera_prefab.json", + "components" : { + "named" : { + "name" : "camera" + } + } },{ "prototype" : "../prefabs/ship_prefab.json", "components" : { + "named" : { + "name" : "ship(1)" + }, "actor" : { "translation" : [0,0], - "scale" : 1 + "rotation" : 1 }, "behaviour" : { "script" : "../scripts/sample_08/ship.lua" }, "touchable" : {}, + "rigid_body" : { + "type" : "dynamic" + }, "rect_collider" : { "size" : [66,113] } } + },{ + "prototype" : "../prefabs/ship_prefab.json", + "components" : { + "named" : { + "name" : "ship(2)" + }, + "actor" : { + "translation" : [45,100], + "rotation" : 0 + }, + "behaviour" : { + "script" : "../scripts/sample_08/ship.lua" + }, + "touchable" : {}, + "rigid_body" : { + "type" : "dynamic" + }, + "rect_collider" : { + "size" : [66,113] + } + } + },{ + "components" : { + "named" : { + "name" : "floor" + }, + "actor" : { + "translation" : [0,-240] + }, + "rigid_body" : { + "type" : "kinematic" + }, + "rect_collider" : { + "size" : [320, 50] + } + } }] } diff --git a/samples/bin/library/scripts/sample_08/ship.lua b/samples/bin/library/scripts/sample_08/ship.lua index b0f8f5a4..9ed09365 100644 --- a/samples/bin/library/scripts/sample_08/ship.lua +++ b/samples/bin/library/scripts/sample_08/ship.lua @@ -6,13 +6,6 @@ ---@class ship_meta ----@param meta ship_meta ----@param go gobject -local function update_ship_rotation(meta, go) - local time = the_engine.time - go.actor.node.rotation = time -end - -- ----------------------------------------------------------------------------- -- -- meta @@ -24,13 +17,11 @@ local M = {} ---@param meta ship_meta ---@param go gobject function M.on_start(meta, go) - meta.life_time = 5 end ---@param meta ship_meta ---@param go gobject function M.on_update(meta, go) - update_ship_rotation(meta, go) end return M diff --git a/sources/enduro2d/high/components/rigid_body.cpp b/sources/enduro2d/high/components/rigid_body.cpp index a66ce5b9..8991d15b 100644 --- a/sources/enduro2d/high/components/rigid_body.cpp +++ b/sources/enduro2d/high/components/rigid_body.cpp @@ -13,6 +13,16 @@ namespace e2d "required" : [], "additionalProperties" : false, "properties" : { + "type" : { "$ref": "#/definitions/types" } + }, + "definitions" : { + "types" : { + "type" : "string", + "enum" : [ + "dynamic", + "kinematic" + ] + } } })json"; @@ -20,7 +30,15 @@ namespace e2d rigid_body& component, const fill_context& ctx) const { - E2D_UNUSED(component, ctx); + if ( ctx.root.HasMember("type") ) { + rigid_body::types type = component.type(); + if ( !json_utils::try_parse_value(ctx.root["type"], type) ) { + the().error("RIGID_BODY: Incorrect formatting of 'type' property"); + return false; + } + component.type(type); + } + return true; } @@ -38,6 +56,10 @@ namespace e2d const char* component_inspector::title = "rigid_body"; void component_inspector::operator()(gcomponent& c) const { - E2D_UNUSED(c); + if ( rigid_body::types type = c->type(); + imgui_utils::show_enum_combo_box("type", &type) ) + { + c->type(type); + } } }