X-Git-Url: https://deadsoftware.ru/gitweb?p=cavecraft.git;a=blobdiff_plain;f=src%2Fplayer.mpsrc;h=812a2b46829db0f2f7569a59afe7fb310bb5b3c5;hp=1891939e113195d38530b4fb149d2a35378a3c43;hb=90bc20fc7666db31948953ec7ae11f6f5f90f509;hpb=89a93092c75dfc61007345054269c0c3c3d4b823 diff --git a/src/player.mpsrc b/src/player.mpsrc index 1891939..812a2b4 100644 --- a/src/player.mpsrc +++ b/src/player.mpsrc @@ -1,368 +1,277 @@ unit player; interface - const - ANIM_HAND=0; - ANIM_LEGS=1; - - var - fly:boolean; - hp, hunger, air:integer; - posi:integer;//Сторона в которую повёрнут игрок - velx,vely:integer;//Переменные для физики игрока - jmp, an_pr:boolean; - s_get_drp:boolean; - invslot:integer; - - procedure setX(val:integer); - procedure setY(val:integer); - function getX:integer; - function getY:integer; - function getW:integer; - function getH:integer; - - procedure loadSkin(str, path:string); - procedure freeSkin; - procedure draw(camx, camy:integer); - procedure playAnim(anim:integer); - procedure cancelAnim(anim:integer); - - procedure dropItem(item, sum:integer); - procedure biteIt(hp : integer; vector : integer); - - procedure calcPhysics; - - procedure gotoUP; - procedure gotoDOWN; - procedure gotoLEFT; - procedure gotoRIGHT; - - procedure getDrop; + + const + ANIM_HAND=0; + ANIM_LEGS=1; + + var + fly : boolean; + hp, hunger, air : integer; + posi : integer; + velx, vely : integer; + jmp, an_pr : boolean; + s_get_drp : boolean; + invslot : integer; + + function GetX : integer; + function GetY : integer; + function GetW : integer; + function GetH : integer; + procedure SetX(val : integer); + procedure SetY(val : integer); + + procedure LoadSkin(str, path : string); + procedure FreeSkin; + procedure Draw(camx, camy : integer); + procedure PlayAnim(anim : integer); + procedure CancelAnim(anim : integer); + + procedure DropItem(item, sum : integer); + procedure BiteIt(hp : integer; vector : integer); + + procedure CalcPhysics; + + procedure GotoUP; + procedure GotoDOWN; + procedure GotoLEFT; + procedure GotoRIGHT; + + procedure GetDrop; implementation - uses func, phy, vars, Canvas, drop, items, jsr75i, inv; - const - PLAYER_W=8; - PLAYER_H=32; - PLAYER_SPEED=2; - - POSI_LEFT=0; - POSI_RIGHT=1; - PLAYER_ANIM_DEL=1; - var - plx, ply:integer; - - //Skin - PlayerBody: array [0..1] of image; - PlayerHand: array [0..1, 0..3] of image; - PlayerLegs: array [0..1, 0..2] of image; - animHand, animLegs, animDelay:integer; - - procedure setX(val:integer); - begin - plx:=val; - end; - - procedure setY(val:integer); - begin - ply:=val; - end; - - function getX:integer; - begin - getX:=plx; - end; - - function getY:integer; - begin - getY:=ply; - end; - - function getW:integer; - begin - getW:=PLAYER_W; - end; - - function getH:integer; - begin - getH:=PLAYER_H; - end; - - procedure setVelX(val:integer); - begin - velx:=val; - end; - - procedure setVelY(val:integer); - begin - vely:=val; - end; - - function getVelX:integer; - begin - getVelX:=velx; - end; - - function getVelY:integer; - begin - getVelY:=vely; - end; - - procedure setJmp(val:boolean); - begin - jmp:=val; - end; - - function getJmp:boolean; - begin - getJmp:=jmp; - end; - - procedure dropItem(item, sum:integer); - begin - if posi=POSI_LEFT then - drop.create(item, sum, getX-8, getY); - else - drop.create(item, sum, getX+getW, getY); - end; - - procedure loadSkin(str, path:string); + + uses func, phy, vars, Canvas, drop, items, jsr75i, inv; + + const + PLAYER_W=8; + PLAYER_H=32; + PLAYER_SPEED=2; + POSI_LEFT=0; + POSI_RIGHT=1; + PLAYER_ANIM_DEL=2; + jumpVelocity = 7; + var - im:image; - i:integer; - begin - im:=ld_tex(str, path, 'mobs/'); + plx, ply:integer; + playerBody: array [0..1] of image; + playerHand: array [0..1, 0..3] of image; + playerLegs: array [0..1, 0..2] of image; + animHand, animLegs, animDelay:integer; - PlayerBody[POSI_LEFT]:=rotate_image_from_image(im, 0, 0, 8, 20, 0); - PlayerBody[POSI_RIGHT]:=rotate_image_from_image(im, 8, 0, 8, 20, 0); + procedure SetX(val : integer); + begin + plx := val; + end; - PlayerHand[POSI_LEFT, 0]:=rotate_image_from_image(im, 40, 0, 4, 12, 0); - PlayerHand[POSI_RIGHT, 0]:=rotate_image_from_image(im, 44, 0, 4, 12, 0); + procedure SetY(val : integer); + begin + ply := val; + end; - for i:=1 to 3 do + function GetX : integer; begin - PlayerHand[POSI_LEFT, i]:=rotate_image_from_image(im, 16, (i-1)*6, 12, 6, 0); - PlayerHand[POSI_RIGHT, i]:=rotate_image_from_image(im, 28, (i-1)*6, 12, 6, 0); + result := plx; end; - for i:=0 to 2 do + function GetY : integer; begin - PlayerLegs[POSI_LEFT, i]:=rotate_image_from_image(im, i*12, 20, 12, 12, 0); - PlayerLegs[POSI_RIGHT, i]:=rotate_image_from_image(im, 36+i*12, 20, 12, 12, 0); + result := ply; end; - end; - procedure freeSkin; - var - i, j:integer; - nullimg:image; - begin - for i:=0 to 1 do + function GetW : integer; + begin + result := PLAYER_W; + end; + + function GetH : integer; + begin + result := PLAYER_H; + end; + + procedure SetVelX(val : integer); + begin + velx := val; + end; + + procedure SetVelY(val : integer); + begin + vely := val; + end; + + function GetVelX : integer; + begin + result := velx; + end; + + function GetVelY : integer; + begin + result := vely; + end; + + procedure SetJmp(val : boolean); + begin + jmp := val; + end; + + function GetJmp : boolean; + begin + result := jmp; + end; + + procedure DropItem(item, sum : integer); begin - PlayerBody[i]:=nullimg; - for j:=0 to 3 do - PlayerHand[i, j]:=nullimg; - for j:=0 to 2 do - PlayerLegs[i, j]:=nullimg; + if posi = POSI_LEFT then Drop.Create(item, sum, GetX - 8, GetY); + else Drop.Create(item, sum, GetX + GetW, GetY); end; - end; - procedure playAnim(anim:integer); - begin - if anim=ANIM_HAND then + procedure LoadSkin(str, path:string); + var + im : image; + i : integer; begin - animHand:=animHand+1; - if animHand>3 then - animHand:=1; + im := ld_tex(str, path, 'mobs/'); + playerBody[POSI_LEFT] := rotate_image_from_image(im, 0, 0, 8, 20, 0); + playerBody[POSI_RIGHT] := rotate_image_from_image(im, 8, 0, 8, 20, 0); + playerHand[POSI_LEFT, 0] := rotate_image_from_image(im, 40, 0, 4, 12, 0); + playerHand[POSI_RIGHT, 0] := rotate_image_from_image(im, 44, 0, 4, 12, 0); + for i := 1 to 3 do begin + playerHand[POSI_LEFT, i] := rotate_image_from_image(im, 16, (i - 1) * 6, 12, 6, 0); + playerHand[POSI_RIGHT, i] := rotate_image_from_image(im, 28, (i - 1) * 6, 12, 6, 0); + end; + for i:=0 to 2 do begin + playerLegs[POSI_LEFT, i] := rotate_image_from_image(im, i * 12, 20, 12, 12, 0); + playerLegs[POSI_RIGHT, i] := rotate_image_from_image(im, 36 + i * 12, 20, 12, 12, 0); + end; end; - else - if anim=ANIM_LEGS then + + procedure FreeSkin; + var + i, j : integer; + nullimg : image; begin - if animDelay=0 then - begin - animLegs:=animLegs+1; - if animLegs>2 then - animLegs:=0; + for i := 0 to 1 do begin + playerBody[i] := nullimg; + for j:=0 to 3 do playerHand[i, j] := nullimg; + for j:=0 to 2 do playerLegs[i, j] := nullimg; end; + end; - animDelay:=animDelay+1; - if animDelay>PLAYER_ANIM_DEL then - animDelay:=0; + procedure PlayAnim(anim : integer); + begin + if anim = ANIM_HAND then begin + animHand := animHand + 1; + if animHand > 3 then animHand:=1; + end else if anim = ANIM_LEGS then begin + if animDelay = 0 then begin + animLegs := animLegs + 1; + if animLegs > 2 then animLegs := 0; + end; + animDelay:=animDelay+1; + if animDelay > PLAYER_ANIM_DEL then animDelay := 0; + end; end; - end; - procedure cancelAnim(anim:integer); - begin - if anim=ANIM_HAND then + procedure CancelAnim(anim : integer); begin - animHand:=0; + if anim = ANIM_HAND then animHand := 0; + else if anim = ANIM_LEGS then animLegs := 0; end; - else - if anim=ANIM_LEGS then + + procedure Draw(camx, camy : integer); + var + x, y : integer; begin - animLegs:=0; + x := GetX; + y := GetY; + DrawImage(playerBody[posi], x - camx, y - camy); + DrawImage(playerLegs[posi, animLegs], x - 2 - camx, y + 20 - camy); + + if animHand = 0 then DrawSmallItem(Inv.GetItem(invslot), x - camx, y + 14 - camy); + else if posi = POSI_RIGHT then DrawSmallItem(Inv.GetItem(invslot), x + 10 - camx, y + 5 - camy + animHand); + else if posi = POSI_LEFT then DrawSmallItem(Inv.GetItem(invslot), x - 10 - camx, y + 5 - camy + animHand); + + if (animHand = 0) or (posi = POSI_RIGHT) then DrawImage(playerHand[posi, animHand], x + 2 - camx, y + 8 - camy); + else DrawImage(playerHand[posi, animHand], x - 6 - camx, y + 8 - camy); end; - end; - procedure draw(camx, camy:integer); - var - x, y:integer; - begin - x:=getX; - y:=getY; - - DrawImage(PlayerBody[posi], x-camx, y-camy); - DrawImage(PlayerLegs[posi, animLegs], x-2-camx, y+20-camy); - - if animHand=0 then - drawSmallItem(inv.getItem(invslot), x-camx, y+14-camy); - else - if posi=POSI_RIGHT then - drawSmallItem(inv.getItem(invslot), x+10-camx, y+5-camy+animHand); - else - if posi=POSI_LEFT then - drawSmallItem(inv.getItem(invslot), x-10-camx, y+5-camy+animHand); - - if (animHand=0) or (posi=POSI_RIGHT) then - DrawImage(PlayerHand[posi, animHand], x+2-camx, y+8-camy); - else - DrawImage(PlayerHand[posi, animHand], x-6-camx, y+8-camy); - end; - - procedure biteIt(damage : integer; vector : integer); - begin - hp := hp - damage; - end; + procedure BiteIt(damage : integer; vector : integer); + begin + hp := hp - damage; + velx := velx + vector; + end; -procedure loadPhy; - begin - phy.loadObject(getX, getY, getW, getH, getVelX, getVelY, getJmp); - end; - -procedure storePhy; - begin - setX(phy.getX); - setY(phy.getY); - setVelX(phy.getVelX); - setVelY(phy.getVelY); - setJmp(phy.getJmp); - end; - - //Player collision. -{function coll:boolean; - begin - coll:=CollObj(getX, getY, getW, getH); - end; - -//Player collision by block id. -function coll_bl(id:integer):boolean; - begin - coll_bl:=CollObjBlock(getX, getY, getW, getH, id); - end; - -//Player collision by XY. -function coll_xy(xx,yy:integer):boolean; - begin - coll_xy:=CollObjXY(getX, getY, getW, getH, xx, yy); - end; - -//Controll jump velocity -procedure jmp_ctrl; - begin - if (coll_bl(49)) or (coll_bl(103)) or (coll_bl(108)) then - vely:=5; - if (coll_bl(50)) or (coll_bl(51)) then - vely:=4; - if (coll_bl(0)) and (coll_bl(50) or coll_bl(51)) then - vely:=7; - end; - -//Controll fall velocity - procedure phy_ctrl; - begin - if (coll_bl(49)) or (coll_bl(103)) then - vely:=-5; - if (coll_bl(50)) or (coll_bl(51)) then - if vely<-4 then - vely:=-4; - end;} - - procedure calcPhysics; - var - old_vely:integer; - cl:boolean; - begin - loadPhy; - phy.calc(not fly); - storePhy; - - {if fly=false then + procedure LoadPhy; begin - old_vely:=vely; - calcGravY(getX, getY, getW, getH, CONST_PHY_ACC, vely, CONST_PHY_MAXVEL, jmp); - setY(PhyGetY); - vely:=PhyGetVelY; - jmp:=PhyGetJump; - cl:=PhyGetColl; - - if cl then - if old_vely<-10 then - hp:=(hp-(abs(old_vely)-10)); - end;} - end; - - procedure gotoUP; - begin - if fly then - setVelY(-PLAYER_SPEED); - else + Phy.SetObject(GetX, GetY, GetW, GetH, GetVelX, GetVelY, GetJmp); + end; + + procedure StorePhy; begin - loadPhy; - jumpObj(7); - storePhy; + SetX(Phy.GetX); + SetY(Phy.GetY); + SetVelX(Phy.GetVX); + SetVelY(Phy.GetVY); + SetJmp(Phy.GetJump); end; - end; - procedure gotoDOWN; - begin - if fly then - setVelY(PLAYER_SPEED); - end; + procedure CalcPhysics; + var + vel : integer; + begin + vel := vely; - procedure gotoLEFT; - begin - setVelX(-PLAYER_SPEED); + LoadPhy; + Phy.Step(not fly); + StorePhy; - posi:=POSI_LEFT; + if (vel > 0) and (Phy.GetGCV - vel < -10) then BiteIt(vel - 10, Random(5) - 2); + end; - if fly=false then - playAnim(ANIM_LEGS); - end; + procedure GotoUP; + begin + if fly then begin + SetVelY(-PLAYER_SPEED); + end else begin + LoadPhy; + Phy.Jump(jumpVelocity); + StorePhy; + end; + end; - procedure gotoRIGHT; - begin - setVelX(PLAYER_SPEED); + procedure GotoDOWN; + begin + if fly then SetVelY(PLAYER_SPEED); + end; - posi:=POSI_RIGHT; + procedure GotoLEFT; + begin + SetVelX(-PLAYER_SPEED); + posi := POSI_LEFT; + if not fly then PlayAnim(ANIM_LEGS); + end; - if fly=false then - playAnim(ANIM_LEGS); - end; + procedure GotoRIGHT; + begin + SetVelX(PLAYER_SPEED); + posi := POSI_RIGHT; + if not fly then playAnim(ANIM_LEGS); + end; - procedure getDrop; - var - i, maxd, sum:integer; - begin - maxd:=drop.max; - for i:=0 to maxd do - if drop.isNull(i)=false then - if CollTwoObj(getX, getY, getW, getH, drop.getX(i), drop.getY(i), drop.getW, drop.getH) then - begin - sum:=inv.giveItem(drop.getItem(i), drop.getSum(i)); - drop.setSum(sum, i); - drop.fixNull(i); + procedure GetDrop; + var + i, maxd, sum : integer; + begin + maxd := Drop.Max; + for i := 0 to maxd do if Drop.IsNull(i) = false then begin + if Phy.IntersectRects(GetX, GetY, GetW, GetH, Drop.GetX(i), Drop.GetY(i), Drop.GetW, Drop.GetH) then begin + sum:=inv.giveItem(drop.getItem(i), drop.getSum(i)); + drop.setSum(sum, i); + drop.fixNull(i); + end; end; - end; + end; initialization