tiled 基本数据

This commit is contained in:
yhh
2020-08-12 12:16:35 +08:00
parent c89ed25d8a
commit 167ef03df6
24 changed files with 2625 additions and 3 deletions

View File

@@ -0,0 +1,32 @@
module es {
export class EdgeExt {
public static oppositeEdge(self: Edge) {
switch (self) {
case Edge.bottom:
return Edge.top;
case Edge.top:
return Edge.bottom;
case Edge.left:
return Edge.right;
case Edge.right:
return Edge.left;
}
}
/**
* 如果边是右或左则返回true
* @param self
*/
public static isHorizontal(self: Edge): boolean{
return self == Edge.right || self == Edge.left;
}
/**
* 如果边是顶部或底部则返回true
* @param self
*/
public static isVertical(self: Edge): boolean {
return self == Edge.top || self == Edge.bottom;
}
}
}

8
source/src/Utils/Enum.ts Normal file
View File

@@ -0,0 +1,8 @@
module es {
export enum Edge {
top,
bottom,
left,
right
}
}

View File

@@ -0,0 +1,16 @@
module es {
export class Enumerable {
/**
* 生成包含一个重复值的序列
* @param element 要重复的值
* @param count 在生成的序列中重复该值的次数
*/
public static repeat<T>(element: T, count: number){
let result = [];
while (count--) {
result.push(element)
}
return result;
}
}
}

View File

@@ -1,5 +1,23 @@
module es {
export class RectangleExt {
/**
* 获取指定边的位置
* @param rect
* @param edge
*/
public static getSide(rect: Rectangle, edge: Edge) {
switch (edge) {
case Edge.top:
return rect.top;
case Edge.bottom:
return rect.bottom;
case es.Edge.left:
return rect.left;
case Edge.right:
return rect.right;
}
}
/**
* 计算两个矩形的并集。结果将是一个包含其他两个的矩形。
* @param first
@@ -15,5 +33,65 @@ module es {
result.height = Math.max(first.bottom, result.bottom) - result.y;
return result;
}
public static getHalfRect(rect: Rectangle, edge: Edge) {
switch (edge) {
case Edge.top:
return new Rectangle(rect.x, rect.y, rect.width, rect.height / 2);
case Edge.bottom:
return new Rectangle(rect.x, rect.y + rect.height / 2, rect.width, rect.height / 2);
case Edge.left:
return new Rectangle(rect.x, rect.y, rect.width / 2, rect.height);
case Edge.right:
return new Rectangle(rect.x + rect.width / 2, rect.y, rect.width / 2, rect.height);
}
}
/**
* 获取矩形的一部分,其宽度/高度的大小位于矩形的边缘,但仍然包含在其中。
* @param rect
* @param edge
* @param size
*/
public static getRectEdgePortion(rect: Rectangle, edge: Edge, size: number = 1) {
switch (edge) {
case es.Edge.top:
return new Rectangle(rect.x, rect.y, rect.width, size);
case Edge.bottom:
return new Rectangle(rect.x, rect.y + rect.height - size, rect.width, size);
case Edge.left:
return new Rectangle(rect.x, rect.y, size, rect.height);
case Edge.right:
return new Rectangle(rect.x + rect.width - size, rect.y, size, rect.height);
}
}
public static expandSide(rect: Rectangle, edge: Edge, amount: number) {
amount = Math.abs(amount);
switch (edge) {
case Edge.top:
rect.y -= amount;
rect.height += amount;
break;
case es.Edge.bottom:
rect.height += amount;
break;
case Edge.left:
rect.x -= amount;
rect.width += amount;
break;
case Edge.right:
rect.width += amount;
break;
}
}
public static contract(rect: Rectangle, horizontalAmount, verticalAmount) {
rect.x += horizontalAmount;
rect.y += verticalAmount;
rect.width -= horizontalAmount * 2;
rect.height -= verticalAmount * 2;
}
}
}

View File

@@ -0,0 +1,27 @@
module es {
/**
* 管理数值的简单助手类。它存储值直到累计的总数大于1。一旦超过1该值将在调用update时添加到amount中。
*/
export class SubpixelNumber {
public remainder: number;
/**
* 以amount递增余数将值截断为int存储新的余数并将amount设置为当前值。
* @param amount
*/
public update(amount: number){
this.remainder += amount;
let motion = Math.trunc(this.remainder);
this.remainder -= motion;
return motion;
}
/**
* 将余数重置为0。当一个物体与一个不可移动的物体碰撞时有用。
* 在这种情况下,您将希望将亚像素余数归零,因为它是空的和无效的碰撞。
*/
public reset(){
this.remainder = 0;
}
}
}