DEADSOFTWARE

b41c8e3b7ff82cfcc4eda09aa5e8cbb6f7f2895b
[cavecraft.git] / src / Player.mpsrc
1 unit Player;
3 interface
5 const
6 ANIM_HAND=0;
7 ANIM_LEGS=1;
9 var
10 fly : boolean;
11 hp, hunger, air : integer;
12 posi : integer;
13 velx, vely : integer;
14 jmp, an_pr : boolean;
15 s_get_drp : boolean;
16 invslot : integer;
18 function GetX : integer;
19 function GetY : integer;
20 function GetW : integer;
21 function GetH : integer;
22 procedure SetX(val : integer);
23 procedure SetY(val : integer);
25 procedure LoadSkin(str, path : string);
26 procedure FreeSkin;
27 procedure Draw(camx, camy : integer);
28 procedure PlayAnim(anim : integer);
29 procedure CancelAnim(anim : integer);
31 procedure DropItem(item, sum : integer);
32 procedure BiteIt(hp : integer; vector : integer);
34 procedure CalcPhysics;
36 procedure GotoUP;
37 procedure GotoDOWN;
38 procedure GotoLEFT;
39 procedure GotoRIGHT;
41 procedure GetDrop;
43 implementation
45 uses func, phy, vars, Canvas, drop, items, jsr75i, inv;
47 const
48 PLAYER_W=8;
49 PLAYER_H=32;
50 PLAYER_SPEED=2;
51 POSI_LEFT=0;
52 POSI_RIGHT=1;
53 PLAYER_ANIM_DEL=2;
54 jumpVelocity = 7;
56 var
57 plx, ply:integer;
58 playerBody: array [0..1] of image;
59 playerHand: array [0..1, 0..3] of image;
60 playerLegs: array [0..1, 0..2] of image;
61 animHand, animLegs, animDelay:integer;
63 procedure SetX(val : integer);
64 begin
65 plx := val;
66 end;
68 procedure SetY(val : integer);
69 begin
70 ply := val;
71 end;
73 function GetX : integer;
74 begin
75 result := plx;
76 end;
78 function GetY : integer;
79 begin
80 result := ply;
81 end;
83 function GetW : integer;
84 begin
85 result := PLAYER_W;
86 end;
88 function GetH : integer;
89 begin
90 result := PLAYER_H;
91 end;
93 procedure SetVelX(val : integer);
94 begin
95 velx := val;
96 end;
98 procedure SetVelY(val : integer);
99 begin
100 vely := val;
101 end;
103 function GetVelX : integer;
104 begin
105 result := velx;
106 end;
108 function GetVelY : integer;
109 begin
110 result := vely;
111 end;
113 procedure SetJmp(val : boolean);
114 begin
115 jmp := val;
116 end;
118 function GetJmp : boolean;
119 begin
120 result := jmp;
121 end;
123 procedure DropItem(item, sum : integer);
124 begin
125 if posi = POSI_LEFT then Drop.Create(item, sum, GetX - 8, GetY);
126 else Drop.Create(item, sum, GetX + GetW, GetY);
127 end;
129 procedure LoadSkin(str, path:string);
130 var
131 im : image;
132 i : integer;
133 begin
134 im := ld_tex(str, path, 'mobs/');
135 playerBody[POSI_LEFT] := rotate_image_from_image(im, 0, 0, 8, 20, 0);
136 playerBody[POSI_RIGHT] := rotate_image_from_image(im, 8, 0, 8, 20, 0);
137 playerHand[POSI_LEFT, 0] := rotate_image_from_image(im, 40, 0, 4, 12, 0);
138 playerHand[POSI_RIGHT, 0] := rotate_image_from_image(im, 44, 0, 4, 12, 0);
139 for i := 1 to 3 do begin
140 playerHand[POSI_LEFT, i] := rotate_image_from_image(im, 16, (i - 1) * 6, 12, 6, 0);
141 playerHand[POSI_RIGHT, i] := rotate_image_from_image(im, 28, (i - 1) * 6, 12, 6, 0);
142 end;
143 for i:=0 to 2 do begin
144 playerLegs[POSI_LEFT, i] := rotate_image_from_image(im, i * 12, 20, 12, 12, 0);
145 playerLegs[POSI_RIGHT, i] := rotate_image_from_image(im, 36 + i * 12, 20, 12, 12, 0);
146 end;
147 end;
149 procedure FreeSkin;
150 var
151 i, j : integer;
152 nullimg : image;
153 begin
154 for i := 0 to 1 do begin
155 playerBody[i] := nullimg;
156 for j:=0 to 3 do playerHand[i, j] := nullimg;
157 for j:=0 to 2 do playerLegs[i, j] := nullimg;
158 end;
159 end;
161 procedure PlayAnim(anim : integer);
162 begin
163 if anim = ANIM_HAND then begin
164 animHand := animHand + 1;
165 if animHand > 3 then animHand:=1;
166 end else if anim = ANIM_LEGS then begin
167 if animDelay = 0 then begin
168 animLegs := animLegs + 1;
169 if animLegs > 2 then animLegs := 0;
170 end;
171 animDelay:=animDelay+1;
172 if animDelay > PLAYER_ANIM_DEL then animDelay := 0;
173 end;
174 end;
176 procedure CancelAnim(anim : integer);
177 begin
178 if anim = ANIM_HAND then animHand := 0;
179 else if anim = ANIM_LEGS then animLegs := 0;
180 end;
182 procedure Draw(camx, camy : integer);
183 var
184 x, y : integer;
185 begin
186 x := GetX;
187 y := GetY;
188 DrawImage(playerBody[posi], x - camx, y - camy);
189 DrawImage(playerLegs[posi, animLegs], x - 2 - camx, y + 20 - camy);
191 if animHand = 0 then DrawSmallItem(Inv.GetItem(invslot), x - camx, y + 14 - camy);
192 else if posi = POSI_RIGHT then DrawSmallItem(Inv.GetItem(invslot), x + 10 - camx, y + 5 - camy + animHand);
193 else if posi = POSI_LEFT then DrawSmallItem(Inv.GetItem(invslot), x - 10 - camx, y + 5 - camy + animHand);
195 if (animHand = 0) or (posi = POSI_RIGHT) then DrawImage(playerHand[posi, animHand], x + 2 - camx, y + 8 - camy);
196 else DrawImage(playerHand[posi, animHand], x - 6 - camx, y + 8 - camy);
197 end;
199 procedure BiteIt(damage : integer; vector : integer);
200 begin
201 hp := hp - damage;
202 velx := velx + vector;
203 end;
205 procedure LoadPhy;
206 begin
207 Phy.SetObject(GetX, GetY, GetW, GetH, GetVelX, GetVelY, GetJmp);
208 end;
210 procedure StorePhy;
211 begin
212 SetX(Phy.GetX);
213 SetY(Phy.GetY);
214 SetVelX(Phy.GetVX);
215 SetVelY(Phy.GetVY);
216 SetJmp(Phy.GetJump);
217 end;
219 procedure CalcPhysics;
220 var
221 vel : integer;
222 begin
223 vel := vely;
225 LoadPhy;
226 Phy.Step(not fly);
227 StorePhy;
229 if (vel > 0) and (Phy.GetGCV - vel < -10) then BiteIt(vel - 10, Random(5) - 2);
230 end;
232 procedure GotoUP;
233 begin
234 if fly then begin
235 SetVelY(-PLAYER_SPEED);
236 end else begin
237 LoadPhy;
238 Phy.Jump(jumpVelocity);
239 StorePhy;
240 end;
241 end;
243 procedure GotoDOWN;
244 begin
245 if fly then SetVelY(PLAYER_SPEED);
246 end;
248 procedure GotoLEFT;
249 begin
250 SetVelX(-PLAYER_SPEED);
251 posi := POSI_LEFT;
252 if not fly then PlayAnim(ANIM_LEGS);
253 end;
255 procedure GotoRIGHT;
256 begin
257 SetVelX(PLAYER_SPEED);
258 posi := POSI_RIGHT;
259 if not fly then playAnim(ANIM_LEGS);
260 end;
262 procedure GetDrop;
263 var
264 i, maxd, sum : integer;
265 begin
266 maxd := Drop.Max;
267 for i := 0 to maxd do if Drop.IsNull(i) = false then begin
268 if Phy.IntersectRects(GetX, GetY, GetW, GetH, Drop.GetX(i), Drop.GetY(i), Drop.GetW, Drop.GetH) then begin
269 sum:=inv.giveItem(drop.getItem(i), drop.getSum(i));
270 drop.setSum(sum, i);
271 drop.fixNull(i);
272 end;
273 end;
274 end;
276 initialization
278 end.