Colliding with an obstacle
To have collisions, we'll have to actually put the bounding boxes we've seen on both RHB and the stone. Then, in the update
function of WalkTheDog
, we'll need to detect that collision, and when that collision happens, we'll move RHB into the Falling
and KnockedOut
states, which correspond to the Dead
animation in the sprite sheet. Much of that code, particularly the state machine, will be very familiar, so I'll refrain from reproducing the parts that are repetitive and highlight the differences. I will remind you of what needs to change in new states, and you can always check the final code at https://github.com/PacktPublishing/Rust-Game-Development-with-WebAssembly/tree/chapter_5/.
Let's start with the easiest bounding box, the one for the stone.
A bounding box for a stone
The stone is the simplest of the bounding boxes because we can just use the size of HTMLImageElement
. This won't always be the case. If you...