protected override void LoadContent() { Effect = Content.Load <Effect>("terrain"); SFont = Content.Load <SpriteFont>("SpriteFont"); for (int i = 0; i < 9; i++) { if (i == 4) { TerrainList.Add(new Terrain(GraphicsDevice, Content.Load <Texture2D>("heightmap4"), Content.Load <Texture2D>("sand"), Content.Load <Texture2D>("sand"), Content.Load <Texture2D>("vetta"), 1f, 128, 128, 5f)); } else { TerrainList.Add(new Terrain(GraphicsDevice, Content.Load <Texture2D>("heightmapext"), Content.Load <Texture2D>("sand"), Content.Load <Texture2D>("sand"), Content.Load <Texture2D>("vetta"), 1f, 128, 128, 5f)); } } PlayerTank = new Tank(Content.Load <Model>("tank"), new Vector3(100, TerrainList[4].GetHeight(100, 100), 100), new Quaternion(), new Vector3(0.001f, 0.001f, 0.001f), GraphicsDevice); EnemyTank = new Tank(Content.Load <Model>("enemy"), new Vector3(15, TerrainList[4].GetHeight(15, 118), 118), new Quaternion(), new Vector3(0.001f, 0.001f, 0.001f), GraphicsDevice); PlayerTank.Bullet = new Bullet(Content.Load <Model>("Bullet"), PlayerTank.GetTransformPaths(PlayerTank.Model.Bones[10]).Translation, new Quaternion(), new Vector3(0.001f, 0.001f, 0.001f), GraphicsDevice); EnemyTank.Bullet = new Bullet(Content.Load <Model>("Bullet"), EnemyTank.GetTransformPaths(EnemyTank.Model.Bones[10]).Translation, new Quaternion(), new Vector3(0.001f, 0.001f, 0.001f), GraphicsDevice); //Arrow = new CModel(Content.Load<Model>("bull"), PlayerTank.Position + new Vector3(0,0.3f,0), // new Quaternion(), new Vector3(0.005f, 0.005f, 0.005f), GraphicsDevice); TankList.Add(PlayerTank); TankList.Add(EnemyTank); camera = new FreeCamera(new Vector3(11, TerrainList[4].GetHeight(11, 61) + 2, 61), -20f, 0f, GraphicsDevice); Arrow = new CModel(Content.Load <Model>("bull"), PlayerTank.Position, new Quaternion(), new Vector3(0.005f, 0.005f, 0.005f), GraphicsDevice); SFXManager.AddEffect("Explosion", Content.Load <SoundEffect>("Explosion1")); SFXManager.AddEffect("Jump", Content.Load <SoundEffect>("Jump")); SFXManager.AddEffect("PlayerShot", Content.Load <SoundEffect>("Shot1")); SFXManager.AddEffect("EnemyShot", Content.Load <SoundEffect>("Shot2")); ParticleManager.Initialize(GraphicsDevice, Content.Load <Effect>("Particles"), Content.Load <Texture2D>("Explosion")); }
public void EnemyAutoFiring(GameTime gameTime, Tank Enemy) //Gestione AI carro nemico { Vector3 directionOfEnemyTurret = Enemy.Bullet.CalculateBulletDirection(Enemy); //Calcolo vettore direzione attuale della torretta nemica Vector3 directionOfFiring = (PlayerTank.Position - Enemy.Position); //Calcolo vettore direzione di riferimento per sparare verso il giocatore float distance = directionOfFiring.Length(); //Calcolo la distanza tra i due carri MakeEnemyCanonRotation(Enemy, directionOfFiring, directionOfEnemyTurret, distance); //Ruoto la torretta nemica per mantenere il giocatore a bersaglio (in orizzontale) if (AllowFire(directionOfEnemyTurret, directionOfFiring, distance, playerIsOnFire)) //Controllo di essere in traiettoria per sparare e se sono sotto una certa distanza { playerIsOnFire = true; //Il giocatore è mirato correttamente Enemy.EnemyFiringDirection = PlayerTank.Position - Enemy.Position; //Ricontrollo la posizione del giocatore if (Enemy.precEnemyFiringDirection == Vector3.Zero) { Enemy.precEnemyFiringDirection = Enemy.EnemyFiringDirection; //Se è la prima volta che miro, il valore precedente diventa quello attuale } MakeEnemyTurretRotation(Enemy, Enemy.EnemyFiringDirection, Enemy.precEnemyFiringDirection); //Ruoto la torretta nemica in verticale per mirare il bersaglio if (!Enemy.Bullet.IsFired && AllowFire(directionOfEnemyTurret, directionOfFiring, distance, playerIsOnFire) && bulletTimer.IsTimeEspired(gameTime)) //A questo punto sparo { Enemy.BulletFire(); enemySpeedUpCnt++; if (enemySpeedUpCnt != (bulletTimerCounter / 1000)) //Comandi per rallentare i colpi con timers (evitare effetto mitragliatore e lasciare scampo al giocatore) { bulletTimerCounter = bulletTimerCounter - 1000; } else { bulletTimerCounter = 1000; enemySpeedUpCnt = (bulletTimerCounter / 1000) - 2; } bulletTimer = new TimeClass(bulletTimerCounter); bulletTimer.Start(); SFXManager.Play("PlayerShot"); } if (distance > 6f) //Se esco dalla distanza massima di bersaglio, azzero tutto { playerIsOnFire = false; Enemy.precEnemyFiringDirection = Vector3.Zero; } Enemy.precEnemyFiringDirection = Enemy.EnemyFiringDirection; //Salvo la precedente direzione di bersaglio per effettuare il turno dopo un inclinazione incrementale rispetto a quella precedente } }
private void UpdateBullet(Tank Shooter, GameTime gameTime) //Gestisce la dinamica della collisione dei proiettili { if (Shooter.Bullet.IsFired == true) { if (Shooter.Bullet.Position.Y > TerrainList[4].GetHeight(Shooter.Bullet.Position.X, Shooter.Bullet.Position.Z)) //Controllo se il proiettile è in volo { Shooter.Bullet.Position += Shooter.Bullet.bulletDirection * 0.05f - 0.001f * Vector3.UnitY; } else //Proiettile tocca terra { MakeExplosion(Shooter.Bullet); Shooter.Bullet.Position = Shooter.Position + Shooter.Bullet.BulletTranslation(Shooter) * new Vector3(0.01f, 0.01f, 0.01f) + Shooter.Bullet.bulletDirection * 0.04f; SFXManager.Play("Explosion"); Shooter.Bullet.IsFired = false; } if (Shooter.Bullet.BulletTime.IsTimeEspired(gameTime)) //Proiettile ha esaurito il tempo (volerebbe indefinitivamente, quindi lo ignoro) { MakeExplosion(Shooter.Bullet); Shooter.Bullet.Position = Shooter.Position + Shooter.Bullet.BulletTranslation(Shooter) * new Vector3(0.01f, 0.01f, 0.01f) + Shooter.Bullet.bulletDirection * 0.04f; Shooter.Bullet.IsFired = false; } foreach (Tank tank in TankList) { if (CollisionCheck(tank, Shooter.Bullet)) //Proiettile colpisce un altro carro { if (Shooter != tank) //Proiettile del nemico { MakeExplosion(Shooter.Bullet); Shooter.Bullet.Position = Shooter.Position + Shooter.Bullet.BulletTranslation(Shooter) * new Vector3(0.01f, 0.01f, 0.01f) + Shooter.Bullet.bulletDirection * 0.04f; SFXManager.Play("Explosion"); Shooter.HitsCounter++; Shooter.Bullet.IsFired = false; if (Shooter == PlayerTank) //Gestione comparsa del carro nemico, viene generato nei dintorni del nostro carro in modo casuale { int xCoord = random.Next(-5, 5); int zCoord = random.Next(-5, 5); if (xCoord < 2 && xCoord > -2) { if (xCoord < 0) { xCoord -= 2; } else { xCoord += 2; } } if (zCoord < 2 && zCoord > -2) { if (zCoord < 0) { zCoord -= 2; } else { zCoord += 2; } } tank.Position = PlayerTank.Position + new Vector3(xCoord, 0, zCoord); tank.Position = new Vector3(tank.Position.X, TerrainList[4].GetHeight(tank.Position.X, tank.Position.Z), tank.Position.Z); bulletTimerCounter = 11000; enemySpeedUpCnt = 0; } if (Shooter == EnemyTank) //Se ci ha colpito il nemico, resetto il timer per rallentare il prossimo colpo (altrimenti diventa un mitragliatore) { bulletTimerCounter = 11000; enemySpeedUpCnt = 0; } } } } } }
public void PlayerTankControls(GameTime gameTime) //Gestione pressione tastiera per controllo tank { var delta = (float)gameTime.ElapsedGameTime.TotalSeconds; if (FirstRun) { LastMouseState = Mouse.GetState(); FirstRun = false; } KeyboardState KState = Keyboard.GetState(); if (KState.IsKeyDown(Keys.Escape)) { Exit(); } if (KState.IsKeyDown(Keys.W)) { PlayerTank.canonRot -= 0.01f; PlayerTank.BoneTransform(10, Matrix.CreateRotationX(PlayerTank.canonRot)); } if (KState.IsKeyDown(Keys.S)) { PlayerTank.canonRot += 0.01f; PlayerTank.BoneTransform(10, Matrix.CreateRotationX(PlayerTank.canonRot)); } if (KState.IsKeyDown(Keys.A)) { PlayerTank.turretRot += 0.01f; PlayerTank.BoneTransform(9, Matrix.CreateRotationY(PlayerTank.turretRot)); } if (KState.IsKeyDown(Keys.D)) { PlayerTank.turretRot -= 0.01f; PlayerTank.BoneTransform(9, Matrix.CreateRotationY(PlayerTank.turretRot)); } if (KState.IsKeyDown(Keys.Left)) { PlayerTank.steelRot += 0.015f; if (PlayerTank.steelRot > 0.5f) { PlayerTank.steelRot = 0.5f; } PlayerTank.BoneTransform(3, Matrix.CreateRotationY(PlayerTank.steelRot)); PlayerTank.BoneTransform(7, Matrix.CreateRotationY(PlayerTank.steelRot)); } if (KState.IsKeyDown(Keys.Right)) { PlayerTank.steelRot -= 0.015f; if (PlayerTank.steelRot < -0.5f) { PlayerTank.steelRot = -0.5f; } PlayerTank.BoneTransform(3, Matrix.CreateRotationY(PlayerTank.steelRot)); PlayerTank.BoneTransform(7, Matrix.CreateRotationY(PlayerTank.steelRot)); } if (KState.IsKeyDown(Keys.Down)) { if (CheckInclinationForMove(PlayerTank) || PlayerTank.enableBackward == true) { if (CheckInclinationForMove(PlayerTank)) { PlayerTank.enableBackward = false; } PlayerTank.BodyRot -= delta * PlayerTank.steelRot; DoTankTransform(PlayerTank); PlayerTank.wheelRot -= 0.05f; PlayerTank.RotateWheels(PlayerTank.wheelRot); PlayerTank.Position += PlayerTank.GetTankTranslation() - PlayerTank.GetTankDirection() * 0.01f; PlayerTank.Position = new Vector3(PlayerTank.Position.X, TerrainList[4].GetHeight(PlayerTank.Position.X, PlayerTank.Position.Z), PlayerTank.Position.Z); } else { PlayerTank.enableForward = true; } } if (KState.IsKeyDown(Keys.Up)) { if (CheckInclinationForMove(PlayerTank) || PlayerTank.enableForward == true) { if (CheckInclinationForMove(PlayerTank)) { PlayerTank.enableForward = false; } PlayerTank.BodyRot += delta * PlayerTank.steelRot; DoTankTransform(PlayerTank); PlayerTank.wheelRot += 0.05f; PlayerTank.RotateWheels(PlayerTank.wheelRot); PlayerTank.Position += PlayerTank.GetTankTranslation() + PlayerTank.GetTankDirection() * 0.01f; PlayerTank.Position = new Vector3(PlayerTank.Position.X, TerrainList[4].GetHeight(PlayerTank.Position.X, PlayerTank.Position.Z), PlayerTank.Position.Z); } else { PlayerTank.enableBackward = true; } } if (KState.IsKeyDown(Keys.F)) { if (PlayerTank.Bullet.IsFired == false) { SFXManager.Play("PlayerShot"); } PlayerTank.BulletFire(); } }