-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathZombie.java
171 lines (145 loc) · 4.67 KB
/
Zombie.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
import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
import java.util.*;
public class Zombie extends animatedObject
{
public boolean fallen = false;
public boolean eating = false;
public boolean eatOnce = false;
public int hp;
public int maxHp;
public double walkSpeed;
public MyWorld MyWorld;
public boolean spawnHead = false;
public Plant target;
public int eatSpeed;
public boolean isAlive = true;
public GreenfootImage[] headless;
public GreenfootImage[] headlesseating;
public GreenfootImage[] fall;
public boolean resetAnim = false;
public boolean finalDeath = false;
public boolean fixAnim = false;
/**
* Act - do whatever the Zombie wants to do. This method is called whenever
* the 'Act' or 'Run' button gets pressed in the environment.
*/
public Zombie() {
headless = importSprites("zombieheadless", 7);
fall = importSprites("zombiefall",6);
headlesseating = importSprites("headlesszombieeating", 7);
}
public void act()
{
if (getWorld() != null) {
if (isLiving()) {
update();
} else {
deathAnim();
}
}
}
public void update() {
}
public void deathAnim() {
if (!resetAnim) {
frame = 0;
resetAnim = true;
}
if (frame <=6) {
if (finalDeath) {
if (!fixAnim) {
fixAnim = true;
AudioPlayer.play(80, "zombie_falling_1.mp3", "zombie_falling_2.mp3");
MyWorld.addObject(new fallingZombie(fall), getX()-12, getY()+20);
MyWorld.removeObject(this);
return;
}
} else {
if (!spawnHead) {
spawnHead = true;
AudioPlayer.play(80, "limbs_pop.mp3");
getWorld().addObject(new Head(), getX(), getY()-10);
}
if (!eating) {
animate(headless, 350, false);
move(-walkSpeed);
} else {
animate(headlesseating, 350, false);
}
}
} else if (!finalDeath) {
resetAnim = false;
finalDeath = true;
for (ArrayList<Zombie> i : MyWorld.level.zombieRow) {
if (i.contains(this)) {
i.remove(this);
break;
}
}
}
}
public void playEating() {
if (frame == 5 || frame == 2) {
if (!eatOnce) {
eatOnce = true;
AudioPlayer.play(70, "chomp.mp3", "chomp2.mp3", "chompsoft.mp3");
target.hit(10);
}
} else {
eatOnce = false;
}
}
@Override
protected void addedToWorld(World world) {
MyWorld = (MyWorld)getWorld();
}
public boolean isLiving() {
if (hp <=0) {
isAlive = false;
} else {
isAlive = true;
}
return isAlive;
}
public void hit(int dmg) {
}
public void takeDmg(int dmg) {
hp -= dmg;
if (hp <= 0) {
for (ArrayList<Zombie> i : MyWorld.level.zombieRow) {
if (i.contains(this)) {
i.remove(this);
break;
}
}
getWorld().removeObject(this);
return;
}
}
public boolean isEating() {
var row = MyWorld.board.Board[getYPos()];
for (int i = 0; i < MyWorld.board.Board[0].length; i++) {
if (row[i] != null) {
if (Math.abs(row[i].getX() - getX()+5) < 35) {
if (row[i] instanceof PotatoMine) {
if (((PotatoMine)row[i]).armed == true) {
eating = false;
return false;
}
}
eating = true;
target = row[i];
return eating;
}
}
}
eating = false;
return eating;
}
public int getYPos() {
return ((getY()-MyWorld.level.yOffset)/MyWorld.level.ySpacing);
}
public int getXPos() {
return getX();
}
}