-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMover.java
75 lines (63 loc) · 1.65 KB
/
Mover.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
import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
/**
* Write a description of class Mover here.
*
* @author (your name)
* @version (a version number or a date)
*/
public class Mover extends Actor
{
private Point speed = new Point(0, 0);
private double max_speed = 5;
private Point last_pos = new Point(0,0);
private Point pos;
/**
* Act - do whatever the Mover wants to do. This method is called whenever
* the 'Act' or 'Run' button gets pressed in the environment.
*/
public Mover(){
}
public Mover(double max_speed){
this.max_speed = max_speed;
}
public void act()
{
move();
// Add your action code here.
}
public void setSpeed(Point speed){
this.speed = speed;
if(speed.getLength() > max_speed)
this.speed.setLength(max_speed);
}
public Point getSpeed(){
return speed;
}
public void move(){
last_pos.x = getX();
last_pos.y = getY();
if(pos == null){
pos = new Point(getX(), getY());
}
else{
pos.x += speed.x;
pos.y -= speed.y;
}
setLocation((int)pos.x, (int)pos.y);
}
public void setLocation(Point pos){
setLocation((int)pos.x, (int)pos.y);
}
public Point getLocation(){
return new Point(getX(), getY());
}
public Point getLastPos(){
return last_pos;
}
public void setMaxSpeed(double speed){
max_speed = speed;
}
public double getMaxSpeed(){
return max_speed;
}
}