spawn.cpp 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. // Copyright (c) 2022-2023 the Dviglo project
  2. // Copyright (c) 2008-2023 the Urho3D project
  3. // License: MIT
  4. #include "spawn.h"
  5. #include "../game_object.h"
  6. namespace Urho3D
  7. {
  8. Node* SpawnObject(Scene* scene, const Vector3& position, const Quaternion& rotation, const String& className)
  9. {
  10. ResourceCache* cache = scene->GetSubsystem<ResourceCache>();
  11. XMLFile* xml = cache->GetResource<XMLFile>("native_objects/" + className + ".xml");
  12. return scene->InstantiateXML(xml->GetRoot(), position, rotation);
  13. }
  14. Node* SpawnParticleEffect(Scene* scene, const Vector3& position, const String& effectName, float duration, CreateMode mode)
  15. {
  16. ResourceCache* cache = scene->GetSubsystem<ResourceCache>();
  17. Node* newNode = scene->CreateChild("Effect", mode);
  18. newNode->SetPosition(position);
  19. // Create the particle emitter
  20. ParticleEmitter* emitter = newNode->CreateComponent<ParticleEmitter>();
  21. emitter->SetEffect(cache->GetResource<ParticleEffect>(effectName));
  22. // Create a GameObject for managing the effect lifetime. This is always local, so for server-controlled effects it
  23. // exists only on the server
  24. GameObject* object = newNode->CreateComponent<GameObject>(LOCAL);
  25. object->duration = duration;
  26. return newNode;
  27. }
  28. Node* SpawnSound(Scene* scene, const Vector3& position, const String& soundName, float duration)
  29. {
  30. ResourceCache* cache = scene->GetSubsystem<ResourceCache>();
  31. Node* newNode = scene->CreateChild();
  32. newNode->SetPosition(position);
  33. // Create the sound source
  34. SoundSource3D* source = newNode->CreateComponent<SoundSource3D>();
  35. Sound* sound = cache->GetResource<Sound>(soundName);
  36. source->SetDistanceAttenuation(200.0f, 5000.0f, 1.0f);
  37. source->Play(sound);
  38. // Create a GameObject for managing the sound lifetime
  39. GameObject* object = newNode->CreateComponent<GameObject>(LOCAL);
  40. object->duration = duration;
  41. return newNode;
  42. }
  43. } // namespace Urho3D