SmoothMover.java
| 1 |
import greenfoot.*; // (World, Actor, GreenfootImage, and Greenfoot) |
|---|---|
| 2 |
|
| 3 |
/**
|
| 4 |
* A variation of an actor that maintains precise location (using doubles for the co-ordinates
|
| 5 |
* instead of ints). It also maintains a current movement in form of a movement vector.
|
| 6 |
*
|
| 7 |
* This is a variation of the SmoothMover class presented ealier in the book (version 2.0).
|
| 8 |
* This version implements wrap-around movement: when the actor moves out of the world at one
|
| 9 |
* side, it enters it again at the opposite edge.
|
| 10 |
*
|
| 11 |
* @author Poul Henriksen
|
| 12 |
* @author Michael Kolling
|
| 13 |
*
|
| 14 |
* @version 2.1
|
| 15 |
*/
|
| 16 |
public abstract class SmoothMover extends Actor |
| 17 |
{
|
| 18 |
private Vector movement; |
| 19 |
private double exactX; |
| 20 |
private double exactY; |
| 21 |
|
| 22 |
public SmoothMover()
|
| 23 |
{
|
| 24 |
this(new Vector()); |
| 25 |
} |
| 26 |
|
| 27 |
/**
|
| 28 |
* Create new thing initialised with given speed.
|
| 29 |
*/
|
| 30 |
public SmoothMover(Vector movement) |
| 31 |
{
|
| 32 |
this.movement = movement;
|
| 33 |
} |
| 34 |
|
| 35 |
/**
|
| 36 |
* Move in the current movement direction. Wrap around to the opposite edge of the
|
| 37 |
* screen if moving out of the world.
|
| 38 |
*/
|
| 39 |
public void move() |
| 40 |
{
|
| 41 |
exactX = exactX + movement.getX(); |
| 42 |
exactY = exactY + movement.getY(); |
| 43 |
if(exactX >= getWorld().getWidth()) {
|
| 44 |
exactX = 0;
|
| 45 |
} |
| 46 |
if(exactX < 0) { |
| 47 |
exactX = getWorld().getWidth() - 1;
|
| 48 |
} |
| 49 |
if(exactY >= getWorld().getHeight()) {
|
| 50 |
exactY = 0;
|
| 51 |
} |
| 52 |
if(exactY < 0) { |
| 53 |
exactY = getWorld().getHeight() - 1;
|
| 54 |
} |
| 55 |
super.setLocation((int) exactX, (int) exactY); |
| 56 |
} |
| 57 |
|
| 58 |
/**
|
| 59 |
* Set the location from exact coordinates.
|
| 60 |
*/
|
| 61 |
public void setLocation(double x, double y) |
| 62 |
{
|
| 63 |
exactX = x; |
| 64 |
exactY = y; |
| 65 |
super.setLocation((int) x, (int) y); |
| 66 |
} |
| 67 |
|
| 68 |
/**
|
| 69 |
* Set the location from int coordinates.
|
| 70 |
*/
|
| 71 |
public void setLocation(int x, int y) |
| 72 |
{
|
| 73 |
exactX = x; |
| 74 |
exactY = y; |
| 75 |
super.setLocation(x, y);
|
| 76 |
} |
| 77 |
|
| 78 |
/**
|
| 79 |
* Return the exact x-coordinate (as a double).
|
| 80 |
*/
|
| 81 |
public double getExactX() |
| 82 |
{
|
| 83 |
return exactX;
|
| 84 |
} |
| 85 |
|
| 86 |
/**
|
| 87 |
* Return the exact y-coordinate (as a double).
|
| 88 |
*/
|
| 89 |
public double getExactY() |
| 90 |
{
|
| 91 |
return exactY;
|
| 92 |
} |
| 93 |
|
| 94 |
/**
|
| 95 |
* Increase the speed with the given vector.
|
| 96 |
*/
|
| 97 |
public void addForce(Vector force) |
| 98 |
{
|
| 99 |
movement.add(force); |
| 100 |
} |
| 101 |
|
| 102 |
/**
|
| 103 |
* Accelerate the speed of this mover by the given factor. (Factors < 1 will
|
| 104 |
* decelerate.)
|
| 105 |
*/
|
| 106 |
public void accelerate(double factor) |
| 107 |
{
|
| 108 |
movement.scale(factor); |
| 109 |
if (movement.getLength() < 0.15) { |
| 110 |
movement.setNeutral(); |
| 111 |
} |
| 112 |
} |
| 113 |
|
| 114 |
/**
|
| 115 |
* Return the speed of this actor.
|
| 116 |
*/
|
| 117 |
public double getSpeed() |
| 118 |
{
|
| 119 |
return movement.getLength();
|
| 120 |
} |
| 121 |
|
| 122 |
/**
|
| 123 |
* Stop movement of this actor.
|
| 124 |
*/
|
| 125 |
public void stop() |
| 126 |
{
|
| 127 |
movement.setNeutral(); |
| 128 |
} |
| 129 |
|
| 130 |
/**
|
| 131 |
* Return the current speed.
|
| 132 |
*/
|
| 133 |
public Vector getMovement() |
| 134 |
{
|
| 135 |
return movement;
|
| 136 |
} |
| 137 |
} |