-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathKongActor.java
165 lines (146 loc) · 4.24 KB
/
KongActor.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
import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
/**
* Write a description of class KongActor here.
*
* @author (your name)
* @version (a version number or a date)
*/
public class KongActor extends Actor
{
static final int width = 38;
static final int height = 32;
private int footx = 0;
private int footy = 0;
public static enum State {STANDING,ANGRY,BARREL,NAN}
private GreenfootImage kong[] = new GreenfootImage[5];
BackGround world = null;
private State playerState = State.NAN;
private int angrycounter = 0;
private int standingcounter = 0;
private int barrelcounter = 0;
private int standLimit = -1;
private int angryLimit = -1;
public KongActor()
{
//setFoot(205,76);
kong[0] = new GreenfootImage("Kong01.png");
kong[1] = new GreenfootImage("Kong02.png");
kong[2] = new GreenfootImage("Kong03.png");
kong[3] = new GreenfootImage("Kong04.png");
kong[4] = new GreenfootImage("Kong05.png");
angrycounter = 0;
standingcounter = 0;
barrelcounter = 0;
}
/**
* Act - do whatever the KongActor wants to do. This method is called whenever
* the 'Act' or 'Run' button gets pressed in the environment.
*/
public void act()
{
// Add your action code here.
if(State.NAN == playerState)
{
setImage(kong[0]);
playerState = State.BARREL;
standingcounter = 0;
barrelcounter = 0;
}
if(!world.player.isAlive())
{
playerState = State.NAN;
return;
}
if(!PlayerActor.hasMoved)
{
playerState=State.NAN;
return;
}
if(State.BARREL == playerState)
{
switch(barrelcounter)
{
case 0:
case 1:
case 2:
setImage(kong[2]);
break;
case 3:
case 4:
case 5:
case 6:
setImage(kong[4]);
break;
default:
setImage(kong[1]);
break;
}
barrelcounter++;
if(barrelcounter > 10)
{
barrelcounter = 0;
playerState = State.STANDING;
BarrelActor b = new BarrelActor();
world.addObject(b,0,0);
b.setFoot(190,76);
}
return;
}
if(State.ANGRY == playerState)
{
angrycounter++;
if(angrycounter > angryLimit)
{
playerState = State.BARREL;
angrycounter = 0;
barrelcounter = 0;
}
switch(angrycounter % 4)
{
case 3:
case 2:
setImage(kong[3]);
break;
default:
setImage(kong[0]);
break;
}
return;
}
if(State.STANDING == playerState)
{
standingcounter++;
if(standingcounter > standLimit)
{
int rand = Greenfoot.getRandomNumber(10);
if(rand > 7)
{
playerState = State.ANGRY;
}
else
{
playerState = State.BARREL;
}
standingcounter = 0;
angrycounter = 0;
barrelcounter = 0;
}
standLimit = 15+ Greenfoot.getRandomNumber(30);
angryLimit = 11 + Greenfoot.getRandomNumber(25);
setImage(kong[0]);
return;
}
}
@Override
public void addedToWorld(World world) {
this.world = (BackGround) world;
}
public void setFoot(int x,int y)
{
footx = x;
//setX(x - (int)(width/2));
footy = y;
//setY(y - (int)(height/2));
setLocation(x,y-(int)(height/2));
}
}