That part was easy. But what's a creeper cow to do? We said its responsibility
is to “attack players.” What does that mean? How about this:
Find the closest player. (There might not be one.)
Jump toward the closest player. (Don't die from the impact!)
If you hit the closest player, explode.
So for the CreeperCow plugin, let's take our list of gathered materials plus the
list of steps, make up some function names, and see what we need to code:
CreeperCow (the main plugin class):
• cowList : A static list of cows we've created.
• Math.random() : We need a random number, and the Java docs say this will
return a double between 0 and 1. 1
• spawnCows() : Create some random number of cows, no more than some
established limit. We'll create a CreeperCowTimer to hold each cow and set
its timer going.
• eventListener() : A listener to call spawnCows() .
CreeperCowTimer (one per cow):
• findClosestPlayer() : There might not be one.
• jump(Locationloc) : Jump means fly up into the air and don't die on impact.
• explode() : If we hit a player (or are close enough), explode and tell the plugin
to remove this cow.
There's a lot we haven't figured out yet, but this gives us enough to start
putting some code together. And that will help us figure out the remaining
questions. And probably raise some new ones. Onward we go.
Try This Yourself
Get a couple of index cards or some bits of paper, and jot down your main
class and its responsibilities, and any parts of Canary it might need. If you
need extra classes as we did here (like for a task runner), or anything else
you need to keep track of, create those cards as well.
For each responsibility, write a list of steps that reads like a story. Then go
through your list of “materials” and take a first stab at making a list of func-
tions and variables, asking the same questions we asked here.