public override void Update(float elapsedTime) { State s; s.position = Position; s.velocity = DesiredVelocity; s.direction = Rotation; s.maxSpeed = 100.0f; s.maxTurn = 0.025f; s.shoot = false; State newState = new State(); //controller.Update(s); DesiredVelocity = newState.velocity; Rotation = newState.direction; Position += DesiredVelocity * elapsedTime; // Use "RemoveAll" function to iterate over a list and handle removals. Children.ForEach((Entity ent) => { ent.Update(elapsedTime); }); }
/// <summary> /// Updates the State of a ship /// </summary> public State update(State ship) { Vector2 destination; if (goingStart) destination = start; else destination = finish; float wantedDirection = (float)Math.Atan2(destination.Y - ship.position.Y, destination.X - ship.position.X); if (wantedDirection < 0) wantedDirection += MathHelper.Pi * 2f; if (Vector2.Distance(ship.position, destination) < ship.maxSpeed) { goingStart = !goingStart; ship.velocity = Vector2.Zero; } else if (wantedDirection == ship.direction) { //(Math.Abs(wantedDirection - ship.theta) <0.001) ship.velocity = new Vector2((float) Math.Cos(ship.direction*ship.maxSpeed), (float) Math.Sin(ship.direction*ship.maxSpeed)); } else { //I gave up trying to find a good value for tweaking if wantedDirection and theta are equal //I hate floats now. ship.velocity = Vector2.Zero; if (ship.direction < wantedDirection) { if (ship.direction + ship.maxTurn > wantedDirection) ship.direction = wantedDirection; else ship.direction += ship.maxTurn; } else { if (ship.direction - ship.maxTurn < wantedDirection) ship.direction = wantedDirection; else ship.direction -= ship.maxTurn; } } return ship; }
private float waitTimer; //countdown timer for ships to stay inert while neutral #endregion Fields #region Constructors /// <summary> /// Creates a new AI with given spawnpoint and given environment /// Initial state is Neutral /// </summary> public AIController(SpawnPoint sp, GameEnvironment e) { timeSinceLastStateChange = 0; spawn = sp; env = e; nextState = State.Neutral; currentState = State.Neutral; target = null; answeringDistressCall = false; lookingFor = null; currentShip = null; timeSinceHitWall = 0; ; timeSinceChangedTargets = 0; waitTimer = 0; patrolPoints = new List<Vector2>(); patrolPoints.Add(randomPatrolPoint()); oldTarget = null; timeSinceMoved = 0; oldPosition = new Vector2(-1000, 1000);//I hope this is improbable timeSinceSawTarget = 0; timeSinceAnsweredDistressCall = 0; targetList = new List<GameEntity>(); }
/// <summary> /// Method to call to change to neutral state /// </summary> private void changeToNeutral() { timeSinceSawTarget = 0; oldTarget = target; target = null; lookingFor = null; currentState = State.Neutral; nextState = State.Neutral; answeringDistressCall = false; timeSinceLastStateChange = 0; timeSinceChangedTargets = 0; targetList.Clear(); }
/// <summary> /// Method to call to change to hostile state with target t /// </summary> private void changeToHostile(GameEntity t) { timeSinceSawTarget = 0; oldTarget = target; target = t; lookingFor = null; currentState = State.Hostile; nextState = State.Hostile; answeringDistressCall = false; timeSinceLastStateChange = 0; timeSinceChangedTargets = 0; targetList.Clear(); }
/// <summary> /// Method to call to change to confused state while looking for l and if adc(AnsweringDistressCall) /// </summary> private void changeToConfused(GameEntity l, bool adc) { timeSinceSawTarget = 0; oldTarget = target; target = null; lookingFor = l; currentState = State.Confused; nextState = State.Confused; answeringDistressCall = adc; timeSinceLastStateChange = 0; timeSinceChangedTargets = 0; }
/// <summary> /// Method to call to change to allied state with target t /// </summary> private void changeToAllied(GameEntity t) { timeSinceSawTarget = 0; oldTarget = target; target = t; lookingFor = null; currentState = State.Allied; nextState = State.Allied; answeringDistressCall = false; timeSinceLastStateChange = 0; timeSinceChangedTargets = 0; }
/// <summary> /// Method to call to change to alert state with target t /// </summary> private void changeToAlert(GameEntity t) { if (currentState != State.Alert) { env.AlertEffect.Trigger(currentShip.Position); } timeSinceSawTarget = 0; oldTarget = target; target = t; lookingFor = null; currentState = State.Alert; nextState = State.Alert; answeringDistressCall = false; timeSinceLastStateChange = 0; timeSinceChangedTargets = 0; targetList.Clear(); }
/// <summary> /// Updates the State of a ship /// </summary> public void Update(Ship s, float elapsedTime) { currentShip = s; currentState = nextState; currentShip.ResetMaxRotVel();//For the turning speed slowdown of neutral state if ((oldPosition.Equals(new Vector2(-1000, 1000)) || !oldPosition.Equals(currentShip.Position))) { timeSinceMoved = 0; oldPosition = currentShip.Position; } else { timeSinceMoved += elapsedTime; } timeSinceHitWall += elapsedTime; timeSinceChangedTargets += elapsedTime; timeSinceLastStateChange += elapsedTime; timeSinceAnsweredDistressCall += elapsedTime; switch (currentState) { case State.Allied: Allied(elapsedTime); break; case State.Neutral: Neutral(elapsedTime); break; case State.Alert: Alert(elapsedTime); break; case State.Confused: Confused(elapsedTime); break; case State.Disabled: Disabled(elapsedTime); break; case State.Hostile: Hostile(elapsedTime); break; } }