public override void Update(GameTime gameTime, Map gameMap) { projectileTime -= gameTime.ElapsedGameTime.TotalMilliseconds; if (!gameMap.CheckTileCollision(Position + new Vector2(_faceDir*8, 10))) Speed.X = -Speed.X; if (Helper.Random.Next(20) == 0) _turretRotTarget = Helper.RandomFloat(MathHelper.PiOver2); _turretRot = MathHelper.Lerp(_turretRot, _turretRotTarget, 0.01f); if (_firing) { Vector2 screenPos = Vector2.Transform(Position, Camera.Instance.CameraMatrix); float pan = (screenPos.X - (Camera.Instance.Width / 2f)) / (Camera.Instance.Width / 2f); if (pan < -1f || pan > 1f) gunLoop.Volume = 0f; else gunLoop.Volume = 0.3f; if (Ship.Instance.Position.Y >= 260) gunLoop.Volume = 0f; gunLoop.Pan = MathHelper.Clamp(pan,-1f,1f); if (projectileTime <= 0) { projectileTime = projectileCoolDown; ProjectileController.Instance.Spawn(entity => { ((Projectile) entity).Type = ProjectileType.Forward1; ((Projectile) entity).SourceRect = new Rectangle(8, 8, 20, 8); ((Projectile) entity).Life = 1000; ((Projectile) entity).EnemyOwner = true; ((Projectile) entity).Damage = 1f; entity.Speed = Helper.AngleToVector(_turretRot, -5f); entity.Speed.X *= -_faceDir; entity.Position = Position + entity.Speed; }); } if (Helper.Random.Next(20) == 0) { gunLoop.Pause(); _firing = false; } } else { if (Helper.Random.Next(50) == 0) { gunLoop.Resume(); _firing = true; } } base.Update(gameTime, gameMap); }
public virtual void EnterVehicle(Map gameMap) { if (Dead || !Active) return; if (drivingVehicle == null) { foreach (Vehicle v in VehicleController.Instance.Vehicles) { if ((v.Position - Position).Length() < 200f) { drivingVehicle = v; break; } } } else { // Exit vehicle //bool found = false; if (drivingVehicle is Chopper && ((Chopper)drivingVehicle).Height > 0f) ((Chopper)drivingVehicle).Land(gameMap); else { for (float a = 0f; a < MathHelper.TwoPi; a += 0.5f) { Vector2 pos = Helper.PointOnCircle(ref drivingVehicle.Position, 200, a); if (!gameMap.CheckTileCollision(pos) && !Helper.IsPointInShape(pos, drivingVehicle.CollisionVerts)) { if (drivingVehicle is Boat || !LineCollision(pos, gameMap, true)) { Position = pos; drivingVehicle = null; break; } } } } } }
void CheckCollision(Map gameMap) { //collisionRect.Offset(new Point((int)Speed.X, (int)Speed.Y)); if (respawning) return; Rectangle? collRect; // Check for ledge grabs if ((jumping || falling) && !justUngrabbed) { if (Speed.X<0 && gameMap.CheckTileCollision(new Vector2(collisionRect.Left, collisionRect.Top), Layer)) if (!gameMap.CheckTileCollision(new Vector2(collisionRect.Left, collisionRect.Top - gameMap.TileHeight), Layer) && !gameMap.CheckTileCollision(new Vector2(collisionRect.Left + gameMap.TileWidth, collisionRect.Top - gameMap.TileHeight), Layer) && !gameMap.CheckTileCollision(new Vector2(collisionRect.Left + gameMap.TileWidth, collisionRect.Top), Layer)) { grabbed = true; jumping = false; falling = false; crouching = false; Speed.Y = 0; Speed.X = 0; grabbedPosition = new Vector2((int)(collisionRect.Left / gameMap.TileWidth) * gameMap.TileWidth, (int)(collisionRect.Top / gameMap.TileHeight) * gameMap.TileHeight) + new Vector2(gameMap.TileWidth, collisionRect.Height + 30); faceDir = -1; } if (Speed.X > 0 && gameMap.CheckTileCollision(new Vector2(collisionRect.Right, collisionRect.Top), Layer)) if (!gameMap.CheckTileCollision(new Vector2(collisionRect.Right, collisionRect.Top - gameMap.TileHeight), Layer) && !gameMap.CheckTileCollision(new Vector2(collisionRect.Right - gameMap.TileWidth, collisionRect.Top - gameMap.TileHeight), Layer) && !gameMap.CheckTileCollision(new Vector2(collisionRect.Right - gameMap.TileWidth, collisionRect.Top), Layer)) { grabbed = true; jumping = false; falling = false; crouching = false; Speed.Y = 0; Speed.X = 0; grabbedPosition = new Vector2((int)(collisionRect.Right / gameMap.TileWidth) * gameMap.TileWidth, (int)(collisionRect.Top / gameMap.TileHeight) * gameMap.TileHeight) + new Vector2(0, collisionRect.Height + 30); faceDir = 1; } } if (grabbed || climbing) return; collRect = CheckCollisionBottom(gameMap); if (collRect.HasValue) { if (falling) { Speed.Y = 0f; Position.Y -= collRect.Value.Height; collisionRect.Offset(0, -collRect.Value.Height); jumping = false; falling = false; justUngrabbed = false; Tile t = gameMap.GetTile(Position + new Vector2(0, 30), Layer); if (t != null) { if (t.Properties.Contains("Wood")) AudioController.PlaySFX("fstep-wood", -0.2f, 0.2f); else if (t.Properties.Contains("Metal")) AudioController.PlaySFX("fstep-metal", -0.2f, 0.2f); else AudioController.PlaySFX("fstep-grass", -0.2f, 0.2f); } else AudioController.PlaySFX("fstep-grass", -0.2f, 0.2f); } } else falling = true; if (Speed.Y < 0f) { collRect = CheckCollisionTop(gameMap); if (collRect.HasValue) { Speed.Y = 0f; Position.Y += collRect.Value.Height; collisionRect.Offset(justUngrabbed ? (collRect.Value.Width * (-faceDir)) : 0, collRect.Value.Height); falling = true; jumping = false; } } if (Speed.X > 0f) { collRect = CheckCollisionRight(gameMap); if (collRect.HasValue) { Speed.X = 0f; Position.X -= (collRect.Value.Width); collisionRect.Offset(-collRect.Value.Width, 0); } } if (Speed.X < 0f) { collRect = CheckCollisionLeft(gameMap); if (collRect.HasValue) { Speed.X = 0f; Position.X += collRect.Value.Width; collisionRect.Offset(collRect.Value.Width, 0); } } bool collided = false; for (int y = -1; y > -15; y--) { collisionRect.Offset(0, -1); collRect = CheckCollisionTop(gameMap); if (collRect.HasValue) collided = true; } if (!collided) crouching = false; }
bool CheckLineOfSight(Vector2 pos, Map gameMap) { if ((Position - pos).Length() > 500f) return false; if (checkLOSTime > 200) { checkLOSTime = 0; for (float a = (Rotation - MathHelper.PiOver2) - MathHelper.PiOver4; a < (Rotation - MathHelper.PiOver2) + MathHelper.PiOver4; a += 0.1f) { for (int r = 0; r < 500; r += 50) { Vector2 checkpos = Helper.PointOnCircle(ref Position, r, a); if (gameMap.CheckTileCollision(checkpos)) break; if ((checkpos - pos).Length() < 50f) return true; } } } return false; }
void CheckCollision(Map gameMap) { //collisionRect.Offset(new Point((int)Speed.X, (int)Speed.Y)); Rectangle? collRect; // Check for ledge grabs if ((jumping || falling) && !justUngrabbed) { if (Speed.X<0 && gameMap.CheckTileCollision(new Vector2(collisionRect.Left, collisionRect.Top))) if (!gameMap.CheckTileCollision(new Vector2(collisionRect.Left, collisionRect.Top - 50)) && !gameMap.CheckTileCollision(new Vector2(collisionRect.Left + 50, collisionRect.Top - 50)) && !gameMap.CheckTileCollision(new Vector2(collisionRect.Left + 50, collisionRect.Top))) { grabbed = true; jumping = false; falling = false; crouching = false; Speed.Y = 0; Speed.X = 0; grabbedPosition = new Vector2((int)(collisionRect.Left / gameMap.TileWidth) * gameMap.TileWidth, (int)(collisionRect.Top / gameMap.TileHeight) * gameMap.TileWidth) + new Vector2(50, 150); faceDir = -1; } if (Speed.X>0 && gameMap.CheckTileCollision(new Vector2(collisionRect.Right, collisionRect.Top))) if (!gameMap.CheckTileCollision(new Vector2(collisionRect.Right, collisionRect.Top - 50)) && !gameMap.CheckTileCollision(new Vector2(collisionRect.Right - 50, collisionRect.Top - 50)) && !gameMap.CheckTileCollision(new Vector2(collisionRect.Right - 50, collisionRect.Top))) { grabbed = true; jumping = false; falling = false; crouching = false; Speed.Y = 0; Speed.X = 0; grabbedPosition = new Vector2((int)(collisionRect.Right / gameMap.TileWidth) * gameMap.TileWidth, (int)(collisionRect.Top / gameMap.TileHeight) * gameMap.TileWidth) + new Vector2(0, 150); faceDir = 1; } } if (grabbed || climbing) return; collRect = CheckCollisionBottom(gameMap); if (collRect.HasValue) { if (falling) { Speed.Y = 0f; Position.Y -= collRect.Value.Height; collisionRect.Offset(0, -collRect.Value.Height); jumping = false; falling = false; justUngrabbed = false; } } else falling = true; if (Speed.Y < 0f) { collRect = CheckCollisionTop(gameMap); if (collRect.HasValue) { Speed.Y = 0f; Position.Y += collRect.Value.Height; collisionRect.Offset(justUngrabbed ? (collRect.Value.Width * (-faceDir)) : 0, collRect.Value.Height); falling = true; jumping = false; } } if (Speed.X > 0f) { collRect = CheckCollisionRight(gameMap); if (collRect.HasValue) { Speed.X = 0f; Position.X -= (collRect.Value.Width); collisionRect.Offset(-collRect.Value.Width, 0); } } if (Speed.X < 0f) { collRect = CheckCollisionLeft(gameMap); if (collRect.HasValue) { Speed.X = 0f; Position.X += collRect.Value.Width; collisionRect.Offset(collRect.Value.Width, 0); } } bool collided = false; for (int y = -1; y > -15; y--) { collisionRect.Offset(0, -1); collRect = CheckCollisionTop(gameMap); if (collRect.HasValue) collided = true; } if (!collided) crouching = false; }
public override void Update(GameTime gameTime, Map gameMap) { _target.X = Position.X + (_faceDir*200); //if (_target.X < 0) _target.X = (gameMap.Width * gameMap.TileWidth) - _target.X; //if (_target.X >= (gameMap.Width * gameMap.TileWidth)) _target.X = _target.X - (gameMap.Width * gameMap.TileWidth); if (Helper.Random.Next(100) == 0) _target.Y = Helper.RandomFloat(30f, (gameMap.TileHeight*gameMap.Height) -30f); //if (Helper.Random.Next(500) == 0) // _faceDir = -_faceDir; for(int dist = 0;dist<=150;dist+=50) if (gameMap.CheckTileCollision(new Vector2(_target.X + (dist*-_faceDir), Position.Y))) { _target.Y = Position.Y + (Position.Y < 260f ? -50f : 50f); Speed.X = MathHelper.Lerp(Speed.X, 0f, 0.1f); } //if (gameMap.CheckTileCollision(_target + new Vector2(-_faceDir * 50, 0))) //{ // _target.Y += _target.Y < 260f ? -50f : 50f; // Speed.X = MathHelper.Lerp(Speed.X, 0f, 0.1f); //} //if (gameMap.CheckTileCollision(_target + new Vector2(-_faceDir * 100, 0))) //{ // _target.Y += _target.Y < 260f ? -50f : 50f; // Speed.X = MathHelper.Lerp(Speed.X, 0f, 0.1f); //} //if (gameMap.CheckTileCollision(_target + new Vector2(-_faceDir * 130, 0))) //{ // _target.Y += _target.Y < 260f ? -50f : 50f; // Speed.X = MathHelper.Lerp(Speed.X, 0f, 0.1f); //} //if (Speed.Length() < 0.1f) _target = Position; //Rotation = Helper.V2ToAngle(Speed); Speed = Vector2.Lerp(Speed, Vector2.Zero, 0.05f); if (Vector2.Distance(_target, Position) > 5f) { Vector2 dir = _target - Position; dir.Normalize(); Speed += dir*0.2f; } Speed.X = MathHelper.Clamp(Speed.X, -3f, 3f); Speed.Y = MathHelper.Clamp(Speed.Y, -3f, 3f); if (Position.Y < 260f) ParticleController.Instance.Add(Position + new Vector2(-_faceDir*5f, 0f), new Vector2(Helper.RandomFloat(0f, -_faceDir*1f), Helper.RandomFloat(-0.2f, 0.2f)), 0, Helper.RandomFloat(500, 1000), 500, false, true, Position.Y < 260f ?new Rectangle(0,0,3,3):new Rectangle(128, 0, 16, 16), new Color(new Vector3(1f) * (0.5f + Helper.RandomFloat(0.5f))), ParticleFunctions.Smoke, Position.Y >= 260f ? 0.3f : 1f, 0f, Helper.RandomFloat(-0.1f, 0.1f), 0, ParticleBlend.Alpha); ParticleController.Instance.Add(Position + new Vector2(-_faceDir * 5f, 0f), new Vector2(Helper.RandomFloat(0f, -_faceDir * 0.2f), Helper.RandomFloat(-0.2f, 0.2f)), 0, Helper.RandomFloat(50, 150), 50, false, true, Position.Y < 260f ? new Rectangle(0, 0, 3, 3) : new Rectangle(128, 0, 16, 16), Position.Y < 260f ? Color.LightGreen : Color.White, ParticleFunctions.FadeInOut, Position.Y >= 260f ? 0.3f : 1f, 0f, Helper.RandomFloat(-0.1f, 0.1f), 0, ParticleBlend.Alpha); projectileTime -= gameTime.ElapsedGameTime.TotalMilliseconds; if (_firing) { Vector2 screenPos = Vector2.Transform(Position, Camera.Instance.CameraMatrix); float pan = (screenPos.X - (Camera.Instance.Width / 2f)) / (Camera.Instance.Width / 2f); if (pan < -1f || pan > 1f) gunLoop.Volume = 0f; else gunLoop.Volume = 0.3f; if (Ship.Instance.Position.Y >= 260 && Position.Y < 260) gunLoop.Volume = 0f; if (Ship.Instance.Position.Y < 260 && Position.Y >= 260) gunLoop.Volume = 0f; gunLoop.Pan = MathHelper.Clamp(pan, -1f, 1f); if (projectileTime <= 0) { projectileTime = projectileCoolDown; ProjectileController.Instance.Spawn(entity => { ((Projectile)entity).Type = ProjectileType.Forward1; ((Projectile)entity).SourceRect = new Rectangle(8, 8, 20, 8); ((Projectile)entity).Life = 1000; ((Projectile)entity).EnemyOwner = true; ((Projectile)entity).Damage = 1f; entity.Speed = new Vector2(6f*_faceDir, 0f); entity.Position = Position + entity.Speed; }); } if (Helper.Random.Next(20) == 0) { _firing = false; gunLoop.Pause(); } } else { if (Helper.Random.Next(50) == 0) { _firing = true; gunLoop.Resume(); } } if (Helper.Random.Next(200) == 0 && GameController.Wave >= 8) { AudioController.PlaySFX("seeker", 0.5f, -0.1f, 0.1f, Camera.Instance, Position); ProjectileController.Instance.Spawn(entity => { ((Projectile) entity).Type = ProjectileType.Seeker; ((Projectile) entity).SourceRect = new Rectangle(1, 18, 8, 4); ((Projectile) entity).Life = 2000; ((Projectile) entity).Scale = 1f; ((Projectile) entity).EnemyOwner = true; ((Projectile) entity).Damage = 5f; ((Projectile) entity).Target = Position + new Vector2(_faceDir*300, 0); ; entity.Speed = new Vector2(0f, -0.5f); entity.Position = Position + new Vector2(0, 0); }); } //if (Vector2.Distance(Position, _target) < 1f) //{ // _idleAnim.Pause(); // _hitAnim.Pause(); // if (Helper.Random.Next(100) == 0) // { // _target = Position + new Vector2(Helper.RandomFloat(-30, 30), Helper.RandomFloat(-30, 30)); // if (_target.Y < 280) _target.Y = 280; // Vector2 dir = _target - Position; // dir.Normalize(); // Speed = dir*5f; // _idleAnim.Play(); // _hitAnim.Play(); // } // if (Vector2.Distance(Ship.Instance.Position, Position) < 100f) // { // _target = Ship.Instance.Position; // if (_target.Y < 280) _target.Y = 280; // Vector2 dir = _target - Position; // dir.Normalize(); // Speed = dir * 5f; // _idleAnim.Play(); // _hitAnim.Play(); // } //} //else //{ // ParticleController.Instance.Add(Position, // new Vector2(Helper.RandomFloat(-0.1f,0.1f), Helper.RandomFloat(-0.1f,0.1f)), // 0, Helper.Random.NextDouble() * 1000, Helper.Random.NextDouble() * 1000, // false, false, // new Rectangle(128, 0, 16, 16), // new Color(new Vector3(1f) * (0.25f + Helper.RandomFloat(0.5f))), // ParticleFunctions.FadeInOut, // 0.5f, 0f, 0f, // 1, ParticleBlend.Alpha); //} base.Update(gameTime, gameMap); }
internal void Land(Map gameMap) { if (landing || takingOff) return; bool found = false; for (float a = 0f; a < MathHelper.TwoPi; a += 0.5f) { for (int r = 0; r < 300; r += 20) { Vector2 pos = Helper.PointOnCircle(ref Position, r, a); if (gameMap.CheckTileCollision(pos)) found = true; foreach (Vehicle v in VehicleController.Instance.Vehicles) if (v != this && Helper.IsPointInShape(pos, v.CollisionVerts)) found = true; foreach (Compound c in gameMap.Compounds) foreach (Building b in c.Buildings) if (b.Type == BuildingType.Building) if (b.Rect.Contains(Helper.VtoP(Position / 100))) found = true; } } if(!found) landing = true; }
public void Update(GameTime gameTime, Map gameMap, HeroDude gameHero, bool[,] mapFog, Camera gameCamera) { int count = 0; foreach (AIDude e in Enemies.Where(en => (gameHero.Position - en.Position).Length() < 4000f)) { count++; if ((gameHero.Position - e.Position).Length() < 2000f) { e.Update(gameTime, gameMap, gameHero, mapFog, gameCamera); bool alerted = false; if (gameHero.HuntedLevel.Level > 0f) { if (!alerted && Helper.Random.Next(11000 - (int)(gameHero.HuntedLevel.Level * 100)) == 1 && e.State == AIState.Patrolling && (gameHero.Position - e.Position).Length() < 2000f) { //e.InvestigatePosition(); alerted = true; } } } } // Spawn some new enemies if (count < 10 + (int)(gameHero.HuntedLevel.Level / 10)) { Vector2 pos = Helper.RandomPointInCircle(gameHero.Position, 2000f, 4000f); if (!gameMap.CheckTileCollision(pos) && pos.X > 0 && pos.X < (gameMap.Width * gameMap.TileWidth) && pos.Y > 0 && pos.Y < (gameMap.Height * gameMap.TileHeight) && !VehicleController.Instance.CheckVehicleCollision(pos)) { AIDude newDude = new AIDude(pos); newDude.LoadContent(SpriteSheet, graphicsDevice, lightingEngine, gameHero); newDude.Health = 10 + Helper.Random.Next(30); Enemies.Add(newDude); } } Enemies.RemoveAll(e => !e.Active); }