-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPlayer1.java
291 lines (276 loc) · 7.43 KB
/
Player1.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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
/**
* Write a description of class Enemy1 here.
*
* @author (your name)
* @version (a version number or a date)
*/
public class Player1 extends Actor
{
private GreenfootImage[][] walk;
private GreenfootImage[][] stand;
private GreenfootImage[][] still1;
private int counter, dalay, standCounter, side;
private int checker;
private int Mana, Counter, ManaChecker;
private final static int SPEED = 5;
private int speed;
private int vSpeed;
private boolean check;
private int checkSpace;
/**
* This is for initialize and input all the value for the variable
*/
public Player1(){
walk = new GreenfootImage[2][8];
stand = new GreenfootImage[2][12];
still1 = new GreenfootImage[2][6];
counter = 0;
dalay = 0;
standCounter = 0;
side = 0;
checker = 0;
Mana = 10;
Counter = 0;
ManaChecker = 0;
speed = 7;
vSpeed = 5;
check = false;
checkSpace = 0;
for(int j = 1; j <= 8; j++){
walk[0][j-1] = new GreenfootImage("walk"+j+".png");
GreenfootImage flap = new GreenfootImage("walk"+j+".png");
flap.mirrorHorizontally();
walk[1][j-1] = flap;
}
for(int j = 1; j <= 12; j++){
stand[0][j-1] = new GreenfootImage("stand" + j + ".png");
GreenfootImage flap = new GreenfootImage("stand"+j+".png");
flap.mirrorHorizontally();
stand[1][j-1] = flap;
}
for(int j = 1; j <= 6; j++){
still1[0][j-1] = new GreenfootImage("skill1_"+j+".png");
GreenfootImage flap = new GreenfootImage("skill1_"+j+".png");
flap.mirrorHorizontally();
still1[1][j-1] = flap;
}
setImage(stand[side][standCounter]);
}
public void act()
{
// Add your action code here.
checkOutScreen();
Anmation();
CheckHit();
trackMana();
falling();
}
/**
* Method checkOutScreen
* Check and to make sure that player can not go though the Edge
*/
public void checkOutScreen(){
if(getX() > 1050){
move(-5);
}else if(getX() < 0){
move(5);
}
}
/**
* Method Anmation
* Animation for walk, skill, jump and stand
*/
public void Anmation(){
if(Greenfoot.isKeyDown("a")&&checker == 0){
Walk("left");
}else if(Greenfoot.isKeyDown("d") && checker == 0){
Walk("right");
}else if(Greenfoot.isKeyDown("q")||checker == 1){
Skill1();
checker = 1;
Skill1Animation(side);
}else{
dalay++;
counter = 0;
if(dalay % SPEED == 0){
setImage(stand[side][standCounter]);
if(standCounter == 11){
standCounter = 0;
dalay = 0;
}
else
standCounter++;
}
}
if(Greenfoot.isKeyDown("space") && checkSpace <= 2){
moveUp();
}else{
check = false;
}
}
/**
* Method falling
* Falling's main method
*/
private void falling(){
if(isTouching(Floor.class)){
checkSpace = 0;
vSpeed = 0;
return;
}
checkGround();
}
/**
* Method moveUp
* Jumping Method
*/
private void moveUp()
{
if(check == false && checkSpace < 2){
vSpeed = -20;
fall();
checkSpace++;
check = true;
}
}
/**
* Method fall
* falling/gravity Method
*/
public void fall(){
setLocation(getX(),getY()+vSpeed);
vSpeed += 2;
}
/**
* Method checkGround
* Check if the player is on the Ground object or not
*/
public void checkGround(){
Actor Gunder = getOneObjectAtOffset(0, getImage().getHeight()/2, Ground.class);
if(Gunder == null){
fall();
}else{
Ground s = (Ground)getOneIntersectingObject(Ground.class);
vSpeed = 5;
if(s != null){
if(s.getSide() == 1){
move(5);
}else{
move(-5);
}
}
}
}
/**
* Method CheckHit
* Check if the player got hit or not
* And also calculate how much damage it need to take
*/
public void CheckHit(){
Actor Hit = getOneObjectAtOffset(0, 30, Player2_Skill1 .class);
if(Hit != null){
Player2 d = (Player2)getOneIntersectingObject(Player2.class);
if(d != null){
((MyWorld)getWorld()).sub_Health("P1",(Greenfoot.getRandomNumber(d.damage()+1)+1)*3);
}
}
}
/**
* Method Skill1
* Skill animation main method
*/
public void Skill1(){
if(checker == 0){
if(Mana > 0){
Mana = Mana / 2;
getWorld().addObject(new P1Mana(Mana), 10, 0);
getWorld().addObject(new Player1_Skill1 (side), getX(), getY());
}else{
ManaChecker = 1;
}
}
}
/**
* Method trackMana
* Add Mana if it is less then 10
* the speed for this is every 100 iteration
*/
public void trackMana(){
if (Mana < 10){
Counter++;
if (Counter % 100 == 0){
Mana++;
if(Mana > 0){
ManaChecker = 0;
}
getWorld().addObject(new P1Mana(Mana), 10, 0);
}
if (Mana >= 10){
Mana = 10;
Counter = 0;
}
}
}
/**
* Method Skill1Animation
*
* skill main Animation method
* and the parameter for is just for keep track on which side the player are facing to
*/
public void Skill1Animation(int faceSide){
if(ManaChecker == 0){
dalay++;
if(dalay % SPEED == 0){
if(counter >= 5){
counter = 0;
checker = 0;
}
setImage(still1[faceSide][counter]);
counter++;
}
}else{
checker = 0;
}
}
/**
* Method Walk
*
* Walk Animation main method
* and the parameter for is just for keep track on which side the player are facing to
*/
public void Walk(String faceSide){
if(faceSide == "left"){
side = 1;
dalay++;
if(dalay % SPEED == 0){
setImage(walk[1][counter]);
move(-10);
if(counter >= 7)
counter = 0;
else
counter++;
}
}else if(faceSide == "right"){
side = 0;
dalay++;
if(dalay % SPEED == 0){
setImage(walk[0][counter]);
move(10);
if(counter >= 7)
counter = 0;
else
counter++;
}
}
}
/**
* Method damage
*
* return the Mana
* This is use for let player2 calculate the damage
*/
public int damage(){
return Mana;
}
}