unit _World;

interface

Uses
  QuadEngine,
  _Scene,
  _Forms,
  _Geometry,
  _SysUtils,
  _HexMap,
  _Effects,
  _Resources;

const
  TURN_TIME = 4; //in seconds

type

TWorld = class(TBasicScene)
  private
    FQuad: IQuadRender;
    FMap: THexMap;     
    FEffects: TEffects;
    FSelectedUnit: TBaseUnit;
    EndTurnButtonPosition: TPoint;
    EndTurnButtonSize: TPoint;
    EndTurnHover: Boolean;
  public                                         
    procedure Win;
    Procedure Lose;    
    constructor Create(QuadEngine: IQuadRender);
    destructor Destroy; override;   
    procedure StartLevel(FileName: String; Tip: Byte);
    procedure Render; override;
    procedure Update(dt: single); override;
    procedure Control; override;
  end;

var
  World: TWorld;

implementation
//  World
{$REGION 'World'}
constructor TWorld.Create;
begin
  FQuad:= QuadEngine;            
  FEffects:= TEffects.Create(FQuad);
  FQuad.SetBlendMode(qbmSrcAlpha);
  FMap:= THexMap.Create;
  EndTurnButtonPosition:= Point(Window.Width - 77, Window.Height - 82);
  EndTurnButtonSize:= Point(TILE_SIZE, TILE_SIZE);
  EndTurnHover:= false;
end;

destructor TWorld.Destroy;
begin
  Inherited;
end;

procedure TWorld.StartLevel(FileName: String; Tip: Byte);
begin
  case Tip of
    0:FMap.LoadMap(FileName);
    1:FMap.LoadMap(FileName, GMPlayerVsPlayer);
    2:FMap.LoadMap(FileName, GMComputerVsComputer);
  end;
  Manager.SetScene(Self);
end;

procedure TWorld.Win();
begin
  if Random(2) = 0 then
    Manager.SetScene(
      TLogo.Create(
        Menu,
        FQuad,
        Resources.Win1Texture,
        10))
  else
    Manager.SetScene(
      TLogo.Create(Menu,
        FQuad,
        Resources.Win2Texture,
        10));
end;

procedure TWorld.Lose();
begin
  if Random(2) = 0 then
    Manager.SetScene(
      TLogo.Create(Menu,
        FQuad,
        Resources.Lose1Texture,
        10))
  else
    Manager.SetScene(
      TLogo.Create(Menu,
        FQuad,
        Resources.Lose2Texture,
        10));
end;

procedure TWorld.Control;
var
  TilePosition: TPoint;
begin        
  TilePosition:= FMap.GetTileCoords(Point(Window.Mouse.x, Window.Mouse.y));
//  Window.Title:= IntToStr(TilePosition.X) + ' ' + IntToStr(TilePosition.Y) ;
  EndTurnHover:= (TilePosition = Point(21,11));
  if (Vector2(Window.Mouse.x, Window.Mouse.y).InRectangle(EndTurnButtonPosition, EndTurnButtonPosition + EndTurnButtonSize))
     and (Window.Mouse.mbl) then
    begin
      Window.Mouse.mbl:= false;
      if Not FMap.ComputerTurn then
        FMap.EndTurn();
    end;
  FMap.ShowInfo(TilePosition);
  if Window.Mouse.mbl then
    begin
      Window.Mouse.mbl:= false;
      FSelectedUnit:= FMap.SelectTile(TilePosition);
    end;
  if Window.Mouse.mbr then
    if FSelectedUnit <> nil then
      begin
        Window.Mouse.mbr:= false;
        FSelectedUnit:= FMap.CommandToTile(FSelectedUnit, TilePosition);
      end;
  if FSelectedUnit <> nil then
    FSelectedUnit.ShowFireRadius;

  if Window.key[KEY_TILDA] or Window.key[KEY_ESC] then  //esc
  begin
    Window.key[KEY_TILDA]:= false;
    Window.key[KEY_ESC]:= false;
    //Window.Close;
    Menu.ShowMenu;
  end;
end;


procedure TWorld.Render;
var
  i:Word;
begin
  FQuad.Clear($ff000000);
  FMap.Render;          
  FEffects.Render;
  Resources.EndTurnTexture.Draw(
    EndTurnButtonPosition.X,
    EndTurnButtonPosition.Y,
    SideColor(FMap.Side),
    integer(EndTurnHover));
end;

procedure TWorld.Update(dt: Single);
begin
  FMap.Update(dt);
  FEffects.Update(dt);
end;

end.
