unit u_Game;

interface
uses
  Omega, Classes, SysUtils;

type
  TScene = ( sMenu, sGame, sLogo );
  Rect = Record
  Rect: OERect;
  Tex: PTexture;
  Draw: Boolean;
  TType:byte;
  end;
  Button = Record
  Rect: OERect;
  Alpha: integer;
  end;
  TPlayer = Record
  X, Y: Single;
  Texture, Main_Legs: PTexture;
  Legs: array[0..6] of PTexture;
  Go: boolean;
  Angle, Frame, damage: integer;
  end;
  TEnemy = Record
  X, Y: Single;
  Texture, Main_Legs: PTexture;
  Legs: array[0..6] of PTexture;
  Go: boolean;
  Angle, Frame, damage: integer;
  end;
  TBullet = Record
  X, Y, Speed, Angle, EnableTimer: Single;
  Texture: PTexture;
  Enable: Boolean;
  end;
  TStone = Record
  X, Y: integer;
  Texture: PTexture;
  end;
  TPart = Record
  Texture: PTexture;
  X, Y, Alpha, Angle: single;
  Enable: boolean;
  end;

var
  Enemys: array[0..49] of TEnemy;
  Bullets: array[0..5] of TBullet;
  Player: TPlayer;
  Camera : PCamera2D;
  F : TFileStream;
  Font : PFont2D;
  Timer: POTimer;
  Scene: TScene;
  Grid: array[0..31, 0..23] of Rect;
  stones: array[0..49] of TStone;
  parts: array[0..10] of TPart;
  Buttons: array[0..6] of Button;
  tile1, tile2, tile3, tile4, tile5, tile6, tile7,
  cur, back, select, glock, weapon, weapon2, ttenemy: PTexture;
  Number, n, k: integer;
  Main_Timer: POTimer;
  Help, Setka: Boolean;
  procedure Game_Process;
  procedure Game_Paint;
  procedure Frame_Legs;
  {procedure Lavel_Save;
  procedure Lavel_Open;}

implementation
uses
  u_Init;

procedure Frame_Legs;
begin
  if Player.Go then
  begin
    player.Frame := player.Frame + 1;
    if player.Frame = 7 then player.Frame := 1;
    player.Main_Legs := player.legs[player.frame];
  end;
end;

procedure Game_Process;
var
i, j, r : integer;
begin
 for i := 0 to 10 do
 begin
   if parts[i].enable then parts[i].alpha := parts[i].alpha - 1.5;
   if parts[i].alpha <= 0 then parts[i].enable := false;
 end;
 if player.Go then
 begin
  n := n - 1;
  if n = 0 then
  begin
    if k = 10 then k := 0;
    n := 50;
    parts[k].Texture := player.Main_Legs;
    parts[k].x := player.X;
    parts[k].y := player.Y;
    parts[k].alpha := 200;
    parts[k].Enable := true;
    parts[k].Angle := player.Angle;
    k := k + 1;
  end;
  end;
  player.angle := M_FindAngle( round(player.x), round(player.y), round(Camera^.X) + Mouse_X - 12, round(Camera^.Y) + Mouse_Y - 12);
  if Key_Down( K_A ) then
  begin
    player.X := player.X - 0.45;
  end;
  if Key_Down( K_D ) then
  begin
    player.X := player.X + 0.45;
  end;
  if Key_Down( K_W ) then
  begin
    player.Y := player.Y - 0.45;
  end;
  if Key_Down( K_S ) then
  begin
    player.Y := player.Y + 0.45;
  end;
  if Key_Down( K_S ) or Key_Down( K_W )
  or Key_Down( K_A ) or Key_Down( K_D )
  then player.Go := true else player.Go := false;
  if Key_Down( K_ESCAPE ) then
  begin
    Scene := sMenu;
    player.X := 380;
    player.Y := 280;
  end;
  if Key_Press( K_1 ) then
  begin
   weapon := glock;
   player.damage := 35;
  end;
  if Key_Press( K_2 ) then
  begin
    weapon := weapon2;
    player.damage := 50;
  end;
  if Mouse_Press( M_LBUTTON ) then
  begin
    {player.X := player.X - M_Cos( player.Angle );
    player.Y := player.Y - M_Sin( player.Angle );}
    Bullets[number].Texture := Bullets[0].Texture;
    Bullets[number].EnableTimer := 8;
    Bullets[number].X := player.X + 15;
    Bullets[number].Y := player.Y + 15;
    Bullets[number].Enable := True;
    Bullets[number].Speed := 3;
    Bullets[number].Angle := M_FindAngle( round(player.x), round(player.y), round(Camera^.X) + Mouse_X - 12, round(Camera^.Y) + Mouse_Y - 12);
    number := number + 1;
    if number = 6 then number := 0;
  end;
  for i := 0 to 6 do
  begin
    if Bullets[i].Enable then
    begin
      if Bullets[i].EnableTimer > 0 then Bullets[i].EnableTimer := Bullets[i].EnableTimer - 1;
      Bullets[i].X := Bullets[i].X - M_Cos( round(Bullets[i].Angle) ) * Bullets[i].Speed;
      Bullets[i].Y := Bullets[i].Y - M_Sin( round(Bullets[i].Angle) ) * Bullets[i].Speed;
    end;
  end;
  Camera^.X := player.X - 380;
  Camera^.Y := player.Y - 280;
  if player.X < 390 then player.X := 391;
  if player.X > 810 then player.X := 809;
  if player.Y > 910 then player.Y := 909;
  if player.Y < 290 then player.Y := 291;
  for i := 0 to 49 do
  begin
    enemys[i].Angle := M_FindAngle( round(player.x), round(player.y), round(enemys[i].X), round(enemys[i].Y));
  end;
end;

procedure Game_Paint;
var f, i, j: integer;
begin
  STile_Draw( Tile2, 0, 0, 40, 40, 30, 30, 255, 1 );
  for i := 0 to 29 do
  begin
    SSprite_Draw( stones[i].Texture, stones[i].X, stones[i].Y, 40, 40, 255, 0, 1 );
  end;
  for i := 0 to 49 do
  begin
    SSprite_Draw( enemys[i].Texture, enemys[i].X, enemys[i].Y, 40, 40, 255, enemys[i].Angle - 155, 1 );
  end;
  for i := 0 to 5 do
  begin
    if (Bullets[i].Enable) and (Bullets[i].EnableTimer = 0) then SSprite_Draw( Bullets[i].Texture, Bullets[i].X, Bullets[i].Y, 6, 11, 255, Bullets[i].Angle - 90, 1 );
  end;
  for i := 0 to 10 do
  begin
    FX_Blur_SetTex( parts[i].Texture );
    if (parts[i].Enable) then SSprite_Draw( parts[i].Texture, round(parts[i].x) + 1, round(parts[i].y) + 1, 38, 38, round(parts[i].alpha), round(parts[i].Angle) - 90, FX_DEFAULT );
  end;
  SSprite_Draw( player.Main_Legs, player.X - 1, player.Y - 1, 42, 42, 255, player.Angle - 90, FX_DEFAULT );
  SSprite_Draw( player.Texture, player.X, player.Y, 40, 40, 255, player.Angle, 1 );
  SSprite_Draw( weapon, player.X, player.Y, 40, 40, 255, player.Angle - 90, 1 );
  SSprite_Draw( cur, Camera^.X + Mouse_X, Camera^.Y + Mouse_Y, 32, 32, 255, 0, 1 );
end;

end.

