SDL Mob Class Remake

Well this is my remake of my last topic, which is buried under 100s of different topics now in the General C++ Programming fourm.

Well here it is:

I made a SDL Mob Class, and I want to know how I can make my mobs function properly. I don't know how to make my mobs do stuff. (Eat, Drink, Die, Move.. Etc) So I want to know how to make my mobs do stuff.

My Code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
#ifndef _KYDEN_MOB_H_
#define _KYDEN_MOB_H_

#include <string>

class Mob
{
	public:
		//Functions
		virtual Mob();
		void MobisHit();
		virtual void MobStrike(bool MobisAttacking);
		virtual void MobUseItem(bool MobisHoldingItem, int X, int Y,);

	protected:
		//Interger Variables

		//Doubles & Float Variables
		float Mob X;
		float Mob Y;
		float MobHealth;
		float MobAttack;
		float MobDefense;
		double MobSpeed;

		//Boolean Variables
		bool MobisHostile;
		bool MobisDead;
		bool MobisAttacking;
		bool MobisHit;
		bool MobisHoldingItem;
		bool MobisInMotion;
		
		//String Variables
		std::string MobName;
		std::string Mob

		//SDL_Surfaces
		SDL_Surface* MobImage;
};

#endif 


Someone please help me. I need to make a releasable game demo ASAP or it will die down.
Last edited on by Fredbill30
Only thing I can see wrong here so far is that the 2nd and 3rd parameters for the MobUseItem function have no types.

As for making your mobs do stuff, I'm not familiar with SDL but I'm pretty sure you'll have to write the code for that yourself.
Oh, I fixed that in the updated version of the code, but would I have to make a loop for every mob type in the game? Give me General things I'd have to do. How would I make my program know what mob to make move? I have a feeling I know how to do it, but I'm not sure.
Last edited on by Fredbill30
Write a function that checks the states of the mob (usually it's called Update() ) and calls other functions depending on the states.
Say that your mob moves always horizontally. When it hits a wall a hypotetical collision checking function changes its states from movingRight to movingLeft.
Update() checks if movingRight is true - it's not, so it skips that part of the code. Now it checks if movingLeft is true - yes, so it changes its position: xPos -= speed.
Note that there are other ways to achieve the same result.

Update() must be called for every existing mob. The main game loop is a good place to do this.
Last edited on
So it'd be like:

1
2
3
4
5
6
7
8
9
10
11
12
void Update()
{
   //Mob Updating
   Player.MobUpdate();
   Tiger.MobUpdate();
   Bear.MobUpdate();
   Deer.MobUpdate();
 
   //Vehicle Updating
   Cart.VehicleUpdate();
   /*****ETC******/
}

But I'm using Polymorphism. Would this work if I spawn multiple Tigers, Bears, Deer, Carts etc?
Yes like that. You can also use a vector in which you place all the mobs and iterate through it calling each element's MobUpdate(), so you don't have to modify Update() each time you want to spawn more mobs.

I don't understand what's your concern about polymorphism though.
Give me an example, please.
1
2
3
4
5
void Update(std::vector<Mob> mobs)
{
  for(std::vector<Mob>::iterator it = mobs.begin(); it != mobs.end(); ++it)
    it->MobUpdate();
}

Or with C++11's range-based for
1
2
3
4
5
void Update(std::vector<Mob> mobs)
{
  for(auto& it: mobs) // Equivalent to (Mob& it: mobs)
    it.MobUpdate();
}

If Update() and the vector are members of a class you don't need to pass the vector as parameter
What if they're part of different classes? I'd have to call this for EVERY type of mob?

Or could I do this:
1
2
3
4
5
void KydenEngine::KydenEngineUpdate()
{
     Update(Mob);      //This finds any Mob that needs to be updated
     Update(Vehicle); //Finds any Vehicle that needs to be updated...
}
What if they're part of different classes? I'd have to call this for EVERY type of mob?
Well, yes, isn't this what you're doing in your example too?
Oh I see... I call Update(); And it updates ALL mobs :) Right?
BUMP
BUMP
Yes, that is the pattern that I had in mind
Oh :D Thanks!!!
Topic archived. No new replies allowed.