unit GameEng;

interface
uses DGLEngine_header, Variables, Math, OpenGL, GameMap, GameUtils, Windows, GameObjects, SysUtils;

procedure DrawGame;
procedure ProcessGame;


implementation
var PlayerCoord : Tpoint;

procedure DrawPlayer;
var i : integer; a : single;
V : TGLVectord3;
begin
for i:=0 to 7 do
if Player[i].Active then
begin
BeginObj3D;
position3D(Player[i].X,Player[i].Y,0.530);

  v[0]:=0; v[1]:=0; v[2]:=0;
  PlayerCoord:=GetOpenGLProject(v);

glEnable(GL_LIGHTING);
 BeginObj3D;
  Scale3D(0.025);

  a:=0.0;
  case round(Player[i].LegsFrame)*2 of
  2: a:=2.0; 3: a:=2.5; 4: a:=2.5;
  5: a:=0.5; 6: a:=-1.2; 7: a:=-2.5;
  8: a:=-1.5; 9: a:=0.3;11: a:=0.4;
  12: a:=2.2;13: a:=3.0;14: a:=2.4;
  15: a:=0.5; 16: a:=-0.3;17: a:=-1.8;
  18: a:=-1.5; 19: a:=0.7;20: a:=1.8;
  end;

  RotateZ(Player[i].LegsAngle);
  Position3D(-a,0,0);

  SetTexture(tex_Legs);
  DrawModel(mdl_legs,round(Player[i].LegsFrame),true);

  Position3D(a,0,0);
  RotateZ(-Player[i].LegsAngle);

  RotateZ(Player[i].Angle);

  RotateY(2*sin(degtorad(Player[i].LegsFrame*9)));
  Position3D(16,-2.0,29);

  SetTexture(tex_body);
  DrawModel(mdl_body,0,true);

  RotateY(-2*sin(degtorad(Player[i].LegsFrame*9)));
  Position3D(-4.7,0,13);
  SetTexture(tex_head);
  DrawModel(mdl_head,0,true);
 EndObj3D;

 BeginObj3D;
  RotateZ(Player[i].Angle);
  Scale3D(0.15);
  RotateY(2*sin(degtorad(Player[i].LegsFrame*9)));
  Position3D(5.45,-0.55,5.5);
   SetTexture(tex_pistol);
   DrawModel(mdl_pistol);

   if ShotWait<5 then
   begin
    glDisable(GL_LIGHTING);
    BeginObj3D;
    Color3D($0080FF,100,true);
    SetTexture(tex_alpharound);
    Position3D(1.0,0,-8.8);
    DrawPoly3D(15,15);
    EndObj3D;

    BeginObj3D;
    Color3D($0080FF,200,true);
    SetTexture(tex_alpharound);
    Position3D(1.0,0,0);
    DrawPoly3D(1.5,3);
    DrawPoly3D(4.5,1.5);
    EndObj3D;
   end;

 EndObj3D;

glDisable(GL_LIGHTING);
EndObj3D;
end;
end;

procedure DrawGame;
begin
Begin2D;
DrawTexture2D_Simple(tex_Sky,0,0,GetScreenResX,GetScreenResY);
End2D;
ClearZBuffer;

SetCamera(GameCamera);

 DrawMapSurface;

 DrawBlood;

 DrawGroundObj;

 DrawMonsters;

 DrawPlayer;

 DrawTurel;

 DrawBullets;


 DrawAirObj;

Begin2D;
DrawText2D(tex_MainFont,20,GetScreenResY-30,$FFFFFF,'HEALTH:'+inttostr(Player[0].Health)+'  '+'SCORE:'+inttostr(Player[0].Points),255,1,22);

DrawTexture2D_Simple(tex_Pricel,GetMousePos.X-8,GetMousePos.Y-8,16,16);
End2D;
end;

function TestPlayerCol : boolean;
var i : integer;
begin
result:=false;
 for i:=0 to length(m_GObjects)-1 do
  if  RectInRect(m_GObjects[i].col,boundsS(Player[0].X,Player[0].Y,0.150,0.150)) then
  begin
   result:=true;
   Exit;
  end;
 for i:=0 to MonstersCount-1 do
 if Monsters[i].Status<>5 then
  if RectInRect(Monsters[i].col,boundsS(Player[0].X,Player[0].Y,0.150,0.150)) then
  begin
   result:=true;
   Exit;
  end;

for i:=0 to TurelsCount-1 do
 if Turels[i].Active then
  if RectInRect(boundsS(Turels[i].X+0.25,Turels[i].Y+0.25,0.5,0.5),boundsS(Player[0].X,Player[0].Y,0.150,0.150)) then
  begin
   result:=true;
   Exit;
  end;

 if ((Player[0].X<0)or(Player[0].X>m_Header.CellsX_count) or (Player[0].Y<0)or(Player[0].Y>m_Header.CellsX_count))  then result:=true;

end;

procedure ProcessPlayer;
var Move : integer;
begin

if Player[0].Health<=0 then
begin
PlaySample(s_death);

GameCamera.Eye.X:=Player[0].X-10;
GameCamera.Eye.Y:=Player[0].Y-10;
GameCamera.Eye.Z:=2;

GameCamera.Center.X:=Player[0].X;
GameCamera.Center.Y:=Player[0].Y;
GameCamera.Center.Z:=2.5;

PlayerDead:=true;

end;

Move := 0;

Player[0].OldX:=Player[0].X;
Player[0].OldY:=Player[0].Y;

if IsKeyPressed(keyUp) then
 begin
 Move:=1;
 Player[0].Y:=Player[0].Y+0.08;
 if TestPlayerCol then Player[0].Y:=Player[0].OldY;
 end;
if IsKeyPressed(keyDown) then
 begin
 Move:=2;
 Player[0].Y:=Player[0].Y-0.08;
 if TestPlayerCol then Player[0].Y:=Player[0].OldY;
 end;
if IsKeyPressed(keyRight) then
 begin
 Move:=Move+3;
 Player[0].X:=Player[0].X+0.08;
 if TestPlayerCol then Player[0].X:=Player[0].OldX;
 end;
if IsKeyPressed(keyLeft) then
 begin
 Move:=Move+6;
 Player[0].X:=Player[0].X-0.08;
 if TestPlayerCol then Player[0].X:=Player[0].OldX;
 end;

 case Move of
 1:Player[0].LegsAngle:=-90;
 2:Player[0].LegsAngle:=90;
 3:Player[0].LegsAngle:=0;
 4:Player[0].LegsAngle:=-45;
 5:Player[0].LegsAngle:=45;
 6:Player[0].LegsAngle:=180;
 7:Player[0].LegsAngle:=225;
 8:Player[0].LegsAngle:=135;
 end;

if Move<>0 then
 begin
 Zoom:=Zoom+0.05;
 if Zoom>7 then Zoom:=7;
 Player[0].LegsFrame:=Player[0].LegsFrame+0.5;
 if round(Player[0].LegsFrame)>11 then Player[0].LegsFrame:=0;
 end else
 begin
 Zoom:=Zoom-0.15;
 if Zoom<0 then Zoom:=0;
 Player[0].LegsFrame:=1;
 end;

Player[0].Angle:=trunc(RadToDeg(ArcTan2(GetMousePos.Y-PlayerCoord.Y, GetMousePos.X-PlayerCoord.X)));

if (IsRightMouseButtonPressed) and (ShotWait>10) and (Player[0].Points>=500) then
 begin
 Player[0].Points:=Player[0].Points-500;
 ShotWait:=0;
 CreateTurel(CurCellX,CurCellY);
 end;

if (IsLeftMouseButtonPressed) and (ShotWait>10) then
 begin
 PlaySample(s_shot);
 ShotWait:=0;
 CreateBullet(Player[0].X,Player[0].Y,0.45,-Player[0].Angle,35);
 end;
inc(ShotWait);

if cameratyp=1 then
begin
GameCamera.Eye.X:=Player[0].X;
GameCamera.Eye.Y:=Player[0].Y;
GameCamera.Eye.Z:=8+Zoom;
end else
begin
GameCamera.Eye.X:=Player[0].X;
GameCamera.Eye.Y:=Player[0].Y-10;
GameCamera.Eye.Z:=6;
end;

GameCamera.Center.X:=Player[0].X;
GameCamera.Center.Y:=Player[0].Y;
GameCamera.Center.Z:=2.5;

end;

procedure ProcessGame;
begin
inc(ResWait);
if ResWait=500 then
begin
ResWait:=0;
ReadSpecialObj;
end;
ProcessPlayer;
ProcessBullets;
ProcessMonsters;
ProcessBLood;
ProcessTurel;
end;

end.
