unit GameScenes;

interface
uses SysUtils, DGLEngine_header, Variables, Math, GameEng, MenuButtons, OpenGL, GameObjects;

type
  TGameScene=(gsNone,gsInit,gsMenu,gsGame);

var
    FScene: TGameScene = gsNone;
    FNextScene: TGameScene;


    procedure StartScene(Scene: TGameScene);
    procedure EndScene;

    procedure StartSceneMenu;
    procedure ProcessSceneMenu;
    procedure DrawSceneMenu;
    procedure EndSceneMenu;

    procedure StartSceneGame;
    procedure ProcessSceneGame;
    procedure DrawSceneGame;
    procedure EndSceneGame;

    procedure DrawSceneInit;
    procedure ProcessSceneInit;

var InitType : byte = FIRST_START;

implementation
uses GameMap;

var InitAction,TexturesCount : integer;
    PlanetAngle : single = 0;
    CursorFrame : single = 1.0;
    AsteroidX : single = 0.0;

procedure EndScene;
begin
 case FScene of
 gsMenu    : EndSceneMenu;
 gsGame    : EndSceneGame;
 end;
end;

procedure StartScene(Scene: TGameScene);
begin
EndScene;
FScene := Scene;
 case FScene of
 gsInit    : InitAction:=0;
 gsMenu    : StartSceneMenu;
 gsGame    : StartSceneGame;
 end;
end;

procedure DrawSceneInit;
begin
Begin2D;
DrawTexture2D_Simple(tex_bg1,0,0,GetScreenResX div 3,GetScreenResY);
DrawTexture2D_Simple(tex_bg2,GetScreenResX div 3,0,GetScreenResX div 3,GetScreenResY);
DrawTexture2D_Simple(tex_bg3,(GetScreenResX div 3)*2,0,GetScreenResX div 3,GetScreenResY);
if InitType<>MAP_LOAD then
begin
DrawRectangle2D(GetScreenResX-(GetScreenResX div 3),GetScreenResY-(GetScreenResY div 8),GetScreenResX div 4,20,$00458A,150,true);
DrawRectangle2D(GetScreenResX-(GetScreenResX div 3),GetScreenResY-(GetScreenResY div 8),GetScreenResX div 4,20,$0062C4,200);
DrawRectangle2D(GetScreenResX-(GetScreenResX div 3)+4,GetScreenResY-(GetScreenResY div 8)+4,round((GetScreenResX/4-8)/TexturesCount*(InitAction div 2)),12,$00458A,227,true);
end else
DrawText2D(tex_MainFont,GetScreenResX-(GetScreenResX div 3),GetScreenResY-(GetScreenResY div 8),$00458A,'ÇÀÃÐÓÇÊÀ',255,1,22);
End2D;
end;

procedure ProcessSceneInit;
begin
case InitType of

FIRST_START:
begin
TexturesCount:=27;
 case InitAction of
 2: LoadTextureFromPackage('..\Data\Textures.dpc','space.jpg',tex_space,TexDetail);
 4:LoadTextureFromPackage('..\Data\Textures.dpc','clouds.jpg',tex_clouds,TexDetail);
 6:LoadTextureFromPackage('..\Data\Textures.dpc','planet.jpg',tex_planet,TexDetail);
 10:LoadTextureFromPackage('..\Data\Textures.dpc','alpharound.tga',tex_alpharound);
 12:LoadTextureFromPackage('..\Data\Textures.dpc','cursor.bmp',tex_cursor,0,$FFFF00);
 14:LoadTextureFromPackage('..\Data\Textures.dpc','reflection.jpg',tex_reflection,TexDetail);
 16:LoadTextureFromPackage('..\Data\Textures.dpc','asteroid.jpg',tex_asteroid,TexDetail);
 18:LoadTextureFromPackage('..\Data\Models\Player.mpk','head.jpg',tex_Head,TexDetail);
 20:LoadTextureFromPackage('..\Data\Models\Player.mpk','body.jpg',tex_Body,TexDetail);
 22:LoadTextureFromPackage('..\Data\Models\Player.mpk','legs.jpg',tex_Legs,TexDetail);
 24:begin
    ExtractFromPackage('..\Data\Models\Menu.mpk','Shatl.dmd','Temp.dmd');
    mdl_Ship:=LoadModel('temp.dmd');
    DeleteFile('temp.dmd');
    end;
 26:begin
    ExtractFromPackage('..\Data\Models\Menu.mpk','Asteroid.dmd','Temp.dmd');
    mdl_Asteroid:=LoadModel('temp.dmd');
    DeleteFile('temp.dmd');
    end;
 28:begin
    ExtractFromPackage('..\Data\Models\Player.mpk','Player_Head.dmd','Temp.dmd');
    mdl_Head:=LoadModel('temp.dmd',0);
    DeleteFile('temp.dmd');
    end;
 30:begin
    ExtractFromPackage('..\Data\Models\Player.mpk','Player_Body.dmd','Temp.dmd');
    mdl_Body:=LoadModel('temp.dmd',0);
    DeleteFile('temp.dmd');
    end;
 32:begin
    ExtractFromPackage('..\Data\Models\Player.mpk','Player_legs.dmd','Temp.dmd');
    mdl_Legs:=LoadModel('temp.dmd',0);
    DeleteFile('temp.dmd');
    end;
 34:begin
    ExtractFromPackage('..\Data\Models\Player.mpk','pistol.dmd','Temp.dmd');
    mdl_Pistol:=LoadModel('temp.dmd');
    DeleteFile('temp.dmd');
    end;
 36:LoadTextureFromPackage('..\Data\Models\Player.mpk','pistol.jpg',tex_pistol,TexDetail);
 38:LoadTextureFromPackage('..\Data\Textures.dpc','Shatl.jpg',tex_Ship,TexDetail);
 40:LoadTextureFromPackage('..\Data\Textures.dpc','Sky.jpg',tex_Sky,TexDetail);
 42:LoadTextureFromPackage('..\Data\Textures.dpc','aim.bmp',tex_Pricel,0,$FFFF00);
 44:begin
    ExtractFromPackage('..\Data\Models\Monster.mpk','Monster.dmd','Temp.dmd');
    mdl_Enemy_Run:=LoadModel('temp.dmd',0);
    DeleteFile('temp.dmd');
    end;
 46:begin
    ExtractFromPackage('..\Data\Models\Monster.mpk','Monster_attack.dmd','Temp.dmd');
    mdl_Enemy_Attack:=LoadModel('temp.dmd',0);
    DeleteFile('temp.dmd');
    end;
 48:begin
    ExtractFromPackage('..\Data\Models\Monster.mpk','Monster_die.dmd','Temp.dmd');
    mdl_Enemy_die:=LoadModel('temp.dmd',0);
    DeleteFile('temp.dmd');
    end;
 50:LoadTextureFromPackage('..\Data\Models\Monster.mpk','Texture.jpg',tex_Enemy,TexDetail);
 52:LoadTextureFromPackage('..\Data\Textures.dpc','Blood.bmp',tex_Blood,TexDetail);
 54:
 begin
   s_shot:=LoadSample('..\Data\Sounds.dpc',true,'Shot1.wav');
   s_shot1:=LoadSample('..\Data\Sounds.dpc',true,'Shot2.wav');
   s_pain:=LoadSample('..\Data\Sounds.dpc',true,'ay.wav');
   s_pain2:=LoadSample('..\Data\Sounds.dpc',true,'pain100_1.wav');
   s_hit:=LoadSample('..\Data\Sounds.dpc',true,'hit.wav');
   s_death:=LoadSample('..\Data\Sounds.dpc',true,'player_death.wav');
   s_death2:=LoadSample('..\Data\Sounds.dpc',true,'monster_death.wav');
 end;
 end;

if InitAction=TexturesCount*2+1 then StartScene(gsMenu);
end;

MAP_LOAD:
begin
if InitAction=2 then LoadMap('..\Data\Maps\Test.map');
if InitAction=3 then StartScene(gsGame);
end;

MENU_START:
begin
TexturesCount:=8;
 case InitAction of
 2: LoadTextureFromPackage('..\Data\Textures.dpc','space.jpg',tex_space,TexDetail);
 4:LoadTextureFromPackage('..\Data\Textures.dpc','clouds.jpg',tex_clouds,TexDetail);
 6:LoadTextureFromPackage('..\Data\Textures.dpc','planet.jpg',tex_planet,TexDetail);
 10:LoadTextureFromPackage('..\Data\Textures.dpc','asteroid.jpg',tex_asteroid,TexDetail);
 12:LoadTextureFromPackage('..\Data\Textures.dpc','Shatl.jpg',tex_Ship,TexDetail);
 14:begin
    ExtractFromPackage('..\Data\Models\Menu.mpk','Shatl.dmd','Temp.dmd');
    mdl_Ship:=LoadModel('temp.dmd');
    DeleteFile('temp.dmd');
    end;
 16:begin
    ExtractFromPackage('..\Data\Models\Menu.mpk','Asteroid.dmd','Temp.dmd');
    mdl_Asteroid:=LoadModel('temp.dmd');
    DeleteFile('temp.dmd');
    end;
 end;
if InitAction=TexturesCount*2+1 then StartScene(gsMenu);
end;

end;

inc(InitAction);
end;

procedure StartSceneMenu;
 procedure StartGame;
 begin
  InitType:=MAP_LOAD;
  StartScene(gsInit);
 end;
 procedure ShowAuthors;
 begin
  MenuStatus:=MENU_AUTHORS;
 end;
 procedure ShowMenu;
 begin
  MenuStatus:=MENU_MAIN;
 end;
var i : integer;
begin

for i:=0 to 7 do Player[i].Active:=false;

PlanetAngle := 0;
AsteroidX := 0.0;

NewGame:=TGLTextButton.Create;
NewGame.X:=((GetScreenResX-GetTextWidth('Íîâàÿ èãðà',1,22)) div 2)+GetScreenResX div 4;
NewGame.Y:=GetScreenResY div 2;
NewGame.Text:='Íîâàÿ èãðà';
NewGame.proc:=@StartGame;

Aouthors:=TGLTextButton.Create;
Aouthors.X:=((GetScreenResX-GetTextWidth('Àâòîðû',1,22)) div 2)+GetScreenResX div 4;
Aouthors.Y:=(GetScreenResY div 2)+40;
Aouthors.Text:='Àâòîðû';
Aouthors.proc:=@ShowAuthors;

Quit:=TGLTextButton.Create;
Quit.X:=((GetScreenResX-GetTextWidth('Âûõîä',1,22)) div 2)+GetScreenResX div 4;
Quit.Y:=(GetScreenResY div 2)+80;
Quit.Text:='Âûõîä';
Quit.proc:=@QuitEngine;

Ok:=TGLTextButton.Create;
Ok.X:=((GetScreenResX-GetTextWidth('OK',1,22)) div 2)+GetScreenResX div 4;
Ok.Y:=(GetScreenResY div 2)+210;
Ok.Text:='OK';
Ok.proc:=@ShowMenu;
end;

procedure ProcessSceneMenu;
begin
PlanetAngle:=PlanetAngle+0.5;

AsteroidX:=AsteroidX+0.0015;

CursorFrame:=CursorFrame+0.5;
if round(CursorFrame)>11 then CursorFrame:=1.0;

case MenuStatus of
 MENU_MAIN:
 begin
 NewGame.Process;
 Aouthors.Process;
 Quit.Process;
 end;
 MENU_AUTHORS: Ok.Process;
end;

end;

procedure DrawSceneMenu;
begin
Begin2D;
DrawTexture2D_Simple(tex_space,0,0,GetScreenResX,GetScreenResY);
End2D;

 glEnable(GL_LIGHT0);

BeginObj3D;
 glEnable(GL_LIGHTING);
 Position3D(-0.95,-0.4,-4);
 RotateY(PlanetAngle);
 SetTexture(tex_Planet);
 DrawSphere(1);
 glDisable(GL_LIGHTING);
 if GameDetail<2 then
 begin
 RotateY(-PlanetAngle+PlanetAngle/3);
 RotateX(PlanetAngle/6);
 RotateZ(-PlanetAngle/4);
 Color3D($FFFFFF,100,false);
 SetTexture(tex_clouds);
 DrawSphere(1.005);
 end;
EndObj3D;

BeginObj3D;
 glEnable(GL_LIGHTING);
 Position3D(0.85,0.4,-2);
 RotateY(sin(PlanetAngle/12)*5+40);
 RotateX(sin(PlanetAngle/12)*5+45);
 RotateZ(sin(PlanetAngle/16)*5);

 Scale3D(0.15);
 SetTexture(tex_ship);
 DrawModel(mdl_Ship,0,true);
 if GameDetail<2 then
 begin
 Scale3D(1.02);
 SetTexture(tex_reflection);
 EnableSphereMapping;
 Color3D($FFFFFF,150,true);
 DrawModel(mdl_Ship);
 end;
 glDisable(GL_LIGHTING);
EndObj3D;

BeginObj3D;
 Position3D(AsteroidX-1,-0.4,-2-AsteroidX/1.5*2);
 glEnable(GL_LIGHTING);
 RotateX(PlanetAngle);
 RotateY(-PlanetAngle*2);
 RotateZ(PlanetAngle/2);
 SetTexture(tex_Asteroid);
 Scale3D(0.3);
 DrawModel(mdl_Asteroid,0,true);
 glDisable(GL_LIGHTING);
EndObj3D;

Begin2D;
DrawText2D(tex_MainFont,(GetScreenResX-GetTextWidth('ÄÐÓÃÎÉ ÌÈÐ',2,24)) div 2,GetScreenResY div 4,$FF0000,'ÄÐÓÃÎÉ ÌÈÐ',155,2,24);

case MenuStatus of
 MENU_MAIN:
 begin
 NewGame.Draw;
 Aouthors.Draw;
 Quit.Draw;
 end;
 MENU_AUTHORS:
 begin
 DrawText2D(tex_MainFont,((GetScreenResX-GetTextWidth(GameVersion,0.5,18)) div 2)+GetScreenResX div 4,GetScreenResY div 2-40,$D26900,GameVersion,255,0.5,18);
 DrawText2D(tex_MainFont,((GetScreenResX-GetTextWidth('Ïðîãðàììèðîâàíèå',0.5,18)) div 2)+GetScreenResX div 4,GetScreenResY div 2,$FFFFFF,'Ïðîãðàììèðîâàíèå',255,0.5,18);
 DrawText2D(tex_MainFont,((GetScreenResX-GetTextWidth('DRON',0.5,18)) div 2)+GetScreenResX div 4,GetScreenResY div 2+20,$9D6A00,'DRON',255,0.5,18);
 DrawText2D(tex_MainFont,((GetScreenResX-GetTextWidth('Ãðàôèêà',0.5,18)) div 2)+GetScreenResX div 4,GetScreenResY div 2+40,$FFFFFF,'Ãðàôèêà',255,0.5,18);
 DrawText2D(tex_MainFont,((GetScreenResX-GetTextWidth('DRON, NinjaZ, none',0.5,18)) div 2)+GetScreenResX div 4,GetScreenResY div 2+60,$9D6A00,'DRON, NinjaZ, none',255,0.5,18);
 DrawText2D(tex_MainFont,((GetScreenResX-GetTextWidth('Çâóêè',0.5,18)) div 2)+GetScreenResX div 4,GetScreenResY div 2+80,$FFFFFF,'Çâóêè',255,0.5,18);
 DrawText2D(tex_MainFont,((GetScreenResX-GetTextWidth('HALF-LIFE',0.5,18)) div 2)+GetScreenResX div 4,GetScreenResY div 2+100,$9D6A00,'HALF-LIFE',255,0.5,18);
 DrawText2D(tex_MainFont,((GetScreenResX-GetTextWidth('Ìóçûêà',0.5,18)) div 2)+GetScreenResX div 4,GetScreenResY div 2+120,$FFFFFF,'Ìóçûêà',255,0.5,18);
 DrawText2D(tex_MainFont,((GetScreenResX-GetTextWidth('Mortal Combat',0.5,18)) div 2)+GetScreenResX div 4,GetScreenResY div 2+140,$9D6A00,'Mortal Combat',255,0.5,18);
 DrawText2D(tex_MainFont,((GetScreenResX-GetTextWidth('Èãðà èñïîëüçóåò DGLEngine',0.5,18)) div 2)+GetScreenResX div 4,GetScreenResY div 2+160,$FFFFFF,'Èãðà èñïîëüçóåò DGLEngine',255,0.5,18);
 DrawText2D(tex_MainFont,((GetScreenResX-GetTextWidth('http://dronprogs.mirgames.ru',0.5,18)) div 2)+GetScreenResX div 4,GetScreenResY div 2+180,$FFFFFF,'http://dronprogs.mirgames.ru',255,0.5,18);
 Ok.Draw;
 end;
end;
  DrawSprite2D_Tile(tex_Cursor,GetMousePos.X,GetMousePos.Y,128,64,20,20,6,2,round(CursorFrame),0,255,$9D6A00);
End2D;
end;

procedure EndSceneMenu;
begin
FreeTexture(tex_space);
FreeTexture(tex_clouds);
FreeTexture(tex_planet);
FreeTexture(tex_title);
FreeTexture(tex_asteroid);
FreeTexture(tex_Ship);

FreeModel(mdl_Ship);
FreeModel(mdl_Asteroid);

 NewGame.Free;
 Aouthors.Free;
 Quit.Free;
 Ok.Free;
end;

procedure StartSceneGame;
begin
PlayerDead:=false;
cameratyp:=1;
  Player[0].LegsFrame:=0;
  Player[0].Angle:=0;
  Player[0].Health:=100;
  Player[0].Points:=0;
end;

procedure ProcessSceneGame;
begin
if IsKeyPressed(key_Escape) then
 begin
 InitType:=MENU_START;
 StartScene(gsInit);
 end;
if not PlayerDead then ProcessGame else
begin
PlanetAngle:=PlanetAngle+2;
GameCamera.Eye.X:=Player[0].X+cos(degtorad(PlanetAngle))*2;
GameCamera.Eye.Y:=Player[0].Y+sin(degtorad(PlanetAngle))*2-6;
GameCamera.Eye.Z:=6;

GameCamera.Center.X:=Player[0].X;
GameCamera.Center.Y:=Player[0].Y;
GameCamera.Center.Z:=2.5;
end;
end;

procedure DrawSceneGame;
begin

DrawGame;
if PlayerDead  then
begin
Begin2D;
DrawText2D(tex_MainFont,(GetScreenResX-GetTextWidth('ÒÛ ÑÄÎÕ! ÆÌÈ "ESC"',1,22)) div 2,GetScreenResY div 2,$0000FF,'ÒÛ ÑÄÎÕ! ÆÌÈ "ESC"',155,1,22);
End2D;

end;
end;

procedure EndSceneGame;
begin
MonsterScount:=0;
BulletsCount:=0;
TurelsCount:=0;
BloodCount:=0;

FreeMap;

end;

end.
