-
Notifications
You must be signed in to change notification settings - Fork 0
/
SmoothMover.java
77 lines (70 loc) · 1.74 KB
/
SmoothMover.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
import greenfoot.*; // (World, Actor, GreenfootImage, and Greenfoot)
/**
* A variation of an actor that maintains a precise location (using doubles for the co-ordinates
* instead of ints). This allows small precise movements (e.g. movements of 1 pixel or less)
* that do not lose precision.
*
* @author Poul Henriksen
* @author Michael Kolling
* @author Neil Brown
*
* @version 3.0
*/
public abstract class SmoothMover extends Actor
{
private double exactX;
private double exactY;
/**
* Move forward by the specified distance.
* (Overrides the method in Actor).
*/
@Override
public void move(int distance)
{
move((double)distance);
}
/**
* Move forward by the specified exact distance.
*/
public void move(double distance)
{
double radians = Math.toRadians(getRotation());
double dx = Math.cos(radians) * distance;
double dy = Math.sin(radians) * distance;
setLocation(exactX + dx, exactY + dy);
}
/**
* Set the location using exact coordinates.
*/
public void setLocation(double x, double y)
{
exactX = x;
exactY = y;
super.setLocation((int) (x + 0.5), (int) (y + 0.5));
}
/**
* Set the location using integer coordinates.
* (Overrides the method in Actor.)
*/
@Override
public void setLocation(int x, int y)
{
exactX = x;
exactY = y;
super.setLocation(x, y);
}
/**
* Return the exact x-coordinate (as a double).
*/
public double getExactX()
{
return exactX;
}
/**
* Return the exact y-coordinate (as a double).
*/
public double getExactY()
{
return exactY;
}
}