Sulock(Game::game()->Screen());
}
+bool Collision::checkCollisions(const vector<Collision*>& 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<const CollisionRectangle*>(c)){
+ const CollisionRectangle* rec = dynamic_cast<const CollisionRectangle*>(c);
+
}
else if(dynamic_cast<const CollisionCircle*>(c)){
}
bool CollisionCircle::collision(const Collision *c){
if (dynamic_cast<const CollisionRectangle*>(c)) {
}
- else if(dynamic_cast<const CollisionCircle*>(c)){
+ else if(const CollisionCircle* col = dynamic_cast<const CollisionCircle*>(c)){
+ if((col->mPos - mPos).length() < col->radius + radius)
+ return true;
+ else
+ return false;
}
return false;
public:
Collision():mPos(0.0,0.0),color(0xFF00FF00){ }
Point2D mPos;/**< The position of the center of the collision data */
+ bool checkCollisions(const vector<Collision*>& 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 */
/** Draw Boundary Data */
DrawCollisions();
-
+
/** Flip the working image buffer with the mScreen buffer */
SDL_Flip (mScreen);
}
void Level::update(){
/// \todo fix this too
//mBehavior.update();
+
+ //check collision with sun sprite
+ mSprites[0]->collisionWithSprite("sun");
}
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
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;
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
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)
{
}
+vector<Collision*>& 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
+}
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<Collision*>& getCollisionData();/**< Returns collision data */
private:
std::string mName;/**< Sprite's name */