-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathNonPlayerCharacter.java
134 lines (117 loc) · 3.7 KB
/
NonPlayerCharacter.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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
import java.util.*;
/**
* NPC class
*
* @author (your name)
* @version (a version number or a date)
*/
public class NonPlayerCharacter extends Character
{
/**
* For ramdom move
*/
private int moveRadius;
public NonPlayerCharacter(String name) {
this.setName(name);
}
public void act() {
super.act();
move();
}
public int getMoveRadius() {
return this.moveRadius;
}
public void setMoveRadius(int moveRadius) {
this.moveRadius = moveRadius;
}
/**
* Test if we can move forward. Return true if we can, false otherwise.
*/
public boolean canMove(Direction direction)
{
Location iLocation = getInitialLocation();
int width = SceneManager.getInstance().getScene().getWidth();
int height = SceneManager.getInstance().getScene().getHeight();
int x = getX();
int y = getY();
switch(direction) {
case SOUTH :
y++;
break;
case EAST :
x++;
break;
case NORTH :
y--;
break;
case WEST :
x--;
break;
}
int minX = moveRadius == 0 || iLocation.getX() - moveRadius < 0 ? 0 : iLocation.getX() - moveRadius;
int minY = moveRadius == 0 || iLocation.getY() - moveRadius < 0 ? 0 : iLocation.getY() - moveRadius;
int maxX = moveRadius == 0 || iLocation.getX() + moveRadius > (width - 1) ? width - 1 : iLocation.getX() + moveRadius;
int maxY = moveRadius == 0 || iLocation.getY() + moveRadius > (height - 1) ? height - 1 : iLocation.getY() + moveRadius;
if (x < minX || y < minY || x > maxX || y > maxY)
{
return false;
}
return true;
}
/**
* Test if we can move forward. Return true if we can, false otherwise.
*/
public boolean canMove()
{
return canMove(this.getDirection());
}
public void move() {
if(isFrozen()) return;
while(!canMove()) {
turn();
return;
}
if(Greenfoot.getRandomNumber(1000) < 10) {
Direction newDirection = Direction.fromInt(Greenfoot.getRandomNumber(4));
while(!canMove(newDirection)) {
newDirection = Direction.fromInt(Greenfoot.getRandomNumber(4));
}
setDirection(newDirection);
}
else {
animate();
switch(getDirection()) {
case SOUTH :
setLocation(getX(), getY() + (getSpeed() / 10));
break;
case EAST :
setLocation(getX() + (getSpeed() / 10), getY());
break;
case NORTH :
setLocation(getX(), getY() - (getSpeed() / 10));
break;
case WEST :
setLocation(getX() - (getSpeed() / 10), getY());
break;
}
}
}
public void turn()
{
switch(getDirection()) {
case SOUTH :
setDirection(Direction.EAST);
break;
case EAST :
setDirection(Direction.NORTH);
break;
case NORTH :
setDirection(Direction.WEST);
break;
case WEST :
setDirection(Direction.SOUTH);
break;
}
}
}