#include "behavior.h" #include #include #include #include #include #include #include #include #include #include #include #include "../../scenes/worldscape//battle.h" #include "../../scenes/worldscape/terrain/generator.h" #include UF_BEHAVIOR_REGISTER_CPP(ext::CraetureBehavior) #define this (&self) void ext::CraetureBehavior::initialize( uf::Object& self ) { pod::Transform<>& transform = this->getComponent>(); auto& camera = this->getComponent(); camera.getTransform().reference = &transform; uf::Serializer& metadata = this->getComponent(); #if 0 pod::Physics& physics = this->getComponent(); physics.linear.velocity = {0,0,0}; physics.linear.acceleration = {0,-9.81,0}; physics.rotational.velocity = uf::quaternion::axisAngle( {0,1,0}, (pod::Math::num_t) 0 ); physics.rotational.acceleration = {0,0,0,0}; /* Gravity */ { if ( !ext::json::isNull( metadata["system"]["physics"]["gravity"] ) ) { physics.linear.acceleration.x = metadata["system"]["physics"]["gravity"][0].as(); physics.linear.acceleration.y = metadata["system"]["physics"]["gravity"][1].as(); physics.linear.acceleration.z = metadata["system"]["physics"]["gravity"][2].as(); } if ( !metadata["system"]["physics"]["collision"].as() ) { physics.linear.acceleration.x = 0; physics.linear.acceleration.y = 0; physics.linear.acceleration.z = 0; } } /* Collider */ { uf::Collider& collider = this->getComponent(); collider.clear(); auto* box = new uf::BoundingBox( {0, 1.5, 0}, {0.7, 1.6, 0.7} ); box->getTransform().reference = &transform; collider.add(box); } /* RPG */ { ext::HousamoBattle& battle = this->getComponent(); } #endif // Hooks /* struct { uf::Timer flash = uf::Timer(false); uf::Timer sound = uf::Timer(false); } timers; */ static uf::Timer timer(true); this->addHook( "world:Collision.%UID%", [&](ext::json::Value& json){ size_t uid = json["uid"].as(); // do not collide with children // if ( this->findByUid(uid) ) return; pod::Vector3 normal; float scale = metadata["system"]["physics"]["collision"].as(); float depth = json["depth"].as() * scale; // if ( fabs(depth) < 0.005 ) return; //std::cout << "Collision depth: " << depth << std::endl; normal.x = json["normal"][0].as(); normal.y = json["normal"][1].as(); normal.z = json["normal"][2].as(); pod::Vector3 correction = normal * depth; #if 0 transform.position -= correction; if ( normal.x == 1 || normal.x == -1 ) physics.linear.velocity.x = 0; if ( normal.y == 1 || normal.y == -1 ) physics.linear.velocity.y = 0; if ( normal.z == 1 || normal.z == -1 ) physics.linear.velocity.z = 0; #endif }); this->addHook( "asset:Cache.Sound.%UID%", [&](ext::json::Value& json){ uf::Scene& world = uf::scene::getCurrentScene(); uf::Serializer& masterdata = world.getComponent(); std::string filename = json["filename"].as(); if ( uf::io::extension(filename) != "ogg" ) return; if ( filename == "" ) return; uf::Audio& sfx = this->getComponent().add(filename); sfx.setVolume(masterdata["volumes"]["sfx"].as()); auto& pTransform = world.getController().getComponent>(); sfx.setPosition( transform.position ); sfx.play(); }); this->addHook( "world:Craeture.OnHit.%UID%", [&](ext::json::Value& json){ uint64_t phase = json["phase"].as(); // start color pod::Vector4f color = { 1, 1, 1, 0 }; if ( phase == 0 ) { color = pod::Vector4f{ json["color"][0].as(), json["color"][1].as(), json["color"][2].as(), json["color"][3].as(), }; } metadata["color"][0] = color[0]; metadata["color"][1] = color[1]; metadata["color"][2] = color[2]; metadata["color"][3] = color[3]; if ( metadata["timers"]["hurt"].as() < timer.elapsed().asDouble() ) { metadata["timers"]["hurt"] = timer.elapsed().asDouble() + 1.0f; uf::Scene& scene = uf::scene::getCurrentScene(); uf::Asset& assetLoader = scene.getComponent(); assetLoader.cache(uf::io::root+"/audio/battle/hurt.ogg", "asset:Cache.Sound." + std::to_string(this->getUid())); } }); this->addHook( "world:Craeture.Hurt.%UID%", [&](ext::json::Value& json){ if ( metadata["timers"]["flash"].as() < timer.elapsed().asDouble() ) { metadata["timers"]["flash"] = timer.elapsed().asDouble() + 0.4f; for ( int i = 0; i < 16; ++i ) { uf::Serializer payload; payload["phase"] = i % 2 == 0 ? 0 : 1; payload["color"][0] = 1.0f; payload["color"][1] = 0.0f; payload["color"][2] = 0.0f; payload["color"][3] = 0.6f; this->queueHook("world:Craeture.OnHit.%UID%", payload, 0.05f * i); } } }); } void ext::CraetureBehavior::tick( uf::Object& self ) { uf::Serializer& metadata = this->getComponent(); uf::Scene& scene = uf::scene::getCurrentScene(); uf::Serializer& sMetadata = scene.getComponent(); uf::Serializer& pMetadata = scene.getController().getComponent(); #if 0 if ( !pMetadata["system"]["control"].as() ) return; if ( !sMetadata["system"]["physics"]["optimizations"]["entity-local update"].as() ) return; pod::Transform<>& transform = this->getComponent>(); pod::Physics& physics = this->getComponent(); /* Gravity */ { if ( !ext::json::isNull( metadata["system"]["physics"]["gravity"] ) ) { physics.linear.acceleration.x = metadata["system"]["physics"]["gravity"][0].as(); physics.linear.acceleration.y = metadata["system"]["physics"]["gravity"][1].as(); physics.linear.acceleration.z = metadata["system"]["physics"]["gravity"][2].as(); } if ( !metadata["system"]["physics"]["collision"].as() ) { physics.linear.acceleration.x = 0; physics.linear.acceleration.y = 0; physics.linear.acceleration.z = 0; } } transform = uf::physics::update( transform, physics ); #endif } void ext::CraetureBehavior::render( uf::Object& self ){} void ext::CraetureBehavior::destroy( uf::Object& self ){} #undef this