C# 漫游及;蔡斯人工智能代码不工作

C# 漫游及;蔡斯人工智能代码不工作,c#,xna,artificial-intelligence,behavior,C#,Xna,Artificial Intelligence,Behavior,为敌人建立一个敌人AI系统,当玩家不在敌人距离内时,该系统应在敌人周围闲逛,当玩家在敌人距离内时,该系统应启动追逐行为并追逐玩家,直到玩家成功退出敌人的追逐半径 目前,敌人可以自由思考,但当玩家靠近敌人时,敌人将继续游荡,而不是追逐玩家 有人帮我解决这个问题吗 代码如下 public enum AIState { Chasing, Wander } private float maxSpeed; privat

为敌人建立一个敌人AI系统,当玩家不在敌人距离内时,该系统应在敌人周围闲逛,当玩家在敌人距离内时,该系统应启动追逐行为并追逐玩家,直到玩家成功退出敌人的追逐半径

目前,敌人可以自由思考,但当玩家靠近敌人时,敌人将继续游荡,而不是追逐玩家

有人帮我解决这个问题吗

代码如下

public enum AIState
    {
        Chasing,           
        Wander
    }

    private float maxSpeed;
    private float maxRotation;
    private float chaseDistance; 
    private float hysteresis;

    private Texture2D texture;
    private Vector2 drawingOrigin;
    private Vector2 position;
    public AIState aiState = AIState.Wander;
    private float orientation;

    private Random random = new Random();
    private Rectangle viewportbounds;
    public Rectangle boundingBox;
    public Vector2 playerPosition;

    private Vector2 heading;


    public Virtual_Aliens(Rectangle pos, Rectangle b)
    {


        position = new Vector2(300, 400);

        boundingBox = new Rectangle(pos.X, pos.Y, pos.Width, pos.Height);

        viewportbounds = new Rectangle(b.X, b.Y, b.Width, b.Height);

        orientation = 0.0f;

        heading = new Vector2(0, 0);

        maxSpeed = 2.0f;

        maxRotation = 0.20f;

        hysteresis = 15.0f;

        chaseDistance = 250.0f;

        Thread.Sleep(200);

        random = new Random();

    }

    public void LoadContent(ContentManager Content)
    {
        texture = Content.Load<Texture2D>("images/asteroid");
    }

    private Vector2 OrientationAsVector(float orien)
    {
        Vector2 orienAsVect;

        orienAsVect.X = (float)Math.Cos(orien);
        orienAsVect.Y = (float)Math.Sin(orien);

        return orienAsVect;
    }

    Vector2 wanderPosition = new Vector2();

    public void Wander()
    {
        // the max +/- the agent will wander from its current position
        float wanderLimits = 0.5f;

        // this defines what proportion of its maxRotation speed the agent will turn
        float turnFactor = 0.15f;

        // randomly define a new position
        wanderPosition.X += MathHelper.Lerp(-wanderLimits, wanderLimits, (float)random.NextDouble());
        wanderPosition.Y += MathHelper.Lerp(-wanderLimits, wanderLimits, (float)random.NextDouble());

        if (wanderPosition != Vector2.Zero)
        {
            wanderPosition.Normalize();
        }

        orientation = TurnToFace(wanderPosition, orientation, turnFactor * maxRotation);

        heading = OrientationAsVector(orientation);

        position += heading * 0.5f * maxSpeed;

        WrapForViewport();
    }

    private void WrapForViewport()
    {
        if (position.X < 0)
        {
            position.X = viewportbounds.Width;
        }
        else if (position.X > viewportbounds.Width)
        {
            position.X = 0;
        }

        if (position.Y < 0)
        {
            position.Y = viewportbounds.Height;
        }
        else if (position.Y > viewportbounds.Height)
        {
            position.Y = 0;
        }
    }

    private float WrapAngle(float radian)
    {
        while (radian < -MathHelper.Pi)
        {
            radian += MathHelper.TwoPi;
        }
        while (radian > MathHelper.Pi)
        {
            radian -= MathHelper.TwoPi;
        }
        return radian;
    }

    private float TurnToFace(Vector2 steering, float currentOrientation, float turnSpeed)
    {
        float newOrientation;
        float desiredOrientation;
        float orientationDifference;

        float x = steering.X;
        float y = steering.Y;

        // the desiredOrientation is given by the steering vector
        desiredOrientation = (float)Math.Atan2(y, x);

        // find the difference between the orientation we need to be
        // and our current Orientation
        orientationDifference = desiredOrientation - currentOrientation;

        // now using WrapAngle to get result from -Pi to Pi 
        // ( -180 degrees to 180 degrees )
        orientationDifference = WrapAngle(orientationDifference);

        // clamp that between -turnSpeed and turnSpeed.
        orientationDifference = MathHelper.Clamp(orientationDifference, -turnSpeed, turnSpeed);

        // the closest we can get to our target is currentAngle + orientationDifference.
        // return that, using WrapAngle again.
        newOrientation = WrapAngle(currentOrientation + orientationDifference);

        return newOrientation;
    }

    public void Update(GameTime gameTime)
    {            
        if (aiState == AIState.Wander)
        {
            chaseDistance -= hysteresis / 2;
        }

        else if (aiState == AIState.Chasing)
        {
            chaseDistance += hysteresis / 2;    
        }

        float distanceFromPlayer = Vector2.Distance(position, playerPosition);

        if (distanceFromPlayer > chaseDistance)
        {     
            aiState = AIState.Wander;
        }
        else
        {
            aiState = AIState.Chasing;
        }

        float currentSpeed;

        if (aiState == AIState.Chasing)
        {
            orientation = TurnToFace(playerPosition, orientation, maxRotation);
            currentSpeed = maxSpeed;
        }
        else if (aiState == AIState.Wander)
        {
            Wander();
        }
    }

    public void Draw(SpriteBatch spriteBatch)
    {
        boundingBox.X = (int)position.X;
        boundingBox.Y = (int)position.Y;

        drawingOrigin = new Vector2(texture.Width / 2, texture.Height / 2);

        spriteBatch.Draw(texture, boundingBox, null, Color.White, orientation, drawingOrigin, SpriteEffects.None, 0.0f);
    }

    public Vector2 PlayerPosition
    {
        set
        {
            playerPosition = value;
        }
        get
        {
            return playerPosition;
        }
    }
public enum AIState
{
追逐,
漫步
}
私有浮点最大速度;
私有浮动最大旋转;
私人距离;
私人浮动滞后;
私有纹理2D纹理;
私有向量2绘制源;
私有向量2位置;
公共AIState=AIState.Wander;
私人浮动导向;
私有随机=新随机();
私有矩形视口边界;
公共矩形边框;
公共矢量2播放位置;
专用矢量2标题;
公共虚拟单元(矩形位置,矩形b)
{
位置=新矢量2(300400);
boundingBox=新矩形(位置X、位置Y、位置宽度、位置高度);
视口边界=新矩形(b.X、b.Y、b.宽度、b.高度);
方向=0.0f;
航向=新矢量2(0,0);
最大速度=2.0f;
最大旋转=0.20f;
滞后=15.0f;
距离=250.0f;
睡眠(200);
随机=新随机();
}
公共void加载内容(ContentManager内容)
{
纹理=内容。加载(“图像/小行星”);
}
专用矢量2方向矢量(浮动方向)
{
矢量2定向矢量;
orienAsVect.X=(float)Math.Cos(orien);
orienAsVect.Y=(float)Math.Sin(orien);
返回方向向量;
}
Vector2 wanderPosition=新Vector2();
公共空间漫游()
{
//代理将从其当前位置漂移的最大+/-
浮动漂移限值=0.5f;
//这定义了代理将转动的最大旋转速度的比例
浮动转动系数=0.15f;
//随机定义一个新位置
wanderPosition.X+=MathHelper.Lerp(-wanderLimits,wanderLimits,(float)random.NextDouble());
wanderPosition.Y+=MathHelper.Lerp(-wanderLimits,wanderLimits,(float)random.NextDouble());
if(wanderPosition!=矢量2.0)
{
wanderPosition.Normalize();
}
方向=旋转面(移动位置、方向、旋转系数*最大旋转);
航向=方向航向(方向);
位置+=航向*0.5f*最大速度;
WrapForViewport();
}
私有void WrapForViewport()
{
如果(位置X<0)
{
position.X=viewportbounds.Width;
}
else if(position.X>viewportbounds.Width)
{
位置X=0;
}
如果(位置Y<0)
{
position.Y=viewportbounds.Height;
}
else if(position.Y>viewportbounds.Height)
{
位置Y=0;
}
}
私人浮标船(浮标弧度)
{
而(弧度<-MathHelper.Pi)
{
弧度+=MathHelper.TwoPi;
}
而(弧度>MathHelper.Pi)
{
弧度-=MathHelper.TwoPi;
}
返回弧度;
}
专用浮动回转面(矢量2转向、浮动电流方向、浮动回转速度)
{
浮动新方向;
漂浮欲望定向;
浮动方向差;
浮动x=转向.x;
浮动y=转向。y;
//期望方向由转向矢量给出
desiredOrientation=(浮点)数学Atan2(y,x);
//找出我们需要的方向之间的差异
//以及我们目前的方向
方向差异=期望方向-当前方向;
//现在使用WrapAngle从-Pi到-Pi得到结果
//(-180度到180度)
方向差异=缠绕(方向差异);
//将其夹在-turnSpeed和turnSpeed之间。
方向差=MathHelper.Clamp(方向差,-转速,转速);
//最接近目标的是currentAngle+orientationDifference。
//再次使用WrapAngle返回。
newOrientation=WrapAngle(当前方向+方向差异);
回归新方向;
}
公开作废更新(游戏时间游戏时间)
{            
if(aiState==aiState.Wander)
{
距离-=滞后/2;
}
else if(aiState==aiState.Chasing)
{
距离+=滞后/2;
}
float Distance from player=Vector2.距离(位置,playerPosition);
if(距离玩家>追逐距离)
{     
aiState=aiState.Wander;
}
其他的
{
aiState=aiState.追逐;
}
浮子电流速度;
if(aiState==aiState.Chasing)
{
方向=旋转面(播放位置、方向、最大旋转);
当前速度=最大速度;
}
else if(aiState==aiState.Wander)
{
漫游();
}
}
公共作废抽签(SpriteBatch SpriteBatch)
{
boundingBox.X=(int)position.X;
boundingBox.Y=(int)position.Y;
drawingOrigin=新矢量2(纹理.宽度/2,纹理.高度/2);
spriteBatch.Draw(纹理、边界框、空值、颜色、白色、方向、drawingOrigin、SpriteEffects.None、0.0f);
}
公共矢量2播放机位置
{
设置
{
播放位置=值;
}
得到
{
返回p
float distanceFromPlayer = Vector2.Distance(position, playerPosition);