Exemplo n.º 1
0
        /// <summary>
        /// Gameloop Update
        /// </summary>
        public void Run()
        {
            ContentLoader.LoadContent();
            Initialize();

            tFpsSet = cFpsSet.ElapsedTime;

            while (Window.IsOpen)
            {
                // Game Logic

                if (UpdateTime)
                {
                    Window.DispatchEvents();
                    Update();
                    UpdateTime = false;
                    tFpsSet    = cFpsSet.ElapsedTime;
                }

                if (tFpsSet.AsSeconds() >= (float)1 / (float)iFPSlimit)
                {
                    cFpsSet.Restart();

                    Window.Clear(ClearColor);
                    Draw();
                    Window.Display();

                    iframesreview++;

                    tFpsSet    = cFpsSet.ElapsedTime;
                    UpdateTime = true;
                }
                else
                {
                    tFpsSet = cFpsSet.ElapsedTime;
                }



                // Reviewing FPS on Console

                tFpsReview = cFpsReview.ElapsedTime;
                if (tFpsReview.AsMilliseconds() >= 1000)
                {
                    //Console.Clear();
                    //Console.WriteLine(iframesreview + " Frames per Second");
                    iframesreview = 0;
                    cFpsReview.Restart();
                }
            }
        }
Exemplo n.º 2
0
        // DECLARING METHODS: DETECTION

        /// <summary>
        /// Updates Detect Logic of the Archer.
        /// If it detects the Player, the Archer shoots Projectiles.
        /// If it is hit or has detected the Player previously it moves to the registered Position.
        /// </summary>
        protected void DetectLogic()
        {
            if (DetectPlayer())
            {
                vRegisteredPlayerPosition = MainMap.GetVirtualCharacterPosition() + new Vector2f(25, 25);
                RotateEnemy(ref fAngle, MainMap.GetStartCharacterPosition() + new Vector2f(25, 25));
                sEntity.Rotation = fAngle;
                cSuspecting.Restart();
                Closed = new List <Node>();
                Path   = new List <Node>();

                Move();
                Alert();

                if (tShooting.AsMilliseconds() >= 1200)
                {
                    Shoot();
                    cShooting.Restart();
                }

                if (bSuspecting)
                {
                    bSuspecting = false;
                }
            }

            else if (Utilities.MakePositive(vRegisteredPlayerPosition.X) > 0)
            {
                if (!bSuspecting)
                {
                    cSuspecting.Restart();
                    bSuspecting = true;
                    PathFinder(vEntityPosition, vRegisteredPlayerPosition);
                }

                tSuspecting = cSuspecting.ElapsedTime;

                if (tSuspecting.AsMilliseconds() <= 100000)
                {
                    PathfinderLogic();
                }

                else
                {
                    vRegisteredPlayerPosition = new Vector2f();
                }
            }
        }
Exemplo n.º 3
0
        public void Run()
        {
            world.Spawn();

            generationClock.Restart();

            while (window.IsOpen)
            {
                // Get the amount of time that has passed since we drew the last frame.
                float deltaT = clock.Restart().AsMicroseconds() / 1000000f;

                // Clear the previous frame
                window.Clear(Configuration.Background);

                // Process events
                window.DispatchEvents();

                // Draw the paths of the current best individual
                screen.UpdateSequence(world.GetBestNeighbour());

                // Update all the screen components that are unrelated to our sequence.
                screen.Update(deltaT);

                screen.Draw();

                // Update the window
                window.Display();

                // If one second has passed, breed the next generation
                if (generationClock.ElapsedTime.AsSeconds() > generationTime)
                {
                    // Do one generation of breeding & culling.
                    world.DoGeneration();

                    // Restart our generation timer
                    generationClock.Restart();

                    screen.GenerationString.StringText = $"Generation: {generation++}";
                }

                // Check to see if the user has pressed the quit key.
                if (Keyboard.IsKeyPressed(Configuration.QuitKey) || Keyboard.IsKeyPressed(Keyboard.Key.Escape))
                {
                    return;
                }
            }
        }
Exemplo n.º 4
0
        /// <summary>
        /// Updates Player Logic
        /// </summary>
        /// <param name="VirtualPlayerPosition">Virtual Player Position, aka Position if the Player would move, not the Map</param>
        /// <param name="up">Bool allowing up Movement</param>
        /// <param name="down">Bool allowing down Movement</param>
        /// <param name="right">Bool allowing right Movement</param>
        /// <param name="left">Bool allowing left Movement</param>
        public void Update(ref Vector2f VirtualPlayerPosition, ref bool up, ref bool down, ref bool right, ref bool left)
        {
            CollisionDetection(ref VirtualPlayerPosition, ref up, ref down, ref right, ref left, sCharacter.Radius * 2, sCharacter.Radius * 2);

            PlayerRotation();

            if (Input.Shoot)
            {
                tShoot = cShoot.ElapsedTime;

                if (tShoot.AsMilliseconds() > 333)
                {
                    cShoot.Restart();
                    Shoot(MainMap.GetTileMapPosition());
                }
            }

            for (int x = 0; x < lProjectile.Count; x++)
            {
                lProjectile[x].Update();
            }

            DisposeProjectile(lProjectile, uDamage);

            fProcentualHealth = (float)fHealth / (float)iHealthMax;

            if (fHealth >= 0)
            {
                sCharacter.FillColor = new Color(255, (byte)(0 + (255 * fProcentualHealth)), (byte)(0 + (255 * fProcentualHealth)));
            }

            tRegenerate = cRegenarate.ElapsedTime;

            if (tRegenerate.AsSeconds() > 3 && fHealth < iHealthMax)
            {
                fHealth += (float)100 / 1500;
                if (fHealth > iHealthMax)
                {
                    fHealth = iHealthMax;
                }
            }
        }
Exemplo n.º 5
0
        public void Run()
        {
            while (window.IsOpen)
            {
                float deltaT = Configuration.IsDebugFrameTime
                    ? Configuration.DebugFrameTime
                    : clock.Restart().AsMicroseconds() / 1000000f;

                // Clear the previous frame
                window.Clear(Configuration.Background);

                // Process events
                window.DispatchEvents();

                // Update all the screens
                screenManager.Update(deltaT);

                // Draw all the screens
                screenManager.Draw(deltaT);

                // Update the window
                window.Display();
            }
        }
Exemplo n.º 6
0
        // DECLARING METHODS: PLAYER-DETECTION RELATED

        /// <summary>
        /// Returns true if the Player is in the Radius and Angle of Sight of the Enemy and isn't hidden behind a Tile with Collision
        /// </summary>
        protected bool DetectPlayer()
        {
            DisposingInvisibleListLeft   = false;
            DisposingInvisibleListMiddle = false;
            DisposingInvisibleListRight  = false;

            // UPDATING vEnemyDirection
            vEnemyDirection = sEntity.Position + new Vector2f(0, 25);                                                                                      // Creating distance to Origin

            vEnemyDirection = Utilities.VectorRotation(fAngle / 57, vEnemyDirection, sEntity.Position);                                                    // Rotating to PlayerPosition


            // UPDATING vEnemyAngleOrigin
            vEnemyAngleOrigin = (vEnemyDirection - sEntity.Position) * 0.80f + sEntity.Position;                                                           // Calculating based on EnemyDirection1


            // UPDATING vEnemyBottomRightPosition and vEnemyBottomLeftPosition
            vEnemyBottomRightPosition = Utilities.VectorRotation(fAngle / fNumberToCorrect, sEntity.Position + new Vector2f(25, 25), sEntity.Position);    // Rotating to PlayerPosition
            vEnemyBottomLeftPosition  = Utilities.VectorRotation(fAngle / fNumberToCorrect, sEntity.Position + new Vector2f(-25, 25), sEntity.Position);   // Rotating to PlayerPosition


            // CALCULATING AngleEnemy
            fAngleEnemy = Utilities.AngleBetweenVectors180(vEnemyDirection - vEnemyAngleOrigin, new Vector2f(925, 525) - vEnemyAngleOrigin);

            if (fAngleEnemy.Equals(0 / Zero))
            {
                fAngleEnemy = 0;
            }


            // CALCULATING MaxPermittedAngle
            fMaxPermittedAngle = Utilities.AngleBetweenVectors180(vEnemyDirection - vEnemyAngleOrigin, vEnemyBottomRightPosition - vEnemyAngleOrigin);

            tDetecting = cDetecting.ElapsedTime;

            if (fAngleEnemy == 0)
            {
                fAngleEnemy = 0.0001f;
            }

            if (Utilities.MakePositive(Utilities.DistanceBetweenVectors(MainMap.GetVirtualCharacterPosition(), vEntityPosition)) < iDistanceDetection &&
                fAngleEnemy < fMaxPermittedAngle)
            {
                fAnglecopy = fAngle;
                RotateEnemy(ref fAnglecopy, MainMap.GetStartCharacterPosition() + new Vector2f(25, 25));

                ShootInvisible(MainMap.GetTileMapPosition(), fAnglecopy);

                if (DisposeInvisibleProjectile(lInvisibleProjectileLeft))
                {
                    DisposingInvisibleListLeft = true;
                }

                //if (DisposeInvisibleProjectile(lInvisibleProjectileMiddle))
                //    DisposingInvisibleListMiddle = true;

                if (DisposeInvisibleProjectile(lInvisibleProjectileRight))
                {
                    DisposingInvisibleListRight = true;
                }

                if (DisposingInvisibleListLeft || DisposingInvisibleListMiddle || DisposingInvisibleListRight)
                {
                    cDetecting.Restart();
                    tDetecting = cDetecting.ElapsedTime;
                    bAlert     = true;
                    return(true);
                }

                if (tDetecting.AsMilliseconds() <= 500)
                {
                    return(true);
                }
            }

            bAlert = false;
            return(false);
        }
Exemplo n.º 7
0
        public static void Run()
        {
            RenderWindow Window = WindowOptions.Window;

            Clock c     = new Clock();
            Time  dTime = Time.FromSeconds(0);

            StageEntity  Stage  = new StageEntity();
            PlayerEntity Player = new PlayerEntity();

            List <IActor> ActorList = new List <IActor>();

            ActorList.Add(Player);

            List <IStaticEntity> StaticEntityList = new List <IStaticEntity>();

            StaticEntityList.Add(Stage);

            WindowOptions.AddGlobalEvents(Window);

            // Open
            while (Window.IsOpen)
            {
                c.Restart();
                Window.DispatchEvents();
                c.Restart();

                // In Focus
                while (WindowOptions.InFocus && Window.IsOpen)
                {
                    Window.DispatchEvents();

                    // Delay Exceeded
                    if (c.ElapsedTime.AsMilliseconds() >= WindowOptions.Delay)
                    {
                        Window.DispatchEvents();
                        Window.Clear(Color.Cyan);

                        Window.Draw(Stage.Shape);
                        Window.Draw(Player.Shape);

                        foreach (IActor actor in ActorList)
                        {
                            while (actor.Flags.Count > 0)
                            {
                                actor.Flags.Clear();
                            }
                        }

                        HandleInput(Player, dTime);

                        foreach (IActor actor in ActorList)
                        {
                            Physics.Update(actor, dTime);
                        }

                        foreach (IActor actor in ActorList)
                        {
                            actor.Shape.Position += actor.Transformer.Velocity.Vector;
                        }

                        Window.Display();
                        dTime = c.Restart();
                    }
                }

                Window.Display();
            }
        }
Exemplo n.º 8
0
 /// <summary>
 /// Restarts cRegenerate
 /// </summary>
 public static void RestartRegenerateTimer()
 {
     cRegenarate.Restart();
 }
Exemplo n.º 9
0
        public void Run()
        {
            // Spawn our initial populaiton of individuals
            world.Spawn();

            Thread.Sleep(3000);

            // Set our generation count to zero and restart our timer
            int generation = 0;

            generationClock.Restart();

            while (window.IsOpen)
            {
                // Get the amount of time that has passed since we drew the last frame.
                float deltaT = clock.Restart().AsMicroseconds() / 1000000f;

                // Clear the previous frame
                window.Clear(Configuration.Background);

                // Process events
                window.DispatchEvents();

                // Camera movement is now separate from our other screen update code to minimise overhead.
                screenManager.UpdateCamera(deltaT);

                screenManager.Draw(deltaT);

                // Update the window
                window.Display();

                // If one second has passed, breed the next generation
                if (generationClock.ElapsedTime.AsSeconds() > generationTime &&
                    (doGeneration?.IsCompleted ?? true) &&
                    !world.HasConverged)
                {
                    doGeneration = Task.Run(() =>
                    {
                        // Do one generation of breeding & culling.
                        world.DoGeneration();

                        // Restart our generation timer
                        generationClock.Restart();

                        // Update the screen to show the new generation
                        pathScreen.GenerationString.StringText = $"Generation: {++generation}";

                        // Draw the paths of the current best individual
                        pathScreen.UpdateSequence(world.GetBestIndividual());

                        // Update all the screen components that are unrelated to our sequence.
                        screenManager.Update(deltaT);
                    });
                }

                // Our GA has either hit the max generations or has stopped improving, set the GA as complete
                // and copy the history of fitness values to the clipboard
                if (world.GenerationCount == GAConfig.MaxGenerations ||
                    world.NoImprovementCount == GAConfig.MaxNoImprovementCount)
                {
                    pathScreen.SetGACompleted();

                    // Copy the fitness to clipboard
                    Clipboard.SetText(string.Join(",", world.FitnessOverTime));
                }

                // Process any key presses that the user has made.
                this.ProcessUserInput();

                // Check to see if the user wants to stop the simulation.
                if (Keyboard.IsKeyPressed(Configuration.QuitKey) ||
                    Keyboard.IsKeyPressed(Keyboard.Key.Escape))
                {
                    return;
                }
            }
        }
Exemplo n.º 10
0
        // DECLARING METHODS: MOVEMENT

        /// <summary>
        /// Moves Archer Randomly, but always to a Position within the DetectionRadius and respecting Collision with Tiles. The Archer doesn't psush the Player around.
        /// </summary>
        protected override void Move()
        {
            bCollisionUp    = false;
            bCollisionDown  = false;
            bCollisionRight = false;
            bCollisionLeft  = false;
            bool nearright = false;
            bool nearleft  = false;
            bool nearup    = false;
            bool neardown  = false;


            bool bRepeat    = false;
            int  iRepeating = 0;

            Vector2f vCharacterPositionEnemyOrigin = MainMap.GetStartCharacterPosition() + new Vector2f(25, 25) - sEntity.Position;

            CollisionDetection(ref vEntityPosition, ref bCollisionUp, ref bCollisionDown, ref bCollisionRight, ref bCollisionLeft, tEntity.Size.X, tEntity.Size.Y);
            EnemyEnemyCollision(ref vEntityPosition, ref bCollisionUp, ref bCollisionDown, ref bCollisionRight, ref bCollisionLeft);

            tMoving = cMoving.ElapsedTime;

            if (tMoving.AsMilliseconds() > 500)
            {
                iRandomNumber = rRandom.Next(0, 4);
                cMoving.Restart();
            }

            // Enemy does not Push the Player around
            if (Utilities.MakePositive(MainMap.GetStartCharacterPosition().X + 25 - vEnemyDirection.X + fSpeed) < 50 &&
                Utilities.MakePositive(MainMap.GetStartCharacterPosition().Y + 25 - vEnemyDirection.Y) < 50)
            {
                bCollisionLeft = true;
            }

            if (Utilities.MakePositive(MainMap.GetStartCharacterPosition().X + 25 - vEnemyDirection.X - fSpeed) < 50 &&
                Utilities.MakePositive(MainMap.GetStartCharacterPosition().Y + 25 - vEnemyDirection.Y) < 50)
            {
                bCollisionRight = true;
            }

            if (Utilities.MakePositive(MainMap.GetStartCharacterPosition().X + 25 - vEnemyDirection.X) < 50 &&
                Utilities.MakePositive(MainMap.GetStartCharacterPosition().Y + 25 - vEnemyDirection.Y + fSpeed) < 50)
            {
                bCollisionUp = true;
            }

            if (Utilities.MakePositive(MainMap.GetStartCharacterPosition().X + 25 - vEnemyDirection.X) < 50 &&
                Utilities.MakePositive(MainMap.GetStartCharacterPosition().Y + 25 - vEnemyDirection.Y - fSpeed) < 50)
            {
                bCollisionDown = true;
            }


            float CharacterPosEnemyOriginX = MainMap.GetVirtualCharacterPosition().X - vEntityPosition.X;
            float CharacterPosEnemyOriginY = MainMap.GetVirtualCharacterPosition().Y - vEntityPosition.Y;

            // Enemy does not accidently hide behind Tiles with Collision
            if (CharacterPosEnemyOriginX > 0 &&
                DisposingInvisibleListLeft && !DisposingInvisibleListRight ||

                CharacterPosEnemyOriginX < 0 &&
                !DisposingInvisibleListLeft && DisposingInvisibleListRight)
            {
                bCollisionDown = true;
            }

            else if (CharacterPosEnemyOriginX > 0 &&
                     !DisposingInvisibleListLeft && DisposingInvisibleListRight ||
                     CharacterPosEnemyOriginX < 0 &&
                     DisposingInvisibleListLeft && !DisposingInvisibleListRight)
            {
                bCollisionUp = true;
            }

            if (CharacterPosEnemyOriginY > 0 &&
                DisposingInvisibleListLeft && !DisposingInvisibleListRight ||
                CharacterPosEnemyOriginY < 0 &&
                !DisposingInvisibleListLeft && DisposingInvisibleListRight)
            {
                bCollisionLeft = true;
            }

            else if (CharacterPosEnemyOriginY > 0 &&
                     !DisposingInvisibleListLeft && DisposingInvisibleListRight ||
                     CharacterPosEnemyOriginY < 0 &&
                     DisposingInvisibleListLeft && !DisposingInvisibleListRight)
            {
                bCollisionRight = true;
            }


            // Enemy does not go outside a specific range of the Player
            if (vCharacterPositionEnemyOrigin.X > (-iDistanceDetection / 2) ||
                vCharacterPositionEnemyOrigin.X < (iDistanceDetection / 2))
            {
                if (vCharacterPositionEnemyOrigin.Y < (iDistanceDetection / 2))
                {
                    nearup = true;
                }
                if (vCharacterPositionEnemyOrigin.Y > (-iDistanceDetection / 2))
                {
                    neardown = true;
                }
            }

            if (vCharacterPositionEnemyOrigin.Y < (iDistanceDetection / 2) ||
                vCharacterPositionEnemyOrigin.Y > (-iDistanceDetection / 2))
            {
                if (vCharacterPositionEnemyOrigin.X > (-iDistanceDetection / 2))
                {
                    nearright = true;
                }
                if (vCharacterPositionEnemyOrigin.X < (iDistanceDetection / 2))
                {
                    nearleft = true;
                }
            }


            do
            {
                if (bRepeat)
                {
                    bRepeat       = false;
                    iRandomNumber = rRandom.Next(0, 4);
                    iRepeating++;
                }

                switch (iRandomNumber)
                {
                case (0):
                    if (nearup && !bCollisionUp)
                    {
                        MoveUp();
                    }
                    else
                    {
                        bRepeat = true;
                    }
                    break;

                case (1):
                    if (neardown && !bCollisionDown)
                    {
                        MoveDown();
                    }
                    else
                    {
                        bRepeat = true;
                    }
                    break;

                case (2):
                    if (nearright && !bCollisionRight)
                    {
                        MoveRight();
                    }
                    else
                    {
                        bRepeat = true;
                    }
                    break;

                case (3):
                    if (nearleft && !bCollisionLeft)
                    {
                        MoveLeft();
                    }
                    else
                    {
                        bRepeat = true;
                    }
                    break;
                }
            }while (bRepeat && iRepeating <= 2);
        }