-
Notifications
You must be signed in to change notification settings - Fork 0
/
Enemy.java
64 lines (56 loc) · 2.26 KB
/
Enemy.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
import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
/**
* Write a description of class Enemy here.
*
* @author (your name)
* @version (a version number or a date)
*/
public class Enemy extends Actor
{
GreenfootSound blastSound = new GreenfootSound("blast.wav");
GifImage bat = new GifImage("bat.gif");
int nyawa = ScrollingWorld.health.getValue();
public void act()
{
// Add your action code here.
int mv = ScrollingWorld.movement;
move(mv + (-1));
setImage(bat.getCurrentImage());
getHit();
}
public void getHit(){
if(isTouching(Player.class)){
blastSound.play();
getWorld().addObject(new Enemy(), 620 + Greenfoot.getRandomNumber(200), 240 + Greenfoot.getRandomNumber(100));
getWorld().addObject(new Blast(), getX(), getY());
ScrollingWorld.health.add(-1);
checkLives();
getWorld().removeObject(this);
}else if(getX() < -300){
getWorld().addObject(new Enemy(), 620 + Greenfoot.getRandomNumber(200), 240 + Greenfoot.getRandomNumber(100));
getWorld().removeObject(this);
}
}
public void checkLives(){
if(nyawa == (3)){
//Replace or menimpa Hp to Hp0 in exact same location
//to create effect that Hp reduced
getWorld().addObject(new Hp0(), ScrollingWorld.lives3.getX(),
ScrollingWorld.lives3.getY());
}
else if(nyawa ==(2)){
//Replace or menimpa Hp to Hp0 in exact same location
//to create effect that Hp reduced
getWorld().addObject(new Hp0(), ScrollingWorld.lives2.getX(),
ScrollingWorld.lives2.getY());
}
else if(nyawa == (1)){
//if nyawa == 1, then it will stop bgm.wav playLoop,
//and change current world into World_End
getWorld().addObject(new Hp0(), ScrollingWorld.lives1.getX(),
ScrollingWorld.lives1.getY());
ScrollingWorld.bgm.stop();
Greenfoot.setWorld(new World_End());
}
}
}