Game.cpp 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. #include "Game.h"
  2. #include "Joystick.h"
  3. #include "Player.h"
  4. #include "res.h"
  5. #include "Enemy.h"
  6. Game::Game()
  7. {
  8. }
  9. Game::~Game()
  10. {
  11. }
  12. void Game::init()
  13. {
  14. //scene layer would have size of display
  15. setSize(getStage()->getSize());
  16. //create background
  17. spSprite sky = new Sprite;
  18. sky->setResAnim(res::ui.getResAnim("sky"));
  19. sky->attachTo(this);
  20. //create player ship
  21. _player = new Player;
  22. _player->init(getSize() / 2, this);
  23. //create separate layer for elements virtual joystick and other UI in future
  24. _ui = new Actor;
  25. _ui->attachTo(this);
  26. //it would be higher than other actors with default priority = 0
  27. _ui->setPriority(1);
  28. //create virtual joystick and attach it to UI
  29. _move = new Joystick;
  30. _move->attachTo(_ui);
  31. _move->setY(getHeight() - _move->getHeight());
  32. //create virtual joystick and attach it to UI
  33. _fire = new Joystick;
  34. _fire->attachTo(_ui);
  35. _fire->setX(getWidth() - _fire->getWidth());
  36. _fire->setY(getHeight() - _fire->getHeight());
  37. //create enemies
  38. for (int i = 0; i < 10; ++i)
  39. {
  40. spEnemy enemy = new Enemy;
  41. enemy->init(Vector2(scalar::randFloat(0, getWidth()), scalar::randFloat(0, getHeight())), this);
  42. }
  43. }
  44. void Game::doUpdate(const UpdateState& us)
  45. {
  46. //update all units
  47. //ship, rocket and enemies are in this list
  48. for (units::iterator i = _units.begin(); i != _units.end();)
  49. {
  50. spUnit unit = *i;
  51. unit->update(us);
  52. if (unit->isDead())
  53. {
  54. //it is dead. Time to remove it from list
  55. i = _units.erase(i);
  56. }
  57. else
  58. {
  59. ++i;
  60. }
  61. }
  62. }