From: Ivan Hernanez Date: Mon, 8 Aug 2011 04:31:00 +0000 (-0500) Subject: Behavriors implemented removed extraneous code X-Git-Url: http://git.mmlx.us/?a=commitdiff_plain;h=caaade7f952b8f3d3753c5d4b49bd22bceef325e;p=IvanGame.git Behavriors implemented removed extraneous code --- diff --git a/Animation.cpp b/Animation.cpp index 85d590c..04fc82b 100644 --- a/Animation.cpp +++ b/Animation.cpp @@ -11,7 +11,12 @@ #include "fns.h" using namespace std; -Animation::Animation(std::string animFile) +Animation::Animation(std::string animFile):mName(animFile) +{ + loadAnimation(animFile); +} + +Animation::Animation(std::string animFile, string name):mName(name) { loadAnimation(animFile); } diff --git a/Animation.h b/Animation.h index 2f57b39..36f836b 100644 --- a/Animation.h +++ b/Animation.h @@ -19,15 +19,15 @@ class Animation { public: Animation(std::string animFile); + Animation(std::string animFile, string name); int loadAnimation(std::string animFile);/**< Loads the Animations from a file in the specified format. */ SpriteFrame *mAnim;/**< Pointer to the current animation. As an array of SpriteFrames */ int mBuilt,/**< Using as a bool */ mNumFrames,/**< Number of frames in this Animation */ mW,/**< Animation's current width */ mH;/**< The animation's current Height */ - - private: - std::string mname;//add name to anim file HERE + bool operator==(Animation a){return mName==a.mName;} + std::string mName;//add name to anim file }; #endif diff --git a/ArtAssets/Sprites/sun.anim b/ArtAssets/Sprites/sun.anim index b466db1..c4fbf5b 100644 --- a/ArtAssets/Sprites/sun.anim +++ b/ArtAssets/Sprites/sun.anim @@ -1,6 +1,6 @@ #Number of Frames #ArtAssets/ milisec Transparency -#filename pause r g b xOffset yOffset ( collisionType , ... ) +#filename pause r g b xOffset yOffset ( , ... ) NumFrames: 4 sun1.bmp 20 0 0 255 0.0 0.0 ( c 0.0 0.0 10.0 ) sun2.bmp 20 0 0 255 0.0 0.0 ( c 0.0 0.0 10.0 ) diff --git a/ArtAssets/Sprites/viking.anim b/ArtAssets/Sprites/viking.anim index 22b240c..e4fcbee 100644 --- a/ArtAssets/Sprites/viking.anim +++ b/ArtAssets/Sprites/viking.anim @@ -1,6 +1,6 @@ #Number of Frames #ArtAssets/ milisec Transparency -#filename pause r g b xOffset yOffset ( , ... ) +#filename pause r g b xOffset yOffset ( , ... ) NumFrames: 9 viking1.bmp 50 0 255 0 0.0 0.0 ( r 0 0 150.0 100.0 ) viking2.bmp 50 0 255 0 0.0 0.0 ( r 0 0 150.0 100.0 ) diff --git a/Behavior.h b/Behavior.h deleted file mode 100644 index 0c21a65..0000000 --- a/Behavior.h +++ /dev/null @@ -1,28 +0,0 @@ -#ifndef __BEHAVIOR_H__ -#define __BEHAVIOR_H__ -#include "fns.h" -#include -#include - -using std::vector; -using std::string; - -/** - This is an action that a Behavior, Sprite, or other object can have. - - This class is tasked with the following: - - being a parent container for actions. - */ - -class Behavior -{ -public: - 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 */ -}; -#endif diff --git a/Behaviors.h b/Behaviors.h index fe72e9b..795329a 100644 --- a/Behaviors.h +++ b/Behaviors.h @@ -1,42 +1,13 @@ #ifndef __BEHAVIORS_H__ #define __BEHAVIORS_H__ -#include "Behavior.h" #include #include +#include "Game.h" using std::vector; using std::string; -/** - This is an action that a Behavior, Sprite, or other object can have. +///\file this file should contain all Behaviors for WorldObjects inlcuding Backgrounds - This class is tasked with the following: - - All actions for the World Level. - */ -class LevelBehavior : public Behavior -{ -public: - LevelBehavior(){} - virtual void load(){}/**< Loading Behavior*/ - virtual void update(){}/**< Update Behavior*/ - virtual void unload(){}/**< Delete Behavior*/ - //Level* level; -}; - -/** - This is an action that a Behavior, Sprite, or other object can have. - - This class is tasked with the following: - - All actions for Sprites. - */ -class SpriteBehavior : public Behavior -{ -public: - SpriteBehavior(){} - virtual void load(){}/**< Loading Behavior*/ - virtual void update(){}/**< Update Behavior*/ - virtual void unload(){}/**< Delete Behavior*/ - //Sprite* sprite; -}; #endif diff --git a/CMakeLists.txt b/CMakeLists.txt index b7cb69c..e5fe16e 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -4,6 +4,6 @@ project(game) find_package(SDL REQUIRED) find_package(SDL_image REQUIRED) -add_executable(game Animation.cpp fns.cpp Game.cpp Level.cpp main.cpp WorldObject.cpp Sprite.cpp Text.cpp Actor.cpp Background.cpp LevelWorld.cpp Collision.cpp) +add_executable(game Animation.cpp fns.cpp Game.cpp Level.cpp main.cpp WorldObject.cpp Sprite.cpp Text.cpp Actor.cpp Background.cpp Collision.cpp LoadResources.cpp) target_link_libraries(game ${SDL_LIBRARY} ${SDLIMAGE_LIBRARY}) diff --git a/Game.cpp b/Game.cpp index 8f4ffde..07bcd90 100644 --- a/Game.cpp +++ b/Game.cpp @@ -2,7 +2,6 @@ #include "fns.h" #include #include -#include "LoadLevels.h" const Uint32 Game::waitTime = 1000/60; /* ms */ @@ -10,8 +9,8 @@ const Uint32 Game::waitTime = 1000/60; /* ms */ Game* Game::m_instance = NULL; /** This function is called to create an instance of the class. -Calling the constructor publicly is not allowed. -The constructor is private and is only called by this Instance function. + Calling the constructor publicly is not allowed. + The constructor is private and is only called by this Instance function. */ Game* Game::game(){ if(!m_instance) @@ -39,12 +38,10 @@ Game::Game() : mCurrentLevel(0), mScreen(0), ShowCollisions(false), ShowFPS(fals SDL_WM_SetCaption("Ivan's Game", NULL); SDL_ShowCursor(0); - /** Create the first level if there are none */ - LoadLevels l(mScreen); - if(l.levels.size() == 0) - mLevels.push_back(new Level(mScreen)); - else - mLevels = l.levels; + + /** Load Resources */ + LoadResources(); + // set start time for fps calc startclock = SDL_GetTicks(); } @@ -70,6 +67,22 @@ int Game::getLevelIndex() return mCurrentLevel; } +void Game::setCurrentLevel(string name){ + for(unsigned int i=0, count = mLevels.size(); i < count; i++){ + if(mLevels[i]->getName() == name){ + mCurrentLevel=i; + this->mLevels[mCurrentLevel]->load(); + return; + } + } + cout<<"Level "<postEvent(event); break; } + getCurrentLevel()->update(); if(Game::game()->ShowFPS) cout<getFPS()<getName() == levelName) - mCurrentLevel = i; - else - std::cout<<"Level "< #include @@ -33,11 +34,23 @@ public: Level* getCurrentLevel(); /** + Sets the current level to play. + \param name Name of the level to load + */ + void setCurrentLevel(string name); + + /** Gets the index of the current level being played. \return index of the current level */ int getLevelIndex(); + /** + Gets the total count of levels. + \return number of levels + */ + int getLevelCount(); + /** Makes the game loop begin and load objects. */ void run(); @@ -47,13 +60,18 @@ public: /** Retrns the Current FPS of the game */ Uint32 getFPS() { return currentFPS;} - Behavior* getBehavior(string name);/** mBehaviors;/**< This level's Behaviors */ - void loadLevel(std::string levelName);/**< Loads a level by name. */ - static Uint32 timerCallback(Uint32 interval, void* data); //fps data calc diff --git a/Level.cpp b/Level.cpp index b1e8fac..c32759b 100644 --- a/Level.cpp +++ b/Level.cpp @@ -4,33 +4,15 @@ 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(); + mLoadBehavior = DoNothing; + mUpdateBehavior = DoNothing; } -Level::Level(SDL_Surface* screen,string n, string behaviorName) : mScreen(screen), mName(n) +Level::Level(SDL_Surface* screen, string n, Behavior loadBehave, Behavior updateBehave) : mScreen(screen), mName(n), mBackground(NULL) { - /// load background - mBackground= new Background(screen, n);//mBackground = LoadImage("Backgrounds/bg.bmp"); - /// Set behavior - mBehavior = getBehavior(behaviorName); - - if(!mBehavior) - mBehavior = new LevelBehavior(); - - /// Complete load behavior - mBehavior->load(); + mLoadBehavior = loadBehave; + mUpdateBehavior = updateBehave; } Level::~Level() @@ -72,7 +54,8 @@ void Level::drawScene() SDL_FillRect(mScreen, NULL, color); /** Draw BG */ - mBackground->draw(); + if(mBackground!=NULL) + mBackground->draw(); /** Draw Sprites*/ DrawSprites(); @@ -117,11 +100,11 @@ void Level::postEvent(SDL_Event event) /** Will handle all movement and dynamic triggers. */ void Level::update(){ - mBehavior->update(); - //check collision with sun sprite - mSprites[0]->collisionWithSprite("sun"); - //check collision with sun sprite - mSprites[0]->collisionWithSprite("viking2"); + mUpdateBehavior(); +} + +void Level::load(){ + mLoadBehavior(); } @@ -129,6 +112,15 @@ void Level::addSprite(Sprite* sp){ mSprites.push_back(sp); } +Sprite* Level::getSprite(string name){ + for(int i=0, count=mSprites.size(); i < count; i++){ + if(mSprites[i]->getName()==name){ + return mSprites[i]; + } + } + return NULL; +} + void Level::removeSprite(Sprite* sp){ for(vector::iterator ptr = mSprites.begin(); ptr != mSprites.end(); ptr++){ if(*ptr == sp){ @@ -138,6 +130,28 @@ void Level::removeSprite(Sprite* sp){ } } +void Level::addAnimation(Animation anim){ + mAnims.push_back(anim); +} + +Animation* Level::getAnimation(string name){ + for(int i=0, count=mAnims.size(); i < count; i++){ + if(mAnims[i].mName==name){ + return &mAnims[i]; + } + } + return NULL; +} + +void Level::removeAnimation(Animation anim){ + for(vector::iterator ptr = mAnims.begin(); ptr != mAnims.end(); ptr++){ + if(*ptr == anim){ + mAnims.erase(ptr); + return; + } + } +} + void Level::addActor(string name, Actor actor){ mActors.insert(make_pair(name, actor)); } @@ -154,12 +168,3 @@ Sprite* Level::findSpriteByName(string name){ } return NULL;//if a sprite wasn't found return null } - -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 beda156..c13ddc1 100644 --- a/Level.h +++ b/Level.h @@ -2,7 +2,6 @@ #define __LEVEL_H__ #include "Sprite.h" #include "Background.h" -#include "Behaviors.h" #include #include @@ -26,45 +25,49 @@ using std::string; class Level { public: - Level(SDL_Surface *screen); + Level(SDL_Surface* screen); /** Level constructor \param screen the Screen to be drawn to \param name of the level + \param loadBehavior the Loading Behavior for this level (Run once). + \param updateBehavior the Updating Behavior for this level (Run every loop). */ - Level(SDL_Surface *screen, string name); - /** - Level constructor - \param screen the Screen to be drawn to - \param name of the level - \param behavior the Behavior for this level. - */ - Level(SDL_Surface *screen, string name, string behavior); + Level(SDL_Surface* screen, string name, Behavior loadBehave = DoNothing, Behavior updateBehave = DoNothing); ~Level(); void drawScene();/**< Draws everything that is set to draw on the screen. */ 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 update();/**< Loop that runs every game frame that calculates movement, placement, etc. as well as obtains key strokes (limited by keyboard hardware)*/ + void load();/**< Runs load action*/ void addSprite(Sprite* sp);/**< add a Sprite to the list of sprites \param sp Sprite to add */ + Sprite* getSprite(string name);/**< gets the first sprite with the given name \param name Name of the sprite \return Pointer to the requested Sprite */ void removeSprite(Sprite* sp);/**< remove the Sprite sp from the list of sprites \param ps Sprite to remove */ + void addAnimation(Animation anim);/**< add a Actor to the list of sprites \param anim The actor to add */ + Animation* Level::getAnimation(string name);/**< get a Actor* to the list of sprites \param name Name of the actor \return Pointer to the actor we requested or null */ + void removeAnimation(Animation anim);/**< remove an Actor to the list of sprites \param anim The actor to add */ 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 */ + void setBackground(Background* b){mBackground=b;}/**< sets the current level's background \param b Background */ + Background* getBackground(){return mBackground;}/**< gets the current level's name \return Level's Background */ + SDL_Surface* getScreen(){return mScreen;}/**< gets the current level's name \return Level's SDL_Surface */ + 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);/** mSprites;/**< Vector of all sprites on the level. \todo Maybe make into map? This should be a list of World objects once implemented. \todo add accessor or move to public \todo make a map by name (for collisions) \todo make a list ordered by Zorder (for drawing*/ map mActors;/**< This level's actors. */ - vector mAnim;/**< This level's Animations */ + vector mAnims;/**< This level's Animations */ void DrawIMG();/**< Draws an image to the screen. (Not used) */ 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 Collision data */ - LevelBehavior* mBehavior; + Behavior mLoadBehavior;/**< will be used to define the Load action of the level */ + Behavior mUpdateBehavior;/**< will be used to define the update action of the level */ }; #endif diff --git a/LevelFns.h b/LevelFns.h new file mode 100644 index 0000000..7bb00a3 --- /dev/null +++ b/LevelFns.h @@ -0,0 +1,52 @@ +#ifndef __LEVELFNS_H__ +#define __LEVELFNS_H__ +#include "Game.h" +#include "Level.h" + +///\file this file should contain all Behaviors for Levels + +void LevelWorldLoad(){ + Level* l = Game::game()->getCurrentLevel(); + + SDL_Surface* screen = l->getScreen(); + + /// load background + l->setBackground(new Background(screen, "default", "bg.bmp"));//mBackground = LoadImage("Backgrounds/bg.bmp"); + + /** load animations */ + l->addAnimation(Animation("viking.anim")); + l->addAnimation(Animation("sun.anim")); + + /** load sprites */ + Sprite* vikings1 = new Sprite(screen, "viking1", l->getAnimation("viking.anim")); + vikings1->setPosition(10,30); + vikings1->setSpeed(1); + + Sprite* vikings2 = new Sprite(screen, "viking2", l->getAnimation("viking.anim")); + vikings2->setPosition(350,300); + vikings2->setSpeed(1.5); + + Sprite* sun = new Sprite(screen, "sun", l->getAnimation("sun.anim")); + sun->setPosition(480,50); + sun->setSpeed(1); +} + +void LevelWorldUpdate(){ + Level* LevelWorld = Game::game()->getCurrentLevel(); + + Sprite* player = LevelWorld->getSprite("viking1"); + + Background* bg = LevelWorld->getBackground(); + + Uint8 *keys = SDL_GetKeyState(NULL); + if ( keys[SDLK_LEFT] ) { player->xadd(-1); } + if ( keys[SDLK_RIGHT] ) { player->xadd(1); } + if ( keys[SDLK_UP] ) { player->yadd(-1); } + if ( keys[SDLK_DOWN] ) { player->yadd(1); } + if ( keys[SDLK_a] ) { bg->xadd(-1); } + if ( keys[SDLK_d] ) { bg->xadd(1); } + if ( keys[SDLK_w] ) { bg->yadd(-1); } + if ( keys[SDLK_s] ) { bg->yadd(1); } +} + +#endif \ No newline at end of file diff --git a/LevelWorld.cpp b/LevelWorld.cpp deleted file mode 100644 index 0ef05a0..0000000 --- a/LevelWorld.cpp +++ /dev/null @@ -1,45 +0,0 @@ -#include "LevelWorld.h" -#include "fns.h" - -LevelWorld::LevelWorld(SDL_Surface* screen) : - Level(screen, "World") -{ - /** load animations */ - mAnim.push_back(Animation("viking.anim")); - mAnim.push_back(Animation("sun.anim")); - - /** load sprites */ - Sprite* vikings1 = new Sprite(screen, "viking1", &mAnim[0]); - vikings1->setPosition(10,30); - vikings1->setSpeed(1); - - Sprite* vikings2 = new Sprite(screen, "viking2", &mAnim[0]); - vikings2->setPosition(350,300); - vikings2->setSpeed(1.5); - - Sprite* sun = new Sprite(screen, "sun", &mAnim[1]); - sun->setPosition(480,50); - sun->setSpeed(1); - - mSprites.push_back(vikings1); - mSprites.push_back(vikings2); - mSprites.push_back(sun); -} - - - -/** Will handle all movement and dynamic triggers. */ -void LevelWorld::update() -{ - Uint8 *keys = SDL_GetKeyState(NULL); - if ( keys[SDLK_LEFT] ) { mSprites[0]->xadd(-1); } - if ( keys[SDLK_RIGHT] ) { mSprites[0]->xadd(1); } - if ( keys[SDLK_UP] ) { mSprites[0]->yadd(-1); } - if ( keys[SDLK_DOWN] ) { mSprites[0]->yadd(1); } - if ( keys[SDLK_a] ) { mBackground->xadd(-1); } - if ( keys[SDLK_d] ) { mBackground->xadd(1); } - if ( keys[SDLK_w] ) { mBackground->yadd(-1); } - if ( keys[SDLK_s] ) { mBackground->yadd(1); } - Level::update(); -} - diff --git a/LevelWorld.h b/LevelWorld.h deleted file mode 100644 index 6084926..0000000 --- a/LevelWorld.h +++ /dev/null @@ -1,23 +0,0 @@ -#ifndef __LEVELWORLD_H__ -#define __LEVELWORLD_H__ -#include "Level.h" - -/** - This is a stand alone Level that will have varying jobs in respect to the LevelWorld map - - This class is tasked with the following: - - drawing the scence and all objects in order - - loading the background - - keeping track of animations - - moving objects - - handling events for the Level - */ - -class LevelWorld : public Level -{ -public: - LevelWorld(SDL_Surface *screen); - void drawScene();/**< Draws everything that is set to draw on the screen. */ - void update();/**< Loop that runs every game frame that calculates movement, placement, etc. as well as obtains key strokes (limited by keyboard hardware)*/ -}; -#endif diff --git a/LoadLevels.h b/LoadLevels.h deleted file mode 100644 index 878d499..0000000 --- a/LoadLevels.h +++ /dev/null @@ -1,27 +0,0 @@ -#ifndef __LOADLEVELS_H__ -#define __LOADLEVELS_H__ -#include -#include "fns.h" - -/// Add all levels and includes here -#include "Level.h" -#include "LevelWorld.h" - -using namespace std; - - -/** - This is a container for creating all the levels in a neat location so as not to bulk up the Game class - - This class is tasked with the following: - - Creating all the levels. - */ -class LoadLevels{ -public: - LoadLevels(SDL_Surface* screen){ - levels.push_back(new LevelWorld(screen)); - } - - vector levels; -}; -#endif diff --git a/LoadResources.cpp b/LoadResources.cpp new file mode 100644 index 0000000..64d02d7 --- /dev/null +++ b/LoadResources.cpp @@ -0,0 +1,10 @@ +#include "Game.h" +#include "fns.h" +#include "LevelFns.h" + +///\file this file should contain implementations for loading resources + +void Game::LoadResources(){ + mLevels.push_back(new Level(mScreen, "World", LevelWorldLoad, LevelWorldUpdate)); +} + diff --git a/Sprite.cpp b/Sprite.cpp index 2e9a025..140a825 100644 --- a/Sprite.cpp +++ b/Sprite.cpp @@ -10,9 +10,9 @@ mName(name), mActor(actor), mScreen(screen) { - /** \todo Later add a part that adds name to list of level sprites - Game.Level.SpriteList.pushback(name); - */ + //add to current level + Game::game()->getCurrentLevel()->addSprite((this)); + if(mActor.mAnimations[mActor.mCurrentAnimation] -> mBuilt) { if (mActor.mAnimations[mActor.mCurrentAnimation]->mNumFrames > 1) mAnimating = 1; @@ -71,13 +71,4 @@ Sprite* Sprite::collisionWithSprite(string name){ if(frame->collisionData[i]->checkCollisions(s->getCollisionData(), s->mPos + s->getAnimation()->animationPeg, mPos + frame->animationPeg))//if there is a collision 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(b)){ - return new SpriteBehavior(*sb); - } - else - return NULL; } \ No newline at end of file diff --git a/Sprite.h b/Sprite.h index b04ffcf..e3a8840 100644 --- a/Sprite.h +++ b/Sprite.h @@ -4,7 +4,6 @@ #include #include "fns.h" #include "Actor.h" -#include "Behaviors.h" #include "WorldObject.h" /** @@ -33,7 +32,7 @@ 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 */ - SpriteBehavior* getBehavior(string name);/** using namespace std; +///Behaviors use functions that take nothing and return nothing +typedef void (*Behavior) (); +///As the name denotes, this just simply does nothing +static void DoNothing(){} SDL_Surface* LoadImage( std::string filename );/**< Loads supported images. */ diff --git a/game.vcxproj b/game.vcxproj index c435619..bf0a361 100644 --- a/game.vcxproj +++ b/game.vcxproj @@ -97,8 +97,7 @@ - - + true @@ -113,7 +112,7 @@ - +