unit _var;
//----------------------------------------------------------------------------------------------------------------------
// Alex 'Shirson' Nevskiy 2012-08
//----------------------------------------------------------------------------------------------------------------------
interface
uses Windows, eXgine;

type
  TGameState=(gsMenu, gsHelp, gsInfo, gsGame, gsNone);

//  TInput=record
//    Up,Down,Left,Right,B1,B2,Esc,Ent,Pause:boolean;
//    RawMouseX, RawMouseY : single;
//    MouseX, MouseY: integer;
//  end;

  TKeysInput = record
    Pressed:boolean;
    Delay:integer;
  end;

  TMouseInput = record
    RawX, RawY : single;
    X, Y: integer;
    ShowCursor:boolean;
    
  end;

  TGameInput = record
    Keys: array[0..255] of TKeysInput;
    Mouse: TMouseInput;
  end;

  TKeySet = Set of byte;

const
  GUI_W=80;
  GUI_H=25;
  ViewW=45;
  ViewH=25;  
  CharW=10;
  CharH=14;
  PixelP=CharH/CharW;
  PixelW=CharW/CharH;
  AimDist=30;
  FxDelay=8;
  ShotLineDelay=4;
  enMoveDelay=60;
  BonusTime=1800;

  ClickWait=100;
  MouseWait=200;
  cDelay=8;

  keyUP =    [Ord('W'), VK_UP];
  keyRight = [Ord('D'), VK_Right];
  keyLeft =  [Ord('A'), VK_Left];
  keyDown =  [Ord('S'), VK_Down];
  keyEnter = [Ord(' '), VK_Return];
  key1     = [Ord('1')];
  key2     = [Ord('2')];
  key3     = [Ord('3')];
  key4     = [Ord('4')];
  keyEscape= [          VK_Escape];
  keyHelp=   [          VK_F1];  

  keyLMB = [VK_LBUTTON];
  keyRMB = [VK_RBUTTON];
  
  //sounds
  MaxSounds=15;
  s_Distort=12;
  s_Select=1;
  s_AmmoPickUp=2;
  s_LifePickUp=3;
  s_PickUpArmor=4;
  s_DryShot=5;
  s_PistolShot=6;
  s_ChangeWeapon=7;
  s_ShotGun=8;
  s_RocketL=9;
  s_Explo=10;
  s_Melee=11;
  s_ManHit=0;
  s_dp=13;

var
  LevelW:integer;
  LevelH:integer;
  GameInput:TGameInput;
  GameState:TGameState=gsMenu;
  HideMouseDelay:integer;
  MouseDelay:integer=cDelay;
  KeyState:  TKeyboardState;
  SoundsAr: array[0..MaxSounds] of TSound;

function  isControl(Key: TKeySet): boolean;
procedure GetMouseState;
procedure GetKeysState;
procedure CloseGame;
procedure PlaySnd(ID: Integer; Dist:single; Loop:Boolean=false);

implementation

var OldMouseX, OldMouseY: integer;
    Delay:integer=cDelay;

//----------------------------------------------------------------------------------------------------------------------

procedure PlaySnd(ID: Integer; Dist:single; Loop:Boolean=false);
begin
  snd.Play(SoundsAr[ID],0, 0, 0, Loop);
  //snd.Volume(100);
  snd.Channel_Pos(SoundsAr[ID], 0, 0, Dist*4.5);
end;

//----------------------------------------------------------------------------------------------------------------------

function isControl(Key: TKeySet): boolean;
var //Ticks:integer;
    a:integer;
begin            
  result:=false;
  for a:=0 to High(KeyState) do
    if a in keyUP + keyRight + keyLeft + keyDown then
    begin
      if (a in Key) and (KeyState[a] and 128 <>0) and (delay=1) then
        result:=true;
    end
    else
      if (a in Key) and (KeyState[a] and 128 <>0) then
        result:=true;

  //result:= GameInput.Keys[Key].Pressed;
  //exit;
//  result:=false;
//  Ticks:=ex.GetTime;
//  if GameInput.Keys[Key].Pressed and (Ticks - GameInput.Keys[Key].Delay > ClickWait) then
//  begin
//    GameInput.Keys[Key].Delay:=Ticks;
//    result:=true;
//  end;
end;

//----------------------------------------------------------------------------------------------------------------------

procedure GetMouseState;
begin
  GameInput.Mouse.RawX:= GameInput.Mouse.RawX + inp.MDelta.X;
  GameInput.Mouse.RawY:= GameInput.Mouse.RawY + inp.MDelta.Y;
  if GameInput.Mouse.RawX < 0 then GameInput.Mouse.RawX:= 0;
  if GameInput.Mouse.RawY < 0 then GameInput.Mouse.RawY:= 0;
//  if GameInput.Mouse.RawX >= GUI_W * CharW then GameInput.Mouse.RawX:= (GUI_W-1) * CharW;
//  if GameInput.Mouse.RawY >= GUI_H * CharH then GameInput.Mouse.RawY:= (GUI_H-1) * CharH;
  if GameInput.Mouse.RawX >= VIewW * CharW then GameInput.Mouse.RawX:= (ViewW-1) * CharW;
  if GameInput.Mouse.RawY >= ViewH * CharH then GameInput.Mouse.RawY:= (VIewH-1) * CharH;
  GameInput.Mouse.X := Trunc(GameInput.Mouse.RawX / CharW);
  GameInput.Mouse.Y := Trunc(GameInput.Mouse.RawY / CharH);
  if (OldMouseX=GameInput.Mouse.X) and (OldMouseY=GameInput.Mouse.Y) then
    dec(HideMouseDelay)
  else
    HideMouseDelay:=MouseWait;
  if HideMouseDelay<0 then HideMouseDelay:=0;
  GameInput.Mouse.ShowCursor:=HideMouseDelay<>0;
  OldMouseX:=GameInput.Mouse.X;
  OldMouseY:=GameInput.Mouse.Y;
end;

//----------------------------------------------------------------------------------------------------------------------

procedure GetKeysState;
begin
  GetKeyboardState(KeyState);
  dec(delay);
  if delay=0 then delay:=cDelay;
//  s:='';
//  for Idx_Key:=0 to 255 do
//  begin
//    GameInput.Keys[Idx_key].Pressed:=(GetKeyState(Idx_key) and 128)<> 0;
//    if (Idx_Key=keyUp) and GameInput.Keys[Idx_key].Pressed then s:=s+' U ';
//    if (Idx_Key=keyDown)and GameInput.Keys[Idx_key].Pressed then s:=s+' D ';
//    if (Idx_Key=keyLeft)and GameInput.Keys[Idx_key].Pressed then s:=s+' L ';
//    if (Idx_Key=keyRight)and GameInput.Keys[Idx_key].Pressed then s:=s+' R ';
//  end;
//  wnd.Caption(pchar('ASCII Caverns ['+s+']'));
end;

//----------------------------------------------------------------------------------------------------------------------

procedure CloseGame;
begin
  ex.Quit;
end;

//----------------------------------------------------------------------------------------------------------------------

end.
