Our players currency, regaining lost candy.
|
Hey there! This week I have been working on a way to let the player regain health (or as it is for out game, regain candy as you play as a piñata). I chose to work on this feature and function for no particular reason rather than our game needs this to fully function and as my co-worker was currently working on patching our collision checking I began to work on and create this function. As it is written in our design document object will spawn candy once the are destroyed so I began by write a class for our candy objects and made it into an entity by letting it inherit from our entity class. Which grants it functions such as get and set position, give it a collider and making it visible or not along with sprites. By making it an entity it can pushed into our entity list in our game state along with all other objects displaying on our screen and it removed the purpose of creating a new function for it since it now works in the same way as the rest of our object along with both player and enemy. All our objects that can be destroyed is supposed to drop a candy so all our object would have to have a candy object in stand by, my first thought was to create a new candy object for each of our objects but this would mean almost twice the amount of entities for the game to load. Since this was not an attractive alternative I started to think of another way to deal with this and I came to think about an option our teacher had in a previous project where he reused a limited number of objects multiple times. This could work in our game as well since all candies would not be required to be visible at all times and if we could simply relocate the same ones over and over we decrease the power used by our game. After a few tires and errors I created a for loop that loops four times and creates two different candies which are both pushed into the entities list and are set to invisible at first. So now we have four candies that we can reuse across our game. Below is a picture showing before and after an object has been destroyed.
To relocate them I went to our collision check function and when a collision occurred between projectile and an object an if statement checks to see if a candy already is visible. If it was visible it would go to the next candy in the entities list and pick one which is not visible or if all are visible it chooses the one furthest back and move it forward. So we do not move candies that are currently displayed around the player and place it on the object’s position. When it is set to a new position it is also set to visible if it is not already so. Once the player collides with the candy the life increases and the candy are once again set to invisible so that it can be reused further in. This is one way to make this work and even if it now fully functional at this point it will soon be and we now does not have to deal with to many object but rather shorting the need of objects required in our game down as much as possible. This is all for now and I wish you a good weekend and until next time. |
