/// <summary> /// 随机返回-1或者1 /// </summary> /// <param name="luaState"></param> /// <returns></returns> public static int GetRandomSign(ILuaState luaState) { int ret = MTRandom.GetNextInt(0, 1); luaState.PushNumber(ret == 0 ? -1 : 1); return(1); }
public static int GetRandomInt(ILuaState luaState) { int begin = luaState.ToInteger(-2); int end = luaState.ToInteger(-1); luaState.PushInteger(MTRandom.GetNextInt(begin, end)); return(1); }
/// <summary> /// 敌机在duration的持续时间内进行移动 /// <para>移动的目标地点以及方式在之前设置的范围、振幅、模式里面</para> /// </summary> /// <param name="duration"></param> public virtual void DoWander(int duration) { // 记录移动方向是否有效的数组 List <int> availableDir = new List <int>(); int dir; float minX, maxX, minY, maxY, toX, toY; // 根据dirMode计算重新计算边界 Vector2 playerPos = Global.PlayerPos; // 向左 if (_curPos.x - _wanderAmplitudeX.x >= _wanderRangeX.x) { availableDir.Add(0); } if (_curPos.x + _wanderAmplitudeX.x <= _wanderRangeX.y) { availableDir.Add(1); } if (availableDir.Count == 0) { toX = _curPos.x; } else { dir = MTRandom.GetNextInt(0, 1); if (_dirMode == DirectionMode.MoveXTowardsPlayer || _dirMode == DirectionMode.MoveTowardsPlayer) { //往左移动 if (playerPos.x < _curPos.x) { dir = 0; } else { dir = 1; } if (availableDir.IndexOf(dir) == -1) { dir = 1 - dir; } } else { dir = MTRandom.GetNextInt(0, availableDir.Count - 1); } // 左移动,计算左移动的最大值和最小值 if (dir == 0) { maxX = _curPos.x - _wanderAmplitudeX.x; minX = _curPos.x - _wanderAmplitudeX.y < _wanderRangeX.x ? _wanderRangeX.x : _curPos.x - _wanderAmplitudeX.y; } else { minX = _curPos.x + _wanderAmplitudeX.x; maxX = _curPos.x + _wanderAmplitudeX.y > _wanderRangeX.y ? _wanderRangeX.y : _curPos.x + _wanderAmplitudeX.y; } toX = MTRandom.GetNextFloat(minX, maxX); } // 计算Y的范围 0 = 下, 1 = 上 availableDir.Clear(); // 往下 if (_curPos.y - _wanderAmplitudeY.x >= _wanderRangeY.x) { availableDir.Add(0); } // 往上 if (_curPos.y + _wanderAmplitudeY.x <= _wanderRangeY.y) { availableDir.Add(1); } if (availableDir.Count == 0) { toY = _curPos.y; } else { if (_dirMode == DirectionMode.MoveYTowardsPlayer || _dirMode == DirectionMode.MoveTowardsPlayer) { //往下移动 if (playerPos.y < _curPos.y) { dir = 0; } else { dir = 1; } if (availableDir.IndexOf(dir) == -1) { dir = 1 - dir; } } else { dir = MTRandom.GetNextInt(0, availableDir.Count - 1); } // 向下移动 if (dir == 0) { maxY = _curPos.y - _wanderAmplitudeY.x; minY = _curPos.y - _wanderAmplitudeY.y < _wanderRangeY.x ? _wanderRangeY.x : _curPos.y - _wanderAmplitudeY.y; } else { minY = _curPos.y + _wanderAmplitudeY.x; maxY = _curPos.y + _wanderAmplitudeY.y > _wanderRangeY.y ? _wanderRangeY.y : _curPos.y + _wanderAmplitudeY.y; } toY = MTRandom.GetNextFloat(minY, maxY); } MoveTo(toX, toY, duration, _wanderMode); //Logger.Log("wander from " + _curPos + " to " + new Vector2(toX, toY)); }