public override Vector2 GetVector(GameObject obj) { AIReactions reaction; Vector2 reactionVector = Vector2.Zero; float distance = Math.Abs(Vector2.Distance(mEnemy.Position, obj.Position)); if (distance > 30.0f) { //The current vector of the mouse Vector2 result = obj.Position - this.mEnemy.Position; //Do some awesome trig double radian = Math.Atan(result.Y / result.X); float rotation = (float)radian; if (obj.Position.X <= this.mEnemy.Position.X) { //We have to subtract pi from it to get the correct radian angle rotation = rotation - (float)Math.PI; } reactionVector = new Vector2((float)Math.Cos(rotation), (float)Math.Sin(rotation)); reaction = new AIReactions(rotation, reactionVector); } else { //Align it reactionVector = obj.Direction; } return reactionVector; }
public AIReactions ReactToObjects(List<GameObject> objects) { mReactionVector = Vector2.Zero; mTotalRotation = 0.0f; foreach (GameObject obj in objects) { numOfObjectsSeen++; float distance = Vector2.Distance(this.mEnemy.Position, obj.Position); //So we don't get a negative answer distance = Math.Abs(distance); Vector2 reaction; switch (GetProperReaction(obj, obj.Type)) { case "Align": reaction = mAlign.GetVector(obj); mReactionVector += reaction; break; //case "Dodge": // reaction = mDodge.GetVector(obj); // mReactionVector += reaction; // break; case "Cohesion": reaction = mCohesion.GetVector(obj); mReactionVector += reaction; break; case "Idle": numOfObjectsSeen--; reaction = mIdle.GetVector(obj); mReactionVector += reaction; break; case "Chase": reaction = mChase.GetVector(obj); mReactionVector += reaction; break; default: numOfObjectsSeen--; reaction = mIdle.GetVector(obj); mReactionVector += reaction; break; } } //This will result in a proper vector if (numOfObjectsSeen <= 0) //we don't to freak out the computer { if (mReactionVector.X <= 0.0f) { mTotalRotation -= (float)Math.PI; } return new AIReactions(mTotalRotation, mReactionVector); } mReactionVector /= numOfObjectsSeen; if (mReactionVector == Vector2.Zero) { return new AIReactions(0.0f, mReactionVector); } mTotalRotation = (float)Math.Atan(mReactionVector.Y / mReactionVector.X); Vector2 temp = new Vector2(this.Enemy.Position.X, 0.0f); if (mReactionVector.X <= 0.0f) { mTotalRotation -= (float)Math.PI; } mReturnReaction = new AIReactions(mTotalRotation, mReactionVector); numOfObjectsSeen = 0; return mReturnReaction; }