This repository has been archived by the owner on Nov 10, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Enemy2.java
115 lines (102 loc) · 3.05 KB
/
Enemy2.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
import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
public class Enemy2 extends Actor
{
/**
* Kelas Enemy1 mirip dengan Enemy, hanya saja Parameternya berbeda,
* seperti speedMusuh dan Tambahan score jika kena tembakan
*/
int speedMusuh= 3;
public void act()
{
kejar();
destroy();
if(!Player.matiPak()){
if(checkTembak()){
Enemy.hitungMati++;
LevelOne.score.add(20);
Greenfoot.playSound("sfx/enemyDed.wav");
coinDrop();
hapus();
}
else if(cekNabrak()){
Enemy.hitungMati++;
LevelOne.hp.add(-1);
Greenfoot.playSound("sfx/hit.wav");
getWorld().removeObject(this);
}
}
}
public void started(){
Enemy.hitungMati = 0;
}
public void kejar(){
int x = getX();
int y = getY();
if(Greenfoot.getRandomNumber(1600) > Player.xKoord && x > Player.xKoord){
setImage("images/enemy2L.png");
move("Left");
}
else if(Greenfoot.getRandomNumber(1600) - 20 < Player.xKoord && x < Player.xKoord){
setImage("images/enemy2R.png");
move("Right");
}
if(Greenfoot.getRandomNumber(900) > Player.yKoord && y > Player.yKoord){
move("Up");
}
else if(Greenfoot.getRandomNumber(900) < Player.yKoord && y < Player.yKoord){
move("Down");
}
}
public boolean checkTembak(){
if(isTouching(ShurikenDown.class)) {
return true;
}
if(isTouching(ShurikenUp.class)) {
return true;
}
if(isTouching(ShurikenLeft.class)) {
return true;
}
if(isTouching(ShurikenRight.class)) {
return true;
}
return false;
}
public void move(String arah) {
if(arah == "Left"){
setLocation(getX() - speedMusuh, getY());
}
else if(arah == "Right"){
setLocation(getX() + speedMusuh, getY());
}
else if(arah == "Up"){
setLocation(getX(), getY() - speedMusuh);
}
else if(arah == "Down"){
setLocation(getX(), getY() + speedMusuh);
}
}
public boolean cekNabrak(){
if(isTouching(Player.class)){
return true;
}
return false;
}
private void hapus() {
removeTouching(ShurikenLeft.class);
removeTouching(ShurikenRight.class);
removeTouching(ShurikenUp.class);
removeTouching(ShurikenDown.class);
getWorld().removeObject(this);
}
public void coinDrop(){
if(Greenfoot.getRandomNumber(100) >= 85){
getWorld().addObject(new Koin(3), getX(), getY());
}
}
public void destroy(){
if(Player.matiPak()){
getWorld().removeObject(this);
}
}
}