SDL Mob Class

closed account (N36fSL3A)
How would I make Mobs from this code? I know I can use vectors but how would that make every single Mob react to colision, death, etc? I know I can code functions to do that but how will the vector use my functions?

Here's 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, X, 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 
 
Mob Watts;


every single Mob you declare is its own entity.

closed account (N36fSL3A)
Can a Mod remove this? I'm going to post this in Beginner Fourms. If not someone please HELP ME!!!
Last edited on by Fredbill30
You can use a loop to iterate through all the Mobs in the vector and for each Mob you do whatever it is you want to do.
closed account (N36fSL3A)
Peter can you give me an example?
Ok. I don't know how your game is supposed to work but say that you are storing your Mobs in a vector.
std::vector<Mob> mobs

If Mob has a function update() that updates the mod. It could be updating the position, velocity, making decisions and such things. To call the update() function on all mobs you can use a loop.
1
2
3
4
for (std::size_t i = 0; i < mobs.size(); ++i)
{
	mobs[i].update();
}

If you want to do collision detection and call the MobIsHit() function when the Mob is colliding with another Mob, and you have a function isColliding that returns true if the two Mobs passed to it is colliding, you can use a nested loop to check every mob against each other.
1
2
3
4
5
6
7
8
9
10
11
for (std::size_t i = 0; i < mobs.size(); ++i)
{
	for (std::size_t j = i + 1; j < mobs.size(); ++j)
	{
		if (isColliding(mobs[i], mobs[j]))
		{
			mobs[i].MobIsHit();
			mobs[j].MobIsHit();
		}
	}
}
Last edited on
closed account (N36fSL3A)
But I'm going to use Polymorphism, this is the base class, so would this be a virutal Functon
It can be, but if each derived object from base class Mob behaves in the same way, then there is no reason too.
In that case you will have to store pointers in the vector std::vector<Mob*> mobs;. You can also use smart pointers if you know how they work. Which functions to be virtual is up to you.
closed account (N36fSL3A)
Thanks, I'll test out the concept later when I get home from school.
Topic archived. No new replies allowed.