From: unknown Date: Fri, 28 Jan 2011 18:28:19 +0000 (-0600) Subject: Added Sprite getCollisions getName added ability to check all collisions given a... X-Git-Url: http://git.mmlx.us/?a=commitdiff_plain;h=f1e72f20bba367fbf5c16de4e0296637555f1fca;p=IvanGame.git Added Sprite getCollisions getName added ability to check all collisions given a vec& completed framework for checking collisions --- diff --git a/Collision.cpp b/Collision.cpp index 5f8b60e..9014c5c 100644 --- a/Collision.cpp +++ b/Collision.cpp @@ -183,8 +183,18 @@ void CollisionCircle::draw(const Point2D& pos){ Sulock(Game::game()->Screen()); } +bool Collision::checkCollisions(const vector& c){ + for(int i=0; i < c.size(); i++){ + if(collision(c[i]))//if any are true return true then and there + return true; + } + return false;//if none were found then return false. +} + bool CollisionRectangle::collision(const Collision *c){ if(dynamic_cast(c)){ + const CollisionRectangle* rec = dynamic_cast(c); + } else if(dynamic_cast(c)){ } @@ -194,7 +204,11 @@ bool CollisionRectangle::collision(const Collision *c){ bool CollisionCircle::collision(const Collision *c){ if (dynamic_cast(c)) { } - else if(dynamic_cast(c)){ + else if(const CollisionCircle* col = dynamic_cast(c)){ + if((col->mPos - mPos).length() < col->radius + radius) + return true; + else + return false; } return false; diff --git a/Collision.h b/Collision.h index 5a0481b..a598941 100644 --- a/Collision.h +++ b/Collision.h @@ -19,6 +19,7 @@ class Collision public: Collision():mPos(0.0,0.0),color(0xFF00FF00){ } Point2D mPos;/**< The position of the center of the collision data */ + bool checkCollisions(const vector& c); /**< Check collision with objects */ virtual bool collision(const Collision *c) = 0; /**< Check collision with objects */ /// \todo See this http://www.metanetsoftware.com/technique/tutorialA.html virtual void draw(const Point2D& pos) = 0;/**< Draws the collision data to the screen */ diff --git a/Level.cpp b/Level.cpp index c8d6d1c..4ba86c6 100644 --- a/Level.cpp +++ b/Level.cpp @@ -68,7 +68,7 @@ void Level::drawScene() /** Draw Boundary Data */ DrawCollisions(); - + /** Flip the working image buffer with the mScreen buffer */ SDL_Flip (mScreen); } @@ -108,6 +108,9 @@ void Level::postEvent(SDL_Event event) void Level::update(){ /// \todo fix this too //mBehavior.update(); + + //check collision with sun sprite + mSprites[0]->collisionWithSprite("sun"); } @@ -131,3 +134,11 @@ void Level::addActor(string name, Actor actor){ void Level::removeActor(string name){ mActors.erase(name); } + +Sprite* Level::findSpriteByName(string name){ + for(int i=0; i < mSprites.size(); i++){ + if(mSprites[i]->getName()==name)//find the sprite with the same name + return mSprites[i]; + } + return NULL;//if a sprite wasn't found return null +} \ No newline at end of file diff --git a/Level.h b/Level.h index 48d0e01..c7ecbd0 100644 --- a/Level.h +++ b/Level.h @@ -39,6 +39,7 @@ public: void addActor(string name, Actor actor);///< add a Actor to the list of sprites void removeActor(string name);///< remove the Actor sp from the list of sprites string getName(){ return mName; }///< returns the current level's name + Sprite* findSpriteByName(string name);///< returns the first Sprite with a given name protected://allow inheritance string mName; @@ -52,7 +53,7 @@ protected://allow inheritance void DrawIMG(SDL_Surface *img, int x, int y);/**< Draws the specified image to the screen at location (x,y) */ void DrawIMG(SDL_Surface *img, int x, int y, int w, int h, int x2, int y2);/**< Draws an image at the specified location (x,y) that is blitted with width w and height h from the point (x2,y2) of the given image */ void DrawSprites();/**< Draws all Sprites. */ - void DrawCollisions();/**< Draws all Colision data */ + void DrawCollisions();/**< Draws all Collision data */ }; #endif diff --git a/Sprite.cpp b/Sprite.cpp index dabf21c..5b5c38f 100644 --- a/Sprite.cpp +++ b/Sprite.cpp @@ -4,14 +4,14 @@ using std::cout; using std::endl; Sprite::Sprite(SDL_Surface *screen, std::string name, Actor actor) : - mName(name), +mName(name), mDrawn(0), mLastUpdate(0), mActor(actor), mScreen(screen) { /** \todo Later add a part that adds name to list of level sprites - Game.Level.SpriteList.pushback(name); + Game.Level.SpriteList.pushback(name); */ if(mActor.mAnimations[mActor.mCurrentAnimation] -> mBuilt) { @@ -47,8 +47,26 @@ void Sprite::draw() } +vector& Sprite::getCollisionData(){ + return mActor.mAnimations[mActor.mCurrentAnimation]->mAnim[mActor.mFrame].collisionData; +} + + void Sprite::drawCollisions(){ - SpriteFrame frame = mActor.mAnimations[mActor.mCurrentAnimation]->mAnim[mActor.mFrame]; - Point2D pt = mPos + frame.animationPeg; - WorldObject::drawCollisions(frame.collisionData, pt); -} \ No newline at end of file + //get the frame for readability + SpriteFrame frame = mActor.mAnimations[mActor.mCurrentAnimation]->mAnim[mActor.mFrame]; + //center the location + Point2D pt = mPos + frame.animationPeg; + WorldObject::drawCollisions(frame.collisionData, pt); +} + +Sprite* Sprite::collisionWithSprite(string name){ + //get the frame for readability + SpriteFrame frame = mActor.mAnimations[mActor.mCurrentAnimation]->mAnim[mActor.mFrame]; + //get the first sprite with this name + Sprite* s= Game::game()->getCurrentLevel()->findSpriteByName(name); + for(int i=0; i < frame.collisionData.size(); i++) + if(frame.collisionData[i]->checkCollisions(s->getCollisionData()))//if there is a collision + return s; + return NULL;//if there aren't collisions +} diff --git a/Sprite.h b/Sprite.h index b21c681..ec86c92 100644 --- a/Sprite.h +++ b/Sprite.h @@ -24,13 +24,15 @@ class Sprite : public WorldObject int getFrame() { return mActor.mFrame; }/**< returns active frame. */ void setSpeed(float speed) {mSpeed = speed;}/**< sets the Sprite's speed. */ float getSpeed() {return mSpeed;}/**< returns a Sprite's current speed. */ + std::string getName(){ return mName;}/**< returns the Sprite's name */ void toggleAnim(){ mAnimating = !mAnimating;}/**< Pauses or resumes an animation. */ void startAnim() {mAnimating = 1;}/**< Causes the animation to play. */ void stopAnim() {mAnimating = 0;}/**< Causes the animation to stop. */ void rewind() {mActor.mFrame = 0;}/**< Resets the Sprite's animation to the first frame. */ void draw();/**< draws Sprite. */ void drawCollisions();/**< Draws Collision data for the Object (from outside) */ - + Sprite* collisionWithSprite(string name);/**< checks for collision with sprite of a given name. */ + vector& getCollisionData();/**< Returns collision data */ private: std::string mName;/**< Sprite's name */