public static InteriorNave Instance()
        {
            if (_instance == null)
            {
                _instance = new InteriorNave();
            }

            return(_instance);
        }
Пример #2
0
        /// <summary>
        ///     Se llama una sola vez, al principio cuando se ejecuta el ejemplo.
        ///     Escribir aquí todo el código de inicialización: cargar modelos, texturas, estructuras de optimización, todo
        ///     procesamiento que podemos pre calcular para nuestro juego.
        ///     Borrar el codigo ejemplo no utilizado.
        /// </summary>
        public override void Init()
        {
            //Device de DirectX para crear primitivas.
            var d3dDevice = D3DDevice.Instance.Device;

            timestamp = DateTime.Now;

            //Utilizando esta propiedad puedo activar el update/render a intervalos constantes.
            FixedTickEnable = true;
            //Se puede configurar el tiempo en estas propiedades TimeBetweenUpdates y TimeBetweenRenders, por defecto esta puedo en 1F / FPS_60 es a lo minimo que deberia correr el TP.
            //De no estar a gusto como se ejecuta el metodo Tick (el que maneja el GameLoop) el mismo es virtual con lo cual pueden sobrescribirlo.

            //Esconder cursor
            focusWindows  = d3dDevice.CreationParameters.FocusWindow;
            mousePosition = focusWindows.PointToScreen(new Point(focusWindows.Width / 2, focusWindows.Height / 2));
            //Cursor.Hide();

            Sounds.SoundsManager.Instance().sound    = DirectSound.DsDevice;
            Sounds.SoundsManager.Instance().mediaDir = MediaDir;

            //Burbujas
            D3DDevice.Instance.ParticlesEnabled = true;
            D3DDevice.Instance.EnableParticles();
            Particulas.Init(MediaDir, 20);

            //Oceano
            Oceano.Init(TGCVector3.Up * nivelDelAgua * 0.8f, 100, 100, ShadersDir);

            //Settear jugador y camara
            FPSCamara = new FPSCamara(Camera, Input);

            Player = Player.Instance();
            Player.SetInput(Input);
            Player.Init(FPSCamara);



            //Inicializar camara
            var cameraPosition = new TGCVector3(0, 100, -250);
            var lookAt         = new TGCVector3(0, nivelDelAgua, 0);

            Camera.SetCamera(cameraPosition, lookAt);

            sonidoUnderwater = new TgcStaticSound();
            sonidoUnderwater.loadSound(MediaDir + "Sounds\\mar.wav", DirectSound.DsDevice);

            effect         = TGCShaders.Instance.LoadEffect(ShadersDir + "e_fog.fx");
            efectoInterior = TGCShaders.Instance.LoadEffect(ShadersDir + "interior.fx");


            //Iniciar HUD
            Hud.Init(MediaDir);
            Hud.ChangeStatus(Hud.Status.MainMenu);

            //Cargar enviroment
            oceano = new Fondo(MediaDir, ShadersDir);
            oceano.Init();
            oceano.Camera = Camera;

            heightmap = new TgcSimpleTerrain();
            heightmap.loadHeightmap(MediaDir + marBnwDir, marScaleXZ, marScaleY, new TGCVector3(0, marOffsetY, 0));
            heightmap.loadTexture(MediaDir + marTexDir);

            //Cargar entidades
            var loader = new TgcSceneLoader();
            var scene  = loader.loadSceneFromFile(MediaDir + "yellow_fish-TgcScene.xml");
            var mesh   = scene.Meshes[0];

            peces   = new List <Fish>();
            corales = new List <Coral>();
            metales = new List <Metal>();
            piedras = new List <Metal>();

            int i = 0;

            while (i < 20)
            {
                Fish   fish;
                string meshName = i.ToString();
                fish = new Fish(mesh.clone(meshName));
                fish = (Fish)setearMeshParaLista(fish, i);
                peces.Add(fish);

                fish.Effect(effect);
                fish.Technique("RenderScene");
                i++;
            }

            scene = loader.loadSceneFromFile(MediaDir + "shark-TgcScene.xml");
            mesh  = scene.Meshes[0];

            shark = new Shark(mesh);
            shark.Init();
            sharkSound = new Tgc3dSound(MediaDir + "Sounds\\shark.wav", shark.GetMesh().Position, DirectSound.DsDevice);
            shark.setearSonido(sharkSound);
            shark.setearAlturaMaxima(nivelDelAgua - 19f);

            efectoDesaparecer = TGCShaders.Instance.LoadEffect(ShadersDir + "perlin.fx");

            shark.setearEfectoPerlin(efectoDesaparecer);
            perlinTexture = TgcTexture.createTexture(D3DDevice.Instance.Device, MediaDir + "Textures\\perlin.png");
            shark.setearPerlin(perlinTexture);

            scene = loader.loadSceneFromFile(MediaDir + "coral-TgcScene.xml");
            mesh  = scene.Meshes[0];


            i = 0;
            while (i < 25)
            {
                Coral  coral;
                string meshName = i.ToString();
                coral = new Coral(mesh.createMeshInstance(meshName));
                coral = (Coral)setearMeshParaLista(coral, i * 4, -17);
                corales.Add(coral);

                coral.Effect(effect);
                coral.Technique("RenderScene");
                i++;
            }

            scene = loader.loadSceneFromFile(MediaDir + "Oro-TgcScene.xml");
            mesh  = scene.Meshes[0];
            i     = 0;
            while (i < 15)
            {
                Metal  oro;
                string meshName = i.ToString();
                oro      = new Metal(mesh.createMeshInstance(meshName));
                oro      = (Metal)setearMeshParaLista(oro, i * 8, -17);
                oro.Tipo = ElementoRecolectable.oro;
                metales.Add(oro);

                oro.Effect(effect);
                oro.Technique("RenderSceneLight");
                i++;
            }

            scene = loader.loadSceneFromFile(MediaDir + "Hierro-TgcScene.xml");
            mesh  = scene.Meshes[0];
            i     = 0;
            while (i < 15)
            {
                Metal  hierro;
                string meshName = i.ToString() + i.ToString();
                hierro      = new Metal(mesh.createMeshInstance(meshName));
                hierro      = (Metal)setearMeshParaLista(hierro, (i + 10) * 8, -17);
                hierro.Tipo = ElementoRecolectable.hierro;
                metales.Add(hierro);

                hierro.Effect(effect);
                hierro.Technique("RenderScene");
                i++;
            }

            scene = loader.loadSceneFromFile(MediaDir + "Roca-TgcScene.xml");
            mesh  = scene.Meshes[0];
            i     = 0;
            while (i < 15)
            {
                Metal  roca;
                string meshName = i.ToString() + i.ToString() + i.ToString();
                roca      = new Metal(mesh.createMeshInstance(meshName));
                roca      = (Metal)setearMeshParaLista(roca, (i + 10) * 8, -17);
                roca.Tipo = ElementoRecolectable.madera;
                roca.escalar(new TGCVector3(0.3f, 0.3f, 0.3f));
                piedras.Add(roca);

                roca.Effect(effect);
                roca.Technique("RenderScene");
                i++;
            }

            scene = loader.loadSceneFromFile(MediaDir + "ship-TgcScene.xml");
            nave  = Nave.Instance();
            nave.Init(scene, nivelDelAgua);

            scene = loader.loadSceneFromFile(MediaDir + "EspadaDoble-TgcScene.xml");
            mesh  = scene.Meshes[0];
            arma  = new Arma(mesh);

            scene    = loader.loadSceneFromFile(MediaDir + "Mesa-TgcScene.xml");
            mesaNave = MesaNave.Instance();
            mesaNave.Init(scene);
            mesaNave.Effect(efectoInterior);
            mesaNave.Technique("RenderScene");

            scene        = loader.loadSceneFromFile(MediaDir + "SogaEnrollada-TgcScene.xml");
            mesh         = scene.Meshes[0];
            sogaInterior = SogaInterior.Instance();
            sogaInterior.Init(mesh);
            sogaInterior.Effect(efectoInterior);
            sogaInterior.Technique("RenderScene");

            scene         = loader.loadSceneFromFile(MediaDir + "silla-TgcScene.xml");
            sillaInterior = SillaInterior.Instance();
            sillaInterior.Init(scene);
            sillaInterior.Effect(efectoInterior);
            sillaInterior.Technique("RenderScene");

            scene         = loader.loadSceneFromFile(MediaDir + "Timon-TgcScene.xml");
            timonInterior = TimonInterior.Instance();
            timonInterior.Init(scene);
            timonInterior.Effect(efectoInterior);
            timonInterior.Technique("RenderScene");

            scene       = loader.loadSceneFromFile(MediaDir + "LamparaTecho-TgcScene.xml");
            lamparaNave = new LamparaNave(scene.Meshes[0]);
            lamparaNave.Effect(efectoInterior);
            lamparaNave.Technique("RenderScene");


            //Cargar shaders
            fog               = new TgcFog();
            fog.Color         = Color.FromArgb(30, 144, 255);
            fog.Density       = 1;
            fog.EndDistance   = 1000;
            fog.StartDistance = 1;
            fog.Enabled       = true;

            effect.SetValue("screen_dx", d3dDevice.PresentationParameters.BackBufferWidth);
            effect.SetValue("screen_dy", d3dDevice.PresentationParameters.BackBufferHeight);

            //Fog + Lights
            effect.SetValue("nivelAgua", nivelDelAgua);

            interiorNave = InteriorNave.Instance();
            interiorNave.Init(MediaDir);

            DirectSound.ListenerTracking = Player.Instance().mesh;

            //Mascara post process
            maskTexture = TgcTexture.createTexture(D3DDevice.Instance.Device, MediaDir + "Textures\\mascara.png");
            effect.SetValue("textura_mascara", maskTexture.D3dTexture);

            // seteamos los efectos aca porque son fijos
            oceano.Effect(effect);
            oceano.Technique("RenderScene");

            heightmap.Effect    = effect;
            heightmap.Technique = "RenderScene";

            shark.Effect(effect);
            shark.Technique("RenderScene");

            nave.Effect(effect);
            nave.Technique("RenderSceneLight");

            effect.SetValue("ambientColor", Color.FromArgb(255, 255, 255).ToArgb());
            effect.SetValue("diffuseColor", Color.FromArgb(255, 255, 255).ToArgb());
            effect.SetValue("specularColor", Color.FromArgb(255, 255, 255).ToArgb());

            efectoInterior.SetValue("ambientColor", Color.FromArgb(255, 255, 255).ToArgb());
            efectoInterior.SetValue("diffuseColor", Color.FromArgb(255, 255, 255).ToArgb());
            efectoInterior.SetValue("specularColor", Color.FromArgb(255, 255, 255).ToArgb());

            // dibujo el full screen quad
            CustomVertex.PositionTextured[] vertices =
            {
                new CustomVertex.PositionTextured(-1,  1, 1, 0, 0),
                new CustomVertex.PositionTextured(1,   1, 1, 1, 0),
                new CustomVertex.PositionTextured(-1, -1, 1, 0, 1),
                new CustomVertex.PositionTextured(1,  -1, 1, 1, 1)
            };

            // Vertex buffer de los triangulos
            fullScreenQuad = new VertexBuffer(typeof(CustomVertex.PositionTextured), 4, d3dDevice, Usage.Dynamic | Usage.WriteOnly, CustomVertex.PositionTextured.Format, Pool.Default);
            fullScreenQuad.SetData(vertices, 0, LockFlags.None);

            // dibujo render target

            depthStencil = d3dDevice.CreateDepthStencilSurface(d3dDevice.PresentationParameters.BackBufferWidth, d3dDevice.PresentationParameters.BackBufferHeight, DepthFormat.D24S8, MultiSampleType.None, 0, true);

            renderTarget = new Texture(d3dDevice, d3dDevice.PresentationParameters.BackBufferWidth, d3dDevice.PresentationParameters.BackBufferHeight, 1, Usage.RenderTarget, Format.X8R8G8B8, Pool.Default);

            // inicializo los FB que uso para el Bloom
            coralesBrillantes = new Texture(d3dDevice, d3dDevice.PresentationParameters.BackBufferWidth, d3dDevice.PresentationParameters.BackBufferHeight, 1, Usage.RenderTarget, Format.X8R8G8B8, Pool.Default);

            FBHorizontalBool = new Texture(d3dDevice, d3dDevice.PresentationParameters.BackBufferWidth, d3dDevice.PresentationParameters.BackBufferHeight, 1, Usage.RenderTarget, Format.X8R8G8B8, Pool.Default);

            FBVerticalBloom = new Texture(d3dDevice, d3dDevice.PresentationParameters.BackBufferWidth, d3dDevice.PresentationParameters.BackBufferHeight, 1, Usage.RenderTarget, Format.X8R8G8B8, Pool.Default);
        }
        private void Move(TGCVector3 movement, float ElapsedTime)
        {
            TGCVector3 lastPos = mesh.Position;

            mesh.Position += movement;
            colision.stop();
            if (estaEnNave)
            {
                // todo: chequear colisiones con la mesa y silla
                //Check for collisions
                bool          collided = false;
                List <TGCBox> meshes   = InteriorNave.Instance().obtenerParedes();
                foreach (var pared in meshes)
                {
                    var result = TgcCollisionUtils.classifyBoxBox(mesh.BoundingBox, pared.BoundingBox);
                    if (result == TgcCollisionUtils.BoxBoxResult.Adentro ||
                        result == TgcCollisionUtils.BoxBoxResult.Atravesando)
                    {
                        collided = true;
                        break;
                    }
                }
                if (!collided)
                {
                    List <TgcMesh> elementosMesa = MesaNave.Instance().meshes();
                    foreach (var elemento in elementosMesa)
                    {
                        //var result = TgcCollisionUtils.testAABBAABB(mesh.BoundingBox, elemento.BoundingBox);
                        var result = TgcCollisionUtils.testAABBAABB(new TgcBoundingAxisAlignBox(new TGCVector3(mesh.Position.X, elemento.Position.Y, mesh.Position.Z), mesh.Position), elemento.BoundingBox);
                        if (result)
                        {
                            collided = true;
                            break;
                        }
                    }
                }
                if (!collided)
                {
                    List <TgcMesh> elementosSilla = SillaInterior.Instance().meshes();
                    foreach (var elemento in elementosSilla)
                    {
                        //var result = TgcCollisionUtils.testAABBAABB(mesh.BoundingBox, elemento.BoundingBox);
                        var result = TgcCollisionUtils.testAABBAABB(new TgcBoundingAxisAlignBox(new TGCVector3(mesh.Position.X, elemento.Position.Y, mesh.Position.Z), mesh.Position), elemento.BoundingBox);
                        if (result)
                        {
                            collided = true;
                            break;
                        }
                    }
                }
                if (!collided)
                {
                    List <TgcMesh> elementosTimon = TimonInterior.Instance().meshes();
                    foreach (var elemento in elementosTimon)
                    {
                        var result = TgcCollisionUtils.testAABBAABB(mesh.BoundingBox, elemento.BoundingBox);
                        if (result)
                        {
                            collided = true;
                            break;
                        }
                    }
                }
                //If any collision then go to last position.
                if (collided)
                {
                    mesh.Position = lastPos;
                    colision.play(true);
                }
                if (mesh.Position != lastPos)
                {
                    walking.play(true);
                }
                else
                {
                    walking.stop();
                }
            }
            else
            {
                // todo: manejamos que la colision de la nave haga que el player entre y sacamos lo del press en la "o"?
                bool           collided = false;
                List <TgcMesh> meshes   = Nave.Instance().obtenerMeshes();
                foreach (var meshNave in meshes)
                {
                    var result = TgcCollisionUtils.testAABBAABB(mesh.BoundingBox, meshNave.BoundingBox);
                    if (result)
                    {
                        collided = true;
                        break;
                    }
                }
                //If any collision then go to last position.
                if (collided)
                {
                    mesh.Position = lastPos;
                    colision.play(true);
                    return;
                }

                List <Entity> entities = Entities.GetEntities();
                foreach (var entity in entities)
                {
                    var result = TgcCollisionUtils.testAABBAABB(mesh.BoundingBox, entity.GetMesh().BoundingBox);
                    if (result)
                    {
                        collided = true;
                        break;
                    }
                }
                //If any collision then go to last position.
                if (collided)
                {
                    mesh.Position = lastPos;
                    colision.play(true);
                    return;
                }
                if (mesh.Position.X > 1000f || mesh.Position.X < -1000f || mesh.Position.Z > 1000f || mesh.Position.Z < -1000f)
                {
                    // limitamos cuanto se puede mover el jugador
                    mesh.Position = lastPos;
                    colision.play(true);
                }
                // colisiones contra elementos del mar
            }
        }