unit mob; interface const M_NONE=0; M_ZOMBY=1; MOB_TYPES=1; procedure create(_type, x, y:integer);//Создать моба function findAndHit(value, x, y, w, h, addvx, addvy:integer):integer; procedure update;//Обновление логики procedure updatePhy;//Обновление физики procedure draw(camx, camy:integer);//Отрисовка procedure saveData;//Сохранение всех данных procedure loadData;//Загрузка всех данных procedure resetData;//Сброс всех данных procedure loadSkin(path:string);//Загрузка текстур procedure freeSkin;//Выгрузка текстур implementation uses phy, player, canvas, func; const MAX_MOBS=31; MAX_ANIMREG=3; AI_NONE=0; AI_ZLOY=1; var mob_type, mob_x, mob_y, mob_vx, mob_vy, mob_posi, mob_hp:array [0..MAX_MOBS] of integer; mob_jmp:array [0..MAX_MOBS] of boolean; mob_anim:array [0..MAX_MOBS, 0..MAX_ANIMREG] of integer; tab_w, tab_h, tab_hp, tab_speedx, tab_jumph, tab_ai:array [0..MOB_TYPES] of integer; texture_left, texture_right:array [0..MOB_TYPES, 0..0] of image; tex_x, tex_y, tex_w, tex_h:array [0..1, 0..MOB_TYPES, 0..0] of integer; part_max: array [0..MOB_TYPES] of integer; part_tex, part_x, part_y, part_anim: array [0..MOB_TYPES, 0..0] of integer; function getType(i:integer):integer; begin getType:=mob_type[i]; end; function getX(i:integer):integer; begin getX:=mob_x[i]; end; function getY(i:integer):integer; begin getY:=mob_y[i]; end; function getVX(i:integer):integer; begin getVX:=mob_vx[i]; end; function getVY(i:integer):integer; begin getVY:=mob_vy[i]; end; function getPosi(i:integer):integer; begin getPosi:=mob_posi[i]; end; function getHp(i:integer):integer; begin getHp:=mob_hp[i]; end; function getW(i:integer):integer; begin getW:=tab_w[mob_type[i]]; end; function getH(i:integer):integer; begin getH:=tab_h[mob_type[i]]; end; function getSpeedX(i:integer):integer; begin getSpeedX:=tab_speedx[mob_type[i]]; end; function getJumpH(i:integer):integer; begin getJumpH:=tab_jumph[mob_type[i]]; end; function getJmp(i:integer):boolean; begin getJmp:=mob_jmp[i]; end; procedure setType(value, i:integer); begin mob_type[i]:=value; end; procedure setX(value, i:integer); begin mob_x[i]:=value; end; procedure setY(value, i:integer); begin mob_y[i]:=value; end; procedure setVX(value, i:integer); begin mob_vx[i]:=value; end; procedure setVY(value, i:integer); begin mob_vy[i]:=value; end; procedure setPosi(value, i:integer); begin mob_posi[i]:=value; end; procedure setHp(value, i:integer); begin mob_hp[i]:=value; end; procedure setJmp(value:boolean; i:integer); begin mob_jmp[i]:=value; end; procedure initTab(_type, w, h, hp, speedx, jumph, ai:integer); begin tab_w[_type]:=w; tab_h[_type]:=h; tab_hp[_type]:=hp; tab_speedx[_type]:=speedx; tab_jumph[_type]:=jumph; tab_ai[_type]:=ai; end; procedure initTexture(img:image; mobtype, texid, posi, centerx, centery:integer); begin tex_w[posi, mobtype, texid]:=getImageWidth(img); tex_h[posi, mobtype, texid]:=getImageHeight(img); if posi=0 then texture_left[mobtype, texid]:=img; else texture_right[mobtype, texid]:=img; tex_x[posi, mobtype, texid]:=centerx; tex_y[posi, mobtype, texid]:=centery; end; procedure setMaxTextures(mobtype, max:integer); var i:integer; begin max:=max+1; debug('setMaxTextures(mobtype, max:integer);'); bytecode getstatic field 'mob', 'texture_left', '[[Ljavax/microedition/lcdui/Image;'; iload 0; iload 1; multianewarray class '[Ljavax/microedition/lcdui/Image;', 1; aastore; getstatic field 'mob', 'texture_right', '[[Ljavax/microedition/lcdui/Image;'; iload 0; iload 1; multianewarray class '[Ljavax/microedition/lcdui/Image;', 1; aastore; end; for i:=0 to 1 do bytecode //tex_x[i][mobtype]=new int[max]; getstatic field 'mob', 'tex_x', '[[[I'; iload_2; aaload; iload_0; iload_1; multianewarray class '[I', 1; aastore; getstatic field 'mob', 'tex_y', '[[[I'; iload_2; aaload; iload_0; iload_1; multianewarray class '[I', 1; aastore; getstatic field 'mob', 'tex_w', '[[[I'; iload_2; aaload; iload_0; iload_1; multianewarray class '[I', 1; aastore; getstatic field 'mob', 'tex_h', '[[[I'; iload_2; aaload; iload_0; iload_1; multianewarray class '[I', 1; aastore; end; end; procedure setMaxParts(mobtype, max:integer); begin part_max[mobtype]:=max; max:=max+1; bytecode getstatic field 'mob', 'part_tex', '[[I'; iload_0; iload_1; multianewarray class '[I', 1; aastore; getstatic field 'mob', 'part_x', '[[I'; iload_0; iload_1; multianewarray class '[I', 1; aastore; getstatic field 'mob', 'part_y', '[[I'; iload_0; iload_1; multianewarray class '[I', 1; aastore; getstatic field 'mob', 'part_anim', '[[I'; iload_0; iload_1; multianewarray class '[I', 1; aastore; end; end; procedure initPart(mobtype, partid, tex, x, y, animreg:integer); begin part_tex[mobtype, partid]:=tex; part_x[mobtype, partid]:=x; part_y[mobtype, partid]:=y; part_anim[mobtype, partid]:=animreg; end; procedure loadPhy(i:integer); begin 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]); end; procedure storePhy(i:integer); begin mob_x[i]:=phy.getX; mob_y[i]:=phy.getY; mob_vx[i]:=phy.getVelX; mob_vy[i]:=phy.getVelY; mob_jmp[i]:=phy.getJmp; end; {===== =====} procedure create(_type, x, y:integer);//Создать моба var i, j:integer; begin debug('Create mob '+_type+' @ '+x+'x'+y); for i:=0 to MAX_MOBS do if mob_type[i]=M_NONE then begin mob_type[i]:=_type; mob_x[i]:=x; mob_y[i]:=y; mob_vx[i]:=0; mob_vy[i]:=0; mob_posi[i]:=0; mob_hp[i]:=tab_hp[_type]; mob_jmp[i]:=false; for j:=0 to MAX_ANIMREG do mob_anim[i, j]:=0; debug('Created mob is '+i); exit; end; end; procedure die(i:integer);//Убить begin mob_type[i]:=M_NONE; end; procedure hit(value, i:integer);//Нанести урон var hp:integer; begin hp:=getHp(i)-value; if hp>0 then setHp(hp, i); else die(i); end; function findAndHit(value, x, y, w, h, addvx, addvy:integer):integer; var i:integer; begin for i:=0 to MAX_MOBS do if getType(i)<>M_NONE then if CollTwoObj(x, y, w, h, getX(i), getY(i), getW(i), getH(i)) then begin setVX(getVX(i)+addvx, i); setVX(getVY(i)+addvy, i); hit(value, i); findAndHit:=i; exit; end; findAndHit:=-1; end; procedure goUp(i:integer); begin loadPhy(i); phy.jumpObj(getJumpH(i)); storePhy(i); end; procedure goDown(i:integer); begin end; procedure goLeft(i:integer); begin setVX(-getSpeedX(i), i); setPosi(0, i); end; procedure goRight(i:integer); begin setVX(getSpeedX(i), i); setPosi(1, i); end; function testSolid(i, vector:integer):boolean; var x, y:integer; begin y:=((getY(i)+getH(i)))/16-1; if vector<0 then begin x:=(getX(i)/16)-1; end; else if vector>0 then begin x:=((getX(i)+getW(i))/16){+1}; end; else x:=((getX(i)+getW(i)/2)/16); testSolid:=phy.isSolid(x, y, getVX(i), getVY(i)); end; procedure updateZloyAiAnim(i:integer); const DEL=1; var a_a, a_d, a_f:integer; begin a_a:=mob_anim[i, 1]; a_d:=mob_anim[i, 2]; a_f:=mob_anim[i, 3]; a_d:=a_d+1; if a_d>DEL then begin a_d:=0; a_a:=a_a+1; if a_f=1 then begin a_a:=0; a_f:=0; end; else if a_a=3 then begin a_a:=1; a_f:=1; end; end; mob_anim[i, 1]:=a_a; mob_anim[i, 2]:=a_d; mob_anim[i, 3]:=a_f; end; procedure updateZloyAi(i:integer); begin if player.getXgetX(i) then begin goRight(i); updateZloyAiAnim(i); if testSolid(i, 1) then goUp(i); end; end; procedure update;//Обновление логики var i, _type, ai:integer; begin for i:=0 to MAX_MOBS do begin _type:=getType(i); if _type<>M_NONE then begin ai:=tab_ai[_type]; if ai=AI_ZLOY then updateZloyAi(i); end; end; end; procedure updatePhy;//Обновление физики var i:integer; begin for i:=0 to MAX_MOBS do begin loadPhy(i); phy.calc(true); storePhy(i); end; end; procedure drawPart(mobtype, texid, posi, anim, x, y, camx, camy:integer); begin x:=x-tex_x[posi, mobtype, texid]-camx; y:=y-tex_y[posi, mobtype, texid]-camy; if CollTwoObj(x, y, tex_w[posi, mobtype, texid], tex_h[posi, mobtype, texid], 0, 0, getWidth, getHeight) then if posi=0 then drawImage(texture_left[mobtype, texid+anim], x, y); else drawImage(texture_right[mobtype, texid+anim], x, y); end; procedure draw(camx, camy:integer);//Отрисовка var i, j, _type:integer; begin for i:=0 to MAX_MOBS do begin _type:=mob_type[i]; if _type<>M_NONE then begin for j:=0 to part_max[_type] do 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); end; end; end; procedure loadSkin(path:string);//Загрузка текстур var im:image; begin setMaxTextures(M_ZOMBY, 5); im:=ld_tex('zombie_ani.png', path, 'mobs/'); // IMG, TYPE, ID, POSI, X, Y initTexture(rotate_image_from_image(im, 6, 2, 8, 8, 0), M_ZOMBY, 0, 0, 4, 8);//head[left] initTexture(rotate_image_from_image(im, 8, 10, 4, 12, 0), M_ZOMBY, 1, 0, 2, 0);//body[left] initTexture(rotate_image_from_image(im, 0, 10, 12, 4, 0), M_ZOMBY, 2, 0, 10, 2);//hands[left] initTexture(rotate_image_from_image(im, 0, 52, 12, 12, 0), M_ZOMBY, 3, 0, 6, 0);//legs[left] initTexture(rotate_image_from_image(im, 13, 52, 12, 12, 0), M_ZOMBY, 4, 0, 6, 0); initTexture(rotate_image_from_image(im, 26, 52, 12, 12, 0), M_ZOMBY, 5, 0, 6, 0); initTexture(rotate_image_from_image(im, 27, 2, 8, 8, 0), M_ZOMBY, 0, 1, 4, 8);//head[right] initTexture(rotate_image_from_image(im, 29, 10, 4, 12, 0), M_ZOMBY, 1, 1, 2, 0);//body[right] initTexture(rotate_image_from_image(im, 29, 10, 12, 4, 0), M_ZOMBY, 2, 1, 2, 2);//hands[right] initTexture(rotate_image_from_image(im, 39, 52, 12, 12, 0), M_ZOMBY, 3, 1, 6, 0);//legs[right] initTexture(rotate_image_from_image(im, 52, 52, 12, 12, 0), M_ZOMBY, 4, 1, 6, 0); initTexture(rotate_image_from_image(im, 65, 52, 12, 12, 0), M_ZOMBY, 5, 1, 6, 0); end; procedure freeSkin;//Выгрузка текстур var i, j, len:integer; nullimg:image; begin for i:=0 to MOB_TYPES do begin bytecode getstatic field 'mob', 'texture_left', '[[Ljavax/microedition/lcdui/Image;'; iload_0;//i aaload; arraylength; iconst_1; isub; istore_2;//len end; for j:=0 to len do begin texture_left[i, j]:=nullimg; texture_right[i, j]:=nullimg; end; end; end; procedure saveData;//Сохранение всех данных begin end; procedure loadData;//Загрузка всех данных begin end; procedure resetData;//Сброс всех данных var i, j:integer; begin for i:=0 to MAX_MOBS do begin mob_type[i]:=M_NONE; mob_x[i]:=0; mob_y[i]:=0; mob_vx[i]:=0; mob_vy[i]:=0; mob_posi[i]:=0; mob_hp[i]:=0; mob_jmp[i]:=false; for j:=0 to MAX_ANIMREG do mob_anim[i, j]:=0; end; end; initialization // TYPE W H HP SX JH, AI_TYPE initTab(M_NONE, 0, 0, 0, 0, 0, AI_NONE); setMaxParts(M_NONE, -1); initTab(M_ZOMBY, 8, 32, 10, 1, 7, AI_ZLOY); setMaxParts(M_ZOMBY, 3); // TYPE PART, TEX, X, Y ANIM initPart(M_ZOMBY, 0, 0, 4, 8, 0);//Head initPart(M_ZOMBY, 1, 1, 4, 8, 0);//Body initPart(M_ZOMBY, 2, 2, 4, 10, 0);//Hands initPart(M_ZOMBY, 3, 3, 4, 20, 1);//Legs end.