The component stores a reference to the objects velocity component, we’ll use this to calculate the movement direction. It also stores the current FacingDirection in a variable called currentDir, which, unsurprisingly, stores the entities current direction. We do this because if the entity is not moving that frame and we cannot calculate its direction based on its velocity, we can pass this as its last known direction (this will become more clear shortly).
The component has an Awake function that we won’t be calling directly (for more information on for the Awake and Start functions work see here) but will be used to retrieve the velocity component from the object. So the only public function that we will call is ‘Get’ which returns a FacingDirection.
The hash object is a functor object, which is an object that defines the () operator and can be treated as if they are a function. If we use them in the core code for the game I’ll go through it in more detail.
Using the image above we know that four directions and there corresponding texture locations are:
- Up = 0, 0, 64, 64
- Left = 64, 0, 64, 64
- Down = 128, 0, 64, 64
- Right = 192, 0, 64, 64
And now we can use the rectangle returned from the unordered_map to specify which sprite we want to use.