From de0afe76e025be2c076f8b30b67004ce061337e0 Mon Sep 17 00:00:00 2001 From: Ivan Hernandez Date: Tue, 11 Jan 2011 11:22:26 -0600 Subject: [PATCH] Implemented unit vector as well as changed comments about multiple input and edited ~Game so that it doesn't delete the Screen (need to discuss how to handle this) removed public name from sprite. (may need to make mName public or add accessors) Created Behavior class to use in place of function pointers (so that there is only one level class and it has a behavior instead) --- Behavior.h | 30 ++++++++++++++++++++++++++++++ Game.cpp | 4 ++-- Level.h | 2 +- LevelWorld.h | 3 +-- Sprite.cpp | 6 +++--- Sprite.h | 3 +-- fns.cpp | 12 +++++++++++- fns.h | 5 ++++- 8 files changed, 53 insertions(+), 12 deletions(-) create mode 100644 Behavior.h diff --git a/Behavior.h b/Behavior.h new file mode 100644 index 0000000..a4169e4 --- /dev/null +++ b/Behavior.h @@ -0,0 +1,30 @@ +#ifndef BEHAVIOR +#define BEHAVIOR +#include "Sprite.h" +#include "Background.h" +#include +#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); + 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/Game.cpp b/Game.cpp index 99a8e00..ce12c8f 100644 --- a/Game.cpp +++ b/Game.cpp @@ -40,7 +40,8 @@ Game::~Game() for (size_t i = 0; i < mLevels.size(); ++i) { delete mLevels[i]; } - delete mScreen; + /// \todo Does this even need to be here or will closing the program unallocate the mem? + //delete mScreen; } Level* Game::getCurrentLevel() @@ -78,7 +79,6 @@ void Game::run() break; } - ///\todo This needs to be changed to where every single key-press is registered at once and put into a list of sorts and then events on keys is handled elsewhere to allow multi-keypresses and for better program flow. switch (event.type) { /** If the event is a click on the close button in the top right corner of the window, we kill the application diff --git a/Level.h b/Level.h index 76f1ff5..efacc27 100644 --- a/Level.h +++ b/Level.h @@ -29,7 +29,7 @@ public: virtual void drawScene();/**< Draws everything that is set to draw on the screen. */ void LoadBG(string name);/**< Loads the Background. */ virtual void postEvent(SDL_Event event);/**< Passes along SDL events */ - virtual void update();/**< Loop that runs every game frame that calculates movement, placement, etc. */ + 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 diff --git a/LevelWorld.h b/LevelWorld.h index 436d4c8..0b5a0ba 100644 --- a/LevelWorld.h +++ b/LevelWorld.h @@ -18,7 +18,6 @@ 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.*/ - + 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/Sprite.cpp b/Sprite.cpp index 39fb7e9..e2c3bff 100644 --- a/Sprite.cpp +++ b/Sprite.cpp @@ -2,13 +2,13 @@ #include using std::cout; using std::endl; -Sprite::Sprite(SDL_Surface *screen, std::string name, Actor actor) : +Sprite::Sprite(SDL_Surface *screen, std::string name, Actor actor, int z) : mDrawn(0), mVisible(true), mLastUpdate(0), mActor(actor), - mScreen(screen) - /** \todo implement name and mName seee if I even need both of them. */ + mScreen(screen), + ZOrder(z) { /** \todo Later add a part that adds name to list of level sprites Game.Level.SpriteList.pushback(name); diff --git a/Sprite.h b/Sprite.h index 3062991..61d1aad 100644 --- a/Sprite.h +++ b/Sprite.h @@ -15,7 +15,7 @@ class Sprite { public: - Sprite(SDL_Surface *screen, std::string name, Actor actor); + Sprite(SDL_Surface *screen, std::string name, Actor actor, int z = 0); void setAnimation(int animation){ mActor.mCurrentAnimation = animation; }/**< changes to the specified animation beginning at 0. */ int getAnimation(){ return mActor.mCurrentAnimation; }/**< returns active animation. */ void setFrame(int frame) { mActor.mFrame = frame; }/**< cahnges to the specified frame of the animation beginning at 0. */ @@ -26,7 +26,6 @@ class Sprite 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. */ - std::string name; void draw();/**< draws Sprite. */ void xadd(int num) {mPos.x += num;}/**< Increase X coordiante by a given amount. */ void yadd(int num) {mPos.y += num;}/**< Increase y coordinate by a given amount. */ diff --git a/fns.cpp b/fns.cpp index 17b4405..1a7da66 100644 --- a/fns.cpp +++ b/fns.cpp @@ -40,8 +40,18 @@ SDL_Surface* LoadImage( std::string filename ) return compatible_image; } +Point2D::Point2D (const SizeD& copy){ + x = copy.w; + y = copy.h; +} + double Point2D::length(){ - return sqrt(x*x + y*y);///< \todo find math.sqrt + return sqrt(x*x + y*y); +} + +Point2D Point2D::unitVec(){ + double len = length();///< \todo optimize + return Point2D((x/len), (y/len)); } Point2D Point2D::add(Point2D pt){ diff --git a/fns.h b/fns.h index ee09912..33b8056 100644 --- a/fns.h +++ b/fns.h @@ -21,7 +21,7 @@ class Point2D x = copy.x; y = copy.y; } - Point2D (/*const SizeD& copy*/int i);///< Copy object \todo make SizeD work here + Point2D (const SizeD& copy);///< Copy object /** \todo add typical vector functionality like length, nomal vectors etc. */ double x;/**< x position */ @@ -29,6 +29,9 @@ class Point2D double length();/**< The length of the Point (from origin) \return length of the vector/point */ + Point2D unitVec();/**< The unit vector of the Point (from origin) + \return length of the vector/point + */ Point2D add(Point2D pt);/**< Adds the value of the point to this \return This after modificaiton*/ Point2D sub(Point2D pt);/**< Subtracts the value of the point from this \return This after modification*/ Point2D mult(double d);/**< Multiplies the values of this by i \return This after modificaiton*/ -- 2.11.0