-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMovableAnimatedActor.java
More file actions
197 lines (177 loc) · 4.79 KB
/
MovableAnimatedActor.java
File metadata and controls
197 lines (177 loc) · 4.79 KB
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
import mayflower.*;
public class MovableAnimatedActor extends AnimatedActor
{
private Animation walkRight;
private Animation walkLeft;
private Animation idle;
private Animation idleLeft;
private Animation fallRight;
private Animation fallLeft;
private String currentAction;
private String direction;
private boolean isJumping;
private int verticalVelocity;
private final int jumpStrength;
private boolean climbing;
int count;
public MovableAnimatedActor()
{
super();
direction = "right";
isJumping = false;
verticalVelocity = 0;
climbing = false;
jumpStrength = 80;
}
public void act()
{
String newAction = null;
int x = getX();
int y = getY();
int w = getWidth();
int h = getHeight();
int speed = 5;
if (Mayflower.isKeyDown( Keyboard.KEY_RIGHT ))
{
newAction = "walkRight";
direction = "right";
setLocation(x+speed,y);
if (isBlocked())
setLocation(x-1,y);
else
newAction = "fallRight";
if (!isFalling() && count > 0)
{
setLocation(x,y-5);
count--;
}
}
else if (Mayflower.isKeyDown( Keyboard.KEY_LEFT ))
{
newAction = "walkLeft";
direction = "left";
setLocation(x-speed,y);
if (isBlocked())
setLocation(x+1,y);
else
newAction = "fallLeft";
if (!isFalling() && count > 0)
{
setLocation(x,y-5);
count--;
}
}
if (Mayflower.isKeyDown( Keyboard.KEY_UP ))
{
if (!isFalling())
count = 10;
if (isTouching(Ladder.class))
{
if (direction.equals("left"))
newAction = "walkLeft";
else if (direction.equals("right"))
newAction = "walkRight";
if (Mayflower.isKeyDown(Keyboard.KEY_UP)) {
setLocation(x, y - 2*speed); // Move up the ladder
}
}
}
else
{
if (direction.equals("left"))
newAction = "idleLeft";
else if (direction.equals("right"))
newAction = "idle";
}
if (count > 0)
{
setLocation(x,y-20);
count--;
}
if (x>=(1600-w))
{
setLocation(800-w-1,y);
}
if (x<=0)
{
setLocation(1,y);
}
if (y<=0)
{
setLocation(x,1);
}
if (y>=(900-h))
{
setLocation(x,600-h-1);
}
super.act();
if (newAction!=null && !newAction.equals(currentAction))
{
if (newAction.equals("idleLeft"))
setAnimation(idleLeft);
else if (newAction.equals("idle"))
setAnimation(idle);
else if (newAction.equals("walkRight"))
setAnimation(walkRight);
else if (newAction.equals("walkLeft"))
setAnimation(walkLeft);
else if (newAction.equals("fallRight"))
setAnimation(fallRight);
else if (newAction.equals("fallLeft"))
setAnimation(fallLeft);
}
}
public void jump()
{
int velocity = 7;
for (int i=0;i<30;i++)
{
setLocation(getX(),getY()-velocity);
}
}
public boolean isClimbing()
{
// Check if the cat is touching a ladder and the UP or DOWN keys are pressed
if (isTouching(Ladder.class) && (Mayflower.isKeyDown(Keyboard.KEY_UP) || Mayflower.isKeyDown(Keyboard.KEY_DOWN))) {
climbing = true; // Climbing
return true;
}
else {
climbing = false; // Not climbing
return false;
}
}
public boolean isBlocked(){
if(isTouching(Block.class))
return true;
return false;
}
public void setWalkRightAnimation(Animation ani)
{
walkRight = ani;
}
public void setWalkLeftAnimation(Animation ani)
{
walkLeft = ani;
}
public void setIdleAnimation(Animation ani)
{
idle = ani;
}
public void setIdleLeftAnimation(Animation ani)
{
idleLeft = ani;
}
public void setFallRightAnimation(Animation ani)
{
fallRight = ani;
}
public void setFallLeftAnimation(Animation ani)
{
fallLeft = ani;
}
public void setAnimation(Animation a)
{
super.setAnimation(a);
}
}