protected override void InitializeWorld() { World.SetWorldCoordinate(new Vector2(0, 0), kWorldWidth); mPatrols = new List <PatrolObject>(); mPatrols.Add(new PatrolObject()); mHero = new HeroObject(); }
private const float kDistToBeginChase = 10f; // this is the distance to trigger partol chasing of hero private void DetectHero(HeroObject hero) { Vector2 toHero = hero.Center - Center; if (toHero.Length() < kDistToBeginChase) { mStateTimer = kStateTimer * 2; // 5 times as much time for chasing Speed *= 3f; // twice the current speed! mCurrentState = PatrolState.ChaseHero; mTargetPosition = hero.Center; Color = Color.Red; } }
private bool UpdateChaseHeroState(HeroObject hero, float distToHero) { bool caught = false; caught = Collided(hero); mTargetPosition = hero.Center; if (caught || (mStateTimer < 0)) { if (caught) { hero.Caught(); } Color = new Color(XNACS1Base.RandomFloat(0f, 0.4f), XNACS1Base.RandomFloat(0f, 0.4f), XNACS1Base.RandomFloat(0f, 0.4f)); // now tansit out of current state ... Vector2 midPt = 0.5f * (XNACS1Base.World.WorldMax - XNACS1Base.World.WorldMin); if (CenterX > midPt.X) { if (CenterY > midPt.Y) { mCurrentState = PatrolState.TopRightRegion; mTargetPosition = RandomTopRightPosition(); } else { mCurrentState = PatrolState.BottomRightRegion; mTargetPosition = RandomBottomRightPosition(); } } else { if (CenterY > midPt.Y) { mCurrentState = PatrolState.TopLeftRegion; mTargetPosition = RandomTopLeftPosition(); } else { mCurrentState = PatrolState.BottomLeftRegion; mTargetPosition = RandomBottomLeftPosition(); } } mStateTimer = kStateTimer; ComputePositionAndVelocity(); } return(caught); }
private void UpdateTopLeftState(HeroObject hero, float distToTarget) { DetectHero(hero); // check if we should transit to ChaseHero if ((mStateTimer < 0) || (distToTarget < 5f)) { mStateTimer = (int)(kStateTimer * XNACS1Base.RandomFloat(0.8f, 1.2f)); // 20% randomness if (XNACS1Base.RandomFloat() > 0.5f) { mCurrentState = PatrolState.BottomLeftRegion; mTargetPosition = RandomBottomLeftPosition(); } else { mCurrentState = PatrolState.TopRightRegion; mTargetPosition = RandomTopRightPosition(); } ComputePositionAndVelocity(); } }
public bool Update(HeroObject hero) { bool caught = false; // perform operation common to all states ... mStateTimer--; Vector2 toTarget = mTargetPosition - Center; float distToTarget = toTarget.Length(); toTarget /= distToTarget; // this is the same as normalization ComputeNewDirection(toTarget); // operations specific to each states// operations specific to each states switch (mCurrentState) { case PatrolState.BottomLeftRegion: UpdateBottomLeftState(hero, distToTarget); break; case PatrolState.BottomRightRegion: UpdateBottomRightState(hero, distToTarget); break; case PatrolState.TopRightRegion: UpdateTopRightState(hero, distToTarget); break; case PatrolState.TopLeftRegion: UpdateTopLeftState(hero, distToTarget); break; case PatrolState.ChaseHero: caught = UpdateChaseHeroState(hero, distToTarget); break; } return(caught); }