Skip to content

Commit

Permalink
balls
Browse files Browse the repository at this point in the history
  • Loading branch information
krystalsavv committed Jan 13, 2018
1 parent 89db8a1 commit 583b830
Show file tree
Hide file tree
Showing 8 changed files with 235 additions and 21 deletions.
108 changes: 108 additions & 0 deletions SourceCode/AI.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
#ifndef _AI_H_
#define _AI_H_
#include <list>
#include "Ball.h"
#include "Animation.h"

class Snace {
Sprite *sprite;
};


class BallAnim {
Ball* ball;
MovingPathAnimation* BallAnimation;
MovingPathAnimator* BallAnimator;

public:
Ball * GetBall() {
return ball;
}
MovingPathAnimation* GetBallAnimation() {
return BallAnimation;
}
MovingPathAnimator* GetBallAnimator() {
return BallAnimator;
}
BallAnim(Ball* ball, MovingPathAnimation* BallAnimation, MovingPathAnimator* BallAnimator) {
this->ball = ball;
this->BallAnimation = BallAnimation;
this->BallAnimator = BallAnimator;
}
};

class AI {
list<BallAnim*> Balls;
Snace *snace;
unsigned delay = 3700;
unsigned long lastTime;
static int sum;

public:
void logic(unsigned long currTime) {
srand(time(0));
while (currTime > lastTime && currTime - lastTime >= delay && (BallsWithState(ANIMATOR_RUNNING)+ BallsWithState(ANIMATOR_READY)) < 3 ){
int col = rand() % 2 + 1;
BallAnim* ballAnim = nullptr;
if (BallsWithState(ANIMATOR_FINISHED)) { // + BallsWithState(ANIMATOR_STOPPED)
for (auto i = Balls.begin(); i != Balls.end(); ++i) {
if ((*i)->GetBallAnimator()->GetState() == ANIMATOR_FINISHED) {
ballAnim = (*i);
ballAnim->GetBallAnimator()->SetState(ANIMATOR_READY);
ballAnim->GetBall()->SetX(330 +((col - 1) * 100));
ballAnim->GetBall()->SetY(140);
break;
}
}
assert(ballAnim);
}
else {
Ball* ball = new Ball("Ball" + to_string(sum), 330 + ((col - 1) * 100), 140);


list<PathEntry> path;
PathEntry *p;
p = new PathEntry(0, 0, 1, 1000); // current position
path.push_back(*p);
for (int i = 0; i < 5; ++i) {
if (rand() % 2)
p = new PathEntry(-50, 74, 1, 1000); // x +/- 50, y + 74;
else
p = new PathEntry(50, 74, 1, 1000);

// thelei kai endiamesa gia na kanei omala to jump

path.push_back(*p);
}

for (int i = 0; i < 30; ++i) {
p = new PathEntry(0, 10, 1, 70);
path.push_back(*p);
}
MovingPathAnimation* BallAnimation = new MovingPathAnimation(path, "Ball" + to_string(sum));
MovingPathAnimator* BallAnimator = new MovingPathAnimator(ball->GetSprite(), BallAnimation);
ballAnim = new BallAnim(ball, BallAnimation, BallAnimator);
Balls.push_back(ballAnim);
}
ballAnim->GetBallAnimator()->Start(game->GetGameTime());
sum++;
lastTime += delay;
}
}

int BallsWithState(animatorstate_t state) {
int count = 0;
for (auto i = Balls.begin(); i != Balls.end(); ++i) {
if ((*i)->GetBallAnimator()->GetState() == state) ++count;
}
return count;
}

AI(unsigned long t) {
snace = nullptr;
lastTime = t;
}
};


#endif
18 changes: 13 additions & 5 deletions SourceCode/Animation.h
Original file line number Diff line number Diff line change
Expand Up @@ -90,14 +90,15 @@ class MovingPathAnimation : public Animation {
enum animatorstate_t {
ANIMATOR_FINISHED = 0,
ANIMATOR_RUNNING = 1,
ANIMATOR_STOPPED = 2
ANIMATOR_STOPPED = 2,
ANIMATOR_READY = 3
};

class Animator {
public:
typedef void(*FinishCallback)(Animator*, void*);
protected:
unsigned long lastTime; // unsigned long savidis
unsigned long lastTime;
animatorstate_t state;
FinishCallback onFinish;
void* finishClosure;
Expand All @@ -106,6 +107,13 @@ class Animator {
(*onFinish)(this, finishClosure);
}
public:
void SetState(animatorstate_t s) {
state = s;
}
animatorstate_t GetState() {
return state;
}

void Stop(void) {
if (!HasFinished()) {
state = ANIMATOR_STOPPED;
Expand All @@ -130,7 +138,7 @@ class Animator {

Animator() {
lastTime = 0;
state = ANIMATOR_FINISHED;
state = ANIMATOR_READY;
SetOnFinish(nullptr,nullptr);
}
virtual ~Animator() {};
Expand All @@ -157,7 +165,6 @@ class MovingPathAnimator : public Animator {
if (currPathFrame == anim->GetEndPathFrame()) {
cout << "finishhhhh" << endl;
state = ANIMATOR_FINISHED;
//AnimatorHolder_MarkAsSuspended(this);
NotifyStopped();
return true;
}
Expand Down Expand Up @@ -238,8 +245,9 @@ class AnimatorHolder {
bool remove = (*i)->Progress(currTime);
auto prev = i;
++i;
if (remove)
if (remove) {
MarkAsSuspended(*prev);
}
}
}
};
Expand Down
86 changes: 86 additions & 0 deletions SourceCode/Ball.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
#ifndef _BALL_H_
#define _BALL_H_
#include "Sprites.h"
#include <ctime>

class Ball {
Sprite *sprite;
public:
void Create(string id, int x, int y) {
AnimationFilm* film = GetFilm();
sprite = new Sprite(id, film, x, y, 38, 43, 20);
spriteList.Insert(sprite);
}

void SetFrame(unsigned FrameNo) {
sprite->SetFrame(FrameNo);
}
void SetX(int x) {
sprite->SetX(x);
}
void SetY(int y) {
sprite->SetY(y);
}
Sprite* GetSprite() {
return sprite;
}

void Destroy(){} //!!!!!!!!!!!! kathe fora pou feygei kapoio ball apo to window

AnimationFilm* GetFilm() {
AnimationFilm* film = AnimationFilmHolder::Get().GetFilm("balls");
if (film == nullptr) {
SDL_Rect rect;
rect.x = 0;
rect.y = 0;
rect.h = 13;
rect.w = 13;
vector<SDL_Rect> boxes;
boxes.push_back(rect);
rect.x = 13;
rect.y = 0;
rect.h = 13;
rect.w = 13;
boxes.push_back(rect);
rect.x = 26;
rect.y = 0;
rect.h = 13;
rect.w = 10;
boxes.push_back(rect);
rect.x = 36;
rect.y = 0;
rect.h = 13;
rect.w = 10;
boxes.push_back(rect);
rect.x = 46;
rect.y = 0;
rect.h = 13;
rect.w = 16;
boxes.push_back(rect);
rect.x = 62;
rect.y = 0;
rect.h = 13;
rect.w = 18;
boxes.push_back(rect);
string id = "balls";
AnimationFilmHolder::Get().Load(id, "Sprites/BallsFilm.bmp", boxes);
film = AnimationFilmHolder::Get().GetFilm("balls");
}
return film;
}

Ball(char *id, int x, int y){
Create(id, x, y);
SetFrame(1);
}

Ball(string id, int x, int y) {
Create(id, x, y);
SetFrame(1);
}


};


#endif
10 changes: 8 additions & 2 deletions SourceCode/Game.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#include "Terrain.h"
#include "Qbert.h"
#include "Animation.h"
#include "AI.h"

Game::Game(){
m_pWindow = nullptr;
Expand Down Expand Up @@ -47,6 +48,7 @@ void Game::render() {


void Game::update() {
ai->logic(GetGameTime());
AnimatorHolder::Progress(currTime);
}

Expand Down Expand Up @@ -139,20 +141,24 @@ SDL_Renderer* Game::GetRenderer() {
}


void Game::SetSprite(IsometricPyramid *terrain,Qbert *qbert,Disk *diskLeft,Disk *diskRight) {
void Game::SetSprite(IsometricPyramid *terrain,Qbert *qbert,Disk *diskLeft,Disk *diskRight, AI *ai) {
this->terrain = terrain;
this->qbert = qbert;
this->diskLeft = diskLeft;
this->diskRight = diskRight;
this->ai = ai;
}

void Game::SetGameTime() {
currTime = time(0) * 1000; //ms
currTime = SDL_GetTicks(); //ms
}
unsigned long Game::GetGameTime() {
return currTime;
}

AI* Game::GetAI() {
return ai;
}


//gia thn SpriteList (einai ligo akyrh edw alla den exw allo cpp arxeio na thn balw)
Expand Down
6 changes: 5 additions & 1 deletion SourceCode/Game.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,15 @@ using namespace std;
class Qbert;
class IsometricPyramid;
class Disk;
class AI;

class Game {
unsigned long currTime = 0;
IsometricPyramid *terrain;
Qbert *qbert;
Disk *diskLeft;
Disk *diskRight;
AI *ai;
public:
Game();
~Game();
Expand All @@ -28,9 +30,11 @@ class Game {
void SetSprite( IsometricPyramid *terrain,
Qbert *qbert,
Disk *diskLeft,
Disk *diskRight);
Disk *diskRight,
AI *ai);
void SetGameTime();
unsigned long GetGameTime();
AI* GetAI();
// a function to access the private running variable
bool running() { return m_bRunning; }
SDL_Renderer* GetRenderer();
Expand Down
12 changes: 6 additions & 6 deletions SourceCode/Qbert.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ class Qbert {
public:
void Create(int x, int y) {
AnimationFilm* film = GetFilm();
sprite = new Sprite("Qbert",film,x, y, 50, 50, 20); //48,48
spriteList.Insert(sprite);
sprite = new Sprite("Qbert", film, x, y, 50, 50, 20); //48,48
spriteList.Insert(sprite);
}

void SetZOrder(unsigned zOrder) { // check an ta allazei kai sthn list
Expand Down Expand Up @@ -44,7 +44,7 @@ class Qbert {
void moveDownLeft() {
currRow++;
sprite->Move(-50, 75);

}
void moveUpRight() {
currRow--;
Expand Down Expand Up @@ -88,13 +88,13 @@ class Qbert {
path.push_back(*p); // oxi polu kalo pou einai pointer alla einai demo
}

MovingPathAnimation* qbertAnimation = new MovingPathAnimation(path, "qbert_Animation1");
MovingPathAnimation* qbertAnimation = new MovingPathAnimation(path, "qbert_Animation1");
MovingPathAnimator* qbertAnimator = new MovingPathAnimator(sprite, qbertAnimation);
qbertAnimator->Start(game->GetGameTime());
}

Qbert(int x, int y){
Create(x,y);
Qbert(int x, int y) {
Create(x, y);
currRow = 1;
currCol = 1;
}
Expand Down
2 changes: 0 additions & 2 deletions SourceCode/Sprites.h
Original file line number Diff line number Diff line change
Expand Up @@ -223,8 +223,6 @@ bool compare(Sprite* s1, Sprite* s2);

class SpriteList final {
list<Sprite *> list;


public:


Expand Down
Loading

0 comments on commit 583b830

Please sign in to comment.