-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathWizard.java
162 lines (143 loc) · 5.02 KB
/
Wizard.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
import greenfoot.*;
import java.util.List;
public class Wizard extends Actor
{
private int speed = 3;
private int skillCooldown = 0;
private int skillCooldownTime = 50;
private int energy = 0;
private int jumpStrength = 14;
private int vSpeed = 0;
private int acceleration = 1;
private boolean onGround;
public void act()
{
checkKeys();
checkFall();
checkForItems();
checkForGhost();
}
public void checkKeys()
{
if(Greenfoot.isKeyDown("left")) {
setImage("Wizard-Left.png");
move(-speed);
}
if(Greenfoot.isKeyDown("right")) {
setImage("Wizard-Right.png");
move(speed);
}
if(Greenfoot.isKeyDown("space") && onGround) {
jump();
}
if(Greenfoot.isKeyDown("1")) {
setImage("Wizard-Attack-Left.png");
if (skillCooldown == 0) {
useSkill2();
skillCooldown = skillCooldownTime; // Reset the cooldown
}
}
if(Greenfoot.isKeyDown("2")) {
setImage("Wizard-Attack-Right.png");
if (skillCooldown == 0) {
useSkill1();
skillCooldown = skillCooldownTime; // Reset the cooldown
}
}
if (skillCooldown > 0) {
skillCooldown--;
}
}
public void fall()
{
setLocation(getX(), getY() + vSpeed);
vSpeed += acceleration;
onGround = false;
}
public void checkFall() {
if (onGround()) {
vSpeed = 0;
onGround = true;
} else {
fall();
}
}
public void jump() {
vSpeed = -jumpStrength;
fall();
}
public boolean onGround() {
if (getY() >= getWorld().getHeight() - getImage().getHeight() / 2) {
return true;
}
List<Platform1> platforms1 = getObjectsAtOffset(0, getImage().getHeight() / 2, Platform1.class);
List<Platform2> platforms2 = getObjectsAtOffset(0, getImage().getHeight() / 2, Platform2.class);
List<Platform3> platforms3 = getObjectsAtOffset(0, getImage().getHeight() / 2, Platform3.class);
List<Platform4> platforms4 = getObjectsAtOffset(0, getImage().getHeight() / 2, Platform4.class);
List<Platform5> platforms5 = getObjectsAtOffset(0, getImage().getHeight() / 2, Platform5.class);
List<PlatformL2_1> platforms6 = getObjectsAtOffset(0, getImage().getHeight() / 2, PlatformL2_1.class);
List<PlatformL2_2> platforms7 = getObjectsAtOffset(0, getImage().getHeight() / 2, PlatformL2_2.class);
List<PlatformL2_3> platforms8 = getObjectsAtOffset(0, getImage().getHeight() / 2, PlatformL2_3.class);
List<PlatformL2_4> platforms9 = getObjectsAtOffset(0, getImage().getHeight() / 2, PlatformL2_4.class);
List<PlatformL2_5> platforms10 = getObjectsAtOffset(0, getImage().getHeight() / 2, PlatformL2_5.class);
return
!platforms1.isEmpty() ||
!platforms2.isEmpty() ||
!platforms3.isEmpty() ||
!platforms4.isEmpty() ||
!platforms5.isEmpty() ||
!platforms6.isEmpty() ||
!platforms7.isEmpty() ||
!platforms8.isEmpty() ||
!platforms9.isEmpty() ||
!platforms10.isEmpty();
}
public void useSkill1()
{
// Implementasi skill 1
energy--;
Greenfoot.playSound("Magic-Charge.wav");
skill1Charge skillCharge = new skill1Charge();
getWorld().addObject(skillCharge, getX() + 1, getY());
}
public void useSkill2()
{
// Implementasi skill 2
energy--;
Greenfoot.playSound("Magic-Charge.wav");
skill2Charge skillCharge = new skill2Charge();
getWorld().addObject(skillCharge, getX() + 1, getY());
}
public void checkForItems() {
scoreBoard scoreboard = (scoreBoard) getWorld().getObjects(scoreBoard.class).get(0);
if (isTouching(item1.class)) {
Greenfoot.playSound("Item-Collect.mp3");
removeTouching(item1.class);
scoreboard.addPoints(10);
}
if (isTouching(item2.class)) {
Greenfoot.playSound("Item-Collect.mp3");
removeTouching(item2.class);
scoreboard.addPoints(10);
}
if (isTouching(item3.class)) {
Greenfoot.playSound("Item-Collect.mp3");
removeTouching(item3.class);
scoreboard.addPoints(10);
}
}
public void loseCondition() {
getWorld().addObject(new alertGameOver(), getWorld().getWidth() / 2, getWorld().getHeight() / 2);
Greenfoot.playSound("Alert-Lose.mp3");
getWorld().addObject(new btnResume(), getWorld().getWidth() / 2, getWorld().getHeight() / 2 + 150);
getWorld().addObject(new btnQuit(), getWorld().getWidth() / 2, getWorld().getHeight() / 2 + 230);
}
public void checkForGhost() {
if (isTouching(Ghost1.class)) {
loseCondition();
}
if (isTouching(Ghost2.class)) {
loseCondition();
}
}
}