XNA游戏编程:围成一圈的敌人(代码)

时间:2012-12-10 20:19:14

标签: random xna geometry spawning

有人可以就如何在一个圆圈中产生敌人(XNA编程)给我一些指示吗?

我想让敌人沿着窗户边界外的圆周随机产卵。我希望它们通过窗口中心的直线移动到他们开始的相对侧(或尽可能接近)。

理想情况下,这将创造一个环境,让敌人从看似所有方向随机进入。

到目前为止,这是我的敌人(“Baddies”)课程。我处理SetupTraveling游戏状态中的位置。我正在做的事情并没有真正起作用,所以任何帮助都会非常感激。

public class Baddies : Sprite
{
    public enum State
    {
        Inactive,
        SetupTraveling,
        Traveling,
        SetupInactive,
    }

    public State CurrentState
    {
        set
        {
            currentState = value;
            framesInStateCount = 0;
        }
        get
        {
            return currentState;
        }
    }

    int framesInStateCount = 0;
    State currentState = State.SetupInactive;    

    public Baddies()
    {
        Image = getImage("Bad");
        Scale = .2f;
        Rotation = 0f;
        DRotation = .05f;
        TurnedOn = true;
        BounceOn = false;
        WrapOn = false;
        Gravity = 0f;
    }

    public override void Update()
    {
        framesInStateCount++;
        switch (currentState)
        {
            case State.Inactive:
                if (RandOneIn(100)) CurrentState = State.SetupTraveling;
                break;
            case State.SetupTraveling:
                        PositionX = ((Game1.vGameWidth + 100)/2) * (float)Math.Cos(Glob.rnd(0.0, 2.0 * Math.PI));
                        PositionY = ((Game1.vGameHeight + 100)/2) * (float)Math.Sin(Glob.rnd(0.0, 2.0 * Math.PI));                        
                        DDPositionY = 0;
                        DPositionY = -1;
                        DPositionX = 1f;
                        DDPositionX = 0f;                          

                CurrentState = State.Traveling;
                break;
            case State.Traveling:
                if (PositionX > Game1.vGameWidth + (Image.Width / 2) * Scale)
                {
                    currentState = State.SetupInactive;
                }
                if (PositionX < -500f - (Image.Width / 2) * Scale)
                {
                    currentState = State.SetupInactive;
                }
                if (PositionY > Game1.vGameHeight + (Image.Height / 2) * Scale)
                {
                    currentState = State.SetupInactive;
                }
                if (PositionY < 0 - (Image.Height / 2) * Scale)
                {
                    currentState = State.SetupInactive;
                }
                break;
            case State.SetupInactive:
                PositionX = -300f;
                DPositionX = 0f;
                DPositionY = 0f;
                DDPositionX = 0f;
                CurrentState = State.Inactive;

                break;                
        }     

        base.Update();
    } 
}

2 个答案:

答案 0 :(得分:4)

三角法的其他方法:

  1. 决定你的圆半径是什么...通常是:

    raqdius = sqrt(Viewport.Size.Width^2 + Viewport.Size.Height^2) / 2;
    
  2. 生成随机角度

    angle = (float) Random.NextDouble() * MathHelper.PI * 2; 
    
  3. 您的坐标是

    x = ViewPort.Center.X + radius * Math.Cos(angle);
    y = ViewPort.Center.Y + radius * Math.Sin(angle);
    

答案 1 :(得分:1)

这个怎么样:

    1. 决定你圈子的raidius是什么,叫它r
    2. 从[-r,r]
    3. 生成X值
    4. Y = sqrt(r ^ 2 - X ^ 2)
    5. 随机将Y设置为Y或-Y。你的cooridinates将是X,Y