Example #1
0
        /// <summary>
        ///     Se llama en cada frame.
        ///     Se debe escribir toda la lógica de computo del modelo, así como también verificar entradas del usuario y reacciones
        ///     ante ellas.
        /// </summary>
        public override void Update()
        {
            PreUpdate();

            //Coidigo Ejemplo de como capturar teclas

            /*//Capturar Input teclado
             * if (base.Input.keyPressed(Key.F))
             * {
             *  BoundingBox = !BoundingBox;
             * }*/
            if (Input.keyPressed(Key.C))
            {
                desfase.X -= 5;
            }
            if (Input.keyPressed(Key.V))
            {
                desfase.X += 5;
            }
            if (Input.keyPressed(Key.B))
            {
                desfase.Y -= 5;
            }
            if (Input.keyPressed(Key.N))
            {
                desfase.Y += 5;
            }
            if (Input.keyPressed(Key.H))
            {
                desfase.Z -= 5;
            }
            if (Input.keyPressed(Key.J))
            {
                desfase.Z += 5;
            }
            if (Input.keyPressed(Key.G))
            {
                if (dibujarGlow)
                {
                    dibujarGlow = false;
                }
                else
                {
                    dibujarGlow = true;
                }
            }

            if (Input.keyPressed(Key.M))
            {
                if (dibujarShadowMap)
                {
                    dibujarShadowMap = false;
                }
                else
                {
                    dibujarShadowMap = true;
                }
            }

            if (Input.keyPressed(Key.L))
            {
                if (dibujarLuces)
                {
                    dibujarLuces = false;
                }
                else
                {
                    dibujarLuces = true;
                }
            }


            if (Input.keyPressed(Key.E))
            {
                if (spawnaearEnemigos)
                {
                    spawnaearEnemigos = false;
                }
                else
                {
                    spawnaearEnemigos = true;
                }
            }

            if (Input.keyPressed(Key.T))
            {
                if (habilitarDisparosTorres)
                {
                    habilitarDisparosTorres = false;
                }
                else
                {
                    habilitarDisparosTorres = true;
                }
            }



            if (menu.estaEnMenu)
            {
                menu.Update(ElapsedTime);
                if (playerAmbiente.FileName != pathSonidoMenu)
                {
                    playerAmbiente.closeFile();
                    playerAmbiente.FileName = pathSonidoMenu;
                    playerAmbiente.play(true);
                }
                PostUpdate();
                return;
            }
            else
            {
                hud.Update(navePrincipal);
            }

            var movimientoNave = TGCVector3.Empty;

            if (!menu.estaEnMenu)
            {
                if (playerAmbiente.FileName != pathSonidoAmbiente)
                {
                    playerAmbiente.closeFile();
                    playerAmbiente.FileName = pathSonidoAmbiente;
                    playerAmbiente.play(true);
                }
                //Movernos de izquierda a derecha, sobre el eje X.
                if (Input.keyDown(Key.Left) || Input.keyDown(Key.A))
                {
                    movimientoNave.X = 1;
                }
                else if (Input.keyDown(Key.Right) || Input.keyDown(Key.D))
                {
                    movimientoNave.X = -1;
                }

                //Movimiento para elevarse con E y Control para bajar , todo sobre el eje Y.
                if (Input.keyDown(Key.W) || Input.keyDown(Key.UpArrow))
                {
                    movimientoNave.Y = 1;
                }
                else if (Input.keyDown(Key.S) || Input.keyDown(Key.DownArrow))
                {
                    movimientoNave.Y = -1;
                }

                //boost de velocidad con shift
                if (Input.keyDown(Key.LeftShift))
                {
                    navePrincipal.DoSpeedBoost();
                    if (navePrincipal.shouldSpeedBoost)
                    {
                        navePrincipal.GastarFuel(1.5f, hud);
                        if (movimientoZ > movimientoMaximoZ)
                        {
                            movimientoZ -= factorMovimientoZ * 3;
                        }
                        movimientoNave.Z = movimientoZ;
                        cant_pasadas     = 3;
                    }
                    else
                    {
                        movimientoNave.Z = movimientoBaseZ;
                    }
                }
                else
                {
                    cant_pasadas = 2;
                }



                if (movimientoZ < movimientoBaseZ)
                {
                    movimientoZ += factorMovimientoZ;
                }
                movimientoNave.Z = movimientoZ;

                //Activar rotaciones especiales
                if (Input.keyDown(Key.Space))
                {
                    this.navePrincipal.DoBarrelRoll();
                }

                if (Input.keyDown(Key.Z))
                {
                    this.navePrincipal.DoLeft90Spin();
                }

                if (Input.keyDown(Key.X))
                {
                    this.navePrincipal.DoRight90Spin();
                }

                //Disparar
                if (Input.buttonPressed(TgcD3dInput.MouseButtons.BUTTON_LEFT))
                {
                    if (navePrincipal.Disparar(new TGCVector3((((D3DDevice.Instance.Width / 2) - Input.Xpos) * 10) + navePrincipal.MovementVector.X, navePrincipal.MovementVector.Y, navePrincipal.MovementVector.Z - 5000), pathSonidoDisparo, DirectSound.DsDevice))
                    {
                        //Aca va la posicion del disparo creo
                    }
                }
                if (Input.keyDown(Key.F))
                {
                    if (navePrincipal.Disparar(pathSonidoDisparo, DirectSound.DsDevice))
                    {
                    }
                }
                if (habilitarDisparosTorres)
                {
                    var torretasEnRango = currentScene.TorresEnRango(navePrincipal.GetPosition());
                    torretasEnRango.ForEach(torre => { torre.Disparar(navePrincipal.GetPosition(), pathSonidoDisparo, DirectSound.DsDevice); torre.Update(ElapsedTime); });
                }
            }
            NaveEnemiga.resetearPosiciones();

            if (!TgcCollisionUtils.testObbAABB(this.navePrincipal.OOB, currentScene.Scene.BoundingBox))
            {
                int nextSceneIndex = escenarios.FindIndex(es => es.Equals(currentScene)) + 1;

                if (nextSceneIndex == escenarios.Count)
                {
                    nextSceneIndex = 0;
                }

                currentScene.MoveScene(escenarios.Count);
                currentScene.MovementVector = currentScene.GetOffsetVectorMoved();
                currentScene.UpdateBoundingBox();
                currentScene = escenarios[nextSceneIndex];

                NaveEnemiga.resetearPosiciones();

                // enemigosAlMismoTiempo pueden modificarse para aumentar o disminuir la dificultad, tambien para el modo god
                if (spawnaearEnemigos)
                {
                    if (enemigos.FindAll(enemigo => enemigo.EstaViva() && enemigo.EnemigoEstaAdelante()).Count == 0)
                    {
                        enemigos.FindAll(enemigo => !enemigo.EstaViva() || !enemigo.EnemigoEstaAdelante()).ForEach(nave => nave.Relocate());
                    }
                }
            }

            // No permitir que se salga de los limites, el salto que hace para volver es medio brusco, se podria atenuar.
            movimientoNave -= TGCVector3.Multiply(currentScene.CheckLimits(navePrincipal, movimientoNave), 10);

            //Actualiza la matrix de movimiento de la nave.

            this.navePrincipal.Move(movimientoNave * ElapsedTime);
            this.navePrincipal.Update(ElapsedTime);
            if (spawnaearEnemigos)
            {
                enemigos.FindAll(enemigo => enemigo.EstaViva() && enemigo.EnemigoEstaAdelante()).ForEach(enemigo =>
                {
                    enemigo.Perseguir(ElapsedTime, pathSonidoDisparo, DirectSound.DsDevice);
                    enemigo.Update(ElapsedTime);
                }

                                                                                                         );
            }
            var naves = enemigos.FindAll(enemigo => enemigo.EstaViva() && enemigo.EnemigoEstaAdelante() && spawnaearEnemigos).Select(e => (NaveEspacial)e).ToList();

            naves.Add(navePrincipal);
            naves.ForEach(naveActual =>
            {
                //Colision de todas las naves contra el escenario.
                if (currentScene.CheckCollision(naveActual))
                {
                    naveActual.Morir();
                }

                naves.FindAll(n => n != naveActual).ForEach(otraNave =>
                {
                    //Colision fisica entre naves.
                    if (TgcCollisionUtils.testObbObb(naveActual.OOB, otraNave.OOB))
                    {
                        naveActual.Morir();
                        otraNave.Morir();
                    }

                    //Colision de disparos
                    if (naveActual.CheckIfMyShotsCollided(otraNave))
                    {
                        otraNave.Daniar(naveActual.ArmaPrincipal.Danio);
                    }
                });
                currentScene.TorresEnRango(navePrincipal.GetPosition()).ForEach(torre =>
                {
                    if (torre.CheckIfMyShotsCollided(naveActual))
                    {
                        naveActual.Daniar(torre.arma.Danio);
                    }
                });
            });

            if (!navePrincipal.EstaViva())
            {
                this.navePrincipal.MoveTo(new TGCVector3(1200f, -1100f, 4000f) + currentScene.GetOffsetVectorMoved());
                this.skyBox.Center = new TGCVector3(0, 0, -2300f) + currentScene.GetOffsetVectorMoved();
                this.navePrincipal.Revivir();
                this.sol.Position    = new TGCVector3(0, 5000f, 4000f) + currentScene.GetOffsetVectorMoved();
                this.menu.estaEnMenu = true;
            }


            this.skyBox.Center += movimientoNave * ElapsedTime * 1000;
            this.sol.Move(new TGCVector3(0, 0, movimientoNave.Z) * ElapsedTime * 1000);
            estrellasS.ForEach(e =>
            {
                e.Position += new TGCVector3(0, 0, movimientoNave.Z) * ElapsedTime * 1000;
            });

            (this.Camara as CamaraStarWars).Target = this.navePrincipal.GetPosition();

            PostUpdate();
        }