Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ///Mock up code:
- /*Warning, this code has not been compile tested. however it is based off of working code.
- May contain typos. Note: you will need to fill in the blanks [...] with your code to make this work*/
- ...
- //Include whatever else you need
- #include <vector>
- #include "game_object.h"
- ...
- vector<game_object*> objects; // making it a global var is a bad idea.
- // "objects" should be put inside of a class, function or namespace
- //Function Definitions
- void add_object(game_object * gm_object)
- {
- objects.push_back(gm_object);
- }
- void remove_object(game_object * gm_object)
- {
- for (unsigned int i=0;i < objects.size();i++)
- {
- if (objects.at(i)==gm_object)
- {
- delete objects.at(i);
- objects.at(i)==NULL;
- }
- }
- }
- // Class Definitions
- //you really, really need to split up your code into multiple files
- //eg. game_object.h - class prototype
- //and game_object.cpp - class member functions (code)
- class game_object
- {
- public:
- game_object() { }
- virtual void draw() { }
- virtual void update() { }
- virtual ~game_object() { }
- protected:
- private:
- }
- ...
- // More Class Definitions
- /* note to read variables from the ship class you need to make a "getter" function
- --- game_object.h
- class game_object
- {
- public:
- game_object() { }
- virtual void get(string name, bool &return_value) { } //<- new
- virtual void get(string name, int &return_value) { } //<- new
- virtual void get(string name, float &return_value) { } //<- new
- virtual void get(string name, string &return_value) { } //<- new
- virtual void draw() { }
- virtual void update() { }
- virtual ~ai_ship() { }
- protected:
- private:
- }
- --- Ship
- class ship : public game_object
- {
- public:
- ship(float x, float y);
- void get(string name, float &return_value); //<- new
- //add the other get functions defined in game_object to support other data types such as "int"
- void draw();
- void update();
- ~ai_ship();
- protected:
- private:
- SDL_Surface * sprite;
- SDL_Rect * rect;
- float x,y;
- };
- void ship::get(string name, float &return_value)
- {
- if (name=="x")
- {
- return_value=x;
- return;
- }
- if (name=="y")
- {
- return_value=y;
- return;
- }
- }
- --example usage
- ship myship(10,20); //make ship at x: 10 y: 20
- float ship_x;
- myship.get("x", ship_x); //ship_x now equals 10
- To set variables to need to make "setter" functions, I think you
- can figure out how to to that using "get" as an example
- */
- // This is the ship class which inherits from game_object
- class ship : public game_object
- {
- public:
- ship(float x, float y);
- void draw();
- void update();
- ~ai_ship();
- protected:
- private:
- SDL_Surface * sprite;
- SDL_Rect * rect;
- float x,y;
- };
- ...
- int main()
- {
- ...
- ship * myship(10,20);
- ship * myship2(20,10);
- add_object(myship); // add objects to the game loop
- add_object(myship2);
- remove_object(myship2); //myship2 is now removed
- ...
- while( game_is_running )
- {
- ...
- for(unsigned int i; i < object.size(); i++)
- {
- if (objects.at(i)==NULL)
- continue; // skip NULL objects
- object.at(i)->update(); //go through the array of objects and run their logic
- object.at(i)->draw(); //then draw
- }
- //check for NULL objects and remove
- vector<game_object*> new_objects;
- for (unsigned int i=0;i < objects.size();i++)
- {
- if (objects.at(i)!=NULL)
- new_objects.push_back(objects.at(i));//all non-NULL objects
- //get put onto a new vector, which objects is set equal to
- }
- objects=new_objects;
- ...
- }
- //end of game
- objects.clear(); //don't get into the habit of modifying "objects" directly
- }
- ///End of mock up code
Advertisement
Add Comment
Please, Sign In to add comment