public Vector Execute(Tank tank, int timeElapsed) { Vector steeringForce = new Vector(0, 0); //Keep following path until finished if (tank.PlayerInAttackZone()) { ClearPath(); tank.ChangeState(new TankAttackPlayer()); } else if (tank.PlayerNotSeenAtLastLocation()) { ClearPath(); tank.ChangeState(new TankPatrol(tank)); } else if (path.Count == 0) { tank.gameWorld.GridLogic.CalculateNeighborsEntities(tank, Tank.MaxRadiusOfTankSeight); foreach (MovingEntity entity in tank.gameWorld.GridLogic.EntitiesInRange) { if (entity is Player) { searchAStar = new SearchAStar(tank, entity.InCell); searchAStar.Search(); path = searchAStar.GetPathToTarget(); if (path.Count == 0) { return(steeringForce); } //Path starts from end of list so the path following needs to be started at the end of the list i = path.Count - 1; steeringForce = seek.Execute(tank, path[i].Position) * GlobalVars.SeekingWeight; } } } else { if (i > 0) { //Seek current cell in path steeringForce = seek.Execute(tank, path[i].Position) * GlobalVars.SeekingWeight; // steeringForce += avoid.Execute(tank) * GlobalVars.ObstacleAvoidanceWeight; //go to next step in path if the tank approximatly reached the cell if (tank.DistanceToPosition(path[i].Position) <= GlobalVars.cellSize / 2) { i--; } } else { ClearPath(); } } return(steeringForce); }
public Vector Execute(Tank tank, int timeElapsed) { Vector steeringForce = new Vector(0, 0); if (tank.PlayerInSearchZone()) { tank.ChangeState(new TankSearchForPlayer()); } else if (tank.PlayerInDangerZone()) { tank.ChangeState(new TankCreateDistanceBetweenPlayer()); } else if (tank.PlayerIsOutOfSeight()) { tank.ChangeState(new TankPatrol(tank)); } else { tank.gameWorld.GridLogic.CalculateNeighborsEntities(tank, Tank.TankAttackDistance); foreach (MovingEntity entity in tank.gameWorld.GridLogic.EntitiesInRange) { if (entity is Player) { steeringForce = seek.Execute(tank, entity.Position); // Aim turret at player Vector playerTank = entity.Position - tank.Position; tank.AngleTankTurret = (float)Math.Atan2(playerTank.Y, playerTank.X); } } } return(steeringForce / 4); }
public Vector Execute(Vehicle vehicle) { //first find the center of mass of all the agents Vector CenterOfMass = new Vector(0, 0); Vector SteeringForce = new Vector(0, 0); int NeighborCount = 0; vehicle.gameWorld.GridLogic.CalculateNeighborsEntities(vehicle, vehicle.Radius); vehicles = vehicle.gameWorld.GridLogic.EntitiesInRange; //iterate through the neighbors and sum up all the position vectors for (int i = 0; i < vehicles.Count; i++) { //make sure *this* agent isn't included in the calculations and that //the agent being examined is close enough and of the same type if (vehicles[i] != vehicle && vehicles[i].GetType().Equals(vehicle.GetType())) { CenterOfMass += vehicles[i].Position; ++NeighborCount; } } if (NeighborCount > 0) { //the center of mass is the average of the sum of positions CenterOfMass /= NeighborCount; //now seek towards that position SteeringForce = seek.Execute(vehicle, CenterOfMass); //the magnitude of cohesion is usually much larger than separation or //allignment so it usually helps to normalize it. return(SteeringForce.Normalize()); } return(SteeringForce); }