TimeOfDay.cpp 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. // Copyright (c) 2014-2015, THUNDERBEAST GAMES LLC All rights reserved
  2. // Please see LICENSE.md in repository root for license information
  3. // https://github.com/AtomicGameEngine/AtomicGameEngine
  4. #include "Precompiled.h"
  5. #include "../Core/Context.h"
  6. #include "../Scene/Node.h"
  7. #include "../Scene/Scene.h"
  8. #include "../Scene/SceneEvents.h"
  9. #include "../Graphics/Light.h"
  10. #include "../Environment/ProcSky.h"
  11. #include "../Environment/TimeOfDay.h"
  12. namespace Atomic
  13. {
  14. TimeOfDay::TimeOfDay(Context *context) : Component(context),
  15. timeOn_(0.0f),
  16. timeOff_(0.0f),
  17. on_(true)
  18. {
  19. }
  20. TimeOfDay::~TimeOfDay()
  21. {
  22. }
  23. void TimeOfDay::OnNodeSet(Node* node)
  24. {
  25. Component::OnNodeSet(node);
  26. if (node && node->GetScene())
  27. {
  28. SubscribeToEvent(node->GetScene(), E_SCENEUPDATE, HANDLER(TimeOfDay, HandleSceneUpdate));
  29. }
  30. }
  31. #include "stdio.h"
  32. void TimeOfDay::HandleSceneUpdate(StringHash eventType, VariantMap& eventData)
  33. {
  34. float timeOfDay = ProcSky::GetTimeOfDay();
  35. if (timeOfDay >= timeOn_ || timeOfDay < timeOff_)
  36. {
  37. if (!on_)
  38. {
  39. PODVector<Component*> components;
  40. node_->GetComponents(components, Light::GetTypeStatic());
  41. for (unsigned i = 0; i < components.Size(); i++)
  42. {
  43. Light* light = (Light*) components[i];
  44. light->SetEnabled(true);
  45. }
  46. }
  47. on_ = true;
  48. }
  49. else
  50. {
  51. if (on_)
  52. {
  53. PODVector<Component*> components;
  54. node_->GetComponents(components, Light::GetTypeStatic());
  55. for (unsigned i = 0; i < components.Size(); i++)
  56. {
  57. Light* light = (Light*) components[i];
  58. light->SetEnabled(false);
  59. }
  60. }
  61. on_ = false;
  62. }
  63. }
  64. void TimeOfDay::RegisterObject(Context* context)
  65. {
  66. context->RegisterFactory<TimeOfDay>();
  67. ACCESSOR_ATTRIBUTE("TimeOn", GetTimeOn, SetTimeOn, float, 0.0f, AM_DEFAULT);
  68. ACCESSOR_ATTRIBUTE("TimeOff", GetTimeOff, SetTimeOff, float, 0.0f, AM_DEFAULT);
  69. }
  70. }