Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "game.h"
- #include "Galaxian.h"
- #include <math.h>
- #include <iostream>
- #define PI 3.14159265
- using namespace Tmpl8; // to use template classes
- using namespace glm; // to use glm vector stuff
- Galaxian::Galaxian() {}
- Galaxian::~Galaxian() {}
- Galaxian::Galaxian(Surface* image, int screenWidth)
- {
- sprite = image;
- width = screenWidth;
- state = MovingNormally;
- }
- void Galaxian::render(Surface* screen)
- {
- sprite->CopyTo(screen, position.x, position.y);
- }
- void Galaxian::update()
- {
- if(state == MovingNormally)
- {
- if (Game::getInstance()->movingLeft)
- velocity = vec2(-1, 0);
- else
- velocity = vec2(1, 0);
- }
- else if(state == PrepareArcMovement)
- {
- //move normally during prep
- if (Game::getInstance()->movingLeft)
- velocity = vec2(-1, 0);
- else
- velocity = vec2(1, 0);
- //setup old position for returning later
- oldPosition = position;
- if (Game::getInstance()->movingLeft)
- oldPosition += vec2(-1, 0);
- else
- oldPosition += vec2(1, 0);
- //decide to go left or right
- bool goLeft = true;
- if (position.x > width / 2)
- {
- goLeft = false;
- }
- //calculate circle center based on direction
- int cx, cy;
- cy = position.y;
- if (goLeft)
- cx = position.x - 50;
- else
- cx = position.x + 50;
- float angleDif = 180 / steps;
- for (int i = 0; i < steps; i++)
- {
- float rad = (angleDif * i * PI) / 180;
- arcPos[i] = vec2(cx + radius * cos(rad), cy + radius * sin(rad));
- }
- //init first movement target
- newPosition = arcPos[0];
- state = MoveArc;
- }
- else if (state == MoveArc)
- {
- std::cout << "moving" << std::endl;
- //update old position for returning
- if (Game::getInstance()->movingLeft)
- oldPosition += vec2(-1, 0);
- else
- oldPosition += vec2(1, 0);
- //move arc
- if (position != newPosition)
- {
- int dX = (newPosition.x - position.x) / updatesPerStep;
- int dY = (newPosition.y - position.y) / updatesPerStep;
- position += vec2(dX, dY);
- }
- else
- {
- newPosition = arcPos[nextStep];
- nextStep++;
- if (nextStep >= 20)
- {
- nextStep = 1;
- state = PrepareDive;
- }
- }
- }
- //if (state == MoveArc)
- // std::cout << state << std::endl;
- position += velocity;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement