-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathNPC.java
224 lines (193 loc) · 6.42 KB
/
NPC.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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
import greenfoot.*;
import java.nio.file.*;
import java.nio.charset.StandardCharsets;
import java.util.*;
/**
* The NPC class, to allow NPC's in the GameWorld.
*
* @author Matthew Hillier
* @version 1.0
*/
public class NPC extends Movers
{
/**
* Possible directions:
* -1 = Move
* 0 = Wait
* 1 = Look Up
* 2 = Look Down
* 3 = Look Left
* 4 = Look Right
*/
private int[] directions = {}; //An array of directions and movements
private int dirCounter = 0; //A counter to go through the directions
private String lastDir = "Down"; //The last direction of the NPC
private int imageX = 0; //The image X coordinate of the NPC
private int moveCounter = 0; //A counter to go through the movements
private int imgCounter = 1; //A counter for the image
private int INIT_IMG_COUNTER = imgCounter; //The initial image counter
GreenfootImage mainImage = new GreenfootImage("NPC_Sprites.png"); //The image of the NPC
/**
* Creates a new NPC.
* @param imageID The NPC's image id.
* @param directionsID The NPC's direction id.
* @param npcMessage The NPC's message.
*/
public NPC(int imageID, int directionsID, String npcMessage)
{
npcInit(imageID, directionsID, npcMessage);
}
/**
* Creates an NPC with a function.
* @param imageID The NPC's image id.
* @param directionsID The NPC's direction id.
* @param npcMessage The NPC's message.
* @param newFunction The NPC's function.
* @param newID The NPC's ID.
*/
public NPC(int imageID, int directionsID, String npcMessage, String newFunction, int newID)
{
npcInit(imageID, directionsID, npcMessage);
function = newFunction;
id = newID;
}
/**
* Initialises the NPC.
* @param imageID The NPC's image id.
* @param directionsID The NPC's direction id.
* @param npcMessage The NPC's message.
*/
public void npcInit(int imageID, int directionsID, String npcMessage)
{
try
{
List<String> list = Files.readAllLines(Paths.get("npcDirections.txt"), StandardCharsets.UTF_8);
String[] newDirectionsArr1 = list.toArray(new String[list.size()]);
String newDirectionsStr = newDirectionsArr1[directionsID];
String[] newDirectionsArr2 = newDirectionsStr.split("\\s*,\\s*");
int[] newDirections = new int[newDirectionsArr2.length];
for(int i = 0; i < newDirections.length; i++)
{
newDirections[i] = Integer.parseInt(newDirectionsArr2[i]);
}
this.directions = newDirections;
}
catch(Exception error)
{
System.out.println(error.getMessage());
}
GreenfootImage currentImg = new GreenfootImage(48, 48);
imageX = imageID * -192;
int currentX = 0;
switch(directions[0])
{
case 1: lastDir = "Up";
currentX = 3;
break;
case 2: lastDir = "Down";
currentX = 0;
break;
case 3: lastDir = "Left";
currentX = 2;
break;
case 4: lastDir = "Right";
currentX = 1;
break;
}
currentImg.drawImage(mainImage, imageX + (currentX * -48), 0);
setImage(currentImg);
type = "Messenger";
message = npcMessage;
}
/**
* The act method of the NPC.
*
* This makes it move and animate.
*/
public void act()
{
if(moveCounter < directions.length * 2)
{
if(moveCounter % 2 == 0)
{
if(dirCounter < directions.length)
{
switch(directions[dirCounter])
{
case -1:
if(lastDir.equals("Up") && !isObjectAbove(TileBlock.class) && !isObjectAbove(Player.class))
{
setLocation(getX(), getY() - 24);
}
else if(lastDir.equals("Down") && !isObjectBelow(TileBlock.class) && !isObjectBelow(Player.class))
{
setLocation(getX(), getY() + 24);
}
else if(lastDir.equals("Left") && !isObjectLeft(TileBlock.class) && !isObjectLeft(Player.class))
{
setLocation(getX() - 24, getY());
}
else if(lastDir.equals("Right") && !isObjectRight(TileBlock.class) && !isObjectRight(Player.class))
{
setLocation(getX() + 24, getY());
}
updateImage();
break;
case 1: lastDir = "Up";
break;
case 2: lastDir = "Down";
break;
case 3: lastDir = "Left";
break;
case 4: lastDir = "Right";
break;
}
dirCounter++;
}
}
moveCounter++;
}
else
{
moveCounter = 0;
dirCounter = 0;
}
}
/**
* Updates the NPC's image.
*/
public void updateImage()
{
GreenfootImage currentImage = this.getImage();
currentImage.clear();
int currentX = 0;
switch(lastDir)
{
case "Down":
currentX = 0;
break;
case "Right":
currentX = 1;
break;
case "Left":
currentX = 2;
break;
case "Up":
currentX = 3;
break;
default:
currentX = 0;
break;
}
if(imgCounter > 0)
{
imgCounter--;
currentImage.drawImage(mainImage, imageX + (currentX * -48), 0);
}
else
{
imgCounter = INIT_IMG_COUNTER;
currentImage.drawImage(mainImage, imageX + (currentX * -48), -48);
}
}
}