From e91241ed64630132f57bd35c54d5544708b385f8 Mon Sep 17 00:00:00 2001 From: Ivan Hernanez Date: Sun, 7 Aug 2011 05:02:46 -0500 Subject: [PATCH] Behaviors sorta implemented --- Behavior.h | 10 +++++----- Behaviors.h | 16 ++++++++-------- Game.cpp | 11 +++++++++++ Game.h | 5 ++++- Level.cpp | 37 ++++++++++++++++++++++++++----------- Level.h | 18 +++++++++--------- Sprite.cpp | 9 +++++++++ Sprite.h | 7 ++++--- 8 files changed, 76 insertions(+), 37 deletions(-) diff --git a/Behavior.h b/Behavior.h index c38fef6..0c21a65 100644 --- a/Behavior.h +++ b/Behavior.h @@ -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 */ }; diff --git a/Behaviors.h b/Behaviors.h index 39cd4e0..fe72e9b 100644 --- a/Behaviors.h +++ b/Behaviors.h @@ -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 diff --git a/Game.cpp b/Game.cpp index 7625146..8f4ffde 100644 --- a/Game.cpp +++ b/Game.cpp @@ -126,6 +126,8 @@ void Game::run() break; } getCurrentLevel()->update(); + if(Game::game()->ShowFPS) + cout<getFPS()<getName() == levelName) diff --git a/Game.h b/Game.h index 7c443b3..3c27284 100644 --- 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);/** mLevels;/**< Vector of pointers to all of the levels. \todo Make into map? */ SDL_Surface *mScreen;/**< The surface to draw to. */ + map mBehaviors;/**< This level's Behaviors */ void loadLevel(std::string levelName);/**< Loads a level by name. */ diff --git a/Level.cpp b/Level.cpp index 04296a3..b1e8fac 100644 --- 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<getFPS()<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(b)){ + return new LevelBehavior(*lb); + } + else + return NULL; +} diff --git a/Level.h b/Level.h index 360c2b0..beda156 100644 --- 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);/**getBehavior(name); + if(SpriteBehavior* sb = dynamic_cast(b)){ + return new SpriteBehavior(*sb); + } + else + return NULL; +} \ No newline at end of file diff --git a/Sprite.h b/Sprite.h index cb588ac..b04ffcf 100644 --- 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& getCollisionData();/**< Returns collision data */ - - private: + SpriteBehavior* getBehavior(string name);/**