DEADSOFTWARE

Fix spawn point
[cavecraft.git] / src / player.mpsrc
1 unit player;
3 interface
4 const
5 ANIM_HAND=0;
6 ANIM_LEGS=1;
8 var
9 fly:boolean;
10 hp, hunger, air:integer;
11 posi:integer;//Сторона в которую повёрнут игрок
12 velx,vely:integer;//Переменные для физики игрока
13 jmp, an_pr:boolean;
14 s_get_drp:boolean;
15 invslot:integer;
17 procedure setX(val:integer);
18 procedure setY(val:integer);
19 function getX:integer;
20 function getY:integer;
21 function getW:integer;
22 function getH:integer;
24 procedure loadSkin(str, path:string);
25 procedure freeSkin;
26 procedure draw(camx, camy:integer);
27 procedure playAnim(anim:integer);
28 procedure cancelAnim(anim:integer);
30 procedure dropItem(item, sum:integer);
31 procedure biteIt(hp : integer; vector : integer);
33 procedure calcPhysics;
35 procedure gotoUP;
36 procedure gotoDOWN;
37 procedure gotoLEFT;
38 procedure gotoRIGHT;
40 procedure getDrop;
42 implementation
43 uses func, phy, vars, Canvas, drop, items, jsr75i, inv;
44 const
45 PLAYER_W=8;
46 PLAYER_H=32;
47 PLAYER_SPEED=2;
49 POSI_LEFT=0;
50 POSI_RIGHT=1;
51 PLAYER_ANIM_DEL=1;
52 var
53 plx, ply:integer;
55 //Skin
56 PlayerBody: array [0..1] of image;
57 PlayerHand: array [0..1, 0..3] of image;
58 PlayerLegs: array [0..1, 0..2] of image;
59 animHand, animLegs, animDelay:integer;
61 procedure setX(val:integer);
62 begin
63 plx:=val;
64 end;
66 procedure setY(val:integer);
67 begin
68 ply:=val;
69 end;
71 function getX:integer;
72 begin
73 getX:=plx;
74 end;
76 function getY:integer;
77 begin
78 getY:=ply;
79 end;
81 function getW:integer;
82 begin
83 getW:=PLAYER_W;
84 end;
86 function getH:integer;
87 begin
88 getH:=PLAYER_H;
89 end;
91 procedure setVelX(val:integer);
92 begin
93 velx:=val;
94 end;
96 procedure setVelY(val:integer);
97 begin
98 vely:=val;
99 end;
101 function getVelX:integer;
102 begin
103 getVelX:=velx;
104 end;
106 function getVelY:integer;
107 begin
108 getVelY:=vely;
109 end;
111 procedure setJmp(val:boolean);
112 begin
113 jmp:=val;
114 end;
116 function getJmp:boolean;
117 begin
118 getJmp:=jmp;
119 end;
121 procedure dropItem(item, sum:integer);
122 begin
123 if posi=POSI_LEFT then
124 drop.create(item, sum, getX-8, getY);
125 else
126 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/');
136 PlayerBody[POSI_LEFT]:=rotate_image_from_image(im, 0, 0, 8, 20, 0);
137 PlayerBody[POSI_RIGHT]:=rotate_image_from_image(im, 8, 0, 8, 20, 0);
139 PlayerHand[POSI_LEFT, 0]:=rotate_image_from_image(im, 40, 0, 4, 12, 0);
140 PlayerHand[POSI_RIGHT, 0]:=rotate_image_from_image(im, 44, 0, 4, 12, 0);
142 for i:=1 to 3 do
143 begin
144 PlayerHand[POSI_LEFT, i]:=rotate_image_from_image(im, 16, (i-1)*6, 12, 6, 0);
145 PlayerHand[POSI_RIGHT, i]:=rotate_image_from_image(im, 28, (i-1)*6, 12, 6, 0);
146 end;
148 for i:=0 to 2 do
149 begin
150 PlayerLegs[POSI_LEFT, i]:=rotate_image_from_image(im, i*12, 20, 12, 12, 0);
151 PlayerLegs[POSI_RIGHT, i]:=rotate_image_from_image(im, 36+i*12, 20, 12, 12, 0);
152 end;
153 end;
155 procedure freeSkin;
156 var
157 i, j:integer;
158 nullimg:image;
159 begin
160 for i:=0 to 1 do
161 begin
162 PlayerBody[i]:=nullimg;
163 for j:=0 to 3 do
164 PlayerHand[i, j]:=nullimg;
165 for j:=0 to 2 do
166 PlayerLegs[i, j]:=nullimg;
167 end;
168 end;
170 procedure playAnim(anim:integer);
171 begin
172 if anim=ANIM_HAND then
173 begin
174 animHand:=animHand+1;
175 if animHand>3 then
176 animHand:=1;
177 end;
178 else
179 if anim=ANIM_LEGS then
180 begin
181 if animDelay=0 then
182 begin
183 animLegs:=animLegs+1;
184 if animLegs>2 then
185 animLegs:=0;
186 end;
188 animDelay:=animDelay+1;
189 if animDelay>PLAYER_ANIM_DEL then
190 animDelay:=0;
191 end;
192 end;
194 procedure cancelAnim(anim:integer);
195 begin
196 if anim=ANIM_HAND then
197 begin
198 animHand:=0;
199 end;
200 else
201 if anim=ANIM_LEGS then
202 begin
203 animLegs:=0;
204 end;
205 end;
207 procedure draw(camx, camy:integer);
208 var
209 x, y:integer;
210 begin
211 x:=getX;
212 y:=getY;
214 DrawImage(PlayerBody[posi], x-camx, y-camy);
215 DrawImage(PlayerLegs[posi, animLegs], x-2-camx, y+20-camy);
217 if animHand=0 then
218 drawSmallItem(inv.getItem(invslot), x-camx, y+14-camy);
219 else
220 if posi=POSI_RIGHT then
221 drawSmallItem(inv.getItem(invslot), x+10-camx, y+5-camy+animHand);
222 else
223 if posi=POSI_LEFT then
224 drawSmallItem(inv.getItem(invslot), x-10-camx, y+5-camy+animHand);
226 if (animHand=0) or (posi=POSI_RIGHT) then
227 DrawImage(PlayerHand[posi, animHand], x+2-camx, y+8-camy);
228 else
229 DrawImage(PlayerHand[posi, animHand], x-6-camx, y+8-camy);
230 end;
232 procedure biteIt(damage : integer; vector : integer);
233 begin
234 hp := hp - damage;
235 end;
237 procedure loadPhy;
238 begin
239 phy.loadObject(getX, getY, getW, getH, getVelX, getVelY, getJmp);
240 end;
242 procedure storePhy;
243 begin
244 setX(phy.getX);
245 setY(phy.getY);
246 setVelX(phy.getVelX);
247 setVelY(phy.getVelY);
248 setJmp(phy.getJmp);
249 end;
251 //Player collision.
252 {function coll:boolean;
253 begin
254 coll:=CollObj(getX, getY, getW, getH);
255 end;
257 //Player collision by block id.
258 function coll_bl(id:integer):boolean;
259 begin
260 coll_bl:=CollObjBlock(getX, getY, getW, getH, id);
261 end;
263 //Player collision by XY.
264 function coll_xy(xx,yy:integer):boolean;
265 begin
266 coll_xy:=CollObjXY(getX, getY, getW, getH, xx, yy);
267 end;
269 //Controll jump velocity
270 procedure jmp_ctrl;
271 begin
272 if (coll_bl(49)) or (coll_bl(103)) or (coll_bl(108)) then
273 vely:=5;
274 if (coll_bl(50)) or (coll_bl(51)) then
275 vely:=4;
276 if (coll_bl(0)) and (coll_bl(50) or coll_bl(51)) then
277 vely:=7;
278 end;
280 //Controll fall velocity
281 procedure phy_ctrl;
282 begin
283 if (coll_bl(49)) or (coll_bl(103)) then
284 vely:=-5;
285 if (coll_bl(50)) or (coll_bl(51)) then
286 if vely<-4 then
287 vely:=-4;
288 end;}
290 procedure calcPhysics;
291 var
292 old_vely:integer;
293 cl:boolean;
294 begin
295 loadPhy;
296 phy.calc(not fly);
297 storePhy;
299 {if fly=false then
300 begin
301 old_vely:=vely;
302 calcGravY(getX, getY, getW, getH, CONST_PHY_ACC, vely, CONST_PHY_MAXVEL, jmp);
303 setY(PhyGetY);
304 vely:=PhyGetVelY;
305 jmp:=PhyGetJump;
306 cl:=PhyGetColl;
308 if cl then
309 if old_vely<-10 then
310 hp:=(hp-(abs(old_vely)-10));
311 end;}
312 end;
314 procedure gotoUP;
315 begin
316 if fly then
317 setVelY(-PLAYER_SPEED);
318 else
319 begin
320 loadPhy;
321 jumpObj(7);
322 storePhy;
323 end;
324 end;
326 procedure gotoDOWN;
327 begin
328 if fly then
329 setVelY(PLAYER_SPEED);
330 end;
332 procedure gotoLEFT;
333 begin
334 setVelX(-PLAYER_SPEED);
336 posi:=POSI_LEFT;
338 if fly=false then
339 playAnim(ANIM_LEGS);
340 end;
342 procedure gotoRIGHT;
343 begin
344 setVelX(PLAYER_SPEED);
346 posi:=POSI_RIGHT;
348 if fly=false then
349 playAnim(ANIM_LEGS);
350 end;
352 procedure getDrop;
353 var
354 i, maxd, sum:integer;
355 begin
356 maxd:=drop.max;
357 for i:=0 to maxd do
358 if drop.isNull(i)=false then
359 if CollTwoObj(getX, getY, getW, getH, drop.getX(i), drop.getY(i), drop.getW, drop.getH) then
360 begin
361 sum:=inv.giveItem(drop.getItem(i), drop.getSum(i));
362 drop.setSum(sum, i);
363 drop.fixNull(i);
364 end;
365 end;
367 initialization
369 end.