Making a Game Part 4 – Animations

This week has been mostly making this operate again when merging our codes (Git Hub did not cooperate) so as to have a functional game for the beta.

One of the things that needed a bit changing were the animations seeing as they operated as a substitute for the sf::Sprite instead of together with it, meaning that when we tried to make a gameobject-hierarchy it would become problematic since not every object will be animated.
The first solution we tried was to just make another step in the hierarchy, an AnimatedObject, and while that would work as a solution it would be very clunky while trying to call the Draw/Update/Various Get-methods and so another solution was needed.

Later that day I realized that an animation basically only needs a bunch of rectangles and the duration for each rectangle and so I re-wrote the AnimatedSprite-class and renamed it into Animation.
It now only consists of a vector with enum Frames (x,y,w,h,duration) and a sf::intRect m_currentRect. What it does now is return a boolean true in the Update()-method if the duration for the current frame has been reached and I then call the GetNextSpriteRect()-from wherever the Update-method was called. After this change I finally got the animations to cooperate with the GameObject-hierarchy.
Then another problem arose, we need to have different animations for if the player is running, shooting or picking up objects and currently the Animation-class only supports one animation at a time, meaning that I either had to make another object that still only had rectangles and durations and then manually change the sprites texture into the new animation (we have the different animations in different images and not one huge sprite-sheet).
This seemed to be a less than optimal solution so I decided to up my game from having a vector with frames to having a map with an std::string animationname and a vector with frames for each, and after some fiddling around it finally worked, the code for loading/adding animations:

I do realize now that i probably should switch the names of the vector and map to not be so closely reminiscent of each other, m_animation and m_animations will make it a bit confusing…

Anyway, what I do here is simply stream the data that is in the text-file, which is:

duration, x, y, width and height of the texturerectangles.

In every iteration it calls the AddFrame()-method and adds a frame to the vector that holds all frames, and when it has reached the end of the textfile it calls upon the AddAnimation()-method that adds the animation to the map using a name as a key and the vector with frames as its value.

I then have the objects call a SetCurrentAnimation(std::string name) that uses the map.find(name) to set the current animation.

One problem that I have yet to solve is that if you want to change the image (the sprites texture)  along with the animation you have no way of doing that easily and have to manually set both the texture and the current animation separately, which leaves room for unnecessary errors…

This is how the game currently looks:


That is all for this time, thank you for reading and I’ll see you next week!