Merge pull request #11 from esengine/develop

Develop
This commit is contained in:
YHH
2020-07-09 16:37:13 +08:00
committed by GitHub
17 changed files with 635 additions and 3 deletions
+37
View File
@@ -61,6 +61,7 @@ declare class PriorityQueue<T extends PriorityQueueNode> {
constructor(maxNodes: number);
clear(): void;
readonly count: number;
readonly maxSize: number;
contains(node: T): boolean;
enqueue(node: T, priority: number): void;
dequeue(): T;
@@ -158,10 +159,35 @@ declare class WeightedPathfinder {
private static getKey;
static recontructPath<T>(cameFrom: Map<T, T>, start: T, goal: T): T[];
}
declare class Debug {
private static _debugDrawItems;
static drawHollowRect(rectanle: Rectangle, color: number, duration?: number): void;
static render(): void;
}
declare class DebugDefaults {
static verletParticle: number;
static verletConstraintEdge: number;
}
declare enum DebugDrawType {
line = 0,
hollowRectangle = 1,
pixel = 2,
text = 3
}
declare class DebugDrawItem {
rectangle: Rectangle;
color: number;
duration: number;
drawType: DebugDrawType;
text: string;
start: Vector2;
end: Vector2;
x: number;
y: number;
size: number;
constructor(rectangle: Rectangle, color: number, duration: number);
draw(shape: egret.Shape): boolean;
}
declare abstract class Component extends egret.DisplayObjectContainer {
entity: Entity;
private _enabled;
@@ -754,6 +780,7 @@ declare class MathHelper {
static clamp(value: number, min: number, max: number): number;
static pointOnCirlce(circleCenter: Vector2, radius: number, angleInDegrees: number): Vector2;
static isEven(value: number): boolean;
static angleBetweenVectors(from: Vector2, to: Vector2): number;
}
declare class Matrix2D {
m11: number;
@@ -854,6 +881,7 @@ declare class Physics {
static addCollider(collider: Collider): void;
static removeCollider(collider: Collider): void;
static updateCollider(collider: Collider): void;
static debugDraw(secondsToDisplay: any): void;
}
declare abstract class Shape {
bounds: Rectangle;
@@ -954,6 +982,8 @@ declare class SpatialHash {
};
private cellAtPosition;
private cellCoords;
debugDraw(secondsToDisplay: number, textScale?: number): void;
private debugDrawCellDetails;
}
declare class RaycastResultParser {
}
@@ -973,6 +1003,13 @@ declare class ContentManager {
loadRes(name: string, local?: boolean): Promise<any>;
dispose(): void;
}
declare class DrawUtils {
static drawLine(shape: egret.Shape, start: Vector2, end: Vector2, color: number, thickness?: number): void;
static drawLineAngle(shape: egret.Shape, start: Vector2, radians: number, length: number, color: number, thickness?: number): void;
static drawHollowRect(shape: egret.Shape, rect: Rectangle, color: number, thickness?: number): void;
static drawHollowRectR(shape: egret.Shape, x: number, y: number, width: number, height: number, color: number, thickness?: number): void;
static drawPixel(shape: egret.Shape, position: Vector2, color: number, size?: number): void;
}
declare class Emitter<T> {
private _messageTable;
constructor();
+144
View File
@@ -418,7 +418,22 @@ var PriorityQueue = (function () {
enumerable: true,
configurable: true
});
Object.defineProperty(PriorityQueue.prototype, "maxSize", {
get: function () {
return this._nodes.length - 1;
},
enumerable: true,
configurable: true
});
PriorityQueue.prototype.contains = function (node) {
if (!node) {
console.error("node cannot be null");
return false;
}
if (node.queueIndex < 0 || node.queueIndex >= this._nodes.length) {
console.error("node.QueueIndex has been corrupted. Did you change it manually? Or add this node to another queue?");
return false;
}
return (this._nodes[node.queueIndex] == node);
};
PriorityQueue.prototype.enqueue = function (node, priority) {
@@ -866,6 +881,29 @@ var WeightedPathfinder = (function () {
};
return WeightedPathfinder;
}());
var Debug = (function () {
function Debug() {
}
Debug.drawHollowRect = function (rectanle, color, duration) {
if (duration === void 0) { duration = 0; }
this._debugDrawItems.push(new DebugDrawItem(rectanle, color, duration));
};
Debug.render = function () {
if (this._debugDrawItems.length > 0) {
var debugShape = new egret.Shape();
if (SceneManager.scene) {
SceneManager.scene.addChild(debugShape);
}
for (var i = this._debugDrawItems.length - 1; i >= 0; i--) {
var item = this._debugDrawItems[i];
if (item.draw(debugShape))
this._debugDrawItems.removeAt(i);
}
}
};
Debug._debugDrawItems = [];
return Debug;
}());
var DebugDefaults = (function () {
function DebugDefaults() {
}
@@ -873,6 +911,39 @@ var DebugDefaults = (function () {
DebugDefaults.verletConstraintEdge = 0x433E36;
return DebugDefaults;
}());
var DebugDrawType;
(function (DebugDrawType) {
DebugDrawType[DebugDrawType["line"] = 0] = "line";
DebugDrawType[DebugDrawType["hollowRectangle"] = 1] = "hollowRectangle";
DebugDrawType[DebugDrawType["pixel"] = 2] = "pixel";
DebugDrawType[DebugDrawType["text"] = 3] = "text";
})(DebugDrawType || (DebugDrawType = {}));
var DebugDrawItem = (function () {
function DebugDrawItem(rectangle, color, duration) {
this.rectangle = rectangle;
this.color = color;
this.duration = duration;
this.drawType = DebugDrawType.hollowRectangle;
}
DebugDrawItem.prototype.draw = function (shape) {
switch (this.drawType) {
case DebugDrawType.line:
DrawUtils.drawLine(shape, this.start, this.end, this.color);
break;
case DebugDrawType.hollowRectangle:
DrawUtils.drawHollowRect(shape, this.rectangle, this.color);
break;
case DebugDrawType.pixel:
DrawUtils.drawPixel(shape, new Vector2(this.x, this.y), this.color, this.size);
break;
case DebugDrawType.text:
break;
}
this.duration -= Time.deltaTime;
return this.duration < 0;
};
return DebugDrawItem;
}());
var Component = (function (_super) {
__extends(Component, _super);
function Component() {
@@ -1372,6 +1443,7 @@ var SceneManager = (function () {
}
else if (this._scene) {
this._scene.render();
Debug.render();
this._scene.postRender();
}
};
@@ -3392,6 +3464,9 @@ var MathHelper = (function () {
MathHelper.isEven = function (value) {
return value % 2 == 0;
};
MathHelper.angleBetweenVectors = function (from, to) {
return Math.atan2(to.y - from.y, to.x - from.x);
};
MathHelper.Epsilon = 0.00001;
MathHelper.Rad2Deg = 57.29578;
MathHelper.Deg2Rad = 0.0174532924;
@@ -3955,6 +4030,9 @@ var Physics = (function () {
this._spatialHash.remove(collider);
this._spatialHash.register(collider);
};
Physics.debugDraw = function (secondsToDisplay) {
this._spatialHash.debugDraw(secondsToDisplay, 2);
};
Physics.spatialHashCellSize = 100;
Physics.allLayers = -1;
return Physics;
@@ -4502,6 +4580,12 @@ var SpatialHash = (function () {
resultCounter++;
}
}
else if (collider instanceof PolygonCollider) {
if (collider.shape.overlaps(this._overlapTestCircle)) {
results[resultCounter] = collider;
resultCounter++;
}
}
else {
throw new Error("overlapCircle against this collider type is not implemented!");
}
@@ -4546,6 +4630,20 @@ var SpatialHash = (function () {
SpatialHash.prototype.cellCoords = function (x, y) {
return new Vector2(Math.floor(x * this._inverseCellSize), Math.floor(y * this._inverseCellSize));
};
SpatialHash.prototype.debugDraw = function (secondsToDisplay, textScale) {
if (textScale === void 0) { textScale = 1; }
for (var x = this.gridBounds.x; x <= this.gridBounds.right; x++) {
for (var y = this.gridBounds.y; y <= this.gridBounds.bottom; y++) {
var cell = this.cellAtPosition(x, y);
if (cell && cell.length > 0)
this.debugDrawCellDetails(x, y, cell.length, secondsToDisplay, textScale);
}
}
};
SpatialHash.prototype.debugDrawCellDetails = function (x, y, cellCount, secondsToDisplay, textScale) {
if (secondsToDisplay === void 0) { secondsToDisplay = 0.5; }
if (textScale === void 0) { textScale = 1; }
};
return SpatialHash;
}());
var RaycastResultParser = (function () {
@@ -4625,6 +4723,52 @@ var ContentManager = (function () {
};
return ContentManager;
}());
var DrawUtils = (function () {
function DrawUtils() {
}
DrawUtils.drawLine = function (shape, start, end, color, thickness) {
if (thickness === void 0) { thickness = 1; }
this.drawLineAngle(shape, start, MathHelper.angleBetweenVectors(start, end), Vector2.distance(start, end), color, thickness);
};
DrawUtils.drawLineAngle = function (shape, start, radians, length, color, thickness) {
if (thickness === void 0) { thickness = 1; }
shape.graphics.beginFill(color);
shape.graphics.drawRect(start.x, start.y, 1, 1);
shape.graphics.endFill();
shape.scaleX = length;
shape.scaleY = thickness;
shape.$anchorOffsetX = 0;
shape.$anchorOffsetY = 0;
shape.rotation = radians;
};
DrawUtils.drawHollowRect = function (shape, rect, color, thickness) {
if (thickness === void 0) { thickness = 1; }
this.drawHollowRectR(shape, rect.x, rect.y, rect.width, rect.height, color, thickness);
};
DrawUtils.drawHollowRectR = function (shape, x, y, width, height, color, thickness) {
if (thickness === void 0) { thickness = 1; }
var tl = new Vector2(x, y).round();
var tr = new Vector2(x + width, y).round();
var br = new Vector2(x + width, y + height).round();
var bl = new Vector2(x, y + height).round();
this.drawLine(shape, tl, tr, color, thickness);
this.drawLine(shape, tr, br, color, thickness);
this.drawLine(shape, br, bl, color, thickness);
this.drawLine(shape, bl, tl, color, thickness);
};
DrawUtils.drawPixel = function (shape, position, color, size) {
if (size === void 0) { size = 1; }
var destRect = new Rectangle(position.x, position.y, size, size);
if (size != 1) {
destRect.x -= size * 0.5;
destRect.y -= size * 0.5;
}
shape.graphics.beginFill(color);
shape.graphics.drawRect(destRect.x, destRect.y, destRect.width, destRect.height);
shape.graphics.endFill();
};
return DrawUtils;
}());
var Emitter = (function () {
function Emitter() {
this._messageTable = new Map();
File diff suppressed because one or more lines are too long
+37
View File
@@ -61,6 +61,7 @@ declare class PriorityQueue<T extends PriorityQueueNode> {
constructor(maxNodes: number);
clear(): void;
readonly count: number;
readonly maxSize: number;
contains(node: T): boolean;
enqueue(node: T, priority: number): void;
dequeue(): T;
@@ -158,10 +159,35 @@ declare class WeightedPathfinder {
private static getKey;
static recontructPath<T>(cameFrom: Map<T, T>, start: T, goal: T): T[];
}
declare class Debug {
private static _debugDrawItems;
static drawHollowRect(rectanle: Rectangle, color: number, duration?: number): void;
static render(): void;
}
declare class DebugDefaults {
static verletParticle: number;
static verletConstraintEdge: number;
}
declare enum DebugDrawType {
line = 0,
hollowRectangle = 1,
pixel = 2,
text = 3
}
declare class DebugDrawItem {
rectangle: Rectangle;
color: number;
duration: number;
drawType: DebugDrawType;
text: string;
start: Vector2;
end: Vector2;
x: number;
y: number;
size: number;
constructor(rectangle: Rectangle, color: number, duration: number);
draw(shape: egret.Shape): boolean;
}
declare abstract class Component extends egret.DisplayObjectContainer {
entity: Entity;
private _enabled;
@@ -754,6 +780,7 @@ declare class MathHelper {
static clamp(value: number, min: number, max: number): number;
static pointOnCirlce(circleCenter: Vector2, radius: number, angleInDegrees: number): Vector2;
static isEven(value: number): boolean;
static angleBetweenVectors(from: Vector2, to: Vector2): number;
}
declare class Matrix2D {
m11: number;
@@ -854,6 +881,7 @@ declare class Physics {
static addCollider(collider: Collider): void;
static removeCollider(collider: Collider): void;
static updateCollider(collider: Collider): void;
static debugDraw(secondsToDisplay: any): void;
}
declare abstract class Shape {
bounds: Rectangle;
@@ -954,6 +982,8 @@ declare class SpatialHash {
};
private cellAtPosition;
private cellCoords;
debugDraw(secondsToDisplay: number, textScale?: number): void;
private debugDrawCellDetails;
}
declare class RaycastResultParser {
}
@@ -973,6 +1003,13 @@ declare class ContentManager {
loadRes(name: string, local?: boolean): Promise<any>;
dispose(): void;
}
declare class DrawUtils {
static drawLine(shape: egret.Shape, start: Vector2, end: Vector2, color: number, thickness?: number): void;
static drawLineAngle(shape: egret.Shape, start: Vector2, radians: number, length: number, color: number, thickness?: number): void;
static drawHollowRect(shape: egret.Shape, rect: Rectangle, color: number, thickness?: number): void;
static drawHollowRectR(shape: egret.Shape, x: number, y: number, width: number, height: number, color: number, thickness?: number): void;
static drawPixel(shape: egret.Shape, position: Vector2, color: number, size?: number): void;
}
declare class Emitter<T> {
private _messageTable;
constructor();
+144
View File
@@ -418,7 +418,22 @@ var PriorityQueue = (function () {
enumerable: true,
configurable: true
});
Object.defineProperty(PriorityQueue.prototype, "maxSize", {
get: function () {
return this._nodes.length - 1;
},
enumerable: true,
configurable: true
});
PriorityQueue.prototype.contains = function (node) {
if (!node) {
console.error("node cannot be null");
return false;
}
if (node.queueIndex < 0 || node.queueIndex >= this._nodes.length) {
console.error("node.QueueIndex has been corrupted. Did you change it manually? Or add this node to another queue?");
return false;
}
return (this._nodes[node.queueIndex] == node);
};
PriorityQueue.prototype.enqueue = function (node, priority) {
@@ -866,6 +881,29 @@ var WeightedPathfinder = (function () {
};
return WeightedPathfinder;
}());
var Debug = (function () {
function Debug() {
}
Debug.drawHollowRect = function (rectanle, color, duration) {
if (duration === void 0) { duration = 0; }
this._debugDrawItems.push(new DebugDrawItem(rectanle, color, duration));
};
Debug.render = function () {
if (this._debugDrawItems.length > 0) {
var debugShape = new egret.Shape();
if (SceneManager.scene) {
SceneManager.scene.addChild(debugShape);
}
for (var i = this._debugDrawItems.length - 1; i >= 0; i--) {
var item = this._debugDrawItems[i];
if (item.draw(debugShape))
this._debugDrawItems.removeAt(i);
}
}
};
Debug._debugDrawItems = [];
return Debug;
}());
var DebugDefaults = (function () {
function DebugDefaults() {
}
@@ -873,6 +911,39 @@ var DebugDefaults = (function () {
DebugDefaults.verletConstraintEdge = 0x433E36;
return DebugDefaults;
}());
var DebugDrawType;
(function (DebugDrawType) {
DebugDrawType[DebugDrawType["line"] = 0] = "line";
DebugDrawType[DebugDrawType["hollowRectangle"] = 1] = "hollowRectangle";
DebugDrawType[DebugDrawType["pixel"] = 2] = "pixel";
DebugDrawType[DebugDrawType["text"] = 3] = "text";
})(DebugDrawType || (DebugDrawType = {}));
var DebugDrawItem = (function () {
function DebugDrawItem(rectangle, color, duration) {
this.rectangle = rectangle;
this.color = color;
this.duration = duration;
this.drawType = DebugDrawType.hollowRectangle;
}
DebugDrawItem.prototype.draw = function (shape) {
switch (this.drawType) {
case DebugDrawType.line:
DrawUtils.drawLine(shape, this.start, this.end, this.color);
break;
case DebugDrawType.hollowRectangle:
DrawUtils.drawHollowRect(shape, this.rectangle, this.color);
break;
case DebugDrawType.pixel:
DrawUtils.drawPixel(shape, new Vector2(this.x, this.y), this.color, this.size);
break;
case DebugDrawType.text:
break;
}
this.duration -= Time.deltaTime;
return this.duration < 0;
};
return DebugDrawItem;
}());
var Component = (function (_super) {
__extends(Component, _super);
function Component() {
@@ -1372,6 +1443,7 @@ var SceneManager = (function () {
}
else if (this._scene) {
this._scene.render();
Debug.render();
this._scene.postRender();
}
};
@@ -3392,6 +3464,9 @@ var MathHelper = (function () {
MathHelper.isEven = function (value) {
return value % 2 == 0;
};
MathHelper.angleBetweenVectors = function (from, to) {
return Math.atan2(to.y - from.y, to.x - from.x);
};
MathHelper.Epsilon = 0.00001;
MathHelper.Rad2Deg = 57.29578;
MathHelper.Deg2Rad = 0.0174532924;
@@ -3955,6 +4030,9 @@ var Physics = (function () {
this._spatialHash.remove(collider);
this._spatialHash.register(collider);
};
Physics.debugDraw = function (secondsToDisplay) {
this._spatialHash.debugDraw(secondsToDisplay, 2);
};
Physics.spatialHashCellSize = 100;
Physics.allLayers = -1;
return Physics;
@@ -4502,6 +4580,12 @@ var SpatialHash = (function () {
resultCounter++;
}
}
else if (collider instanceof PolygonCollider) {
if (collider.shape.overlaps(this._overlapTestCircle)) {
results[resultCounter] = collider;
resultCounter++;
}
}
else {
throw new Error("overlapCircle against this collider type is not implemented!");
}
@@ -4546,6 +4630,20 @@ var SpatialHash = (function () {
SpatialHash.prototype.cellCoords = function (x, y) {
return new Vector2(Math.floor(x * this._inverseCellSize), Math.floor(y * this._inverseCellSize));
};
SpatialHash.prototype.debugDraw = function (secondsToDisplay, textScale) {
if (textScale === void 0) { textScale = 1; }
for (var x = this.gridBounds.x; x <= this.gridBounds.right; x++) {
for (var y = this.gridBounds.y; y <= this.gridBounds.bottom; y++) {
var cell = this.cellAtPosition(x, y);
if (cell && cell.length > 0)
this.debugDrawCellDetails(x, y, cell.length, secondsToDisplay, textScale);
}
}
};
SpatialHash.prototype.debugDrawCellDetails = function (x, y, cellCount, secondsToDisplay, textScale) {
if (secondsToDisplay === void 0) { secondsToDisplay = 0.5; }
if (textScale === void 0) { textScale = 1; }
};
return SpatialHash;
}());
var RaycastResultParser = (function () {
@@ -4625,6 +4723,52 @@ var ContentManager = (function () {
};
return ContentManager;
}());
var DrawUtils = (function () {
function DrawUtils() {
}
DrawUtils.drawLine = function (shape, start, end, color, thickness) {
if (thickness === void 0) { thickness = 1; }
this.drawLineAngle(shape, start, MathHelper.angleBetweenVectors(start, end), Vector2.distance(start, end), color, thickness);
};
DrawUtils.drawLineAngle = function (shape, start, radians, length, color, thickness) {
if (thickness === void 0) { thickness = 1; }
shape.graphics.beginFill(color);
shape.graphics.drawRect(start.x, start.y, 1, 1);
shape.graphics.endFill();
shape.scaleX = length;
shape.scaleY = thickness;
shape.$anchorOffsetX = 0;
shape.$anchorOffsetY = 0;
shape.rotation = radians;
};
DrawUtils.drawHollowRect = function (shape, rect, color, thickness) {
if (thickness === void 0) { thickness = 1; }
this.drawHollowRectR(shape, rect.x, rect.y, rect.width, rect.height, color, thickness);
};
DrawUtils.drawHollowRectR = function (shape, x, y, width, height, color, thickness) {
if (thickness === void 0) { thickness = 1; }
var tl = new Vector2(x, y).round();
var tr = new Vector2(x + width, y).round();
var br = new Vector2(x + width, y + height).round();
var bl = new Vector2(x, y + height).round();
this.drawLine(shape, tl, tr, color, thickness);
this.drawLine(shape, tr, br, color, thickness);
this.drawLine(shape, br, bl, color, thickness);
this.drawLine(shape, bl, tl, color, thickness);
};
DrawUtils.drawPixel = function (shape, position, color, size) {
if (size === void 0) { size = 1; }
var destRect = new Rectangle(position.x, position.y, size, size);
if (size != 1) {
destRect.x -= size * 0.5;
destRect.y -= size * 0.5;
}
shape.graphics.beginFill(color);
shape.graphics.drawRect(destRect.x, destRect.y, destRect.width, destRect.height);
shape.graphics.endFill();
};
return DrawUtils;
}());
var Emitter = (function () {
function Emitter() {
this._messageTable = new Map();
+1 -1
View File
File diff suppressed because one or more lines are too long
@@ -3,6 +3,12 @@
* IAstarGraph和开始/
*/
class AStarPathfinder {
/**
* null
* @param graph
* @param start
* @param goal
*/
public static search<T>(graph: IAstarGraph<T>, start: T, goal: T){
let foundPath = false;
let cameFrom = new Map<T, T>();
@@ -60,6 +66,12 @@ class AStarPathfinder {
return null;
}
/**
* cameFrom字典重新构造路径
* @param cameFrom
* @param start
* @param goal
*/
public static recontructPath<T>(cameFrom: Map<T, T>, start: T, goal: T): T[]{
let path = [];
let current = goal;
@@ -33,13 +33,18 @@ class AstarGridGraph implements IAstarGraph<Vector2> {
}
/**
*
* walls是不可逾越的
* @param node
*/
public isNodePassable(node: Vector2): boolean {
return !this.walls.firstOrDefault(wall => JSON.stringify(wall) == JSON.stringify(node));
}
/**
* AStarPathfinder.search的快捷方式
* @param start
* @param goal
*/
public search(start: Vector2, goal: Vector2){
return AStarPathfinder.search(this, start, goal);
}
@@ -1,5 +1,22 @@
/**
* graph的接口AstarPathfinder.search方法
*/
interface IAstarGraph<T> {
/**
* getNeighbors方法应该返回从传入的节点可以到达的任何相邻节点
* @param node
*/
getNeighbors(node: T): Array<T>;
/**
* from到to的成本
* @param from
* @param to
*/
cost(from: T, to: T): number;
/**
* node到to的启发式WeightedGridGraph了解常用的Manhatten方法
* @param node
* @param goal
*/
heuristic(node: T, goal: T);
}
@@ -1,27 +1,74 @@
/**
* 使 O(1)
* 使5-10
* IPriorityQueue.contains()
*/
class PriorityQueue<T extends PriorityQueueNode> {
private _numNodes: number;
private _nodes: T[];
private _numNodesEverEnqueued;
/**
*
* @param maxNodes (undefined的行为)
*/
constructor(maxNodes: number) {
this._numNodes = 0;
this._nodes = new Array(maxNodes + 1);
this._numNodesEverEnqueued = 0;
}
/**
*
* O(n)
*/
public clear() {
this._nodes.splice(1, this._numNodes);
this._numNodes = 0;
}
/**
*
* O(1)
*/
public get count() {
return this._numNodes;
}
/**
* (Count == MaxSize)undefined的行为
* O(1)
*/
public get maxSize() {
return this._nodes.length - 1;
}
/**
* (O(1))
* O (1)
* @param node
*/
public contains(node: T): boolean {
if (!node){
console.error("node cannot be null");
return false;
}
if (node.queueIndex < 0 || node.queueIndex >= this._nodes.length){
console.error("node.QueueIndex has been corrupted. Did you change it manually? Or add this node to another queue?");
return false;
}
return (this._nodes[node.queueIndex] == node);
}
/**
*
* undefinedundefined
* O(log n)
* @param node
* @param priority
*/
public enqueue(node: T, priority: number) {
node.priority = priority;
this._numNodes++;
@@ -31,12 +78,21 @@ class PriorityQueue<T extends PriorityQueueNode> {
this.cascadeUp(this._nodes[this._numNodes]);
}
/**
* (;)undefined
* O(log n)
*/
public dequeue(): T {
let returnMe = this._nodes[1];
this.remove(returnMe);
return returnMe;
}
/**
* Contains()
* O(log n)
* @param node
*/
public remove(node: T) {
if (node.queueIndex == this._numNodes) {
this._nodes[this._numNodes] = null;
@@ -52,6 +108,9 @@ class PriorityQueue<T extends PriorityQueueNode> {
this.onNodeUpdated(formerLastNode);
}
/**
* /
*/
public isValidQueue(): boolean {
for (let i = 1; i < this._nodes.length; i++) {
if (this._nodes[i]) {
@@ -71,24 +130,29 @@ class PriorityQueue<T extends PriorityQueueNode> {
}
private onNodeUpdated(node: T) {
// 将更新后的节点按适当的方式向上或向下冒泡
let parentIndex = Math.floor(node.queueIndex / 2);
let parentNode = this._nodes[parentIndex];
if (parentIndex > 0 && this.hasHigherPriority(node, parentNode)) {
this.cascadeUp(node);
} else {
// 注意,如果parentNode == node(即节点是根),则将调用CascadeDown。
this.cascadeDown(node);
}
}
private cascadeDown(node: T) {
// 又名Heapify-down
let newParent: T;
let finalQueueIndex = node.queueIndex;
while (true) {
newParent = node;
let childLeftIndex = 2 * finalQueueIndex;
// 检查左子节点的优先级是否高于当前节点
if (childLeftIndex > this._numNodes) {
// 这可以放在循环之外,但是我们必须检查newParent != node两次
node.queueIndex = finalQueueIndex;
this._nodes[finalQueueIndex] = node;
break;
@@ -99,6 +163,7 @@ class PriorityQueue<T extends PriorityQueueNode> {
newParent = childLeft;
}
// 检查右子节点的优先级是否高于当前节点或左子节点
let childRightIndex = childLeftIndex + 1;
if (childRightIndex <= this._numNodes) {
let childRight = this._nodes[childRightIndex];
@@ -107,13 +172,17 @@ class PriorityQueue<T extends PriorityQueueNode> {
}
}
// 如果其中一个子节点具有更高(更小)的优先级,则交换并继续级联
if (newParent != node) {
// 将新的父节点移动到它的新索引
// 节点将被移动一次,这样做比调用Swap()少一个赋值操作。
this._nodes[finalQueueIndex] = newParent;
let temp = newParent.queueIndex;
newParent.queueIndex = finalQueueIndex;
finalQueueIndex = temp;
} else {
// 参见上面的笔记
node.queueIndex = finalQueueIndex;
this._nodes[finalQueueIndex] = node;
break;
@@ -121,13 +190,20 @@ class PriorityQueue<T extends PriorityQueueNode> {
}
}
/**
*
* @param node
*/
private cascadeUp(node: T) {
// 又名Heapify-up
let parent = Math.floor(node.queueIndex / 2);
while (parent >= 1) {
let parentNode = this._nodes[parent];
if (this.hasHigherPriority(parentNode, node))
break;
// 节点具有较低的优先级值,因此将其向上移动到堆中
// 出于某种原因,使用Swap()比使用单独的操作更快,如CascadeDown()
this.swap(node, parentNode);
parent = Math.floor(node.queueIndex / 2);
@@ -135,14 +211,22 @@ class PriorityQueue<T extends PriorityQueueNode> {
}
private swap(node1: T, node2: T) {
// 交换节点
this._nodes[node1.queueIndex] = node2;
this._nodes[node2.queueIndex] = node1;
// 交换他们的indicies
let temp = node1.queueIndex;
node1.queueIndex = node2.queueIndex;
node2.queueIndex = temp;
}
/**
* higher的优先级高于lowertruefalse
* HasHigherPriority()()false
* @param higher
* @param lower
*/
private hasHigherPriority(higher: T, lower: T) {
return (higher.priority < lower.priority ||
(higher.priority == lower.priority && higher.insertionIndex < lower.insertionIndex));
+22
View File
@@ -0,0 +1,22 @@
class Debug {
private static _debugDrawItems: DebugDrawItem[] = [];
public static drawHollowRect(rectanle: Rectangle, color: number, duration = 0){
this._debugDrawItems.push(new DebugDrawItem(rectanle, color, duration));
}
public static render(){
if (this._debugDrawItems.length > 0){
let debugShape = new egret.Shape();
if (SceneManager.scene){
SceneManager.scene.addChild(debugShape);
}
for (let i = this._debugDrawItems.length - 1; i >= 0; i --){
let item = this._debugDrawItems[i];
if (item.draw(debugShape))
this._debugDrawItems.removeAt(i);
}
}
}
}
+46
View File
@@ -0,0 +1,46 @@
enum DebugDrawType {
line,
hollowRectangle,
pixel,
text
}
class DebugDrawItem {
public rectangle: Rectangle;
public color: number;
public duration: number;
public drawType: DebugDrawType;
public text: string;
public start: Vector2;
public end: Vector2;
public x: number;
public y: number;
public size: number;
constructor(rectangle: Rectangle, color: number, duration: number){
this.rectangle = rectangle;
this.color = color;
this.duration = duration;
this.drawType = DebugDrawType.hollowRectangle;
}
public draw(shape: egret.Shape): boolean{
switch (this.drawType){
case DebugDrawType.line:
DrawUtils.drawLine(shape, this.start, this.end, this.color);
break;
case DebugDrawType.hollowRectangle:
DrawUtils.drawHollowRect(shape, this.rectangle, this.color);
break;
case DebugDrawType.pixel:
DrawUtils.drawPixel(shape, new Vector2(this.x, this.y), this.color, this.size);
break;
case DebugDrawType.text:
break;
}
this.duration -= Time.deltaTime;
return this.duration < 0;
}
}
+3
View File
@@ -81,6 +81,9 @@ class SceneManager {
}
} else if (this._scene) {
this._scene.render();
Debug.render();
this._scene.postRender();
}
}
+8
View File
@@ -50,7 +50,15 @@ class MathHelper {
return new Vector2(Math.cos(radians) * radians + circleCenter.x, Math.sin(radians) * radians + circleCenter.y);
}
/**
* true
* @param value
*/
public static isEven(value: number){
return value % 2 == 0;
}
public static angleBetweenVectors(from: Vector2, to: Vector2){
return Math.atan2(to.y - from.y, to.x - from.x);
}
}
+8
View File
@@ -53,4 +53,12 @@ class Physics {
this._spatialHash.remove(collider);
this._spatialHash.register(collider);
}
/**
* debug绘制空间散列的内容
* @param secondsToDisplay
*/
public static debugDraw(secondsToDisplay){
this._spatialHash.debugDraw(secondsToDisplay, 2);
}
}
+19
View File
@@ -179,6 +179,25 @@ class SpatialHash {
private cellCoords(x: number, y: number): Vector2 {
return new Vector2(Math.floor(x * this._inverseCellSize), Math.floor(y * this._inverseCellSize));
}
/**
* debug绘制空间散列的内容
* @param secondsToDisplay
* @param textScale
*/
public debugDraw(secondsToDisplay: number, textScale: number = 1){
for (let x = this.gridBounds.x; x <= this.gridBounds.right; x ++){
for (let y = this.gridBounds.y; y <= this.gridBounds.bottom; y ++){
let cell = this.cellAtPosition(x, y);
if (cell && cell.length > 0)
this.debugDrawCellDetails(x, y, cell.length, secondsToDisplay, textScale);
}
}
}
private debugDrawCellDetails(x: number, y: number, cellCount: number, secondsToDisplay = 0.5, textScale = 1){
}
}
class RaycastResultParser {
+46
View File
@@ -0,0 +1,46 @@
/** 各种辅助方法来辅助绘图 */
class DrawUtils {
public static drawLine(shape: egret.Shape, start: Vector2, end: Vector2, color: number, thickness: number = 1){
this.drawLineAngle(shape, start, MathHelper.angleBetweenVectors(start, end), Vector2.distance(start, end), color, thickness);
}
public static drawLineAngle(shape: egret.Shape, start: Vector2, radians: number, length: number, color: number, thickness = 1){
shape.graphics.beginFill(color);
shape.graphics.drawRect(start.x, start.y, 1, 1);
shape.graphics.endFill();
shape.scaleX = length;
shape.scaleY = thickness;
shape.$anchorOffsetX = 0;
shape.$anchorOffsetY = 0;
shape.rotation = radians;
}
public static drawHollowRect(shape: egret.Shape, rect: Rectangle, color: number, thickness = 1){
this.drawHollowRectR(shape, rect.x, rect.y, rect.width, rect.height, color, thickness);
}
public static drawHollowRectR(shape: egret.Shape, x: number, y: number, width: number, height: number, color: number, thickness = 1){
let tl = new Vector2(x, y).round();
let tr = new Vector2(x + width, y).round();
let br = new Vector2(x + width, y + height).round();
let bl = new Vector2(x, y + height).round();
this.drawLine(shape, tl, tr, color, thickness);
this.drawLine(shape, tr, br, color, thickness);
this.drawLine(shape, br, bl, color, thickness);
this.drawLine(shape, bl, tl, color, thickness);
}
public static drawPixel(shape: egret.Shape, position: Vector2, color: number, size: number = 1){
let destRect = new Rectangle(position.x, position.y, size, size);
if (size != 1){
destRect.x -= size * 0.5;
destRect.y -= size * 0.5;
}
shape.graphics.beginFill(color);
shape.graphics.drawRect(destRect.x, destRect.y, destRect.width, destRect.height);
shape.graphics.endFill();
}
}