コード例 #1
0
        public void Update(FrameEventArgs e)
        {
            //Game time and delta time
            dt = (float)e.Time;
            sceneManager.dt   = dt;
            time             += (float)e.Time;
            SceneManager.time = time;
            //Pause button toggle timer
            pauseToggleTimer += dt;

            //Pauses game when escape is pressed and unpauses when pressed again
            if (keyboardInput.Contains("Escape") && pauseToggleTimer > 0.25f)
            {
                if (paused != true)
                {
                    paused           = true;
                    pauseToggleTimer = 0;
                }
                else
                {
                    paused           = false;
                    pauseToggleTimer = 0;
                }
            }

            //Checks game isn't paused
            if (paused != true)
            {
                light.GetTransform().Translation -= light.GetTransform().Forward *dt;
                test.GetTransform().Translation  -= test.GetTransform().Forward *test.GetVelocity().Velocity *dt;
                test.GetTransform().Translation  += test.GetTransform().Right *test.GetVelocity().Velocity *dt;
                test.GetTransform().Rotation     += new Vector3(0, 0.1f * dt, 0);
                //Button toggle on/off timers
                noClipToggleTimer           += dt;
                disableCollisionToggleTimer += dt;
                disableDroneToggleTimer     += dt;

                //Buff timers
                damageBuffTimer   += dt;
                speedBuffTimer    += dt;
                disableDroneTimer += dt;

                //Damage delay timers
                damageDelay += dt;

                //Gets mouse position from input manager every frame
                mousePos = inputManager.MousePosition();

                #region Player Movement and collision with drone logic
                if (keyboardInput.Contains("Up"))
                {
                    //Moves player forward when W is pressed
                    player.GetTransform().Translation += player.GetTransform().Forward *player.GetVelocity().Velocity *dt;
                    //Moves camera forward to stay in line with player position
                    mainCamera.MoveCamera(player.GetTransform().Translation);
                }
                if (keyboardInput.Contains("Down"))
                {
                    //Moves player backwards when S is pressed
                    player.GetTransform().Translation -= player.GetTransform().Forward *player.GetVelocity().Velocity *dt;
                    //Moves camera backwards to stay in line with player position
                    mainCamera.MoveCamera(player.GetTransform().Translation);
                }
                if (keyboardInput.Contains("Right"))
                {
                    //Moves player right when D is pressed
                    player.GetTransform().Translation += player.GetTransform().Right *player.GetVelocity().Velocity *dt;
                    //Moves camera right to stay in line with player position
                    mainCamera.MoveCamera(player.GetTransform().Translation);
                }
                if (keyboardInput.Contains("Left"))
                {
                    //Moves player left when A is pressed
                    player.GetTransform().Translation -= player.GetTransform().Right *player.GetVelocity().Velocity *dt;
                    //Moves camera left to stay in line with player position
                    mainCamera.MoveCamera(player.GetTransform().Translation);
                }

                if (player.GetCollidedWith().Count > 0)
                {
                    foreach (string entityName in player.GetCollidedWith())
                    {
                        //Removes a life from the player when colliding with a drone
                        if (entityName.Contains("Drone") && damageDelay > 0.5f)
                        {
                            player.GetHealth().Health -= 1;
                            health     -= 1;
                            damageDelay = 0;
                        }

                        if (entityName.Contains("Health") || entityName.Contains("Disable") || entityName.Contains("Speed") || entityName.Contains("Damage"))
                        {
                            //    player.GetAudio().PlayOnAwake = true;
                        }
                    }
                }

                //Checks if drone collides with player
                foreach (Entity entity in entityManager.Entities())
                {
                    if (entity.Name.Contains("Drone"))
                    {
                        if (entity.GetCollidedWith().Count > 0)
                        {
                            foreach (string entityName in entity.GetCollidedWith())
                            {
                                if (entityName.Contains("Player"))
                                {
                                    //     entity.GetAudio().PlayOnAwake = true;
                                }
                            }
                        }
                    }
                }
                //Game over screen when player runs out of lives
                if (player.GetHealth().Health <= 0)
                {
                    //Game over logic
                    sceneManager.LoadScene(new GameOverScene(sceneManager));
                }
                #endregion

                #region Player Rotation on the Y axis based on mouse movement on the X axis
                if (sceneManager.Focused)
                {
                    //Amount mouse has moved since last update call
                    mouseDelta = oldMousePos - mousePos;

                    //Adjusts player rotation on the x and y axis based on the mouse movement on the x and y axis if no clip is on
                    if (noClip == true)
                    {
                        player.GetTransform().Rotation += new Vector3(mouseDelta.Y, mouseDelta.X, 0.0f) * mouseSensitivity;
                    }
                    //Adjusts player rotation on the y axis based on mouse movement on the x axis if no clip is off
                    else
                    {
                        player.GetTransform().Rotation += new Vector3(0.0f, mouseDelta.X, 0.0f) * mouseSensitivity;
                    }

                    //clamp x rotation
                    if (player.GetTransform().Rotation.X > MathHelper.DegreesToRadians(89))
                    {
                        player.GetTransform().Rotation = new Vector3(MathHelper.DegreesToRadians(89), player.GetTransform().Rotation.Y, 0.0f);
                    }
                    if (player.GetTransform().Rotation.X < MathHelper.DegreesToRadians(-89))
                    {
                        player.GetTransform().Rotation = new Vector3(MathHelper.DegreesToRadians(-89), player.GetTransform().Rotation.Y, 0.0f);
                    }

                    //Adjusts camera rotation to stay in line with player rotation
                    mainCamera.RotateCamera(player.GetTransform().Rotation);

                    //Centers cursor to center of screen
                    inputManager.CenterCursor();

                    //Resets mouse position
                    oldMousePos = mousePos;
                }
                #endregion

                #region Shooting logic on left click
                //Creates new bullet entity on click based on fire rate
                if (mouseInput.Contains("Left") && time > nextShot)
                {
                    Entity bullet = new Entity("Bullet(" + bulletCount + ")");
                    bullet.AddComponent(new ComponentTransform(player.GetTransform().Translation - new Vector3(0, 0.25f, 0), player.GetTransform().Rotation, new Vector3(0.05f, 0.05f, 0.05f)));
                    bullet.AddComponent(new ComponentGeometry("Geometry/sphere.obj"));
                    bullet.AddComponent(new ComponentTexture("Textures/Blue.png"));
                    bullet.AddComponent(new ComponentVelocity(10f));
                    bullet.AddComponent(new ComponentShader("Shaders/vPulsating.glsl", "Shaders/fPulsating.glsl"));
                    bullet.AddComponent(new ComponentAudio("Audio/GUNSHOT.wav", false, true));
                    bullet.AddComponent(new ComponentSphereCollider((0.05f), bulletIgnoreCollisionWith));
                    entityManager.AddEntity(bullet);

                    //Assigns new entity to appropriate system(s)
                    systemManager.AssignNewEntity(bullet);

                    //Iterates bullet count
                    bulletCount++;

                    //Sets timestamp for when next shot can be fired
                    nextShot = time + fireRate;
                }

                //Bullet movement and collisions
                foreach (Entity entity in entityManager.Entities())
                {
                    //Moves bullet and destroys on collision
                    if (entity.Name.Contains("Bullet"))
                    {
                        entity.GetTransform().Translation += entity.GetTransform().Forward *entity.GetVelocity().Velocity *dt;

                        //Destroys bullet if it has collided with something
                        if (entity.GetCollidedWith().Count > 0)
                        {
                            //Removes entity from systems to remove it from game
                            systemManager.DestroyEntity(entity);

                            //Adds entity to despawn list to remove final reference of entity from entity manager to clear it from memory
                            despawnedEntities.Add(entity);
                        }
                    }

                    //Bullet collision with drones
                    if (entity.Name.Contains("Drone"))
                    {
                        if (entity.GetCollidedWith().Count > 0)
                        {
                            foreach (string entityName in entity.GetCollidedWith())
                            {
                                //Removes life from drone when bullet hits
                                if (entityName.Contains("Bullet"))
                                {
                                    if (damageBuff == true)
                                    {
                                        entity.GetHealth().Health -= 2;
                                    }
                                    else
                                    {
                                        entity.GetHealth().Health -= 1;
                                    }
                                }
                            }
                        }
                        //Destroys drone when it reaches 0 lives
                        if (entity.GetHealth().Health <= 0)
                        {
                            //Removes entity from systems to remove it from game
                            systemManager.DestroyEntity(entity);
                            droneCount--;

                            //Adds entity to despawn list to remove final reference of entity from entity manager to clear it from memory
                            despawnedEntities.Add(entity);
                        }
                    }
                }
                #endregion

                #region No Clip and debug keys logic
                //Activates no clip, allows player to move and rotate freely in 3d space, multiplies speed by 4
                if (keyboardInput.Contains("N") && noClipToggleTimer > 0.25f)
                {
                    if (noClip == false)
                    {
                        noClip            = true;
                        disableCollisions = true;
                        noClipToggleTimer = 0;
                        player.GetVelocity().Velocity *= 4;
                    }
                    else
                    {
                        noClip            = false;
                        disableCollisions = false;
                        noClipToggleTimer = 0;
                        player.GetVelocity().Velocity /= 4;
                    }
                }

                //Moves player up when noclip is true
                if (keyboardInput.Contains("Space") && noClip == true)
                {
                    player.GetTransform().Translation += new Vector3(0, 1, 0) * player.GetVelocity().Velocity *dt;
                    mainCamera.MoveCamera(player.GetTransform().Translation);
                }

                //Disables/Enables drones when D is pressed
                if (keyboardInput.Contains("D") && disableDroneToggleTimer > 0.25f)
                {
                    if (disableDrone == false)
                    {
                        disableDrone            = true;
                        disableDroneToggleTimer = 0;
                    }
                    else
                    {
                        disableDrone            = false;
                        disableDroneToggleTimer = 0;
                    }
                }

                //Disables/Enables collisions when C is pressed
                if (keyboardInput.Contains("C") && disableCollisionToggleTimer > 0.25f)
                {
                    if (disableCollisions == false)
                    {
                        disableCollisions           = true;
                        disableCollisionToggleTimer = 0;
                    }
                    else
                    {
                        disableCollisions           = false;
                        disableCollisionToggleTimer = 0;
                    }
                }
                #endregion

                #region Power Up logic
                foreach (Entity entity in entityManager.Entities())
                {
                    if (entity.Name.Contains("Health"))
                    {
                        if (entity.GetCollidedWith().Contains("Player"))
                        {
                            //If player
                            if (player.GetHealth().Health < 3)
                            {
                                player.GetHealth().Health += 1;
                                health += 1;
                            }

                            //Removes entity from systems to remove it from game
                            systemManager.DestroyEntity(entity);

                            //Adds entity to despawn list to remove final reference of entity from entity manager to clear it from memory
                            despawnedEntities.Add(entity);
                        }
                    }

                    if (entity.Name.Contains("Damage"))
                    {
                        if (entity.GetCollidedWith().Contains("Player"))
                        {
                            //Sets damage buff to true and sets timer for damage buff
                            if (damageBuff == false)
                            {
                                damageBuff      = true;
                                damageBuffTimer = 0;
                            }

                            //Removes entity from systems to remove it from game
                            systemManager.DestroyEntity(entity);

                            //Adds entity to despawn list to remove final reference of entity from entity manager to clear it from memory
                            despawnedEntities.Add(entity);
                        }
                    }

                    if (entity.Name.Contains("Speed"))
                    {
                        if (entity.GetCollidedWith().Contains("Player"))
                        {
                            //Sets speed buff to true and sets timer for speed buff
                            if (speedBuff == false)
                            {
                                speedBuff      = true;
                                speedBuffTimer = 0;
                                player.GetVelocity().Velocity *= 2;
                            }

                            //Removes entity from systems to remove it from game
                            systemManager.DestroyEntity(entity);

                            //Adds entity to despawn list to remove final reference of entity from entity manager to clear it from memory
                            despawnedEntities.Add(entity);
                        }
                    }

                    if (entity.Name.Contains("Disable"))
                    {
                        if (entity.GetCollidedWith().Contains("Player"))
                        {
                            //Sets disable drones to true and sets timer for disabled drones
                            if (disableDroneBuff == false)
                            {
                                disableDroneBuff  = true;
                                disableDroneTimer = 0;
                            }

                            //Removes entity from systems to remove it from game
                            systemManager.DestroyEntity(entity);

                            //Adds entity to despawn list to remove final reference of entity from entity manager to clear it from memory
                            despawnedEntities.Add(entity);
                        }
                    }

                    //Disables buffs when timers reach their end
                    if (damageBuff == true && damageBuffTimer > 3)
                    {
                        damageBuff = false;
                    }

                    if (speedBuff == true && speedBuffTimer > 3)
                    {
                        speedBuff = false;
                        player.GetVelocity().Velocity /= 2;
                    }

                    if (disableDroneBuff == true && disableDroneTimer > 3)
                    {
                        disableDroneBuff = false;
                    }
                }
                #endregion

                #region Victory condition check
                //Checks if all drones are destroyed
                dronesDestroyed = true;
                foreach (Entity entity in entityManager.Entities())
                {
                    if (entity.Name.Contains("Drone"))
                    {
                        dronesDestroyed = false;
                    }
                }

                //Exits game when all drones destroyed, planned to go to victory screen instead of exit.
                if (dronesDestroyed || keyboardInput.Contains("V"))
                {
                    sceneManager.LoadScene(new VictoryScene(sceneManager));
                }
                #endregion

                //Enables/Disables drone and collisions based on appropriate bools
                foreach (Entity entity in entityManager.Entities())
                {
                    //Collisions
                    if (entity.GetComponentBoxCollider() != null)
                    {
                        if (entity.GetComponentBoxCollider().Disabled != disableCollisions)
                        {
                            entity.GetComponentBoxCollider().Disabled = disableCollisions;
                        }
                    }
                    else if (entity.GetComponentSphereCollider() != null)
                    {
                        if (entity.GetComponentSphereCollider().Disabled != disableCollisions)
                        {
                            entity.GetComponentSphereCollider().Disabled = disableCollisions;
                        }
                    }

                    //Drones
                    if (entity.GetComponentAI() != null)
                    {
                        if (entity.GetComponentAI().Disabled != disableDrone)
                        {
                            entity.GetComponentAI().Disabled = disableDrone;
                        }

                        if (entity.GetComponentAI().Disabled != disableDroneBuff && disableDrone != true)
                        {
                            entity.GetComponentAI().Disabled = disableDroneBuff;
                        }
                    }
                }

                //Removes all references to destroyed entities to remove them from memory
                foreach (Entity entity in despawnedEntities)
                {
                    entityManager.Entities().Remove(entity);
                }
                despawnedEntities.Clear();

                //Calls Update Systems every frame
                systemManager.UpdateSystems();
            }
        }