通用API
通用模块(Utils)
全局常量
脚本环境常量
常量 | 类型 | 值 | 描述 |
Utils.netMode | NetMode | 见描述 | 若当前脚本为服务端环境,值为 若当前脚本为客户端环境,值为 |
数学常量
常量 | 类型 | 值 | 描述 |
Utils.E | double | 2.71828175 | 自然常数 |
Utils.LOG2E | double | 1.442695 | 以 2 为底 e 的对数 |
Utils.LOG10E | double | 0.4342945 | 以 10 为底 e 的对数 |
Utils.PI | double | 3.14159274 | 圆周率 |
Utils.TWO_PI | double | 6.28318548 | 圆周率 x 2 |
Utils.PI_OVER_2 | double | 1.57079637 | 圆周率 / 2 |
Utils.PI_OVER_4 | double | 0.7853982 | 圆周率 / 4 |
随机数
函数 | 返回值 | 描述 |
Utils.RandInt(int n) | int | 若n大于0,返回[0, n)的随机整数,否则返回0。 |
Utils.RandIntArea(int begin, int len) | int | 若len大于0,返回[begin, begin + len)的随机整数,否则返回begin。 |
Utils.RandDouble(double value) | double | 若value大于0,返回[0, value)的随机浮点数,否则返回0。 |
Utils.RandDoubleArea(double begin, double len) | double | 若len大于0,返回[begin, begin + len)的随机浮点数,否则返回begin。 |
Utils.RandSym(double value) | double | 返回(-value, value)的随机浮点数。 |
Utils.RandTry(int n) | bool | 当n为正数时1/n概率返回true,否则始终返回false。
|
数学运算与模型
函数 | 返回值 | 描述 |
Utils.Cell(double a) | int | 返回实际横/纵坐标对应的格子横/纵坐标。注意每个格子为16像素,实际结果为除以16后向下取整。 |
Utils.PositiveMod(int a, int b) | int | 返回a与b求余的非负数结果。
|
Utils.FloorDivide(int a, int b) | int | 若b非0,返回a向下取整整除b的结果,否则返回0。
|
Utils.SinValue(int phase, int period, int begin = 0) | double | 返回以period为周期、以begin为初相位的正弦波在相位phase的值。 |
Utils.CosValue(int phase, int period, int begin = 0) | double | 返回以period为周期、以begin为初相位的余弦波在相位phase的值。 |
Utils.ToTargetValue(double start, double target, double step) | double | 返回start值往target值方向移动step长度的结果,若到达target值,则返回target值。
|
二维空间几何
函数 | 返回值 | 描述 |
Utils.GetPointsDistance(double x1, double y1, double x2, double y2) | double | 返回点(x1, y1)到点(x2, y2)的距离。
|
Utils.GetDistance(double x, double y) | double | 返回点(x, y)到原点(0, 0)的距离。
|
Utils.GetPointSegmentDistance(double x, double y, double x1, double y1, double x2, double y2) | double | 返回点(x, y)到以点(x1, y1)和点(x2, y2)为两端点的线段的距离。 |
Utils.GetAngle(double x, double y) | double | 返回向量(x, y)与横坐标的夹角。
|
Utils.FixAngle(double angle) | double | 将角度按2π周期增加或减少,返回最终限定在区间(-π, π]内的结果。 |
Utils.GetXYFromPolar(double length, double angle) | double, double | 将极坐标转换为直角坐标,返回横坐标和纵坐标。 |
Utils.RotateXY(double x, double y, double angle) | double, double | 将点(x, y)绕原点旋转指定角度,返回旋转后的横坐标和纵坐标。 |
简单物理运动
函数 | 返回值 | 描述 |
Utils.SlowSpeed2D(double speedX, double speedY, double dec) | double, double | 将一个二维速度(speedX, speedY)以恒定速度(dec)降低,返回新的横速度和纵速度。 |
Utils.SlowSpeed1D(double speed, double dec) | double | 将一个速度以恒定速度(dec)降低,返回新的速度。 |
Utils.ForceSpeed2D(double speedX, double speedY, double force, double forceAngle, double maxSpeed) | double, double | 将一个二维速度(speedX, speedY)进行受力,返回新的横速度和纵速度。 |
源码参考
源码均以C++伪代码的形式展示,你可以通过这些源码来理解API的具体功能。
int Utils.PositiveMod(int a, int b)
int Utils.FloorDivide(int a, int b)
double Utils.GetPointsDistance(double x1, double y1, double x2, double y2)
double Utils.GetDistance(double x, double y)
double Utils.GetPointSegmentDistance(double x, double y, double x1, double y1, double x2, double y2)
double Utils.GetAngle(double x, double y)
double Utils.FixAngle(double angle)
double Utils.SinValue(int phase, int period, int begin)
double Utils.CosValue(int phase, int period, int begin)
double Utils.ToTargetValue(double start, double target, double step)
void Utils.GetXYFromPolar(double & x, double & y, double length, double angle)
void Utils.RotateXY(double & x, double & y, double angle)
void Utils.SlowSpeed2D(double & spx, double & spy, double dec)
double Utils.SlowSpeed1D(double speed, double dec)
void ForceSpeed2D(double & spx, double & spy, double force, double forceAngle, double maxSpeed)
Last updated