|
So, here we are again.
This week I chose to try implementing animations and shooting, both of which being pretty big additions to a game.
However, this post will only be about the shooting, leaving the animations for another day.
First thoughts about shooting: bullets need to be their own object and only spawned/despawned when actually in use, so as to not cause unnecessary performance-drops.
The bullets also need to be associated with the player, the enemies and the walls, so an efficient way to get them all in the same place will be needed.
After thinking about how to approach this I wrote a Bullet-class with a BulletManager that will be handling the creation/deletion of bullets as well as the collisions for every bullet.
The Bullet-class contains basically the same things as any other object would, a texture, sprite and position.
Now I had to make the bullet not only move from and to the correct positions but also to face the correct way so I sat down and did the math, the results were a bit odd but functional:

So my thought process for this was that I had to find out the angle (so that the bullet moves away from the player in the direction that he is looking) and then getting the direction through the angle.
Seeing as I had done the very same calculation before when rotating the player to always look towards the mouse I simply copied the formula and then I took the cosinus and sinus-values to get the directional vector. After that I tied the bullets to the ammo-icon, meaning that you only had 6 shots:

This screenshot was taken after I made the bullet also rotate using the angle earlier calculated.
So now the player can shoot, well what about the enemies?
They have to be able to shoot and also die from the players bullets.
But when I tried to just nestle the event that causes the enemies to chase with firing a bullets the results were a bit peculiar, the bullets flew in seemingly random directions with really odd angles on the sprite.
After some investigation I realized that the first calculation is done my making the players position relative to the game window instead of the game world since the mouses position is either in relation to the computers screen or the game window.
So after delving back into the depths of math I ended up having to make the bullets all have a std::string type because of that fact and then having an overloaded method GetDirection that looked like this:

Which first makes the two coordinates relative to the game-world using mapCoordsToPixel instead of mapPixelToCoords (if I didn’t do this it bugged out and sent bullets randomly) and then does a pythagorean calculation followed by a normalization of the vector to get the direction. After this I noticed something really strange, every bullet the enemies shoot were exactly right except going in the exact opposite direction, but seeing as every single bullet they shot did this I simply put ‘-‘ before the vector, which you can see on the last line of code there.
After this the player and the guards could both shoot accurate bullets that was rotated correctly, now they just have to kill things they hit…
So, inside of the BulletManager I create a CheckCollisions-method that takes an Enemy as a parameter, this was just due to the fact that the Enemy- and Player-objects aren’t related and thus I couldnt just send in a GameObject that I would normally have done.
The calculation for this turned out to be pretty interesting as I had recently implemented animations and apparently the coordinates for the sprites are thos of the entire spritesheet (500×500 instead of the 50×30-something that the player actually is) so everything died no matter where you hit it.
This was fixed by accident as I was looking around in the various methods in sf::Sprite. I found the getLocalBounds-method that “returns the bounds of the entity in the entity’s coordinate-system” meaning: just what I needed! The code:

This takes the local bounds for the enemy and then adds its position plus half of the current texture rectangle (the current sprite in the spritesheet) making it almost pixelperfect to the actual sprite seen on the screen.
It then loops through every bullet and if it was shot by the player it checks if the enemy collisions-rectangle contains the point that the bullet is currently on, and if so return true and, finally, kill the guard.
I then did the same thing for the player and you can now both kill and get killed by the bullets in our game, and that concludes this weeks post!
I hope you had a great time and I’ll see you next week.

|