-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathNPC.java
183 lines (172 loc) · 5.73 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
import greenfoot.*;
import java.awt.Color;
/**
* Write a description of class NPC here.
*
* @author (your name)
* @version (a version number or a date)
*/
public class NPC extends Actor
{
private int movin = 0;
private int movinUp = 0;
private int movinDown = 0;
private int movinRight = 0;
private int movinLeft = 0;
int a = 0;
int bulletDelay;
private int name;
/**
* Act - do whatever the NPC wants to do. This method is called whenever
* the 'Act' or 'Run' button gets pressed in the environment.
*/
public void act()
{
npcshoot();
movement();
drift();
engineStatus();
Actor asteroid = getOneIntersectingObject(Asteroid.class);
if(asteroid != null){
getWorld().addObject(new Explosion(), getX(), getY());
generateDeathline();
Greenfoot.delay(100);
getWorld().removeObject(this);
}
if(a == 0) {
getWorld().addObject(new NPCText(), getX(), getY() - 15);
a = 1;
}
}
public NPC()
{
bulletDelay = 10;
}
/**
* Depending on the last direction of the player, the ship will continue to "drift" in that direction.
*/
public void drift()
{
if (movinUp == 1) {
setLocation(getX(), getY() - 1);
}
if (movinDown == 1) {
setLocation(getX(), getY() + 1);
}
if (movinRight == 1) {
setLocation(getX() + 1, getY());
}
if (movinLeft == 1) {
setLocation(getX() - 2, getY());
}
}
public void engineStatus()
{
if ( Greenfoot.isKeyDown("right") || Greenfoot.isKeyDown("d") )
{
setImage("Ship Engine On.png");
}
else
{
setImage("Ship Engine Off.png");
}
}
public void movement()
{
if ( (Greenfoot.isKeyDown("right") || Greenfoot.isKeyDown("d")) && !(Greenfoot.isKeyDown("left") || Greenfoot.isKeyDown("a")) )
{
setLocation(getX() + 2, getY());
//setImage("Ship Engine On.png");
GreenfootImage image = getImage();
//image.scale(image.getWidth() - 350, image.getHeight() - 60);
setImage(image);
movin = 1;
movinRight = 1;
movinUp = 0;
movinDown = 0;
movinLeft = 0;
//change image to Engine On, else Engine Off
}
else if ( (Greenfoot.isKeyDown("left") || Greenfoot.isKeyDown("a")) && !(Greenfoot.isKeyDown("right") || Greenfoot.isKeyDown("d")) )
{
setLocation(getX() - 2, getY());
//setImage("Ship Engine Off.png");
GreenfootImage image = getImage();
//image.scale(image.getWidth() - 260, image.getHeight() - 53);
setImage(image);
movinLeft = 1;
movinUp = 0;
movinDown = 0;
movinRight = 0;
}
if ( (Greenfoot.isKeyDown("up") || Greenfoot.isKeyDown("w")) && !(Greenfoot.isKeyDown("down") || Greenfoot.isKeyDown("s")) )
{
setLocation(getX(), getY() - 3);
movin = 1;
movinUp = 1;
movinLeft = 0;
movinDown = 0;
movinRight = 0;
}
else if ( (Greenfoot.isKeyDown("down") || Greenfoot.isKeyDown("s")) && !(Greenfoot.isKeyDown("up") || Greenfoot.isKeyDown("w")) )
{
setLocation(getX(), getY() + 3);
movin = 1;
movinDown = 1;
movinUp = 0;
movinLeft = 0;
movinRight = 0;
}
if (movin == 0) {
//setImage("Ship Engine Off.png");
GreenfootImage image = getImage();
//image.scale(image.getWidth() - 260, image.getHeight() - 53);
setImage(image);
}
movin = 0;
}
public void generateDeathline(){
name = Greenfoot.getRandomNumber(10);
if(name == 1){
setImage(new GreenfootImage("To Hell", 12, Color.WHITE, Color.BLACK));
}
if(name == 2){
setImage(new GreenfootImage("I forgive", 12, Color.WHITE, Color.BLACK));
}
if(name == 3){
setImage(new GreenfootImage("All of us had a reason to find home", 20, Color.WHITE, Color.BLACK));
}
if(name == 4){
setImage(new GreenfootImage("Home remembers", 12, Color.WHITE, Color.BLACK));
}
if(name == 5){
setImage(new GreenfootImage("I understand.", 12, Color.WHITE, Color.BLACK));
}
if(name == 6){
setImage(new GreenfootImage("I lived.", 12, Color.WHITE, Color.BLACK));
}
if(name == 8){
setImage(new GreenfootImage("Must the End justify my death to you?", 20, Color.WHITE, Color.BLACK));
}
if(name == 9){
setImage(new GreenfootImage("I'm sorry.", 12, Color.WHITE, Color.BLACK));
}
if(name == 10){
setImage(new GreenfootImage("Home, now Forever distant.", 20, Color.WHITE, Color.BLACK));
}
}
/**
* The NPC ship needs to be able to move on its own according to where the asteroids are around it
* Basically it can't move randomllu, it has to be somewhat smart about it.
*/
public void npcshoot()
{
if (bulletDelay == 0) {
getWorld().addObject(new Bullet(), getX() + 50, getY());
bulletDelay = Greenfoot.getRandomNumber(200);
}
if (bulletDelay > 0) {
bulletDelay --;
}
}
}