DEADSOFTWARE

95815db35f60c894ba81fd5da88288eb5f65968a
[cavecraft.git] / src / mobs.pas
1 unit mob;
3 interface
4 const
5 M_NONE=0;
6 M_ZOMBY=1;
8 MOB_TYPES=1;
10 procedure create(_type, x, y:integer);//Создать моба
11 function findAndHit(value, x, y, w, h, addvx, addvy:integer):integer;
13 procedure update;//Обновление логики
14 procedure updatePhy;//Обновление физики
15 procedure draw(camx, camy:integer);//Отрисовка
17 procedure saveData;//Сохранение всех данных
18 procedure loadData;//Загрузка всех данных
19 procedure resetData;//Сброс всех данных
21 procedure loadSkin(path:string);//Загрузка текстур
22 procedure freeSkin;//Выгрузка текстур
24 implementation
25 uses phy, player, canvas, func;
26 const
27 MAX_MOBS=31;
28 MAX_ANIMREG=3;
30 AI_NONE=0;
31 AI_ZLOY=1;
32 var
33 mob_type, mob_x, mob_y, mob_vx, mob_vy, mob_posi, mob_hp:array [0..MAX_MOBS] of integer;
34 mob_jmp:array [0..MAX_MOBS] of boolean;
35 mob_anim:array [0..MAX_MOBS, 0..MAX_ANIMREG] of integer;
36 tab_w, tab_h, tab_hp, tab_speedx, tab_jumph, tab_ai:array [0..MOB_TYPES] of integer;
38 texture_left, texture_right:array [0..MOB_TYPES, 0..0] of image;
39 tex_x, tex_y, tex_w, tex_h:array [0..1, 0..MOB_TYPES, 0..0] of integer;
41 part_max: array [0..MOB_TYPES] of integer;
42 part_tex, part_x, part_y, part_anim: array [0..MOB_TYPES, 0..0] of integer;
44 function getType(i:integer):integer;
45 begin
46 getType:=mob_type[i];
47 end;
49 function getX(i:integer):integer;
50 begin
51 getX:=mob_x[i];
52 end;
54 function getY(i:integer):integer;
55 begin
56 getY:=mob_y[i];
57 end;
59 function getVX(i:integer):integer;
60 begin
61 getVX:=mob_vx[i];
62 end;
64 function getVY(i:integer):integer;
65 begin
66 getVY:=mob_vy[i];
67 end;
69 function getPosi(i:integer):integer;
70 begin
71 getPosi:=mob_posi[i];
72 end;
74 function getHp(i:integer):integer;
75 begin
76 getHp:=mob_hp[i];
77 end;
79 function getW(i:integer):integer;
80 begin
81 getW:=tab_w[mob_type[i]];
82 end;
84 function getH(i:integer):integer;
85 begin
86 getH:=tab_h[mob_type[i]];
87 end;
89 function getSpeedX(i:integer):integer;
90 begin
91 getSpeedX:=tab_speedx[mob_type[i]];
92 end;
94 function getJumpH(i:integer):integer;
95 begin
96 getJumpH:=tab_jumph[mob_type[i]];
97 end;
99 function getJmp(i:integer):boolean;
100 begin
101 getJmp:=mob_jmp[i];
102 end;
104 procedure setType(value, i:integer);
105 begin
106 mob_type[i]:=value;
107 end;
109 procedure setX(value, i:integer);
110 begin
111 mob_x[i]:=value;
112 end;
114 procedure setY(value, i:integer);
115 begin
116 mob_y[i]:=value;
117 end;
119 procedure setVX(value, i:integer);
120 begin
121 mob_vx[i]:=value;
122 end;
124 procedure setVY(value, i:integer);
125 begin
126 mob_vy[i]:=value;
127 end;
129 procedure setPosi(value, i:integer);
130 begin
131 mob_posi[i]:=value;
132 end;
134 procedure setHp(value, i:integer);
135 begin
136 mob_hp[i]:=value;
137 end;
139 procedure setJmp(value:boolean; i:integer);
140 begin
141 mob_jmp[i]:=value;
142 end;
144 procedure initTab(_type, w, h, hp, speedx, jumph, ai:integer);
145 begin
146 tab_w[_type]:=w;
147 tab_h[_type]:=h;
148 tab_hp[_type]:=hp;
149 tab_speedx[_type]:=speedx;
150 tab_jumph[_type]:=jumph;
151 tab_ai[_type]:=ai;
152 end;
154 procedure initTexture(img:image; mobtype, texid, posi, centerx, centery:integer);
155 begin
156 tex_w[posi, mobtype, texid]:=getImageWidth(img);
157 tex_h[posi, mobtype, texid]:=getImageHeight(img);
158 if posi=0 then
159 texture_left[mobtype, texid]:=img;
160 else
161 texture_right[mobtype, texid]:=img;
162 tex_x[posi, mobtype, texid]:=centerx;
163 tex_y[posi, mobtype, texid]:=centery;
164 end;
166 procedure setMaxTextures(mobtype, max:integer);
167 var
168 i:integer;
169 begin
170 max:=max+1;
171 debug('setMaxTextures(mobtype, max:integer);');
172 bytecode
173 getstatic field 'mob', 'texture_left', '[[Ljavax/microedition/lcdui/Image;';
174 iload 0;
175 iload 1;
176 multianewarray class '[Ljavax/microedition/lcdui/Image;', 1;
177 aastore;
179 getstatic field 'mob', 'texture_right', '[[Ljavax/microedition/lcdui/Image;';
180 iload 0;
181 iload 1;
182 multianewarray class '[Ljavax/microedition/lcdui/Image;', 1;
183 aastore;
184 end;
186 for i:=0 to 1 do
187 bytecode
188 //tex_x[i][mobtype]=new int[max];
189 getstatic field 'mob', 'tex_x', '[[[I';
190 iload_2;
191 aaload;
192 iload_0;
193 iload_1;
194 multianewarray class '[I', 1;
195 aastore;
197 getstatic field 'mob', 'tex_y', '[[[I';
198 iload_2;
199 aaload;
200 iload_0;
201 iload_1;
202 multianewarray class '[I', 1;
203 aastore;
205 getstatic field 'mob', 'tex_w', '[[[I';
206 iload_2;
207 aaload;
208 iload_0;
209 iload_1;
210 multianewarray class '[I', 1;
211 aastore;
213 getstatic field 'mob', 'tex_h', '[[[I';
214 iload_2;
215 aaload;
216 iload_0;
217 iload_1;
218 multianewarray class '[I', 1;
219 aastore;
220 end;
221 end;
223 procedure setMaxParts(mobtype, max:integer);
224 begin
225 part_max[mobtype]:=max;
226 max:=max+1;
228 bytecode
229 getstatic field 'mob', 'part_tex', '[[I';
230 iload_0;
231 iload_1;
232 multianewarray class '[I', 1;
233 aastore;
235 getstatic field 'mob', 'part_x', '[[I';
236 iload_0;
237 iload_1;
238 multianewarray class '[I', 1;
239 aastore;
241 getstatic field 'mob', 'part_y', '[[I';
242 iload_0;
243 iload_1;
244 multianewarray class '[I', 1;
245 aastore;
247 getstatic field 'mob', 'part_anim', '[[I';
248 iload_0;
249 iload_1;
250 multianewarray class '[I', 1;
251 aastore;
252 end;
253 end;
255 procedure initPart(mobtype, partid, tex, x, y, animreg:integer);
256 begin
257 part_tex[mobtype, partid]:=tex;
258 part_x[mobtype, partid]:=x;
259 part_y[mobtype, partid]:=y;
260 part_anim[mobtype, partid]:=animreg;
261 end;
263 procedure loadPhy(i:integer);
264 begin
265 phy.loadObject(mob_x[i], mob_y[i], tab_w[mob_type[i]], tab_h[mob_type[i]], mob_vx[i], mob_vy[i], mob_jmp[i]);
266 end;
268 procedure storePhy(i:integer);
269 begin
270 mob_x[i]:=phy.getX;
271 mob_y[i]:=phy.getY;
272 mob_vx[i]:=phy.getVelX;
273 mob_vy[i]:=phy.getVelY;
274 mob_jmp[i]:=phy.getJmp;
275 end;
277 {===== =====}
279 procedure create(_type, x, y:integer);//Создать моба
280 var
281 i, j:integer;
282 begin
283 debug('Create mob '+_type+' @ '+x+'x'+y);
284 for i:=0 to MAX_MOBS do
285 if mob_type[i]=M_NONE then
286 begin
287 mob_type[i]:=_type;
288 mob_x[i]:=x;
289 mob_y[i]:=y;
290 mob_vx[i]:=0;
291 mob_vy[i]:=0;
292 mob_posi[i]:=0;
293 mob_hp[i]:=tab_hp[_type];
294 mob_jmp[i]:=false;
295 for j:=0 to MAX_ANIMREG do
296 mob_anim[i, j]:=0;
298 debug('Created mob is '+i);
299 exit;
300 end;
301 end;
303 procedure die(i:integer);//Убить
304 begin
305 mob_type[i]:=M_NONE;
306 end;
308 procedure hit(value, i:integer);//Нанести урон
309 var
310 hp:integer;
311 begin
312 hp:=getHp(i)-value;
313 if hp>0 then
314 setHp(hp, i);
315 else
316 die(i);
317 end;
319 function findAndHit(value, x, y, w, h, addvx, addvy:integer):integer;
320 var
321 i:integer;
322 begin
323 for i:=0 to MAX_MOBS do
324 if getType(i)<>M_NONE then
325 if CollTwoObj(x, y, w, h, getX(i), getY(i), getW(i), getH(i)) then
326 begin
327 setVX(getVX(i)+addvx, i);
328 setVX(getVY(i)+addvy, i);
329 hit(value, i);
330 findAndHit:=i;
331 exit;
332 end;
334 findAndHit:=-1;
335 end;
337 procedure goUp(i:integer);
338 begin
339 loadPhy(i);
340 phy.jumpObj(getJumpH(i));
341 storePhy(i);
342 end;
344 procedure goDown(i:integer);
345 begin
347 end;
349 procedure goLeft(i:integer);
350 begin
351 setVX(-getSpeedX(i), i);
352 setPosi(0, i);
353 end;
355 procedure goRight(i:integer);
356 begin
357 setVX(getSpeedX(i), i);
358 setPosi(1, i);
359 end;
361 function testSolid(i, vector:integer):boolean;
362 var
363 x, y:integer;
364 begin
365 y:=((getY(i)+getH(i)))/16-1;
367 if vector<0 then
368 begin
369 x:=(getX(i)/16)-1;
370 end;
371 else
372 if vector>0 then
373 begin
374 x:=((getX(i)+getW(i))/16){+1};
375 end;
376 else
377 x:=((getX(i)+getW(i)/2)/16);
379 testSolid:=phy.isSolid(x, y, getVX(i), getVY(i));
380 end;
382 procedure updateZloyAiAnim(i:integer);
383 const
384 DEL=1;
385 var
386 a_a, a_d, a_f:integer;
387 begin
388 a_a:=mob_anim[i, 1];
389 a_d:=mob_anim[i, 2];
390 a_f:=mob_anim[i, 3];
392 a_d:=a_d+1;
393 if a_d>DEL then
394 begin
395 a_d:=0;
396 a_a:=a_a+1;
397 if a_f=1 then
398 begin
399 a_a:=0;
400 a_f:=0;
401 end;
402 else
403 if a_a=3 then
404 begin
405 a_a:=1;
406 a_f:=1;
407 end;
408 end;
410 mob_anim[i, 1]:=a_a;
411 mob_anim[i, 2]:=a_d;
412 mob_anim[i, 3]:=a_f;
413 end;
415 procedure updateZloyAi(i:integer);
416 begin
417 if player.getX<getX(i) then
418 begin
419 goLeft(i);
420 updateZloyAiAnim(i);
421 if testSolid(i, -1) then
422 goUp(i);
423 end;
424 else
425 if player.getX>getX(i) then
426 begin
427 goRight(i);
428 updateZloyAiAnim(i);
429 if testSolid(i, 1) then
430 goUp(i);
431 end;
432 end;
434 procedure update;//Обновление логики
435 var
436 i, _type, ai:integer;
437 begin
438 for i:=0 to MAX_MOBS do
439 begin
440 _type:=getType(i);
442 if _type<>M_NONE then
443 begin
444 ai:=tab_ai[_type];
445 if ai=AI_ZLOY then
446 updateZloyAi(i);
447 end;
448 end;
449 end;
451 procedure updatePhy;//Обновление физики
452 var
453 i:integer;
454 begin
455 for i:=0 to MAX_MOBS do
456 begin
457 loadPhy(i);
458 phy.calc(true);
459 storePhy(i);
460 end;
461 end;
463 procedure drawPart(mobtype, texid, posi, anim, x, y, camx, camy:integer);
464 begin
465 x:=x-tex_x[posi, mobtype, texid]-camx;
466 y:=y-tex_y[posi, mobtype, texid]-camy;
468 if CollTwoObj(x, y, tex_w[posi, mobtype, texid], tex_h[posi, mobtype, texid], 0, 0, getWidth, getHeight) then
469 if posi=0 then
470 drawImage(texture_left[mobtype, texid+anim], x, y);
471 else
472 drawImage(texture_right[mobtype, texid+anim], x, y);
473 end;
475 procedure draw(camx, camy:integer);//Отрисовка
476 var
477 i, j, _type:integer;
478 begin
479 for i:=0 to MAX_MOBS do
480 begin
481 _type:=mob_type[i];
482 if _type<>M_NONE then
483 begin
484 for j:=0 to part_max[_type] do
485 drawPart(_type, part_tex[_type, j], mob_posi[i], mob_anim[i, part_anim[_type, j]], mob_x[i]+part_x[_type, j], mob_y[i]+part_y[_type, j], camx, camy);
486 end;
487 end;
488 end;
490 procedure loadSkin(path:string);//Загрузка текстур
491 var
492 im:image;
493 begin
494 setMaxTextures(M_ZOMBY, 5);
495 im:=ld_tex('zombie_ani.png', path, 'mobs/');
496 // IMG, TYPE, ID, POSI, X, Y
497 initTexture(rotate_image_from_image(im, 6, 2, 8, 8, 0), M_ZOMBY, 0, 0, 4, 8);//head[left]
498 initTexture(rotate_image_from_image(im, 8, 10, 4, 12, 0), M_ZOMBY, 1, 0, 2, 0);//body[left]
499 initTexture(rotate_image_from_image(im, 0, 10, 12, 4, 0), M_ZOMBY, 2, 0, 10, 2);//hands[left]
500 initTexture(rotate_image_from_image(im, 0, 52, 12, 12, 0), M_ZOMBY, 3, 0, 6, 0);//legs[left]
501 initTexture(rotate_image_from_image(im, 13, 52, 12, 12, 0), M_ZOMBY, 4, 0, 6, 0);
502 initTexture(rotate_image_from_image(im, 26, 52, 12, 12, 0), M_ZOMBY, 5, 0, 6, 0);
504 initTexture(rotate_image_from_image(im, 27, 2, 8, 8, 0), M_ZOMBY, 0, 1, 4, 8);//head[right]
505 initTexture(rotate_image_from_image(im, 29, 10, 4, 12, 0), M_ZOMBY, 1, 1, 2, 0);//body[right]
506 initTexture(rotate_image_from_image(im, 29, 10, 12, 4, 0), M_ZOMBY, 2, 1, 2, 2);//hands[right]
507 initTexture(rotate_image_from_image(im, 39, 52, 12, 12, 0), M_ZOMBY, 3, 1, 6, 0);//legs[right]
508 initTexture(rotate_image_from_image(im, 52, 52, 12, 12, 0), M_ZOMBY, 4, 1, 6, 0);
509 initTexture(rotate_image_from_image(im, 65, 52, 12, 12, 0), M_ZOMBY, 5, 1, 6, 0);
510 end;
512 procedure freeSkin;//Выгрузка текстур
513 var
514 i, j, len:integer;
515 nullimg:image;
516 begin
517 for i:=0 to MOB_TYPES do
518 begin
519 bytecode
520 getstatic field 'mob', 'texture_left', '[[Ljavax/microedition/lcdui/Image;';
521 iload_0;//i
522 aaload;
523 arraylength;
524 iconst_1;
525 isub;
526 istore_2;//len
527 end;
528 for j:=0 to len do
529 begin
530 texture_left[i, j]:=nullimg;
531 texture_right[i, j]:=nullimg;
532 end;
533 end;
534 end;
536 procedure saveData;//Сохранение всех данных
537 begin
539 end;
541 procedure loadData;//Загрузка всех данных
542 begin
544 end;
546 procedure resetData;//Сброс всех данных
547 var
548 i, j:integer;
549 begin
550 for i:=0 to MAX_MOBS do
551 begin
552 mob_type[i]:=M_NONE;
553 mob_x[i]:=0;
554 mob_y[i]:=0;
555 mob_vx[i]:=0;
556 mob_vy[i]:=0;
557 mob_posi[i]:=0;
558 mob_hp[i]:=0;
559 mob_jmp[i]:=false;
560 for j:=0 to MAX_ANIMREG do
561 mob_anim[i, j]:=0;
562 end;
563 end;
565 initialization
566 // TYPE W H HP SX JH, AI_TYPE
567 initTab(M_NONE, 0, 0, 0, 0, 0, AI_NONE);
568 setMaxParts(M_NONE, -1);
570 initTab(M_ZOMBY, 8, 32, 10, 1, 7, AI_ZLOY);
571 setMaxParts(M_ZOMBY, 3);
572 // TYPE PART, TEX, X, Y ANIM
573 initPart(M_ZOMBY, 0, 0, 4, 8, 0);//Head
574 initPart(M_ZOMBY, 1, 1, 4, 8, 0);//Body
575 initPart(M_ZOMBY, 2, 2, 4, 10, 0);//Hands
576 initPart(M_ZOMBY, 3, 3, 4, 20, 1);//Legs
577 end.