There's a lot of branching going on here, and I don't know whether it'd be possible to reorder the instructions to reduce the number of if statements. Reducing the number of if statements would potentially make the code clearer.
I've refactored this a bit. There is still a bit of branching, but I can't see a way around this without changing how sprites work, which just isn't worth it. Hence I'll close this.
There's a lot of branching going on here, and I don't know whether it'd be possible to reorder the instructions to reduce the number of if statements. Reducing the number of if statements would potentially make the code clearer.
Addresses #6