BallActor.cpp 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. // ----------------------------------------------------------------
  2. // From Game Programming in C++ by Sanjay Madhav
  3. // Copyright (C) 2017 Sanjay Madhav. All rights reserved.
  4. //
  5. // Released under the BSD License
  6. // See LICENSE in root directory for full details.
  7. // ----------------------------------------------------------------
  8. #include "BallActor.h"
  9. #include "Game.h"
  10. #include "Renderer.h"
  11. #include "MeshComponent.h"
  12. #include "Mesh.h"
  13. #include "BallMove.h"
  14. #include "AudioComponent.h"
  15. #include "LevelLoader.h"
  16. BallActor::BallActor(Game* game)
  17. :Actor(game)
  18. ,mLifeSpan(2.0f)
  19. {
  20. //SetScale(10.0f);
  21. MeshComponent* mc = new MeshComponent(this);
  22. Mesh* mesh = GetGame()->GetRenderer()->GetMesh("Assets/Sphere.gpmesh");
  23. mc->SetMesh(mesh);
  24. BallMove* move = new BallMove(this);
  25. move->SetForwardSpeed(1500.0f);
  26. mAudioComp = new AudioComponent(this);
  27. }
  28. void BallActor::UpdateActor(float deltaTime)
  29. {
  30. Actor::UpdateActor(deltaTime);
  31. mLifeSpan -= deltaTime;
  32. if (mLifeSpan < 0.0f)
  33. {
  34. SetState(EDead);
  35. }
  36. }
  37. void BallActor::HitTarget()
  38. {
  39. mAudioComp->PlayEvent("event:/Ding");
  40. }
  41. void BallActor::LoadProperties(const rapidjson::Value& inObj)
  42. {
  43. Actor::LoadProperties(inObj);
  44. JsonHelper::GetFloat(inObj, "lifespan", mLifeSpan);
  45. }
  46. void BallActor::SaveProperties(rapidjson::Document::AllocatorType& alloc, rapidjson::Value& inObj) const
  47. {
  48. Actor::SaveProperties(alloc, inObj);
  49. JsonHelper::AddFloat(alloc, inObj, "lifespan", mLifeSpan);
  50. }