Advertisement
Guest User

NeoAxis Turret Demo Source

a guest
Apr 11th, 2021
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 13.52 KB | None | 0 0
  1. // Copyright (C) NeoAxis Group Ltd. This is part of NeoAxis 3D Engine SDK.
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Text;
  5. using System.Diagnostics;
  6. using Engine;
  7. using Engine.Renderer;
  8. using Engine.MathEx;
  9. using Engine.EntitySystem;
  10. using Engine.MapSystem;
  11. using Engine.UISystem;
  12. using Engine.PhysicsSystem;
  13. using ProjectCommon;
  14. using ProjectEntities;
  15.  
  16. namespace Game
  17. {
  18.     /// <summary>
  19.     /// Defines a game window for TurretDemo.
  20.     /// </summary>
  21.     public class TurretDemoGameWindow : ActionGameWindow
  22.     {
  23.         float remainingTimeForCreateEnemy;
  24.         float gameTime;
  25.         int level;
  26.         int remainingCount;
  27.         int remainingCreateCount;
  28.         float createInterval;
  29.  
  30.         Vec3 turretCreatePosition;
  31.         Quat turretCreateRotation;
  32.  
  33.         Font screenFont;
  34.  
  35.         List<MapObject> enemySpawnPoints = new List<MapObject>();
  36.  
  37.         //
  38.  
  39.         protected override void OnAttach()
  40.         {
  41.             base.OnAttach();
  42.  
  43.             //World serialized data
  44.             {
  45.                 if( World.Instance.GetCustomSerializationValue( "remainingTimeForCreateEnemy" ) != null )
  46.                 {
  47.                     remainingTimeForCreateEnemy = (float)World.Instance.GetCustomSerializationValue(
  48.                         "remainingTimeForCreateEnemy" );
  49.                 }
  50.  
  51.                 if( World.Instance.GetCustomSerializationValue( "gameTime" ) != null )
  52.                     gameTime = (float)World.Instance.GetCustomSerializationValue( "gameTime" );
  53.  
  54.                 if( World.Instance.GetCustomSerializationValue( "level" ) != null )
  55.                     level = (int)World.Instance.GetCustomSerializationValue( "level" );
  56.  
  57.                 if( World.Instance.GetCustomSerializationValue( "remainingCount" ) != null )
  58.                     remainingCount = (int)World.Instance.GetCustomSerializationValue( "remainingCount" );
  59.  
  60.                 if( World.Instance.GetCustomSerializationValue( "remainingCreateCount" ) != null )
  61.                 {
  62.                     remainingCreateCount = (int)World.Instance.GetCustomSerializationValue(
  63.                         "remainingCreateCount" );
  64.                 }
  65.  
  66.                 if( World.Instance.GetCustomSerializationValue( "createInterval" ) != null )
  67.                 {
  68.                     createInterval = (float)World.Instance.GetCustomSerializationValue(
  69.                         "createInterval" );
  70.                 }
  71.             }
  72.  
  73.             GameGuiObject billboard = Entities.Instance.GetByName( "HangingBillboard_Game" ) as GameGuiObject;
  74.             billboard.Damage += GameBillboard_Damage;
  75.  
  76.             //find enemy spawn points
  77.             foreach( Entity entity in Map.Instance.Children )
  78.             {
  79.                 MapObject point = entity as MapObject;
  80.  
  81.                 if( !string.IsNullOrEmpty( entity.GetTag( "TextUserData" ) ) )
  82.                 {
  83.                     if( point != null && point.Type.Name == "HelperPoint" )
  84.                         enemySpawnPoints.Add( point );
  85.                 }
  86.             }
  87.  
  88.             screenFont = FontManager.Instance.LoadFont( "Default", .05f );
  89.  
  90.             if( level == 0 )//for world serialization
  91.                 level = 1;
  92.  
  93.             //get turret start position
  94.             Turret turret = (Turret)Entities.Instance.GetByName( "Turret_Game" );
  95.             if( turret != null )
  96.             {
  97.                 turretCreatePosition = turret.Position;
  98.                 turretCreateRotation = turret.Rotation;
  99.             }
  100.  
  101.             UpdateVictoryObjects( false );
  102.  
  103.             //for world serialization
  104.             foreach( Entity entity in Map.Instance.Children )
  105.             {
  106.                 if( entity.IsSetForDeletion )
  107.                     continue;
  108.  
  109.                 Unit unit = entity as Unit;
  110.                 if( unit == null )
  111.                     continue;
  112.  
  113.                 if( unit is PlayerCharacter )
  114.                     continue;
  115.  
  116.                 if( ( unit.Intellect as AI ) != null || unit is Aircraft )
  117.                 {
  118.                     unit.ViewRadius = 300;
  119.                     unit.Destroying += EnemyUnitDestroying;
  120.                     unit.Tick += EnemyUnitTick;
  121.                 }
  122.             }
  123.  
  124.             //for world serialization
  125.             if( gameTime != 0 )
  126.                 MainPlayerUnitSubscribeToDestroying();
  127.  
  128.             //for world serialization
  129.             if( gameTime >= 8 )
  130.                 GameMusic.MusicPlay( "Sounds/Music/Action.ogg", true );
  131.  
  132.             Map.Instance.Tick += Map_Tick;
  133.         }
  134.  
  135.         protected override void OnDetach()
  136.         {
  137.             if( Map.Instance != null )
  138.                 Map.Instance.Tick += Map_Tick;
  139.  
  140.             GameStop( false );
  141.  
  142.             base.OnDetach();
  143.         }
  144.  
  145.         public override void OnBeforeWorldSave()
  146.         {
  147.             base.OnBeforeWorldSave();
  148.  
  149.             //World serialized data
  150.             World.Instance.ClearAllCustomSerializationValues();
  151.             World.Instance.SetCustomSerializationValue( "remainingTimeForCreateEnemy",
  152.                 remainingTimeForCreateEnemy );
  153.             World.Instance.SetCustomSerializationValue( "gameTime", gameTime );
  154.             World.Instance.SetCustomSerializationValue( "level", level );
  155.             World.Instance.SetCustomSerializationValue( "remainingCount", remainingCount );
  156.             World.Instance.SetCustomSerializationValue( "remainingCreateCount", remainingCreateCount );
  157.             World.Instance.SetCustomSerializationValue( "createInterval", createInterval );
  158.         }
  159.  
  160.         protected override void OnRender()
  161.         {
  162.             base.OnRender();
  163.  
  164.             if( EntitySystemWorld.Instance.Simulation )
  165.             {
  166.                 string text = "";
  167.                 float alpha = 0;
  168.  
  169.                 if( gameTime >= 2 && gameTime < 4 )
  170.                     text = ">     3     <";
  171.                 else if( gameTime >= 4 && gameTime < 6 )
  172.                     text = ">   2   <";
  173.                 else if( gameTime >= 6 && gameTime < 8 )
  174.                 {
  175.                     text = "> 1 <";
  176.                     alpha = ( gameTime - 6 ) / 2;
  177.                 }
  178.                 else if( gameTime >= 8 && gameTime < 10 )
  179.                     text = "FIGHT";
  180.  
  181.                 if( alpha != 0 )
  182.                 {
  183.                     EngineApp.Instance.ScreenGuiRenderer.AddQuad( new Rect( 0, 0, 1, 1 ),
  184.                         new ColorValue( 0, 0, 0, alpha ) );
  185.                 }
  186.  
  187.                 if( text != "" )
  188.                 {
  189.                     EngineApp.Instance.ScreenGuiRenderer.AddText( screenFont, text, new Vec2( .5f, .4f ),
  190.                         HorizontalAlign.Center, VerticalAlign.Center, new ColorValue( 1, 0, 0 ) );
  191.                 }
  192.             }
  193.         }
  194.  
  195.         void Map_Tick( Entity entity )
  196.         {
  197.             //Game tick
  198.             if( gameTime != 0 )
  199.             {
  200.                 gameTime += Entity.TickDelta;
  201.  
  202.                 //Pre start
  203.                 if( gameTime >= 2 && gameTime < 2 + Entity.TickDelta * 1.5 )
  204.                     GameEngineApp.Instance.ControlManager.PlaySound( "Sounds/Feedback/Three.ogg" );
  205.                 if( gameTime >= 4 && gameTime < 4 + Entity.TickDelta * 1.5 )
  206.                     GameEngineApp.Instance.ControlManager.PlaySound( "Sounds/Feedback/Two.ogg" );
  207.                 if( gameTime >= 6 && gameTime < 6 + Entity.TickDelta * 1.5 )
  208.                     GameEngineApp.Instance.ControlManager.PlaySound( "Sounds/Feedback/One.ogg" );
  209.                 if( gameTime >= 8 && gameTime < 8 + Entity.TickDelta * 1.5 )
  210.                     GameEngineApp.Instance.ControlManager.PlaySound( "Sounds/Feedback/Fight.ogg" );
  211.  
  212.                 if( gameTime >= 8 )
  213.                     GameMusic.MusicPlay( "Sounds/Music/Action.ogg", true );
  214.  
  215.                 //Create enemies
  216.                 if( gameTime > 10 && remainingCreateCount != 0 )
  217.                 {
  218.                     remainingTimeForCreateEnemy -= Entity.TickDelta;
  219.  
  220.                     if( remainingTimeForCreateEnemy <= 0 )
  221.                     {
  222.                         remainingTimeForCreateEnemy = createInterval;
  223.                         if( CreateEnemy() )
  224.                             remainingCreateCount--;
  225.                     }
  226.                 }
  227.             }
  228.  
  229.             //Update billboard text
  230.             {
  231.                 string mainText = "";
  232.                 string downText = "";
  233.  
  234.                 if( gameTime < 8 )
  235.                 {
  236.                     if( gameTime == 0 )
  237.                     {
  238.                         if( level <= 6 )
  239.                         {
  240.                             mainText = string.Format( "Level {0}", level );
  241.                             downText = "Fire here to start";
  242.                         }
  243.                         else
  244.                         {
  245.                             mainText = "Victory";
  246.                             downText = "You killed the entire army of chaos!";
  247.                         }
  248.                     }
  249.                     else
  250.                     {
  251.                         mainText = "Prepare";
  252.                         downText = "for battle";
  253.                     }
  254.                 }
  255.                 else
  256.                 {
  257.                     mainText = remainingCount.ToString();
  258.                     downText = string.Format( "Level {0}", level );
  259.                 }
  260.  
  261.                 GameGuiObject billboard = (GameGuiObject)Entities.Instance.GetByName( "HangingBillboard_Game" );
  262.                 billboard.MainControl.Controls[ "MainText" ].Text = mainText;
  263.                 billboard.MainControl.Controls[ "DownText" ].Text = downText;
  264.             }
  265.  
  266.             //Recreate Turret
  267.             if( Entities.Instance.GetByName( "Turret_Game" ) == null && gameTime == 0 )
  268.             {
  269.                 Turret turret = (Turret)Entities.Instance.Create( "Turret", Map.Instance );
  270.                 turret.Name = "Turret_Game";
  271.                 turret.Position = turretCreatePosition;
  272.                 turret.Rotation = turretCreateRotation;
  273.                 turret.PostCreate();
  274.             }
  275.         }
  276.  
  277.         bool CreateEnemy()
  278.         {
  279.             //Get need enemy type
  280.             string typeName = GetCreateEnemyTypeName();
  281.  
  282.             //Get point
  283.             MapObject point = null;
  284.             int safeCounter = 1;
  285.             while( point == null )
  286.             {
  287.                 point = enemySpawnPoints[ World.Instance.Random.Next( enemySpawnPoints.Count ) ];
  288.                 safeCounter++;
  289.                 if( safeCounter > 1000 )
  290.                     break;
  291.             }
  292.  
  293.             if( point == null )
  294.                 return false;
  295.  
  296.             //check for free position
  297.             bool freePoint;
  298.             {
  299.                 Bounds volume = new Bounds( point.Position );
  300.                 volume.Expand( new Vec3( 2, 2, 2 ) );
  301.                 Body[] bodies = PhysicsWorld.Instance.VolumeCast( volume,
  302.                     (int)ContactGroup.CastOnlyDynamic );
  303.  
  304.                 freePoint = bodies.Length == 0;
  305.             }
  306.             if( !freePoint )
  307.                 return false;
  308.  
  309.             //Create object
  310.             MapObject newObj = (MapObject)Entities.Instance.Create( typeName, Map.Instance );
  311.             newObj.Position = point.Position + new Vec3( 0, 0, 1 );
  312.             if( typeName == "Robot" )
  313.                 newObj.Position += new Vec3( 0, 0, 1 );
  314.             newObj.PostCreate();
  315.  
  316.             if( newObj is Unit )
  317.                 ( (Unit)newObj ).ViewRadius = 300;
  318.             newObj.Destroying += EnemyUnitDestroying;
  319.             newObj.Tick += EnemyUnitTick;
  320.  
  321.             return true;
  322.         }
  323.  
  324.         void GameStart()
  325.         {
  326.             if( level > 6 )
  327.                 level = 1;
  328.  
  329.             remainingCount = 20 + level * 3;
  330.             createInterval = 20.0f / ( level + 15 );
  331.  
  332.             gameTime = .001f;
  333.             remainingCreateCount = remainingCount;
  334.             GameEngineApp.Instance.ControlManager.PlaySound( "Sounds/Feedback/Accept.ogg" );
  335.  
  336.             MainPlayerUnitSubscribeToDestroying();
  337.  
  338.             Turret turret = Entities.Instance.GetByName( "Turret_Game" ) as Turret;
  339.             if( turret != null )
  340.                 turret.Health = turret.Type.HealthMax;
  341.  
  342.             UpdateVictoryObjects( false );
  343.         }
  344.  
  345.         void GameStop( bool complete )
  346.         {
  347.             if( gameTime == 0 )
  348.                 return;
  349.  
  350.             gameTime = 0;
  351.  
  352.             if( complete )
  353.             {
  354.                 GameEngineApp.Instance.ControlManager.PlaySound( "Sounds/Feedback/Complete.ogg" );
  355.                 level++;
  356.             }
  357.  
  358.             //Destroy all alive enemies
  359.             ttt: ;
  360.             foreach( Entity entity in Map.Instance.Children )
  361.             {
  362.                 if( entity.IsSetForDeletion )
  363.                     continue;
  364.  
  365.                 Unit unit = entity as Unit;
  366.                 if( unit == null )
  367.                     continue;
  368.  
  369.                 if( ( unit.Intellect as AI ) != null || unit is Aircraft )
  370.                 {
  371.                     unit.Die();
  372.                     goto ttt;
  373.                 }
  374.             }
  375.  
  376.             //restore default music
  377.             if( GameMap.Instance != null )
  378.                 GameMusic.MusicPlay( GameMap.Instance.GameMusic, true );
  379.  
  380.             if( complete && level > 5 )
  381.                 UpdateVictoryObjects( true );
  382.         }
  383.  
  384.         void UpdateVictoryObjects( bool visible )
  385.         {
  386.             foreach( Entity entity in Map.Instance.Children )
  387.             {
  388.                 if( entity.GetTag( "TextUserData" ) != "Victory" )
  389.                     continue;
  390.  
  391.                 MapObject obj = entity as MapObject;
  392.                 if( obj != null )
  393.                     obj.Visible = visible;
  394.             }
  395.         }
  396.  
  397.         string GetCreateEnemyTypeName()
  398.         {
  399.             float bug = 0;
  400.             float zombie = 0;
  401.             float robot = 0;
  402.  
  403.             switch( level )
  404.             {
  405.             case 1:
  406.                 zombie = 1;
  407.                 break;
  408.  
  409.             case 2:
  410.                 bug = .5f;
  411.                 zombie = .5f;
  412.                 break;
  413.  
  414.             case 3:
  415.                 zombie = .4f;
  416.                 bug = .5f;
  417.                 robot = .1f;
  418.                 break;
  419.  
  420.             case 4:
  421.                 zombie = .1f;
  422.                 bug = .7f;
  423.                 robot = .2f;
  424.                 break;
  425.  
  426.             case 5:
  427.                 bug = .7f;
  428.                 robot = .3f;
  429.                 break;
  430.                
  431.             case 6:
  432.                 bug = .17f;
  433.                 robot = .4f;
  434.                 zombie = .30f;
  435.                 break;
  436.             }
  437.  
  438.             float value = World.Instance.Random.NextFloat();
  439.             if( value >= 0 && value < bug )
  440.                 return "Bug";
  441.             if( value >= bug && value < bug + zombie )
  442.                 return "Zombie";
  443.             if( value >= bug + zombie && value < bug + zombie + robot )
  444.                 return "Robot";
  445.             return "Robot";
  446.         }
  447.  
  448.         void EnemyUnitDestroying( Entity entity )
  449.         {
  450.             if( gameTime != 0 )
  451.             {
  452.                 remainingCount--;
  453.                 if( remainingCount == 0 )
  454.                     GameStop( true );
  455.             }
  456.         }
  457.  
  458.         void EnemyUnitTick( Entity entity )
  459.         {
  460.             Dynamic obj = (Dynamic)entity;
  461.  
  462.             Unit playerUnit = null;
  463.             if( PlayerIntellect.Instance != null )
  464.                 playerUnit = PlayerIntellect.Instance.ControlledObject;
  465.  
  466.             if( playerUnit != null )
  467.             {
  468.                 float playerDistance = ( obj.Position - playerUnit.Position ).Length();
  469.  
  470.                 Vec3 diff = playerUnit.Position - obj.Position;
  471.                 float angle = MathFunctions.ATan( diff.Y, diff.X );
  472.  
  473.                 //Change rotation
  474.                 //temp. fake
  475.                 Aircraft aircraft = entity as Aircraft;
  476.                 if( aircraft != null )
  477.                 {
  478.                     obj.PhysicsModel.Bodies[ 0 ].Rotation =
  479.                         new Angles( 0, 0, -MathFunctions.RadToDeg( angle ) ).ToQuat();
  480.                     //obj.Rotation = new Angles(0,0, -MathFunctions.RadToDeg(angle)).ToQuat();
  481.  
  482.                     aircraft.FlyHeight = 10;
  483.  
  484.                     if( playerDistance < 20 )
  485.                         aircraft.FlyHeight = playerUnit.Position.Z;
  486.                 }
  487.  
  488.                 //Suicide
  489.                 bool allowSuicide = false;
  490.                 float suicideDistance = 3;
  491.  
  492.                 if( playerUnit is Turret )
  493.                     allowSuicide = true;
  494.  
  495.                 if( aircraft != null )
  496.                     allowSuicide = true;
  497.  
  498.                 if( allowSuicide && playerDistance < suicideDistance )
  499.                 {
  500.                     obj.Die();
  501.  
  502.                     Dynamic dieObject = (Dynamic)Entities.Instance.Create( "TurretDemo_MonsterSuicideObject", Map.Instance );
  503.                     dieObject.Position = obj.Position;
  504.                     dieObject.PostCreate();
  505.  
  506.                     dieObject.Die();
  507.  
  508.                     return;
  509.                 }
  510.             }
  511.  
  512.             //Check outside map
  513.             Bounds bounds = Map.Instance.InitialCollisionBounds;
  514.             bounds.Expand( new Vec3( 50, 50, 300 ) );
  515.             if( !bounds.IsContainsPoint( ( (MapObject)entity ).Position ) )
  516.                 entity.SetForDeletion( false );
  517.         }
  518.  
  519.         void MainPlayerUnitSubscribeToDestroying()
  520.         {
  521.             PlayerCharacter mainPlayerUnit = null;
  522.             foreach( Entity entity in Map.Instance.Children )
  523.             {
  524.                 mainPlayerUnit = entity as PlayerCharacter;
  525.                 if( mainPlayerUnit != null )
  526.                     break;
  527.             }
  528.             if( mainPlayerUnit != null )
  529.             {
  530.                 mainPlayerUnit.Destroying += delegate( Entity entity )
  531.                 {
  532.                     GameStop( false );
  533.                 };
  534.             }
  535.         }
  536.  
  537.         void GameBillboard_Damage( Dynamic entity, MapObject prejudicial, Vec3 pos, float damage )
  538.         {
  539.             if( gameTime == 0 )
  540.                 GameStart();
  541.         }
  542.     }
  543. }
  544.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement