Creating a map system using a class. (C++)

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Ajm113
    New Member
    • Jun 2007
    • 161

    Creating a map system using a class. (C++)

    Ok, I am trying too create a class that stores information of a entities in my game and be able to call functions too do what ever too the selected ID.

    I.E Add a new entity, display it, change it's position/size, etc when its being rendered.

    F.Y.I, OpenGL/VS2008.

    Here is my class:

    Car.H
    Code:
    #ifndef CARS_H
    #define CARS_H
    #include "stdafx.h"
    #include "Player.h"
    #include "Vector3.h"
    
    class Car {
    public:
    	Car();
    	virtual ~Car();
    
    	void AddCar(Lane lane, float spawn);
    
    	void ShowCar(unsigned int uiID);
    	void KillCars();
    	int ReturnCars();
    	float ReturnSpawnTime(int uiID);
    
    
    	protected:
    
    		Vector3 Position;
    		float SpawnTime;
    		float tSize;
    		int   mId;
    
    private:
    
    	std::map<int,Car *> m_mpCar;
    };
    #endif

    My uncorrected Car.cpp.
    Code:
    Car::Car(){
    		Position = Vector3(0.0f,0.0f,0.0f);
    		SpawnTime = 0;
    		tSize = 0;
    }
    
    Car::~Car() {
    
    
    }
    
    void Car::AddCar(Lane lane, float spawn) {
    
    	
    
    
    	m_mpCar.insert(
    
    }
    
    void Car::KillCars() {
    
    	m_mpCar.clear();
    
    }
    
    void Car::ShowCar(int uiID) {
    
    //OpenGL Display list function call goes here.
    
    }
    
    float Car::ReturnSpawnTime(int uiID) {
    	float time	= m_mpCar[uiID]->SpawnTime;
    return time;
    }
    
    
    
    int Car::ReturnCars() {
    	return (int)m_mpCar.size();
    }
    I would appreciate help with this if anyone has did this situation.
  • boxfish
    Recognized Expert Contributor
    • Mar 2008
    • 469

    #2
    This is definitely something that can be done. The class would probably store an array of all the cars and have a function that's called every frame to do stuff like call the update methods of all the cars and delete them if they're dead. Is there any particular doubt you have about how to implement this?
    Good luck with your game.

    Comment

    • Ajm113
      New Member
      • Jun 2007
      • 161

      #3
      Too create a array? Why not use map?

      Can you give me a example how you would write it using my code, too speed up the typing?

      Comment

      • Savage
        Recognized Expert Top Contributor
        • Feb 2007
        • 1759

        #4
        It's not a good idea to use just one class.
        Perhaps it would be better if you can use two classes,one to create and describe a single car,and other to manage all the cars.

        E.g

        [CODE=cpp]class Car
        {
        unsigned int ID;
        unsigned int Lane;
        unsigned int Speed;
        .
        .
        .
        public:
        //Constructor/Destructor
        ...............

        //Accessors(GetSp eed(),GetLane() ....)

        };


        class CarManager
        {
        std::map<unsign ed int,Car*> EntityMap;

        public:
        //Constructor/Destructor
        ............... ......

        //Managing functions
        void AddCar(Car *car);
        Car* FindCar(unsigne d int ID);
        void RemoveCar(unsig ned int ID);
        ............... ....

        //Maybe some accessors
        };[/CODE]

        If you are unfamiliar with std::maps you might wont to check http://www.cprogramming.com/tutorial/stl/stlmap.html
        Last edited by Savage; Nov 25 '08, 08:55 AM. Reason: Added extrenal references

        Comment

        • Ajm113
          New Member
          • Jun 2007
          • 161

          #5
          Don't you need too add std::map to the car class? For the return pos and etc too work?

          Comment

          • Savage
            Recognized Expert Top Contributor
            • Feb 2007
            • 1759

            #6
            Originally posted by Ajm113
            Don't you need too add std::map to the car class? For the return pos and etc too work?
            Unless you do not want to store position of the car trough whole race(for something like replay, although it would be better to capture the whole frame if you wish to do this) then I do not see a reason why would you need to use std::map in car class.Can you illustrate this with some code?

            Comment

            Working...