-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathPato.java
303 lines (232 loc) · 5.5 KB
/
Pato.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
292
293
294
295
296
297
298
299
300
301
302
303
/*
Copyright 2003
Nuno Morgadinho (l13591 at alunos dot uevora dot pt)
Claudio Fernandes (l14103 at alunos dot uevora dot pt)
Tiago Fernandes (l13614 at alunos dot uevora dot pt)
Tiago Bilou (l15243 at alunos dot uevora dot pt)
This file is part of Chuckie Egg J2ME.
Chuckie Egg J2ME is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
Chuckie Egg J2ME is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with Chuckie Egg J2ME; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
import javax.microedition.lcdui.Image;
import javax.microedition.lcdui.game.LayerManager;
public class Pato extends GameSprite{
private int newaction;
private int delay;
private long ct,lt=0;
private int direccao, direccaoOld;
private boolean stop = false;
private Grafo grafo;
private int acumx=0,acumy=0;
private boolean oneWalk = true;
// para controlar a animacao do pato a comer o milho
private int[] framesComeMilho={4,5};
private int frameComeMilho = 0;
private boolean animComeMilho = false;
private int frameActual = -1;
private int delayBack;
public Pato(Image img, int d, int xM, int yM,int del){
super(img,11,19,xM,yM);
delay=del;
direccao = d;
direccaoOld = d;
setFrame(0);
defineReferencePixel(5,0);
if(d==Simbolo.RIGHT)
vira();
switch(d){
case Simbolo.LEFT:
grafo = new Grafo(Grafo.LEFT);
break;
case Simbolo.RIGHT:
grafo = new Grafo(Grafo.RIGHT);
break;
}
}
/** selecciona a frame para a direccao e
actualiza a posicao da sprite */
public void move(){
switch(direccao){
case Simbolo.RIGHT:
if(stop){
setFrame(0);
vira();
}else{
setFrame(1);
vira();
}break;
case Simbolo.LEFT:
noTransform();
if(stop)
setFrame(0);
else
setFrame(1);
break;
case Simbolo.UP:
case Simbolo.DOWN:
setFrame(2);
if(stop){
vira();
}else{
noTransform();
}
break;
}
stop = !stop;
detectLock(dx,dy);
setPosition(getX()+dx,getY()+dy);
// reset deslocamento
dx=dy=0;
}
/** decrementa o dx */
public void esquerda(){
direccaoOld = direccao;
direccao = Simbolo.LEFT;
if(oneWalk || checkOneWalk()){
dx = -1;
oneWalk=false;
}else
dx = -2;
move();
}
/** aumenta o dx */
public void direita(){
direccaoOld = direccao;
direccao = Simbolo.RIGHT;
if(oneWalk || checkOneWalk()){
dx = 1;
oneWalk=false;
}else
dx = 2;
move();
}
/** decrementa o dy */
public void cima(){
direccaoOld = direccao;
direccao = Simbolo.UP;
dy = -2;
move();
}
/** incrementa o dy */
public void baixo(){
direccaoOld = direccao;
direccao = Simbolo.DOWN;
dy = 2;
move();
}
/** coloca a sprite no (x,y) referente ao ecran,
dados o x e y em relacao a matriz do nivel*/
public void setPosMatriz(int x, int y){
setPosition(x * Simbolo.frameWidth, ((Simbolo.nivelHeight-y) * Simbolo.frameHeight) + Simbolo.paintStartY - 19);
}
// move o pato de acordo com o grafo
public void nextMove(){
int tmp = yMatriz-1;
int[] x = {xMatriz,xMatriz-1,xMatriz+1,xMatriz,xMatriz};
int[] y = {tmp,tmp,tmp,yMatriz,yMatriz+1,yMatriz};
int[] cond = {-1,-1,-1,-1,-1};
ct=System.currentTimeMillis();
if(ct-lt>delay){
if(animComeMilho){
comeMilho(-1,null);
}else{
if(acumx == 0 && acumy == 0){
for(tmp=0;tmp<5;tmp++){
switch(nivel.getXYnew(x[tmp],y[tmp])){
case Simbolo.VAZIO:
cond[tmp]=0;
break;
case Simbolo.CHAO:
cond[tmp]=1;
break;
case Simbolo.ESCADA:
cond[tmp]=2;
break;
case Simbolo.CHAO_ESCADA:
cond[tmp]=3;
break;
}
}
newaction = grafo.nextAction(cond);
}
switch(newaction){
case Grafo.LEFT:
esquerda();
break;
case Grafo.RIGHT:
direita();
break;
case Grafo.DOWN:
baixo();
break;
case Grafo.UP:
cima();
break;
}
lt=ct;
}
}
}
public boolean checkOneWalk(){
if(direccao!=direccaoOld || acumx==0)
return true;
else
return false;
}
public void detectLock(int x, int y){
if(x!=0){
acumx+=x;
if(acumx==11 || acumx==-11){
if(acumx>0)
xMatriz+=1;
else
xMatriz-=1;
acumx=0;
oneWalk = true;
}
}
if(y!=0){
acumy+=y;
if(acumy==6 || acumy==-6){
if(acumy<0)
yMatriz+=1;
else
yMatriz-=1;
acumy=0;
}
}
}
public void comeMilho(int j, LayerManager lm){
if(frameComeMilho>2){
delay = delayBack;
frameComeMilho = 0;
animComeMilho=false;
}else
if(frameActual==-1){
animComeMilho=true;
frameActual = getFrame();
delayBack = delay;
delay *= 2 ;
}else
if(frameComeMilho>1){
setFrame(frameActual);
frameActual = -1;
frameComeMilho++;
lm.remove(Nivel.milhos[j]);
Nivel.milhos[j] = null;
}else{
setFrame(framesComeMilho[frameComeMilho++]);
if(direccao==Simbolo.RIGHT)
vira();
}
}
}