-
Notifications
You must be signed in to change notification settings - Fork 6
/
Peashooter.java
87 lines (73 loc) · 2.54 KB
/
Peashooter.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
import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
/**
* Write a description of class Peashooter here.
*
* @author (your name)
* @version (a version number or a date)
*/
public class Peashooter extends Plant
{
/**
* Act - do whatever the Peashooter wants to do. This method is called whenever
* the 'Act' or 'Run' button gets pressed in the environment. */
private GreenfootImage[] idle;
private GreenfootImage[] shoot;
private boolean shootOnce = false;
private boolean shooting = false;
private long shootDelay = 1700L;
private long lastFrame2 = System.nanoTime();
private long deltaTime2;
public Peashooter() {
maxHp = 60;
hp = maxHp;
shoot = importSprites("peashootershoot", 3);
idle = importSprites("peashooter", 9);
}
public void hit(int dmg) {
if (isLiving()) {
if (!shootOnce) {
hitFlash(idle, "peashooter");
} else {
hitFlash(shoot, "peashootershoot");
}
hp = hp-dmg;
}
}
public void update() {
MyWorld = (MyWorld)getWorld();
currentFrame = System.nanoTime();
if (!shooting) {
animate(idle, 150, true);
lastFrame2 = System.nanoTime();
} else {
deltaTime2 = (currentFrame - lastFrame2) / 1000000;
if (deltaTime2 < shootDelay) {
animate(idle, 150, true);
shootOnce = false;
} else {
if (!shootOnce) {
shootOnce = true;
frame = 0;
}
if (frame >= 3) {
AudioPlayer.play(80, "throw.mp3", "throw2.mp3");
MyWorld.addObject(new Pea(getYPos()), getX()+25,getY()-17);
lastFrame2 = currentFrame;
}
animate(shoot, 100, false);
}
}
if (MyWorld.level.zombieRow.get(getYPos()).size() == 0) {
shooting = false;
} else {
for (Zombie i : MyWorld.level.zombieRow.get(getYPos())) {
if (i.getX() > getX() && i.getX()<=MyWorld.getWidth()+10){
shooting = true;
break;
} else {
shooting = false;
}
}
}
}
}