protected override void OnNextMove() { base.OnNextMove(); // only attack if not blocked there. if (IsAttacking && !ParentThing.CollidesWithBackground(Leader.FacingDirection)) { TargetMove = Leader.FacingDirection; IsTargetMoveDefined = true; } }
protected override void OnUpdate(ref UpdateParams p) { base.OnUpdate(ref p); Vector2 rightHandDirection = RotateVector2(CurrentDirection, MathHelper.PiOver2); Vector2 leftHandDirection = RotateVector2(CurrentDirection, -MathHelper.PiOver2); bool isRightHandFree = !ParentThing.CollidesWithBackground(rightHandDirection); bool isLeftHandFree = !ParentThing.CollidesWithBackground(leftHandDirection); bool isFrontFree = !ParentThing.CollidesWithBackground(CurrentDirection); // keep this control active if near walls if (didSeeWall || !isRightHandFree || !isLeftHandFree || !isFrontFree) { IsTargetMoveDefined = true; AllowNextMove(); } }
protected override void OnNextMove() { base.OnNextMove(); Vector2 rightHandDirection = RotateVector2(CurrentDirection, MathHelper.PiOver2); Vector2 leftHandDirection = RotateVector2(CurrentDirection, -MathHelper.PiOver2); bool isRightHandFree = !ParentThing.CollidesWithBackground(rightHandDirection); bool isLeftHandFree = !ParentThing.CollidesWithBackground(leftHandDirection); bool isFrontFree = !ParentThing.CollidesWithBackground(CurrentDirection); // change direction to righthand if that's free if (didSeeWall && isRightHandFree) { CurrentDirection = rightHandDirection; didSeeWall = false; TargetMove = CurrentDirection; IsTargetMoveDefined = true; } else if (!isFrontFree) { // turn left if the way is blocked CurrentDirection = leftHandDirection; didSeeWall = true; } else if (didSeeWall || !isRightHandFree || !isLeftHandFree || !isFrontFree) { TargetMove = CurrentDirection; IsTargetMoveDefined = true; } if (!isRightHandFree) { didSeeWall = true; } }
protected override void OnNextMove() { base.OnNextMove(); // compute direction towards chase-target Vector2 dif = ChaseTarget.Position - ParentThing.Target; if (Avoidance) { dif = -dif; } if (dif.Length() == 0f) { return; } Vector2 difX = new Vector2(dif.X, 0f); Vector2 difY = new Vector2(0f, dif.Y); if (difX.Length() > 0f) { difX.Normalize(); } if (difY.Length() > 0f) { difY.Normalize(); } // whats-free bool isBlockedX = (difX.X != 0f) && ParentThing.ChecksCollisions && ParentThing.CollidesWithBackground(difX); bool isBlockedY = (difY.Y != 0f) && ParentThing.ChecksCollisions && ParentThing.CollidesWithBackground(difY); bool isFullyBlocked = (isBlockedX && (dif.Y == 0f)) || (isBlockedY && (dif.X == 0f)) || (isBlockedX && isBlockedY); bool isDiagonal = (dif.X != 0f) && (dif.Y != 0f); // choose one direction 1) based on whats free, then 2) semi-randomly, if diagonals would be required isPauseChase = false; if (isFullyBlocked) { dif = new Vector2(difY.Y, difX.X); // try a swap to get around obstacle } else if (isBlockedX) // choose Y move if x is blocked { dif = difY; } else if (isBlockedY) // choose X move if y is blocked { dif = difX; } else if (isDiagonal) // choose random x/y if a diagonal move would be required { float r = RandomMath.RandomUnit(); float thres = 0.5f; if (r > thres) { dif = difX; } else { dif = difY; } } else { dif.Normalize(); } TargetMove = dif; }