unit MP2;

interface

uses
  objet,
  essai_map,
  gameplay,
  System.Drawing,
  System.Windows.Forms,
  Microsoft.DirectX,
  Microsoft.DirectX.DirectInput,
  Microsoft.DirectX.Direct3D;

type
  TSurface = record
    h, b, d, g : char;
  end;

  TVecteur = record
    x, y , cour : real;

  end;
  TMP = class
    private
      i,j : double;
      surface : TSurface;

    public
      cboost : double;
      vc, vd : TVecteur;
      constructor create;
      procedure new_coordonee(  var move : TMove ;  var Touches:TTouches ; vecteur: array of string);
      procedure get_surface ( move : TMove ; vecteur: array of string);
      procedure deplacement(var  move : TMove ; Touches:TTouches );
      procedure colision(var move : TMove; stockx, stockz : real; surface : TSurface);
      procedure reset(var move : TMove);
      procedure saut(var move : TMove);
      procedure boost;

  end;

implementation

constructor TMP.create;
begin
  i:=0;
  inherited;
end;

procedure TMP.new_coordonee(var  move : TMove ; var Touches:TTouches ; vecteur: array of string );
var
  vt: TVecteur;
begin
  if (move.x <= 0.5) or (move.x >= TMap.tailleX-0.5 ) or (move.z <= 0.5) or (move.z >= TMap.tailleZ-0.5 )or (move.y < 0) then
    move.y := move.y-0.2*cboost
  else
  begin
     if  (move.x <= 0.5) or (move.x >= TMap.tailleX-0.5 ) or (move.z <= 0.5) or (move.z >= TMap.tailleZ-0.5 )or (move.y < 0) then
        move.y := move.y-0.20*cboost
  else
    get_surface(move , vecteur);
    deplacement(move, touches);

  end;
end;



procedure TMP.get_surface ( move : TMove ; vecteur: array of string);
begin
  if  (vecteur[trunc(move.z+0.5)][trunc(1+move.x+0.20)] = '2') or (vecteur[trunc(move.z+0.5)][trunc(1+move.x-0.20)] = '2') then
    surface.h := '2'
  else if (vecteur[trunc(move.z+0.5)][trunc(1+move.x+0.20)] = '1') or (vecteur[trunc(move.z+0.5)][trunc(1+move.x-0.20)] = '1') then
    surface.h :='1'
  else
    begin
      surface.h :='0';
    end;


  if  (vecteur[trunc(move.z-0.5)][trunc(1+move.x+0.20)] = '2') or (vecteur[trunc(move.z-0.5)][trunc(1+move.x-0.20)] = '2') then
    surface.b := '2'
  else if (vecteur[trunc(move.z-0.5)][trunc(1+move.x+0.20)] = '1') or (vecteur[trunc(move.z-0.5)][trunc(1+move.x-0.20)] = '1') then
    surface.b :='1'
  else
    begin
    surface.b :='0';
    end;


  if  (vecteur[trunc(move.z-0.20)][trunc(1+move.x+0.5)] = '2') or (vecteur[trunc(move.z+0.20)][trunc(1+move.x+0.5)] = '2') then
    surface.d := '2'
  else if (vecteur[trunc(move.z-0.20)][trunc(1+move.x+0.5)] = '2') or (vecteur[trunc(move.z+0.20)][trunc(1+move.x+0.5)] = '1') then
    surface.d :='1'
  else
    begin
    surface.d :='0';
    end;


  if  (vecteur[trunc(move.z-0.20)][trunc(1+move.x-0.5)] = '2') or (vecteur[trunc(move.z+0.20)][trunc(1+move.x-0.5)] = '2') then
    surface.g := '2'
  else if (vecteur[trunc(move.z-0.20)][trunc(1+move.x-0.5)] = '1') or (vecteur[trunc(move.z+0.20)][trunc(1+move.x-0.5)] = '1') then
    surface.g :='1'
  else
    begin
    surface.g :='0';
    end;
end;

procedure TMP.deplacement(var  move : TMove ;  Touches:TTouches  );
  var
    stockx, stockz : real;
begin
    stockx := move.x;
    stockz := move.z;
    if Touches.Dr then
    begin
      move.angle := (move.angle - 0.05);
      if move.angle < 0 then
        move.angle := 2*math.pi - 0.05


    end;

    if Touches.Ga then
    begin
      move.angle := (move.angle + 0.05);
      if move.angle >= 2*math.pi then
        move.angle := 0.05
    end;

    if Touches.Ha then
    begin
      move.x := move.x - 0.1*cos(move.angle)*cboost;
      move.z := move.z - 0.1*sin(move.angle)*cboost;
    end;

    //if Touches.Ba then
    //begin
    //  move.x := move.x + 0.1*cos(move.angle)*boost;
    //  move.z := move.z + 0.1*sin(move.angle)*boost;
    //end;
    if Touches.saut or move.saut then
      saut(move);

    Self.colision(move, stockx, stockz, surface );
    if Touches.R then
      reset(move);
    if touches.boost or move.boost then
      boost
    else
    begin
      j := 1;
      cboost:=1;
    end;

end;


procedure TMP.colision(var move : TMove; stockx, stockz : real; surface : TSurface);
begin


  if (surface.h = '2')  and (move.angle > math.pi) then
    move.z := stockz;

  if (surface.b = '2') and (move.angle < math.pi) then
    move.z := stockz;

  if (surface.d = '2') and ((move.angle>0.5*math.pi) and (move.angle < 1.5*math.pi)) then
    move.x := stockx;

  if (surface.g = '2') and ((move.angle<0.5*math.pi) or (move.angle > 1.5*math.pi)) then
    move.x := stockx;

  if (surface.g = '0') and (surface.d = '0') and (surface.h = '0') and (surface.b = '0') then
  begin
    if move.y <= 0 then
    begin
    move.x := stockx;
    move.z := stockz;
    move.y := move.y-0.1;
    end;
  end;


end;

procedure TMP.reset(var move : TMove);
begin
  move.x := 3;
  move.y := 0;
  move.z := 3;
  move.angle := 1.57;
end;

procedure TMP.saut(var move : TMove);
begin
  move.saut := true;
  move.y := 3*sin(0.1*i);
  i:= i+0.5;
  if move.y < 0 then
  begin
    move.saut := false;
    move.y := 0;
    self.i := 0;
    if  (surface.d = '0') and (surface.b >= '0') and (surface.h >= '0') and (surface.g >= '0') then
      move.y := move.y-0.2;

  end;
end;

{procedure TMP.boost(var move : TMove;boost : boolean);
begin
  move.boost := true;
  cboost := 1+sin(j);
  if boost then

    if j<math.pi/2 then
      j := j+0.1
  else
  begin
    if j>0 then
      j := j-0.1
    else
    begin
      j:= 0;
      move.boost := false;
    end;

  end;
end;}

procedure TMP.boost;
begin
  cboost := 3-2/j;
  j:=j+0.01;
end;

end.
