Java Reference
In-Depth Information
I've written some code, tested it, and now I'll do a gitcommit to save this state
of the world before going on.
Try This Yourself
Your turn! Start fleshing out your functions just as I did here. Don't feel they
have to do everything yetâ€”just enough to get started (for instance, our cows
here don't jump yet).
For each function you make, include a test command so you can try it from
inside the game.
Make sure all your test commands work with as much as you have done so
far. And don't forget to save your progress with Git.
Filling In the Details: CreeperCowTimer
Earlier we decided to put the code for the jumping, attacking, exploding cow
into a new CreeperCowTimer class. Eventually we'll crank this up from an event
in the main CreeperCow plugin.
But first off we need a function to make a cow jump and attack. We'll build
and test that first.
Given a target and our cow's location, we create a new Vector3D by taking the
difference in Location coordinates in the x and z directions: that gives us the
displacement between the cow and its target. But that's too large a number
for a velocity, so we then multiply it by a convenient number (0.075) that I
found by experimenting. Finally we set that as the cow's velocity, which will
make it jump along that Vector3D at that speed.
public class CreeperCowTimer {
private Cow cow;
public void jump(Location target) {
Location cowLoc = cow.getLocation();
double multFactor = 0.075;
Vector3D v = new Vector3D(
(target.getX() - cowLoc.getX()) * multFactor,
0.8,
(target.getZ() - cowLoc.getZ()) * multFactor
);
cow.moveEntity(v.getX() + ( Math .random() * -0.1),
v.getY(),
v.getZ() + ( Math .random() * -0.1));
}
}

Search WWH ::

Custom Search