public override void init() { Microsoft.DirectX.Direct3D.Device d3dDevice = GuiController.Instance.D3dDevice; //Cargar escena TgcSceneLoader loader = new TgcSceneLoader(); scene = loader.loadSceneFromFile(GuiController.Instance.ExamplesMediaDir + "MeshCreator\\Scenes\\Ciudad\\Ciudad-TgcScene.xml"); //Cargar mesh principal mainMesh = loader.loadSceneFromFile(GuiController.Instance.ExamplesMediaDir + "MeshCreator\\Meshes\\Vehiculos\\Hummer\\Hummer-TgcScene.xml").Meshes[0]; //Movemos el mesh un poco para arriba. Porque sino choca con el piso todo el tiempo y no se puede mover. mainMesh.move(0, 5, 0); //Camera en 3ra persona GuiController.Instance.ThirdPersonCamera.Enable = true; GuiController.Instance.ThirdPersonCamera.setCamera(mainMesh.Position, 200, 300); }
/// <summary> /// Método que se llama una sola vez, al principio cuando se ejecuta el ejemplo. /// Escribir aquí todo el código de inicialización: cargar modelos, texturas, modifiers, uservars, etc. /// Borrar todo lo que no haga falta /// </summary> public override void init() { //GuiController.Instance: acceso principal a todas las herramientas del Framework //Device de DirectX para crear primitivas d3dDevice = GuiController.Instance.D3dDevice; rand = new Random(); //Carpeta de archivos Media del alumno alumnoMediaFolder = GuiController.Instance.AlumnoEjemplosMediaDir; effect = TgcShaders.loadEffect(alumnoMediaFolder + "Shaders\\viento.fx"); Control focusWindows = GuiController.Instance.D3dDevice.CreationParameters.FocusWindow; mouseCenter = focusWindows.PointToScreen( new Point( focusWindows.Width / 2, focusWindows.Height / 2) ); inicializarTerreno(); pathMusica = GuiController.Instance.AlumnoEjemplosMediaDir + "Sonidos\\Stayin_alive.mp3"; GuiController.Instance.Mp3Player.closeFile(); GuiController.Instance.Mp3Player.FileName = pathMusica; player = GuiController.Instance.Mp3Player; //player.play(true); //Crear Sprite mira = new TgcSprite(); mira.Texture = TgcTexture.createTexture(alumnoMediaFolder + "\\mira.png"); miraActivada = false; mira_zoom = new TgcSprite(); mira_zoom.Texture = TgcTexture.createTexture(alumnoMediaFolder + "\\mira_zoom3.png"); arma = new TgcSprite(); arma.Texture = TgcTexture.createTexture(alumnoMediaFolder + "\\arma.png"); fuegoArma = new TgcAnimatedSprite( GuiController.Instance.ExamplesMediaDir + "\\Texturas\\Sprites\\Explosion.png", //Textura de 256x256 new Size(64, 64), //Tamaño de un frame (64x64px en este caso) 16, //Cantidad de frames, (son 16 de 64x64px) 10 //Velocidad de animacion, en cuadros x segundo ); //Sonidos disparo = new TgcStaticSound(); disparo.loadSound(alumnoMediaFolder + "\\Sonidos\\disparo.wav"); headshot = new TgcStaticSound(); headshot.loadSound(alumnoMediaFolder + "\\Sonidos\\Headshot.wav"); golpe = new TgcStaticSound(); golpe.loadSound(alumnoMediaFolder + "\\Sonidos\\punch.wav"); explosion = new TgcStaticSound(); explosion.loadSound(alumnoMediaFolder + "\\Sonidos\\explosion.wav"); muerte = new TgcStaticSound(); muerte.loadSound(alumnoMediaFolder + "\\Sonidos\\gameover.wav"); ganador = new TgcStaticSound(); ganador.loadSound(alumnoMediaFolder + "\\Sonidos\\ganador.wav"); sorpresa = new TgcStaticSound(); sorpresa.loadSound(alumnoMediaFolder + "\\Sonidos\\sorpresa.wav"); //Ubicarlo centrado en la pantalla screenSize = GuiController.Instance.Panel3d.Size; Size textureSize = mira.Texture.Size; mira.Scaling = new Vector2(0.6f, 0.6f); mira.Position = new Vector2(FastMath.Max(screenSize.Width / 2 - (textureSize.Width * 0.6f) / 2, 0), FastMath.Max(screenSize.Height / 2 - (textureSize.Height * 0.6f) / 2, 0)); mira_zoom.Scaling = new Vector2((float)screenSize.Width / mira_zoom.Texture.Size.Width, (float)screenSize.Height / mira_zoom.Texture.Size.Height); mira_zoom.Position = new Vector2(0, 0); Size armaSize = arma.Texture.Size; float escalaAncho = (screenSize.Width / 2f) / armaSize.Width; arma.Scaling = new Vector2(escalaAncho, escalaAncho); arma.Position = new Vector2(screenSize.Width - (armaSize.Width * escalaAncho), screenSize.Height - (armaSize.Height * escalaAncho)); posicionArmaDisparo = new Vector2(arma.Position.X + 5f, arma.Position.Y + 5f); posicionArmaOriginal = arma.Position; fuegoArma.Position = arma.Position + new Vector2(22f,28f); inicializarArboles(); inicializarPasto(); inicializarPiedras(); totales = new List<TgcMesh>(); totales.AddRange(arboles); totales.AddRange(pasto); totales.AddRange(piedras); qt = new Quadtree(); qt.create(totales, bbSkyBox); qt.createDebugQuadtreeMeshes(); //Crear texto vida, básico vida = new TgcText2d(); vida.Text = "100"; vida.Color = Color.White; vida.Size = new Size(300, 100); vida.changeFont(new System.Drawing.Font("BankGothic Md BT", 25, FontStyle.Bold)); vida.Position = new Point(-60, 0); barraVida = new TgcSprite(); barraVida.Texture = TgcTexture.createTexture(alumnoMediaFolder + "\\barra_vida.png"); barraVida.Position = new Vector2((float)vida.Position.X + 175f, (float)vida.Position.Y); barraVida.Scaling = new Vector2(0.3f, 0.3f); textoPuntaje = new TgcText2d(); textoPuntaje.Text = "Puntos: 0"; textoPuntaje.Color = Color.White; textoPuntaje.Size = new Size(300, 100); textoPuntaje.changeFont(new System.Drawing.Font("BankGothic Md BT", 25, FontStyle.Bold)); textoPuntaje.Position = new Point(screenSize.Width - 300, 0); TgcSceneLoader loaderLogo = new TgcSceneLoader(); logoTgc = loaderLogo.loadSceneFromFile(GuiController.Instance.ExamplesMediaDir + "ModelosTgc\\LogoTGC\\LogoTGC-TgcScene.xml").Meshes[0]; logoTgc.move(new Vector3(0f, 1900f, 0f)); logoTgc.Scale = new Vector3(14f, 14f, 14f); //Cargar Shader de PhongShading logoTgc.Effect = GuiController.Instance.Shaders.TgcMeshPhongShader; logoTgc.Technique = GuiController.Instance.Shaders.getTgcMeshTechnique(logoTgc.RenderType); //Cargar variables shader logoTgc.Effect.SetValue("ambientColor", ColorValue.FromColor(Color.Gray)); logoTgc.Effect.SetValue("diffuseColor", ColorValue.FromColor(Color.LightBlue)); logoTgc.Effect.SetValue("specularColor", ColorValue.FromColor(Color.White)); logoTgc.Effect.SetValue("specularExp", 20f); logoTgc.Effect.SetValue("lightPosition", lightPos); reproducirSorpresa = false; //////VARIABLES DE FRUSTUM aspectRatio = (float)GuiController.Instance.Panel3d.Width / GuiController.Instance.Panel3d.Height; ruedita = 0f; //Inicializo angulo de FOV anguloFov = FastMath.ToRad(45.0f); GuiController.Instance.D3dDevice.Transform.Projection = Matrix.PerspectiveFovLH(anguloFov, aspectRatio, 1f, 50000f); ///////////////USER VARS////////////////// /* //Crear una UserVar GuiController.Instance.UserVars.addVar("variablePrueba"); //Cargar valor en UserVar GuiController.Instance.UserVars.setValue("variablePrueba", 5451); */ ///////////////MODIFIERS////////////////// /* //Crear un modifier para un valor FLOAT GuiController.Instance.Modifiers.addFloat("valorFloat", -50f, 200f, 0f); //Crear un modifier para un ComboBox con opciones string[] opciones = new string[] { "opcion1", "opcion2", "opcion3" }; GuiController.Instance.Modifiers.addInterval("valorIntervalo", opciones, 0); //Crear un modifier para modificar un vértice GuiController.Instance.Modifiers.addVertex3f("valorVertice", new Vector3(-100, -100, -100), new Vector3(50, 50, 50), new Vector3(0, 0, 0)); */ ///////////////CONFIGURAR CAMARA ROTACIONAL////////////////// //Es la camara que viene por default, asi que no hace falta hacerlo siempre //GuiController.Instance.RotCamera.Enable = true; //Configurar centro al que se mira y distancia desde la que se mira //GuiController.Instance.RotCamera.setCamera(new Vector3(0, 0, 0), 300); Vector3 posicion = new Vector3(0f, 150f, 0f); /*GuiController.Instance.FpsCamera.Enable = true; GuiController.Instance.FpsCamera.setCamera(new Vector3(0,120,0), new Vector3(1, 0, 1)); //GuiController.Instance.FpsCamera.LookAt(new Vector3(0,120,0)); GuiController.Instance.FpsCamera.JumpSpeed = 0; GuiController.Instance.FpsCamera.MovementSpeed *= 10;*/ GuiController.Instance.FpsCamera.Enable = false; GuiController.Instance.RotCamera.Enable = false; camaraQ3 = new Q3FpsCamera(); camaraQ3.setCamera(posicion, posicion + new Vector3(1.0f, 0.0f, 0.0f)); camaraQ3.RotationSpeed = velocidadAngular; camaraQ3.MovementSpeed = velocidadMov; camaraQ3.LockCam = false; ultimaPosCamara = camaraQ3.getPosition(); Vector3 posBound = new Vector3(camaraQ3.getPosition().X, camaraQ3.getPosition().Y + 30, camaraQ3.getPosition().Z); boundingCamara = new TgcBoundingBox(); boundingCamScale = new Vector3(1f, 1f, 1f); boundingCamara.scaleTranslate(posBound, boundingCamScale); personaje = TgcBox.fromSize(new Vector3(30f, 60f, 30f), Color.Red); personaje.Position = camaraQ3.getPosition(); personaje.move(new Vector3(0f, -30f, 0f)); //ENEMIGOS instanciasEnemigos = new List<Enemigo>(); //El ultimo parametro es el radio inicializarEnemigos(4, 4, instanciasEnemigos, 3.4f, 200.0f); crearEsferaExplosion(); inicializarBarriles(); //Para disparo col = new Vector3(0f, 0f, 0f); huboDisparo = false; disparoBarril = false; unaBala = new Bala(); puntoDisparo = TgcBox.fromSize(new Vector3(10f, 10f, 10f), Color.Red); #region menu //Defino el estado inicial como menu estadoJuego = estado.menu; //Sprites para menu fondoMenu = new TgcSprite(); fondoMenu.Texture = TgcTexture.createTexture(alumnoMediaFolder + "\\fondo_menu.jpg"); titulo = new TgcSprite(); titulo.Texture = TgcTexture.createTexture(alumnoMediaFolder + "\\Menu\\titulo.png"); titulo.Scaling = new Vector2(0.5f, 0.5f); titulo.Position = new Vector2((screenSize.Width / 2) - titulo.Texture.Width / 4, (titulo.Texture.Height / 2)-50f); instrucciones = new TgcSprite(); instrucciones.Texture = TgcTexture.createTexture(alumnoMediaFolder + "\\Menu\\instrucciones.png"); instrucciones.Scaling = new Vector2(0.4f, 0.5f); instrucciones.Position = new Vector2((screenSize.Width / 2) - (instrucciones.Texture.Width*0.4f) / 2, (instrucciones.Texture.Height / 2) - instrucciones.Texture.Height / 2); creditos = new TgcSprite(); creditos.Texture = TgcTexture.createTexture(alumnoMediaFolder + "\\Menu\\creditos.png"); creditos.Scaling = new Vector2(0.5f, 0.5f); creditos.Position = new Vector2((screenSize.Width / 2) - creditos.Texture.Width / 4, (creditos.Texture.Height / 2) - creditos.Texture.Height / 2); botonJugar = new TgcSprite(); botonJugar.Texture = TgcTexture.createTexture(alumnoMediaFolder + "\\Menu\\boton_jugar.png"); botonJugar.Scaling = new Vector2(0.5f, 0.5f); sizeJugar = botonJugar.Texture.Size; // sizeJugar.Width = sizeJugar.Width / 2; botonJugar.Position = new Vector2((screenSize.Width / 2)-sizeJugar.Width/4, (screenSize.Height / 2)-sizeJugar.Height/4); botonInstrucciones = new TgcSprite(); botonInstrucciones.Texture = TgcTexture.createTexture(alumnoMediaFolder + "\\Menu\\boton_instrucciones.png"); botonInstrucciones.Scaling = new Vector2(0.5f, 0.5f); sizeInstrucciones = botonInstrucciones.Texture.Size; //sizeInstrucciones.Width = sizeInstrucciones.Width / 2; botonInstrucciones.Position = new Vector2((screenSize.Width / 2) - sizeInstrucciones.Width / 4, (screenSize.Height / 2) - sizeInstrucciones.Height / 4 + 85f); botonCreditos = new TgcSprite(); botonCreditos.Texture = TgcTexture.createTexture(alumnoMediaFolder + "\\Menu\\boton_creditos.png"); botonCreditos.Scaling = new Vector2(0.5f, 0.5f); sizeCreditos = botonCreditos.Texture.Size; //sizeCreditos.Width = sizeCreditos.Width / 2; botonCreditos.Position = new Vector2((screenSize.Width / 2) - sizeCreditos.Width / 4, (screenSize.Height / 2) - sizeCreditos.Height / 4 + sizeInstrucciones.Height + 50f); textoPerdiste = new TgcText2d(); textoPerdiste.Text = "Game over"; textoPerdiste.Align = TgcText2d.TextAlign.CENTER; textoPerdiste.Color = Color.Black; textoPerdiste.Size = new Size(600, 100); textoPerdiste.Position = new Point((screenSize.Width / 2) - textoPerdiste.Size.Width / 2, (screenSize.Height / 2) - textoPerdiste.Size.Height / 2); textoPerdiste.changeFont(new System.Drawing.Font("BankGothic Md BT", 50, FontStyle.Bold)); textoGanaste = new TgcText2d(); textoGanaste.Text = "Ganaste!"; textoGanaste.Align = TgcText2d.TextAlign.CENTER; textoGanaste.Color = Color.Black; textoGanaste.Size = new Size(600, 100); textoGanaste.Position = new Point((screenSize.Width / 2) - textoGanaste.Size.Width / 2, (screenSize.Height / 2) - textoGanaste.Size.Height / 2); textoGanaste.changeFont(new System.Drawing.Font("BankGothic Md BT", 50, FontStyle.Bold)); textoPuntajeFinal = new TgcText2d(); textoPuntajeFinal.Color = Color.Black; textoPuntajeFinal.changeFont(new System.Drawing.Font("BankGothic Md BT", 25, FontStyle.Bold)); textoPuntajeFinal.Size = new Size(800, 100); textoPuntajeFinal.Position = new Point((screenSize.Width / 2) - textoPuntajeFinal.Size.Width / 2, (screenSize.Height / 2) - textoPuntajeFinal.Size.Height/2 + textoGanaste.Size.Height); textoPuntajeRecord = new TgcText2d(); textoPuntajeRecord.Color = Color.Black; textoPuntajeRecord.Size = new Size(400, 60); textoPuntajeRecord.changeFont(new System.Drawing.Font("BankGothic Md BT", 15, FontStyle.Bold)); textoPuntajeRecord.Position = new Point((screenSize.Width - textoPuntajeRecord.Size.Width),(screenSize.Height-textoPuntajeRecord.Size.Height/2)); puntajeRecord = Int32.Parse(System.IO.File.ReadAllText(alumnoMediaFolder + "\\record.txt")); textoPuntajeRecord.Text = "Tu puntaje record es: " + puntajeRecord.ToString(); primeraVez = true; #endregion menu }
public Game() { Fuentes.cargarFuentes(); //sprites if (!inicializo) { mapita = new TgcSprite(); mapita.Texture = TgcTexture.createTexture(GuiController.Instance.AlumnoEjemplosMediaDir + "\\sprites\\preview.png"); motoSprite = new TgcSprite(); motoSprite.Texture = TgcTexture.createTexture(GuiController.Instance.AlumnoEjemplosMediaDir + "\\sprites\\dirtbike.png"); timer = new TgcSprite(); timer.Texture = TgcTexture.createTexture(GuiController.Instance.AlumnoEjemplosMediaDir + "\\sprites\\gate.png"); //Ubicarlo centrado en la pantalla screenSize = GuiController.Instance.Panel3d.Size; textureSize = mapita.Texture.Size; motoTextureSize = motoSprite.Texture.Size; mapita.Scaling = (new Vector2(0.7f, 0.7f)); motoSprite.Scaling = (new Vector2(0.5f, 0.5f)); mapita.Position = new Vector2(FastMath.Max(screenSize.Width / 2 - textureSize.Width * 0.7f / 2, 0), 16); motoSprite.Position = new Vector2(FastMath.Max(screenSize.Width / 2 - motoTextureSize.Width * 0.5f / 2 - textureSize.Width * 0.7f / 2, 0), 8); timer.Position = new Vector2(screenSize.Width - 250, screenSize.Height - 125); timer.Scaling = new Vector2(0.9f, 0.8f); posMotoInicial = motoSprite.Position; inicializo = true; } d3dDevice = GuiController.Instance.D3dDevice; d3dDevice.Transform.Projection = Matrix.PerspectiveFovLH(Geometry.DegreeToRadian(45.0f), TgcD3dDevice.aspectRatio, 1f, 5000000f); string texturesPath = GuiController.Instance.AlumnoEjemplosMediaDir + "skybox\\"; TgcSceneLoader loader = new TgcSceneLoader(); collisionManager = new ElipsoidCollisionManager(); collisionManager.GravityEnabled = false; tiempoAcelerando = 0f; tiempoDescelerando = 0f; velIni = 0f; tocandoPiso = false; saltando = true; ultimoMov = new Vector3(0, 0, 0); //skybox inicializarSkybox(texturesPath); //checkpoints int posCP = -150; foreach (TgcBoundingBox bb in checkpoints) { bb.setExtremes(new Vector3(-200, -100, posCP), new Vector3(200, 1000, posCP - 10)); posCP -= 1300; } //carga la ciudad scene = loader.loadSceneFromFile(GuiController.Instance.AlumnoEjemplosMediaDir + "pistaDesierto\\pistaDesierto2-TgcScene.xml"); //cargo la moto motorcycle = loader.loadSceneFromFile(GuiController.Instance.AlumnoEjemplosMediaDir + "moto\\Moto2-TgcScene.xml").Meshes[0]; motorcycle.move(-40, 100, -150); //cargo la piramide piramid = loader.loadSceneFromFile(GuiController.Instance.AlumnoEjemplosMediaDir + "piramide\\piramide-TgcScene.xml").Meshes[0]; piramid.Scale = new Vector3(5, 5, 5); piramid.move(-265, -45, -13750); //cargo texto ganaste textGanaste = new TgcText2d(); textGanaste2 = new TgcText2d(); //Cargar Textos textGanaste.Text = "FELICIDADES, HAS GANADO"; textGanaste2.Text = "APRETE Q PARA VOLVER AL MENU"; textGanaste.Position = new Point(0, 50); textGanaste2.Position = new Point(0, 100); textGanaste.changeFont(new System.Drawing.Font("TimesNewRoman", 23, FontStyle.Bold | FontStyle.Bold)); textGanaste2.changeFont(new System.Drawing.Font("TimesNewRoman", 23, FontStyle.Bold | FontStyle.Bold)); textGanaste.Color = Color.White; textGanaste2.Color = Color.White; // Creo texto contador de tiempo textoContadorTiempo = new TgcText2d(); textoContadorTiempo.Color = Color.Black; textoContadorTiempo.Align = TgcText2d.TextAlign.RIGHT; textoContadorTiempo.Position = new Point(630, 400); //(680, 400) textoContadorTiempo.Size = new Size(300, 100); textoContadorTiempo.changeFont(new System.Drawing.Font(Fuentes.fuente.Families[0], 25, FontStyle.Regular)); // Creo texto mejor tiempo textoMejorTiempo = new TgcText2d(); textoMejorTiempo.Color = Color.Black; textoMejorTiempo.Align = TgcText2d.TextAlign.RIGHT; textoMejorTiempo.Position = new Point(630, 430); //(680, 430) textoMejorTiempo.Size = new Size(300, 100); textoMejorTiempo.changeFont(new System.Drawing.Font(Fuentes.fuente.Families[0], 25, FontStyle.Regular)); //Texto para mostrar fps textFPS = new TgcText2d(); textFPS.Position = new Point((screenSize.Width / (-2)), 0); textFPS.Text = "FPS: "; textFPS.Color = Color.Yellow; //camara GuiController.Instance.ThirdPersonCamera.Enable = true; GuiController.Instance.ThirdPersonCamera.setCamera(motorcycle.Position + new Vector3(0,0,-100), 10, 200); GuiController.Instance.ThirdPersonCamera.rotateY(-0.7f); //creo la bounding elipsoid motorcycle.Scale = new Vector3(0.5f, 0.5f, 0.5f); // motorcycle.AutoUpdateBoundingBox = false; characterElipsoid = new TgcElipsoid(motorcycle.BoundingBox.calculateBoxCenter() + new Vector3(0, 0, 0), new Vector3(23, 23, 23) * 0.5f); //cargo los colliders objetosColisionables.Clear(); foreach (TgcMesh mesh in scene.Meshes) { //Los objetos del layer "TriangleCollision" son colisiones a nivel de triangulo if (mesh.Layer == "TriangleCollision") { objetosColisionables.Add(TriangleMeshCollider.fromMesh(mesh)); } //El resto de los objetos son colisiones de BoundingBox. Las colisiones a nivel de triangulo son muy costosas asi que deben utilizarse solo //donde es extremadamente necesario (por ejemplo en el piso). El resto se simplifica con un BoundingBox else { objetosColisionables.Add(BoundingBoxCollider.fromBoundingBox(mesh.BoundingBox)); } } //agrego Piramide Como objeto Colisionable objetosColisionables.Add(BoundingBoxCollider.fromBoundingBox(piramid.BoundingBox)); //Cargo lineaInicio lineaInicio = new TgcBox(); lineaInicio.Position = new Vector3(-7, 30, -50); lineaInicio.Size = new Vector3(200, 2000, 1); lineaInicio.updateValues(); //Cargo lineaFin lineaFin = new TgcBox(); lineaFin.Size = new Vector3(100, 1000, 1); lineaFin.Position = new Vector3(0, 15, -13250); lineaFin.Color = Color.White; lineaFin.updateValues(); //La agrego como objeto colisionable objetosColisionables.Add(BoundingBoxCollider.fromBoundingBox(lineaInicio.BoundingBox)); showBB = false; //iluminacion ojalaQueAnde = new ShadowMap(scene, motorcycle); //DEBUG //Crear linea para mostrar la direccion del movimiento del personaje directionArrow = new TgcArrow(); directionArrow.BodyColor = Color.Red; directionArrow.HeadColor = Color.Green; directionArrow.Thickness = 0.4f; directionArrow.HeadSize = new Vector2(5, 10); //Linea para normal de colision collisionNormalArrow = new TgcArrow(); collisionNormalArrow.BodyColor = Color.Blue; collisionNormalArrow.HeadColor = Color.Yellow; collisionNormalArrow.Thickness = 0.4f; collisionNormalArrow.HeadSize = new Vector2(2, 5); //Caja para marcar punto de colision collisionPoint = TgcBox.fromSize(new Vector3(4, 4, 4), Color.Red); //TERMINA DEBUG }