Thanks a lot for your replies. I've read through them all and based on that I've decided to use the first method I wrote- Giving a reference to the level to each mob as they are instantiated.
Just some questions, what is your level structure like?. Are you working on a 2D environment?. Is it tile based?... Say you're working on a tile based world stored on a Level class... Well, you could go like this:
Have a game_control class, have it contain both the level and the game actors. When it comes the time to do your logic, have the game_control iterate your game actors and for each of them, have it check against the level class. You could have a method on it returning a list of relevant - read, surrouding - tiles depending on the actor position and then do your checks against them.
The game I am working on is a 2D tile based game. I really like the idea of a game control object, this way each entity in the game can have their own movement logic, and afterwards the game control unit can handle any collision that might need attention.
I don't really have an issue with your actors knowing about your map. It doesn't really make much sense to me to instantiate an actor with no preëxisting world in which to place it.
If you really want to, you could wrap the logic into its own class, and have it hold the map reference. Then in your update loop you would maybe construct a vector of actors that need updating, feed the vector to the logic class, and let it iterate through their locations and determine where they're going.
But I really think your first solution is fine unless it actually starts giving you problems.
You have a good point. The map will always be instantiated first, and then after, the mobs will be placed, based on data from the level class. I think I am going to stick with my first solution, since I can't really see that it should cause any problems.
Your base class for your characters should have virtual functions such as Update. The implementation of Update will be different for each unique character class that inherits from the base class. Now from the engine's point of you, it just need to call Update on your class, and by polymorphism, each character will do what it is supposed to do.
I might have been a bit unclear in my explanation above (I tend to be that, sorry). But I am actually using polymorphism. I have a base "Actor class" that has several virtual methods like Update and Draw. Both the player, and the mobs inherit from this base class. These are then called through the playscreens update and draw methods accordingly.
It depends a lot of the structure of the game and what you're trying to do.
I.e. if you're making a pac-man game then you know the ghosts need to obtain information like the location of the player and other ghosts in comparison to them. You also know the map needs to be able to spawn things like bonus items.
Using that train of thought you can make a list of what each things needs to be able to access and try to design a single point of cohesion to it. I've personally found this to be one of the harder parts of designing. I.e. who should the ghosts talk to in order to get the location of the player, should the map have functions that locate characters and each character has a reference to the map? Or you could use something like a game referee class that has access to the map and sort of "moves the pieces around" based on its state vs the gameplay rules.
In general for something like what you're talking about you have to decide how the mobs will "find things" using the map is an okay idea since you can have functions that belong to the map that help return things in range of you, in that case you would either want to create the mob with a reference to the world or map object it belongs inside or pass it into the update function, you could also do something like make a "third party" object that all the map entities talk to in order to locate other things on the map and let them compare their positions.
Part of why I hate programming, there's a billion ways to do everything and many of them are just as "correct" as another.
In your case this applies as delegation of responsibility, do the mobs just get put into the world and act by themselves or are they just puppets some scripted object needs to control? In a game like minecraft for instance the mobs just get tossed into the world and get an update method called on them and they use the world object to find players and obstacles and decide on their behavior.
This was very helpful. The idea of having methods in the map that assists mobs in finding things near it.. That's a splendid idea, I haven't thought of that. Most of my mobs are going to be more or less static in their behavior, like moving around until they encounter an obstacle, then turn around. Bosses and at least one of the mobs will need to know where the player is though.
Nice to know about how things work in Minecraft- Being a Minecrafter myself, I've always been curious about how things work.
Thanks a lot for all of your replies. You guys have some very nice ideas, that I can definitely use.
I just wanted to check back, and say that I managed to solve the problem. The problem was the two for loops in the WorldCollision method. So basically I had to change the maximum range to check within. See below:
for (int x = sprite.Bounds.X / TILE_SIZE - 1; x < sprite.Bounds.X / TILE_SIZE + 2; x++)
for (int y = sprite.Bounds.Y / TILE_SIZE - 1; y < sprite.Bounds.Y / TILE_SIZE + 2; y++)
for (int x = sprite.Bounds.X / TILE_SIZE - 1; x < (sprite.Bounds.X + sprite.Bounds.Width) / TILE_SIZE + 1; x++)
for (int y = sprite.Bounds.Y / TILE_SIZE - 1; y < (sprite.Bounds.Y + sprite.Bounds.Height) / TILE_SIZE + 1; y++)
I believe that the reason it was jittering is that the player sprite has a different size than the tiles. Each tile is 32X32, while the player sprite varies in size based on the current animation frame (For pixel perfect collision detection). The way I calculate the maximum iteration value (can you say that?) makes sure that it checks the tile exactly next to it, both to the right and downwards, whereas it only did so to the left and upwards before.
Anyways- Thanks for your input, just wanted to post the solution in case someone else could use it :-)
This if (verticalMovement) part also looks a bit suspect, there is a condition in which the position gets updated but not the velocity? That can't happen for horizontal movement though. The position and velocity always get updated symmetrically.
Nice catch- I need some restructuring there. (Structure is really my weakness ) - Thanks a lot.