Behaviors sorta implemented
authorIvan Hernanez <iturtleman128@gmail.com>
Sun, 7 Aug 2011 10:02:46 +0000 (05:02 -0500)
committerIvan Hernanez <iturtleman128@gmail.com>
Sun, 7 Aug 2011 10:02:46 +0000 (05:02 -0500)
Behavior.h
Behaviors.h
Game.cpp
Game.h
Level.cpp
Level.h
Sprite.cpp
Sprite.h

index c38fef6..0c21a65 100644 (file)
@@ -17,11 +17,11 @@ using std::string;
 class Behavior
 {
 public:
-       Behavior();
-       Behavior(string Name):name(Name){}/**< \todo finish this */
-       virtual void load();/**< Loading Behavior*/
-       virtual void update();/**< Update Behavior*/
-       virtual void unload();/**< Delete Behavior*/
+       Behavior(){}
+       Behavior(string Name):name(Name){}/**< Loads functions from Game */
+       virtual void load(){}/**< Loading Behavior*/
+       virtual void update(){}/**< Update Behavior*/
+       virtual void unload(){}/**< Delete Behavior*/
 protected:
        string name;/**< Name of this behavior */
 };
index 39cd4e0..fe72e9b 100644 (file)
@@ -17,10 +17,10 @@ using std::string;
 class LevelBehavior : public  Behavior
 {
 public:
-       LevelBehavior();
-       virtual void load();/**< Loading Behavior*/
-       virtual void update();/**< Update Behavior*/
-       virtual void unload();/**< Delete Behavior*/
+       LevelBehavior(){}
+       virtual void load(){}/**< Loading Behavior*/
+       virtual void update(){}/**< Update Behavior*/
+       virtual void unload(){}/**< Delete Behavior*/
        //Level* level;
 };
 
@@ -33,10 +33,10 @@ public:
 class SpriteBehavior : public  Behavior
 {
 public:
-       SpriteBehavior();
-       virtual void load();/**< Loading Behavior*/
-       virtual void update();/**< Update Behavior*/
-       virtual void unload();/**< Delete Behavior*/
+       SpriteBehavior(){}
+       virtual void load(){}/**< Loading Behavior*/
+       virtual void update(){}/**< Update Behavior*/
+       virtual void unload(){}/**< Delete Behavior*/
        //Sprite* sprite;
 };
 #endif
index 7625146..8f4ffde 100644 (file)
--- a/Game.cpp
+++ b/Game.cpp
@@ -126,6 +126,8 @@ void Game::run()
                        break;
                }
                getCurrentLevel()->update();
+               if(Game::game()->ShowFPS)
+                       cout<<Game::game()->getFPS()<<endl;
                //obtain the current fps data.
                deltaclock = SDL_GetTicks() - startclock;
                startclock = SDL_GetTicks();
@@ -136,6 +138,15 @@ void Game::run()
        SDL_RemoveTimer(timer);
 }
 
+Behavior* Game::getBehavior(string name){
+       Behavior* b = mBehaviors[name];
+       if(b){
+               return new Behavior(*b);
+       }
+       else 
+               return NULL;
+}
+
 void Game::loadLevel(std::string levelName){
        for(unsigned int i=0; i< mLevels.size(); ++i)
                if(mLevels[i]->getName() == levelName)
diff --git a/Game.h b/Game.h
index 7c443b3..3c27284 100644 (file)
--- a/Game.h
+++ b/Game.h
@@ -47,6 +47,8 @@ public:
        /** Retrns the Current FPS of the game */
        Uint32 getFPS() { return currentFPS;}
 
+       Behavior* getBehavior(string name);/**<Gets the requested Behavior \param name Name of the behavior to obtain \return Copy of the requested Behavior. */
+
 protected:
        Game();
        ~Game();
@@ -55,9 +57,10 @@ protected:
 private:
        static const Uint32 waitTime;
 
-       unsigned int mCurrentLevel; /**< Current Level index. */
+       unsigned int mCurrentLevel;/**< Current Level index. */
        std::vector<Level*> mLevels;/**< Vector of pointers to all of the levels. \todo Make into map? */
        SDL_Surface *mScreen;/**< The surface to draw to. */
+       map<string,Behavior*> mBehaviors;/**< This level's Behaviors */
 
        void loadLevel(std::string levelName);/**< Loads a level by name. */
 
index 04296a3..b1e8fac 100644 (file)
--- a/Level.cpp
+++ b/Level.cpp
@@ -6,20 +6,31 @@ Level::Level(SDL_Surface* screen) : mScreen(screen)
 {
        /** load background */
        mBackground= new Background(screen);//mBackground = LoadImage("Backgrounds/bg.bmp");
+
+       mBehavior = new LevelBehavior();
 }
 
 Level::Level(SDL_Surface* screen,string n) : mScreen(screen), mName(n)
 {
        /** load background */
        mBackground= new Background(screen, n);//mBackground = LoadImage("Backgrounds/bg.bmp");
+
+       mBehavior = new LevelBehavior();
 }
 
-Level::Level(SDL_Surface* screen,string n, string behavior/** \todo implement obtaining correct behavior maybe a fn get Behavior */) : mScreen(screen), mName(n)
+Level::Level(SDL_Surface* screen,string n, string behaviorName) : mScreen(screen), mName(n)
 {
-       /** load background */
+       /// load background
        mBackground= new Background(screen, n);//mBackground = LoadImage("Backgrounds/bg.bmp");
-       /// \todo make this wotk
-       //mBehavior.load();
+       
+       /// Set behavior
+       mBehavior = getBehavior(behaviorName);
+
+       if(!mBehavior)
+               mBehavior = new LevelBehavior();
+
+       /// Complete load behavior
+       mBehavior->load();
 }
 
 Level::~Level()
@@ -106,12 +117,7 @@ void Level::postEvent(SDL_Event event)
 
 /** Will handle all movement and dynamic triggers. */
 void Level::update(){
-       /// \todo fix this behavior too
-       //mBehavior.update();
-       
-       if(Game::game()->ShowFPS)
-               cout<<Game::game()->getFPS()<<endl;
-
+       mBehavior->update();
        //check collision with sun sprite
        mSprites[0]->collisionWithSprite("sun");
        //check collision with sun sprite
@@ -147,4 +153,13 @@ Sprite* Level::findSpriteByName(string name){
                        return mSprites[i];
        }
        return NULL;//if a sprite wasn't found return null
-}
\ No newline at end of file
+}
+
+LevelBehavior* Level::getBehavior(string name){
+       Behavior* b = Game::game()->getBehavior(name);
+       if(LevelBehavior* lb = dynamic_cast<LevelBehavior*>(b)){
+               return new LevelBehavior(*lb);
+       }
+       else
+               return NULL;
+}
diff --git a/Level.h b/Level.h
index 360c2b0..beda156 100644 (file)
--- a/Level.h
+++ b/Level.h
@@ -42,16 +42,16 @@ public:
        Level(SDL_Surface *screen, string name, string behavior);
        ~Level();
        void drawScene();/**< Draws everything that is set to draw on the screen. */
-       void LoadBG(string name);/**< Loads the Background. */
+       void LoadBG(string name);/**< Loads the Background. \param name Name of the background to load */
        virtual void postEvent(SDL_Event event);/**< Passes along SDL events */
        virtual void update();/**< Loop that runs every game frame that calculates movement, placement, etc.  as well as obtains key strokes (limited by keyboard hardware)*/
-       void addSprite(Sprite* sp);///< add a Sprite to the list of sprites
-       void removeSprite(Sprite* sp);///< remove the Sprite sp from the list of sprites
-       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
-
+       void addSprite(Sprite* sp);/**< add a Sprite to the list of sprites \param sp Sprite to add */
+       void removeSprite(Sprite* sp);/**< remove the Sprite sp from the list of sprites \param ps Sprite to remove */
+       void addActor(string name, Actor actor);/**< add a Actor to the list of sprites \param name Name of the actor \param actor The actor to add */
+       void removeActor(string name);/**< remove the Actor sp from the list of sprites \param name Name of the actor to remove */
+       string getName(){ return mName; }/**< returns the current level's name \return Level's name */
+       Sprite* findSpriteByName(string name);/**< returns the first Sprite with a given name \param name Name of the sprite to return \return Pointer to the requested sprite */
+       LevelBehavior* getBehavior(string name);/**<Gets the requested Behavior \param name Name of the behavior to obtain \return Copy of the requested Behavior. */
 protected://allow inheritance
        string mName;
        Background* mBackground;/**< Pointer to the current Background. */
@@ -65,6 +65,6 @@ protected://allow inheritance
        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 Collision data */
-       
+       LevelBehavior* mBehavior;
 };
 #endif
index 18f92c7..2e9a025 100644 (file)
@@ -72,3 +72,12 @@ Sprite* Sprite::collisionWithSprite(string name){
                        return s;
        return NULL;//if there aren't collisions
 }
+
+SpriteBehavior* Sprite::getBehavior(string name){
+       Behavior* b = Game::game()->getBehavior(name);
+       if(SpriteBehavior* sb = dynamic_cast<SpriteBehavior*>(b)){
+               return new SpriteBehavior(*sb);
+       }
+       else
+               return NULL;
+}
\ No newline at end of file
index cb588ac..b04ffcf 100644 (file)
--- a/Sprite.h
+++ b/Sprite.h
@@ -33,8 +33,9 @@ class Sprite : public WorldObject
        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:
+       SpriteBehavior* getBehavior(string name);/**<Gets the requested Behavior \param name Name of the behavior to obtain \return Copy of the requested Behavior. */
+       
+private:
        std::string mName;/**< Sprite's name */
        bool mAnimating;/**< Tells whether to animate or not */
        bool mDrawn;/**< Tells if the object has been drawn the first time */
@@ -42,6 +43,6 @@ class Sprite : public WorldObject
        long mLastUpdate;/**< Number that indicates when the Sprite has last updated. Overflows in about 24 days so no worries. */
        Actor mActor;/**< This Sprite's Actor. */
        SDL_Surface *mScreen;/**< Screen to be drawn to. */
-       //SpriteBehavior mBehavior;/**< \todo implement*/
+       SpriteBehavior* mBehavior;/**< Sprite's Behavior */
 };
 #endif