{
  Main game unit

  Mefistofel
  24.03.2013
}
unit WorldScene;

interface

Uses
  _Geometry,
  Frost.Forms,
  Frost.GL,
  Frost.Utils,
  Frost.Renderers, 
  Frost.Engine,
  Planetoid,
  DummyCube,
  Landscape,
  TextMesh,
  RotationScript,    
  Hero,
  StarField;

type
TWorld = class(TScene)
  private
    FInterfaceCamera: TCamera;
    FCameraAngles: TVector3;   
    Hero: THero;
    // test            
    Cube: TDummyCube;
  public                      
    CameraController: TGameObject;    
    procedure OnStart; override;
    procedure OnUpdate(delta: Double); override;    
    procedure OnDestroy; override;
  end;

var
  World: TWorld;

implementation

uses
  Bullet,  
  Rocket,
  SolidParticle,
  BlowEffect;

 //World
{$REGION 'World'}
procedure TWorld.OnStart;
var
  FCameraPosition: TVector3;
  Labe: TTextMesh;
  i: Integer;  
  FCubes: array [0 .. 10] of TGameObject;
  Map: TLandscape;
begin
  World := Self;
  
  FCameraAngles   := Vector3( 0, 0, 0);                     
  FCameraPosition := Vector3( 0, 0.5, 12);
                                                        
  Hero := THero.Create(TGameObject.Create(nil, Vector3(0,0,0)));
  Map :=  TLandscape.Create(TGameObject.Create(nil, Vector3(0, 0, 0)));
//  Map.GenerateMapObjects;
  TBullet.Init;         
  TRocket.Init;         
  TParticle.Init;
  TBlowEffect.Init;
//  MainCamera.GameObject.Parent := CameraController;
//  TStarField.Create(TGameObject.Create(nil, Vector3(0, 0, 0)), 100);
//  TPlanetoid.Create(TGameObject.Create(nil, Vector3(0, 0, -10)), 1).GameObject.Layer := 2;
//  TPlanetoid.Create(TGameObject.Create(nil, Vector3(0, 20, 0)), 10);

//  Cube := TDummyCube.Create(TGameObject.Create(nil, Vector3(0, 0, 0)), 0.05);
//  Cube.GameObject.LocalScale := Vector3(0.1, 0.1, 0.1);
//  TDummyCube.Create(Hero.GameObject, 0.1);
  // TODO - Add "name" parameter for game object
  CameraController := TGameObject.Create(Hero.GameObject, Vector3(0,0,0), Rotate(45, Vector3(-1, 0, 0)) * Rotate(45, Vector3(0, 0, 1)));//, 'Camera_Controller');  
  MainCamera.GameObject.Parent := CameraController;
  MainCamera.GameObject.LocalPosition := FCameraPosition;
//  MainCamera.GameObject.LocalRotation := Rotate(-10, Vector3(0,1,0));
//  for i := 0 to 10 do
//  begin
//    Cube := TDummyCube.Create(TGameObject.Create(FCubes[i], Vector3(0, 0, 3)));
//    Cube.GameObject.AddComponent(TRotateObject.Create);
//    Cube := TDummyCube.Create(TGameObject.Create(Cube.GameObject, Vector3(0, 0, 3)));
//    Cube.GameObject.AddComponent(TRotateObject.Create);
//  end;

//  for i := 0 to 2 do
//  begin
//  Map.GenerateMapObjects;

//  Labe := TTextMesh.Create(TGameObject.Create(nil, Vector3(0, 20, 0)));
//  Labe.GameObject.Layer := 15;
//
//  Labe.Text := 'Test';
//  Labe.Align := TTextAlign(0);

  FInterfaceCamera := TCamera.Create(TGameObject.Create(nil, Vector3(0,0,10)));
  FInterfaceCamera.Mask := 1 shl 15;
  FInterfaceCamera.SetOrthoView(-2 * Window.Width / Window.Height, 2 * Window.Width / Window.Height, -2, 2);
  FInterfaceCamera.ClearMode := ClearDepth;
//  Control.SetMousePosition(Window.Width  div 2, Window.Height div 2);
end;

procedure TWorld.OnUpdate(delta: Double);
var
  i: Integer;
  SP1, SP2: TVector3;
  factor: Single;
begin
  if Control.Key[KEY_ESC] then
    Application.Close;                       
  SP1 := MainCamera.GameObject.View * MainCamera.ScreenToPoint(Control.Mouse.X, Window.Height - Control.Mouse.Y, 1);
  SP2 := MainCamera.GameObject.View * MainCamera.ScreenToPoint(Control.Mouse.X, Window.Height - Control.Mouse.Y, 0);
  factor := (- SP2.Z) / (SP1.Z - SP2.Z);
  SP1 := SP2 + (SP1 - SP2) * factor;
//  cube.GameObject.localPosition := SP1;
  Hero.Aim(SP1);


//  if (Control.Mouse.LeftButton) then
//  begin
//    FCameraAngles.X := FCameraAngles.X + Control.Mouse.DeltaY * delta;
//    FCameraAngles.Y := FCameraAngles.Y + Control.Mouse.DeltaX * delta;
//    CameraController.LocalRotation := Rotate(FCameraAngles.X, Vector3(-1, 0, 0)) * Rotate(FCameraAngles.Y, Vector3(0, 0, -1));
//  end;
//  if (Control.Key[KEY_A]) then
//    CameraController.LocalPosition := CameraController.LocalPosition + Rotate(FCameraAngles.Y, Vector3(0, 0, -1)).Transpose * Vector3(-1 * delta, 0, 0);
//  if (Control.Key[KEY_D]) then
//    CameraController.LocalPosition := CameraController.LocalPosition + Rotate(FCameraAngles.Y, Vector3(0, 0, -1)).Transpose * Vector3( 1 * delta, 0, 0);
//  if (Control.Key[KEY_W]) then
//    CameraController.LocalPosition := CameraController.LocalPosition + Rotate(FCameraAngles.Y, Vector3(0, 0, -1)).Transpose * Vector3(0, 1 * delta, 0);
//  if (Control.Key[KEY_S]) then
//    CameraController.LocalPosition := CameraController.LocalPosition + Rotate(FCameraAngles.Y, Vector3(0, 0, -1)).Transpose * Vector3(0,-1 * delta, 0);
//  Window.Title := IntToStr(Control.Mouse.DeltaX) + ' ' + IntToStr(Control.Mouse.DeltaY);
end;

procedure TWorld.OnDestroy;
begin

end;
{$ENDREGION}

end.
