unit Tank;

interface

uses
  vectorgeometry,vectortypes,
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, GLWin32Viewer, GLCrossPlatform, BaseClasses, GLScene, jpeg, glKeyboard, GLDCE,
  GLHUDObjects, GLObjects, GLCoordinates, GLSound, GLMaterial, GLCadencer;



Type
TPlayerTank=class
private
PlPula: TGLSphere;
Public
Pl_Pushka1,Pl_Pushka2, Pl_Pushka3: TGLCube;
PLPulaList: array[0..4] of  TList;
Ouch1,Ouch2,Ouch3,T1D,T2D,T3D: Boolean;
TankNomer,AutoTankNomer: integer;
time: array[0..20] of single;
Live1,live2,Live3 : single;
Player,Player2,Player3: TGLCube;

Procedure Auto_Defend(AutoTank: TGLCube);
Procedure createPlayerTank;
Procedure MovePlayerTank(obj: TGLCube);
Procedure PlayerTankPli(obj: TGLCube);
Procedure PlayerTankPli2(obj: TGLCube);
Procedure MovePlayerPula(obj: TGLCube);
Procedure BackMover(obj: TGLCube);
Procedure Check_Tank_Live;
Procedure Player_Tank_Destroy;
Procedure DestroyPula(Pulas: TGLSphere);
Procedure Pula_RayCast;

end;

implementation
Uses Unit1;


{ TPlayerTank }

procedure TPlayerTank.Auto_Defend(AutoTank: TGLCube);
var i,j: integer;
x,y,z,pPoint: TVector;
s1,s2: single;
tgt,pt: TGLCube;
begin
for i:=0 to EnemyTAnk.EnTankList.Count-1 do begin
s1:=TGLCube(EnemyTAnk.EnTankList[i]).DistanceTo(AutoTank);
if s1<40 then begin
if EnemyTAnk.Died[i]=false then begin
tgt:=TGLCube(EnemyTAnk.EnTankList[i]);
MakeVector(y,tgt.AbsolutePosition);
MakeVector(x,AutoTank.AbsolutePosition);
z:=VectorSubtract(y,x);
NormalizeVector(z);
AutoTank.Direction.X:=z[0];
AutoTank.Direction.z:=z[2];

if (TGLCube(World.StenaList[0]).RayCastInterSect(AutoTank.AbsolutePosition,AutoTank.AbsoluteDirection,@pPoint,nil))  then begin
if AutoTank.DistanceTo(TGLCube(EnemyTank.EntankList[i]))<AutoTank.DistanceTo(pPoint)  then
UPLayerTAnk.PlayerTankPli(AutoTank);

end else
  UPLayerTAnk.PlayerTankPli(AutoTank);
end;
end;
end;



end;

procedure TPlayerTank.BackMover(obj: TGLCube);
var i: integer;
    Point,Ray: TVector;
    dist: single;
begin
for i:=0 to world.StenaList.Count-1 do begin
if (TGLCube(World.Stenalist[i]).RayCastIntersect(Obj.AbsolutePosition,Obj.AbsoluteDirection,@Point,nil))
then begin
dist:=obj.DistanceTo(Point);
if dist<1 then
obj.Move(-0.03);
end;
end;



for i:=0 to world.StenaList.Count-1 do begin
Ray:=VectorNegate(Obj.AbsoluteDirection);
if (TGLCube(World.Stenalist[i]).RayCastIntersect(Obj.AbsolutePosition,ray, nil,nil))
then obj.Move(0.03);
end;

end;

procedure TPlayerTank.Check_Tank_Live;
begin
If Ouch1=true then begin Live1:=Live1-1; Ouch1:=false; end;
If Ouch2=true then begin Live2:=Live2-1; Ouch2:=false; end;
If Ouch3=true then begin Live3:=Live3-1; Ouch3:=false; end;

Form1.IntegerLive1.Text:=FloatToStr(Live1)+'%';
Form1.IntegerLive2.Text:=FloatToStr(Live2)+'%';
Form1.IntegerLive3.Text:=FloatToStr(Live3)+'%';

Player_Tank_Destroy;

end;

procedure TPlayerTank.createPlayerTank;
var i: integer;
begin
AutoTankNomer:=0;

for i:=0 to 5 do begin
time[i]:=100;
end;
for i:=0 to 4 do begin
PLPulaList[i]:=TList.Create;
end;

Player:=TGLCube.CreateAsChild(form1.GLScene1.Objects);
with player do begin
Position.AsAffineVector:=AffineVectorMAke(-6,0,-50);
CubeWidth:=2;
CubeDepth:=3;
CubeHeight:=0;
Direction.SetVector(0,0,1);
Up.SetVector(0,1,0);
Material.MaterialLibrary:=form1.GLMaterialLibrary1;
Material.Texture.Disabled:=false;
Material.LibMaterialName:='Tank1';
tag:=14;
with GetOrCreateDCEDynamic(player) do begin
manager:=form1.GLDCEManager1;
Size.SetVector(1,1,2);
end;

end;


Player2:=TGLCube.CreateAsChild(form1.GLScene1.Objects);
with player2 do begin
CubeWidth:=2;
CubeDepth:=3;
CubeHeight:=0;
Direction.SetVector(0,0,1);
Up.SetVector(0,1,0);
Material.MaterialLibrary:=form1.GLMaterialLibrary1;
Material.Texture.Disabled:=false;
Material.LibMaterialName:='Tank1';
Position.AsAffineVector:=AffineVectorMAke(-26,0,-50);
tag:=15;
 with GetOrCreateDCEDynamic(player2) do begin
manager:=form1.GLDCEManager1;
Size.SetVector(1,1,2);
end;
end;

Player3:=TGLCube.CreateAsChild(form1.GLScene1.Objects);
with player3 do begin
Position.AsAffineVector:=AffineVectorMAke(5,0,-50);
CubeWidth:=2;
CubeDepth:=3;
CubeHeight:=0;
Direction.SetVector(0,0,1);
Up.SetVector(0,1,0);
Material.MaterialLibrary:=form1.GLMaterialLibrary1;
Material.Texture.Disabled:=false;
Material.LibMaterialName:='Tank1';
tag:=16;
with GetOrCreateDCEDynamic(player3) do begin
manager:=form1.GLDCEManager1;
Size.SetVector(1,1,2);
end;

end;



Pl_Pushka1:=TGLCube.CreateAsChild(Player);
with PL_Pushka1 do begin
CubeWidth:=2;
CubeDepth:=4;
CubeHeight:=0;
Direction.SetVector(0,0,1);
Up.SetVector(0,1,0);
Position.Z:=0.5;
Material.MaterialLibrary:=form1.GLMaterialLibrary1;
Material.Texture.Disabled:=false;
Material.LibMaterialName:='Pushka1';
Tag:=14;
end;


Pl_Pushka2:=TGLCube.CreateAsChild(Player2);
with PL_Pushka2 do begin
CubeWidth:=2;
CubeDepth:=4;
CubeHeight:=0;
Direction.SetVector(0,0,1);
Up.SetVector(0,1,0);
Position.Z:=0.5;
Material.MaterialLibrary:=form1.GLMaterialLibrary1;
Material.Texture.Disabled:=false;
Material.LibMaterialName:='Pushka1';
Tag:=15;
end;


Pl_Pushka3:=TGLCube.CreateAsChild(Player3);
with PL_Pushka3 do begin
CubeWidth:=2;
CubeDepth:=4;
CubeHeight:=0;
Direction.SetVector(0,0,1);
Up.SetVector(0,1,0);
Position.Z:=0.5;
Material.MaterialLibrary:=form1.GLMaterialLibrary1;
Material.Texture.Disabled:=false;
Material.LibMaterialName:='Pushka1';
Tag:=16;
end;

for i:=0 to 5 do begin
Time[i]:=20;
end;

end;

procedure TPlayerTank.DestroyPula(Pulas: TGLSphere);
begin

end;

procedure TPlayerTank.MovePlayerPula(obj: TGLCube);
var i,j: integer;
    dist: single;
begin
for j:=0 to 4 do begin
for i:=0 to PLPulaList[j].Count-1 do begin
dist:=Obj.DistanceTo(TGLSphere(PLPulaList[j][i]));
if dist<50 then begin
TGLSphere(PLPulaList[j][i]).Move(0.25);
end else begin
TGLSphere(PLPulaList[j][i]).Free;
PLPulaList[j][i]:=nil;
end;
end;
PLPulaList[j].Pack;
end;

end;


procedure TPlayerTank.MovePlayerTank(obj: TGLCube);
begin
if iskeyDown('w') then  begin
obj.Move(0.03);
  if World.MissionNumber=1 then begin
  if (Obj.Position.X<-38) or (Obj.Position.X>38) or (obj.Position.Z<-58) or (obj.Position.Z>123) then
  obj.Move(-0.03);
  end;

  if World.MissionNumber=2 then begin
  if (Obj.Position.X<-30) or (Obj.Position.X>30) or (obj.Position.Z<-26) or (obj.Position.Z>90) then
  obj.Move(-0.03);
  end;
end;

if iskeydown('d') then begin
obj.Turn(0.3);
end;

if iskeydown('a') then begin
obj.Turn(-0.3);
end;

if iskeydown('s') then begin
obj.move(-0.03);
  if World.MissionNumber=1 then begin
  if (Obj.Position.X<-38) or (Obj.Position.X>38) or (obj.Position.Z<-58) or (obj.Position.Z>123) then
  obj.Move(0.03);
  end;
  if World.MissionNumber=2 then begin
  if (Obj.Position.X<-30) or (Obj.Position.X>30) or (obj.Position.Z<-26) or (obj.Position.Z>90) then
  obj.Move(-0.03);
  end;

end;

if iskeydown(vk_Space) then
PlayerTankPli2(obj);;

end;

procedure TPlayerTank.PlayerTankPli(obj: TGLCube);
begin
if time[AutoTankNomer]>5 then begin
time[AutoTankNomer]:=0;
PLPula:=TGLSphere.CreateAsChild(form1.GLScene1.Objects);
PLPula.AbsolutePosition:=obj.AbsolutePosition;;
PLPula.AbsoluteDirection:=obj.AbsoluteDirection;
PLPula.Radius:=0.5;
PLPula.Tag:=17;
PLPulaList[AutoTankNomer].add(PLPula);
with GetOrCreateDCEDynamic(PLPula) do begin
manager:=form1.GLDCEManager1;
Size.SetVector(0.3,0.3,0.3);
end;
end;
end;

procedure TPlayerTank.PlayerTankPli2(obj: TGLCube);
begin
if time[TankNomer]>5 then begin
time[TankNomer]:=0;
PLPula:=TGLSphere.CreateAsChild(form1.GLScene1.Objects);
PLPula.AbsolutePosition:=obj.AbsolutePosition;;
PLPula.AbsoluteDirection:=obj.AbsoluteDirection;
PLPula.Radius:=0.5;
PLPula.Tag:=17;
PLPulaList[TankNomer].add(PLPula);
with GetOrCreateDCEDynamic(PLPula) do begin
manager:=form1.GLDCEManager1;
Size.SetVector(0.3,0.3,0.3);
end;
end;
end;
//end;

procedure TPlayerTank.Player_Tank_Destroy;
begin
if Live1<=0 then begin
Player.Visible:=false;
Player.Position.Y:=-400;
T1D:=true;
end;
if Live2<=0 then begin
Player2.Visible:=false;
Player2.Position.Y:=-400;
T2D:=true;
end;
if Live3<=0 then begin
Player3.Visible:=false;
Player3.Position.Y:=-400;
T3D:=true;
end;

end;


procedure TPlayerTank.Pula_RayCast;
var i,j: integer;
    dist: Single;
    pPoint: TVector;
begin
for j:=0 to 3 do begin
for i:=0 to PLPulaList[j].count-1 do begin
if (Form1.DownSizer.RayCastIntersect(TGLCube(PLPulaList[j][i]).AbsolutePosition,TGLCube(PLPulaList[j][i]).AbsoluteDirection,@pPoint,nil) or
    Form1.RightSizer.RayCastIntersect(TGLCube(PLPulaList[j][i]).AbsolutePosition,TGLCube(PLPulaList[j][i]).AbsoluteDirection,@pPoint,nil) or
    Form1.LeftSizer.RayCastIntersect(TGLCube(PLPulaList[j][i]).AbsolutePosition,TGLCube(PLPulaList[j][i]).AbsoluteDirection,@pPoint,nil) or
    Form1.UpSizer.RayCastIntersect(TGLCube(PLPulaList[j][i]).AbsolutePosition,TGLCube(PLPulaList[j][i]).AbsoluteDirection,@pPoint,nil))
    then begin
    if TGLCube(PLPulaList[j][i]).DistanceTo(pPoint)<0.4 then begin
       TGLCube(PLPulaList[j][i]).Free;
       PLPUlaLIst[j][i]:=nil;
    end;
    end;
    end;
PLPulaList[j].Pack;

end;
end;




end.
