示例#1
0
    /// <summary>
    /// 根据ChangeValue的值获取对应的值并返回
    /// </summary>
    /// <param name="changeValue"></param>
    /// <returns></returns>
    private float GetValueByChangeValue(ParaChangeValue changeValue)
    {
        float value = 0;

        if (changeValue.argType == 0)
        {
            value = changeValue.arg0;
        }
        else if (changeValue.argType == 1)
        {
            Vector2 playerPos = new Vector2(Global.PlayerPos.x, Global.PlayerPos.y);
            Vector2 bulletPos = _bullet.GetPosition();
            float   angle     = MathUtil.GetAngleBetweenXAxis(playerPos - bulletPos);
            value = angle;
        }
        else if (changeValue.argType == 2)
        {
            Vector2 targetPos = new Vector2(changeValue.arg0, changeValue.arg1);
            Vector2 bulletPos = _bullet.GetPosition();
            float   angle     = MathUtil.GetAngleBetweenXAxis(targetPos - bulletPos);
            value = angle;
        }
        if (changeValue.offset != 0)
        {
            value += MTRandom.GetNextFloat(-changeValue.offset, changeValue.offset);
        }
        return(value);
    }
    /// <summary>
    /// 做加速运动(限制最大速度)
    /// <para>bullet 敌机子弹</para>
    /// <para>float acce 加速度</para>
    /// <para>float angle 速度方向 or bool useVAngle使用速度方向</para>
    /// <para>bool isAimToPlayer 是否朝向玩家</para>
    /// <para>float maxVelocity 最大速度限制</para>
    /// </summary>
    /// <param name="luaState"></param>
    /// <returns></returns>
    public static int EnemyBulletDoAccelerationWithLimitation(ILuaState luaState)
    {
        EnemyBulletBase bullet = luaState.ToUserData(-5) as EnemyBulletBase;
        float           acce   = (float)luaState.ToNumber(-4);
        float           angle;

        if (luaState.Type(-3) == LuaType.LUA_TBOOLEAN)
        {
            bullet.GetBulletPara(BulletParaType.VAngel, out angle);
        }
        else
        {
            angle = (float)luaState.ToNumber(-3);
        }
        bool  isAimToPlayer = luaState.ToBoolean(-2);
        float maxVelocity   = (float)luaState.ToNumber(-1);

        luaState.Pop(5);
        if (isAimToPlayer)
        {
            Vector2 playerPos = PlayerInterface.GetInstance().GetCharacter().GetPosition();
            angle += MathUtil.GetAngleBetweenXAxis(playerPos - bullet.GetPosition());
        }
        bullet.DoAccelerationWithLimitation(acce, angle, maxVelocity);
        return(0);
    }
    /// <summary>
    /// 设置敌机子弹的直线运动参数
    /// <para>bullet</para>
    /// <para>v</para>
    /// <para>vAngle</para>
    /// <para>bool isAimToPlayer</para>
    /// <para>acce</para>
    /// <para>accAngle</para>
    /// <para>maxV</para>
    /// </summary>
    /// <param name="luaState"></param>
    /// <returns></returns>
    public static int EnemyBulletSetStraightParas(ILuaState luaState)
    {
        EnemyBulletBase bullet        = luaState.ToUserData(-6) as EnemyBulletBase;
        float           v             = (float)luaState.ToNumber(-5);
        float           angle         = (float)luaState.ToNumber(-4);
        bool            isAimToPlayer = luaState.ToBoolean(-3);
        float           acce          = (float)luaState.ToNumber(-2);
        float           accAngle;

        if (luaState.Type(-1) == LuaType.LUA_TBOOLEAN)
        {
            accAngle = angle;
        }
        else
        {
            accAngle = (float)luaState.ToNumber(-1);
        }
        if (isAimToPlayer)
        {
            Vector2 playerPos = PlayerInterface.GetInstance().GetCharacter().GetPosition();
            float   relAngle  = MathUtil.GetAngleBetweenXAxis(playerPos - bullet.GetPosition());
            angle    += relAngle;
            accAngle += relAngle;
        }
        bullet.SetStraightParas(v, angle, acce, accAngle);
        return(0);
    }
    /// <summary>
    /// 做直线运动
    /// <para>bullet 敌机子弹</para>
    /// <para>float v 速度</para>
    /// <para>float angle 速度方向</para>
    /// <para>bool isAimToPlayer 是否朝向玩家</para>
    /// </summary>
    /// <param name="luaState"></param>
    /// <returns></returns>
    public static int EnemyBulletDoStraightMove(ILuaState luaState)
    {
        EnemyBulletBase bullet        = luaState.ToUserData(-4) as EnemyBulletBase;
        float           v             = (float)luaState.ToNumber(-3);
        float           angle         = (float)luaState.ToNumber(-2);
        bool            isAimToPlayer = luaState.ToBoolean(-1);

        luaState.Pop(4);
        if (isAimToPlayer)
        {
            Vector2 playerPos = PlayerInterface.GetInstance().GetCharacter().GetPosition();
            angle += MathUtil.GetAngleBetweenXAxis(playerPos - bullet.GetPosition());
        }
        bullet.DoStraightMove(v, angle);
        return(0);
    }