kyleconroy/hawkthorne-journey — pull request #268 by campadrenalin (2012-09-13 05:27:03+00:00)
Title: Bug265: Rewrote a bunch of code for holding objects

This should be ridiculously stable now, and easy to make new classes for. At some point we may want some sort of generic "holdable" class or mixin, but that should be pretty reasonable now that there's a better structure to that part of the code.

## Changes to player.lua

The player class now uses the variables ```self.currently_held``` and ```self.holdable``` to store, respectively, the object currently being held, and the object you would grab if you were try to pick something up. So, self.holdable is used the same as before, but self.holding is no longer used, traded off for the more descriptively-named and better-used currently_held.

It also contains some new methods, all of them documented.

### setSpriteStates(presetName)

Takes a string "preset name", such as "holding" or "default", and sets some internal variables according to that preset. At present, those variables are walk_state, crouch_state, and gaze_state, although one of the nice things about having that code all in one place is that it would be easy to add more variables to this function's purview, unlike before, where these values were being set by external code all over the place.

### pickup()

Pick up the current self.holdable. This does nothing if you're already holding something, but will set sprite state to "holding" using the above function, set currently_held to holdable, and call currently_held:pickup(self) if the object contains that callback function.

### throw()

Throws the currently held object. Works a lot like pickup(), including setting sprite state to default, and calling the thrown object's "throw" callback if available.

### throw_vertical()

Throw the currently held object straight into the air. You can do this by gazing up and throwing. Callback code is what you'd expect.

### drop()

Can be used to set fragile objects down without injury, drop a baseball to the ground half-heartedly, etc. Callback code is what you'd expect.

## Baseball and Pot

Both of these classes had some surgery on their innards, and no longer listen for keypresses. That job has now been moved to the Player class, along with setting sprite state, so all these classes have to do is provide the optional callbacks dictating their behavior when picked up, thrown, thrown vertically, or dropped. These callbacks mostly involve physics and setting some variables that track internal state.

While I was in there, I also took the liberty of incorporating the player velocity into the object's physics wherever it improved gameplay. There were a few parts where I tried it and it was lame, but those parts I undid before publishing.

## Holding and glancing

One thing that was made obvious during testing is that the wrong sprites are used when looking up or down while holding an object. The animation simply becomes "holdwalk", which doesn't make much sense when you're standing still underneath Cornelius' chin getting ready to pop one straight up at him from underneath. That's probably a fix for another day, though, since it's unrelated to the physics, and was a preexisting problem before I stuck my nose in to fix this bug.