private void SelectTarget(EnemyShip ship) { List <Ship> ships = ship.GameWorld.AllShips; //int index;// = RandomHelper.Rand() % ships.Count; //Locate nearest target Ship targ = null; float distance = float.PositiveInfinity; foreach (var item in ships) { if (item == ship) { continue; } var tmpDist = (item.Position - ship.Position).Length(); if (tmpDist < distance) { distance = tmpDist; targ = item; } } //if (ships[index] == ship) //{ // index++; // if (index == ships.Count) index = 0; //} ship.Target = targ; }
public override void Exit(object owner) { EnemyShip ship = owner as EnemyShip; if (ship != null) { ship.VelocityScalar = 0.0f; } }
public override void Enter(object owner) { EnemyShip ship = owner as EnemyShip; if (ship != null) { ship.VelocityScalar = 5000.0f; } curTime = 0.0; }
public override void Enter(object owner) { EnemyShip ship = owner as EnemyShip; if (ship != null) { ship.VelocityScalar = 5000.0f; SelectTarget(ship); } }
/// <summary> /// Initalize the game /// </summary> protected override void Initialize() { base.Initialize(); debug = new DebugDraw(GraphicsDevice); // Load ship and add to collision manager Ship ship = new Ship(GraphicsDevice); ship.WorldBounds = new Vector3(WorldBoundX, 0.0f, WorldBoundZ); world.SetPlayer(ship); world.Player.Tag(); // Load enemy ships and add to collision manager const int numEnemies = 3; for (int i = 0; i < numEnemies; i++) { EnemyShip enemy = new EnemyShip(GraphicsDevice); // Generate two floats between -1.0f and 1.0f float randX = RandomHelper.RandFloat(); float randZ = RandomHelper.RandFloat(); enemy.Position = new Vector3(randX * WorldBoundX, 350.0f, randZ * WorldBoundZ); enemy.WorldBounds = new Vector3(WorldBoundX, 0.0f, WorldBoundZ); world.AddEnemy(enemy); } // Initialise the collidable objects InitializeCollidableObjects(); // Set camera perspective float nearPlaneDistance = 100.0f; float farPlaneDistance = 1000000.0f; // Create the view and projection matrices view = Matrix.CreateLookAt(new Vector3(0.0f, 40000.0f, 50.0f), new Vector3(0.0f, 0.0f, 0.0f), Vector3.UnitY); float aspectRatio = (float)graphics.GraphicsDevice.Viewport.Width / graphics.GraphicsDevice.Viewport.Height; projection = Matrix.CreatePerspectiveFieldOfView(MathHelper.ToRadians(45.0f), aspectRatio, nearPlaneDistance, farPlaneDistance); InitializeBindings(); }
/// <summary> /// Initalize the game /// </summary> protected override void Initialize() { base.Initialize(); debug = new DebugDraw(GraphicsDevice); // Load ship and add to collision manager ship = new Ship(GraphicsDevice); // Load enemy ships and add to collision manager const int numEnemies = 5; enemies = new List <EnemyShip>(); Random rand = new Random(); for (int i = 0; i < numEnemies; i++) { EnemyShip enemy = new EnemyShip(GraphicsDevice); // Generate two floats between -1.0f and 1.0f float randX = (float)(rand.NextDouble() - rand.NextDouble()); float randZ = (float)(rand.NextDouble() - rand.NextDouble()); enemy.Position = new Vector3(randX * 50000.0f, 350.0f, randZ * 50000.0f); enemies.Add(enemy); } // Initialise the collidable objects InitializeCollidableObjects(); // Set the camera aspect ratio // This must be done after the class to base.Initalize() which will // initialize the graphics device. camera.AspectRatio = (float)graphics.GraphicsDevice.Viewport.Width / graphics.GraphicsDevice.Viewport.Height; // Perform an inital reset on the camera so that it starts at the resting // position. If we don't do this, the camera will start at the origin and // race across the world to get behind the chased object. // This is performed here because the aspect ratio is needed by Reset. UpdateCameraChaseTarget(); camera.Reset(); InitializeBindings(); }
public override void Execute(object owner, GameTime gameTime) { EnemyShip ship = owner as EnemyShip; if (ship == null) { return; } if (ship.GameWorld.CurrentTagger == null) { return; } Vector3 taggerPosition = ship.GameWorld.CurrentTagger.Position; ship.Direction = ship.Position - taggerPosition; ship.Direction.Normalize(); }
public override void Execute(object owner, GameTime gameTime) { EnemyShip ship = owner as EnemyShip; if (ship == null) { return; } if (curTime >= directionChangeTime) { curTime = 0.0; ship.SetRandomDirection(); } else { curTime += gameTime.ElapsedGameTime.TotalSeconds; } }
public override void Execute(object owner, GameTime gameTime) { EnemyShip ship = owner as EnemyShip; if (ship == null) { return; } if (ship.Target == null) { SelectTarget(ship); } if (ship.Target != null) { Vector3 targetPosition = ship.Target.Position; ship.Direction = targetPosition - ship.Position; ship.Direction.Normalize(); } }
public void AddEnemy(EnemyShip e) { e.GameWorld = this; enemies.Add(e); allShips.Add(e); }