文档更新

This commit is contained in:
yhh
2023-03-14 11:22:09 +08:00
parent caa3ffc8f5
commit 78e0b09c7a
8 changed files with 406 additions and 244 deletions
+33 -29
View File
@@ -1,74 +1,78 @@
module es {
/**
* 帮助处理位掩码的实用程序类
* 除了isFlagSet之外,所有方法都期望flag参数是一个非移位的标志
* 允许您使用普通的(0、1、2、3等)来设置/取消您的标记
* 一个用于操作二进制标志(也称为位字段)
*/
export class Flags {
/**
* 检查位标志是否已在数值中设置
* 检查期望标志是否已经移位
* @param self
* @param flag
* 检查指定二进制数字中是否已设置了指定标志位
* @param self 二进制数字
* @param flag 标志位,应该为2的幂
* @returns 如果设置了指定的标志位,则返回true,否则返回false
*/
public static isFlagSet(self: number, flag: number): boolean {
return (self & flag) != 0;
return (self & flag) !== 0;
}
/**
* 检查位标志是否在数值中设置
* @param self
* @param flag
* 检查指定二进制数字中是否已设置未移位的指定标志位
* @param self 二进制数字
* @param flag 标志位,不应移位(应为2的幂)
* @returns 如果设置了指定的标志位,则返回true,否则返回false
*/
public static isUnshiftedFlagSet(self: number, flag: number): boolean {
flag = 1 << flag;
return (self & flag) != 0;
return (self & flag) !== 0;
}
/**
* 设置数值标志位,移除所有已经设置的标志
* @param self
* @param flag
* 将指定的标志位设置为二进制数字的唯一标志
* @param self 二进制数字
* @param flag 标志位,应该为2的幂
*/
public static setFlagExclusive(self: Ref<number>, flag: number) {
self.value = 1 << flag;
}
/**
* 设置标志位
* @param self
* @param flag
* 将指定的标志位设置为二进制数字
* @param self 二进制数字的引用
* @param flag 标志位,应该为2的幂
*/
public static setFlag(self: Ref<number>, flag: number) {
self.value = (self.value | 1 << flag);
self.value |= 1 << flag;
}
/**
* 取消标志位
* @param self
* @param flag
* 将指定的标志位从二进制数字中取消设置
* @param self 二进制数字的引用
* @param flag 标志位,应该为2的幂
*/
public static unsetFlag(self: Ref<number>, flag: number) {
flag = 1 << flag;
self.value = (self.value & (~flag));
self.value &= ~flag;
}
/**
* 反转数值集合位
* @param self
* 反转二进制数字中的所有位(将1变为0,将0变为1)
* @param self 二进制数字的引用
*/
public static invertFlags(self: Ref<number>) {
self.value = ~self.value;
}
/**
* 打印 number 的二进制表示。 方便调试 number 标志
* 返回二进制数字的字符串表示形式(以二进制形式)
* @param self 二进制数字
* @param leftPadWidth 返回的字符串的最小宽度(在左侧填充0)
* @returns 二进制数字的字符串表示形式
*/
public static binaryStringRepresentation(self: number,
leftPadWidth: number = 10) {
public static binaryStringRepresentation(
self: number,
leftPadWidth = 10
): string {
let str = self.toString(2);
while (str.length < (leftPadWidth || 2)) {
str = '0' + str;
str = "0" + str;
}
return str;
}
+36 -28
View File
@@ -98,7 +98,7 @@ module es {
this.height = value.y;
}
/**
/**
* 位于这个矩形中心的一个点
* 如果 "宽度 "或 "高度 "是奇数,则中心点将向下舍入
*/
@@ -163,7 +163,7 @@ module es {
/**
* 获取指定边缘的位置
* @param edge
* @param edge
*/
public getSide(edge: Edge) {
switch (edge) {
@@ -214,28 +214,34 @@ module es {
}
public rayIntersects(ray: Ray2D): { intersected: boolean; distance: number } {
const res = { intersected: false, distance: 0 };
// 存储相交点和相交距离
const res = {intersected: false, distance: 0};
let maxValue = Infinity;
// 计算射线与矩形的相交距离
if (Math.abs(ray.direction.x) < 1E-06) {
// 如果射线方向的x分量很小,说明它是垂直的,那么它就不会相交
if (ray.start.x < this.x || ray.start.x > this.x + this.width) {
return res;
}
} else {
// 计算射线与x边界的交点,以及在矩形上面和下面的交点
const num11 = 1 / ray.direction.x;
let num8 = (this.x - ray.start.x) * num11;
let num7 = (this.x + this.width - ray.start.x) * num11;
if (num8 > num7) {
[num7, num8] = [num8, num7];
}
// 将最远的相交距离更新为上下两个交点中更远的那个
res.distance = Math.max(num8, res.distance);
maxValue = Math.min(num7, maxValue);
if (res.distance > maxValue) {
return res;
}
}
// 计算射线与y边界的交点,以及在矩形左边和右边的交点
if (Math.abs(ray.direction.y) < 1e-06) {
if (ray.start.y < this.y || ray.start.y > this.y + this.height) {
return res;
@@ -247,14 +253,16 @@ module es {
if (num6 > num5) {
[num5, num6] = [num6, num5];
}
// 将最远的相交距离更新为左右两个交点中更远的那个
res.distance = Math.max(num6, res.distance);
maxValue = Math.min(num5, maxValue);
if (res.distance > maxValue) {
return res;
}
}
// 如果相交了,将标志设为真,并返回相交点
res.intersected = true;
return res;
}
@@ -265,7 +273,7 @@ module es {
*/
public containsRect(value: Rectangle) {
return ((((this.x <= value.x) && (value.x < (this.x + this.width))) &&
(this.y <= value.y)) &&
(this.y <= value.y)) &&
(value.y < (this.y + this.height)));
}
@@ -358,8 +366,8 @@ module es {
/**
* 创建一个新的RectangleF,该RectangleF包含两个其他矩形的重叠区域
* @param value1
* @param value2
* @param value1
* @param value2
* @returns 将两个矩形的重叠区域作为输出参数
*/
public static intersect(value1: Rectangle, value2: Rectangle) {
@@ -386,8 +394,8 @@ module es {
/**
* 创建一个完全包含两个其他矩形的新矩形
* @param value1
* @param value2
* @param value1
* @param value2
*/
public static union(value1: Rectangle, value2: Rectangle) {
let x = Math.min(value1.x, value2.x);
@@ -399,8 +407,8 @@ module es {
/**
* 在矩形重叠的地方创建一个新的矩形
* @param value1
* @param value2
* @param value1
* @param value2
*/
public static overlap(value1: Rectangle, value2: Rectangle): Rectangle {
let x = Math.max(value1.x, value2.x, 0);
@@ -411,7 +419,7 @@ module es {
}
public calculateBounds(parentPosition: Vector2, position: Vector2, origin: Vector2, scale: Vector2,
rotation: number, width: number, height: number) {
rotation: number, width: number, height: number) {
if (rotation == 0) {
this.x = Math.trunc(parentPosition.x + position.x - origin.x * scale.x);
this.y = Math.trunc(parentPosition.y + position.y - origin.y * scale.y);
@@ -456,10 +464,10 @@ module es {
/**
* 返回一个横跨当前矩形和提供的三角形位置的矩形
* @param deltaX
* @param deltaY
* @param deltaX
* @param deltaY
*/
public getSweptBroadphaseBounds(deltaX: number, deltaY: number){
public getSweptBroadphaseBounds(deltaX: number, deltaY: number) {
let broadphasebox = Rectangle.empty;
broadphasebox.x = deltaX > 0 ? this.x : this.x + deltaX;
@@ -471,13 +479,13 @@ module es {
}
/**
* 如果发生碰撞,返回true
* 如果发生碰撞,返回true
* moveX和moveY将返回b1为避免碰撞而必须移动的移动量
* @param other
* @param moveX
* @param moveY
* @param other
* @param moveX
* @param moveY
*/
public collisionCheck(other: Rectangle, moveX: Ref<number>, moveY: Ref<number>){
public collisionCheck(other: Rectangle, moveX: Ref<number>, moveY: Ref<number>) {
moveX.value = moveY.value = 0;
let l = other.x - (this.x + this.width);
@@ -504,8 +512,8 @@ module es {
/**
* 计算两个矩形之间有符号的交点深度
* @param rectA
* @param rectB
* @param rectA
* @param rectB
* @returns 两个相交的矩形之间的重叠量。
* 这些深度值可以是负值,取决于矩形/相交的哪些边。
* 这允许调用者确定正确的推送对象的方向,以解决碰撞问题。
@@ -541,7 +549,7 @@ module es {
/**
* 比较当前实例是否等于指定的矩形
* @param other
* @param other
*/
public equals(other: Rectangle) {
return this === other;
@@ -550,7 +558,7 @@ module es {
/**
* 获取这个矩形的哈希码
*/
public getHashCode(): number{
public getHashCode(): number {
return (Math.trunc(this.x) ^ Math.trunc(this.y) ^ Math.trunc(this.width) ^ Math.trunc(this.height));
}