Advertisement
Guest User

Untitled

a guest
Sep 23rd, 2014
192
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.22 KB | None | 0 0
  1. #include "game.h"
  2. #include "Galaxian.h"
  3.  
  4. #include <math.h>
  5. #include <iostream>
  6.  
  7. #define PI 3.14159265
  8.  
  9. using namespace Tmpl8;              // to use template classes
  10. using namespace glm;                // to use glm vector stuff
  11.  
  12. Galaxian::Galaxian() {}
  13. Galaxian::~Galaxian() {}
  14.  
  15. Galaxian::Galaxian(Surface* image, int screenWidth)
  16. {
  17.     sprite = image;
  18.     width = screenWidth;
  19.     state = MovingNormally;
  20. }
  21.  
  22. void Galaxian::render(Surface* screen)
  23. {
  24.     sprite->CopyTo(screen, position.x, position.y);
  25. }
  26.  
  27. void Galaxian::update()
  28. {
  29.     if(state == MovingNormally)
  30.     {
  31.         if (Game::getInstance()->movingLeft)
  32.             velocity = vec2(-1, 0);
  33.         else
  34.             velocity = vec2(1, 0);
  35.     }
  36.     else if(state == PrepareArcMovement)
  37.     {
  38.         //move normally during prep
  39.         if (Game::getInstance()->movingLeft)
  40.             velocity = vec2(-1, 0);
  41.         else
  42.             velocity = vec2(1, 0);
  43.  
  44.         //setup old position for returning later
  45.         oldPosition = position;
  46.  
  47.         if (Game::getInstance()->movingLeft)
  48.             oldPosition += vec2(-1, 0);
  49.         else
  50.             oldPosition += vec2(1, 0);
  51.  
  52.         //decide to go left or right
  53.         bool goLeft = true;
  54.  
  55.         if (position.x > width / 2)
  56.         {
  57.             goLeft = false;
  58.         }
  59.  
  60.         //calculate circle center based on direction
  61.         int cx, cy;
  62.         cy = position.y;
  63.  
  64.         if (goLeft)
  65.             cx = position.x - 50;
  66.         else
  67.             cx = position.x + 50;
  68.  
  69.         float angleDif = 180 / steps;
  70.  
  71.         for (int i = 0; i < steps; i++)
  72.         {
  73.             float rad = (angleDif * i * PI) / 180;
  74.             arcPos[i] = vec2(cx + radius * cos(rad), cy + radius * sin(rad));
  75.         }
  76.  
  77.         //init first movement target
  78.         newPosition = arcPos[0];
  79.  
  80.         state = MoveArc;
  81.     }
  82.     else if (state == MoveArc)
  83.     {
  84.         std::cout << "moving" << std::endl;
  85.         //update old position for returning
  86.         if (Game::getInstance()->movingLeft)
  87.             oldPosition += vec2(-1, 0);
  88.         else
  89.             oldPosition += vec2(1, 0);
  90.  
  91.         //move arc
  92.         if (position != newPosition)
  93.         {
  94.             int dX = (newPosition.x - position.x) / updatesPerStep;
  95.             int dY = (newPosition.y - position.y) / updatesPerStep;
  96.  
  97.             position += vec2(dX, dY);
  98.         }
  99.         else
  100.         {
  101.             newPosition = arcPos[nextStep];
  102.             nextStep++;
  103.  
  104.             if (nextStep >= 20)
  105.             {
  106.                 nextStep = 1;
  107.                 state = PrepareDive;
  108.             }
  109.         }
  110.     }
  111.     //if (state == MoveArc)
  112.     //  std::cout << state << std::endl;
  113.  
  114.     position += velocity;
  115. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement