Player Bullets
|
This week I have made player bullets. Player bullets are the bullets that fire towards where the player is aiming with the mouse. First I decieded what I needed to make the bullets. I would need mouse input to see where the mouse is and to be able to fire by clicking the left mouse button. I would also need a function to tell where the bullets should shoot. And lastly I would need a texture to draw the bullets on the screen and a rectangle so the bullets can collide with other objects. I begun by making the header file and writing the appropriate variables and functions that would need in there. I also included The sfml libraries in the header file because I recieved and error if I would not. After I had created the header I started working on the cpp file. I made a constructor that takes in a sf::Sprite and a sf::Vector2f PlayerPosition. The sf::Sprite because I need to draw the bullets and the sf::Vector2 PlayerPosition because I would need that in a function later. Next I created a deconstuctor that deletes the bullets. Ths is important because if you do not delete the bullets the framerate will drop drasticly once the player has fired a lot of bullets. Then I made a function called GetSprite that returns the sf::Sprite that is used when creating a bullet. This is necessary to draw the bullet later in the main. I also built a GetRect function sets the rectangle to the globalBounds of the sf::Sprite and the returns the rectangle. I did this because I will need the rectangle to check colission later. And finally I created a Update function that takes a float called delta time to Update at the same time as eerything else. Inside the update function I initialze a sf::Vector2f called shotPos to the position of the player. This is so the bullet will fire out of the player. After that I set the position to fire. Then I put it into the main where I check the distance between the mouse and player using Pythagoras theorem. This is makes the bullets fire towards the mouses position in the window. I also create an array of bullets so that the player can fire multiple times. I use an std:vector for this because it has a variable size. in the main there is also a for loop that draws every bullet and updates every bullet. I also check when the player presses the left mousebutton so that I will know when the player wants to shoot. In there I create the bullet because it’s unnecessary earlier. |