Enemy.cpp 947 B

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. #include "Enemy.h"
  2. #include "res.h"
  3. void Enemy::_init()
  4. {
  5. //you could hit enemy 3 times
  6. _hp = 3;
  7. spSprite sprite = new Sprite;
  8. sprite->setResAnim(res::ui.getResAnim("asteroid"));
  9. sprite->attachTo(_view);
  10. sprite->setAnchor(Vector2(0.5f, 0.5f));
  11. //it random scale and rotation
  12. sprite->setRotation(scalar::randFloat(0, MATH_PI * 2));
  13. sprite->setScale(scalar::randFloat(0.5f, 1.0f));
  14. //it is rotating by tween with random speed
  15. float dest = MATH_PI * 2;
  16. if (rand() % 2 == 0)
  17. dest *= -1;
  18. dest += sprite->getRotation();
  19. sprite->addTween(Sprite::TweenRotation(dest), rand() % 15000 + 15000, -1);
  20. }
  21. void Enemy::_update(const UpdateState& us)
  22. {
  23. //nothing to do
  24. }
  25. void Enemy::explode()
  26. {
  27. //hit by rocket
  28. _hp--;
  29. if (_hp == 0)
  30. {
  31. //dead, hide it with alpha tween
  32. _dead = true;
  33. _view->addTween(Actor::TweenAlpha(0), 300)->detachWhenDone();
  34. }
  35. }