修复切换场景未移除问题

This commit is contained in:
yhh
2020-07-08 15:15:15 +08:00
parent b14fee1685
commit 299c1b8e7d
19 changed files with 546 additions and 516 deletions

View File

@@ -32,22 +32,22 @@ declare class AStarNode<T> extends PriorityQueueNode {
data: T;
constructor(data: T);
}
declare class AstarGridGraph implements IAstarGraph<Point> {
dirs: Point[];
walls: Point[];
weightedNodes: Point[];
declare class AstarGridGraph implements IAstarGraph<Vector2> {
dirs: Vector2[];
walls: Vector2[];
weightedNodes: Vector2[];
defaultWeight: number;
weightedNodeWeight: number;
private _width;
private _height;
private _neighbors;
constructor(width: number, height: number);
isNodeInBounds(node: Point): boolean;
isNodePassable(node: Point): boolean;
search(start: Point, goal: Point): Point[];
getNeighbors(node: Point): Point[];
cost(from: Point, to: Point): number;
heuristic(node: Point, goal: Point): number;
isNodeInBounds(node: Vector2): boolean;
isNodePassable(node: Vector2): boolean;
search(start: Vector2, goal: Vector2): Vector2[];
getNeighbors(node: Vector2): Vector2[];
cost(from: Vector2, to: Vector2): number;
heuristic(node: Vector2, goal: Vector2): number;
}
interface IAstarGraph<T> {
getNeighbors(node: T): Array<T>;
@@ -84,34 +84,57 @@ declare class UnweightedGraph<T> implements IUnweightedGraph<T> {
addEdgesForNode(node: T, edges: T[]): this;
getNeighbors(node: T): T[];
}
declare class Point {
declare class Vector2 {
x: number;
y: number;
private static readonly unitYVector;
private static readonly unitXVector;
private static readonly unitVector2;
private static readonly zeroVector2;
static readonly zero: Vector2;
static readonly one: Vector2;
static readonly unitX: Vector2;
static readonly unitY: Vector2;
constructor(x?: number, y?: number);
static add(value1: Vector2, value2: Vector2): Vector2;
static divide(value1: Vector2, value2: Vector2): Vector2;
static multiply(value1: Vector2, value2: Vector2): Vector2;
static subtract(value1: Vector2, value2: Vector2): Vector2;
normalize(): void;
length(): number;
round(): Vector2;
static normalize(value: Vector2): Vector2;
static dot(value1: Vector2, value2: Vector2): number;
static distanceSquared(value1: Vector2, value2: Vector2): number;
static clamp(value1: Vector2, min: Vector2, max: Vector2): Vector2;
static lerp(value1: Vector2, value2: Vector2, amount: number): Vector2;
static transform(position: Vector2, matrix: Matrix2D): Vector2;
static distance(value1: Vector2, value2: Vector2): number;
static negate(value: Vector2): Vector2;
}
declare class UnweightedGridGraph implements IUnweightedGraph<Point> {
declare class UnweightedGridGraph implements IUnweightedGraph<Vector2> {
private static readonly CARDINAL_DIRS;
private static readonly COMPASS_DIRS;
walls: Point[];
walls: Vector2[];
private _width;
private _hegiht;
private _dirs;
private _neighbors;
constructor(width: number, height: number, allowDiagonalSearch?: boolean);
isNodeInBounds(node: Point): boolean;
isNodePassable(node: Point): boolean;
getNeighbors(node: Point): Point[];
search(start: Point, goal: Point): Point[];
isNodeInBounds(node: Vector2): boolean;
isNodePassable(node: Vector2): boolean;
getNeighbors(node: Vector2): Vector2[];
search(start: Vector2, goal: Vector2): Vector2[];
}
interface IWeightedGraph<T> {
getNeighbors(node: T): T[];
cost(from: T, to: T): number;
}
declare class WeightedGridGraph implements IWeightedGraph<Point> {
static readonly CARDINAL_DIRS: Point[];
declare class WeightedGridGraph implements IWeightedGraph<Vector2> {
static readonly CARDINAL_DIRS: Vector2[];
private static readonly COMPASS_DIRS;
walls: Point[];
weightedNodes: Point[];
walls: Vector2[];
weightedNodes: Vector2[];
defaultWeight: number;
weightedNodeWeight: number;
private _width;
@@ -119,11 +142,11 @@ declare class WeightedGridGraph implements IWeightedGraph<Point> {
private _dirs;
private _neighbors;
constructor(width: number, height: number, allowDiagonalSearch?: boolean);
isNodeInBounds(node: Point): boolean;
isNodePassable(node: Point): boolean;
search(start: Point, goal: Point): Point[];
getNeighbors(node: Point): Point[];
cost(from: Point, to: Point): number;
isNodeInBounds(node: Vector2): boolean;
isNodePassable(node: Vector2): boolean;
search(start: Vector2, goal: Vector2): Vector2[];
getNeighbors(node: Vector2): Vector2[];
cost(from: Vector2, to: Vector2): number;
}
declare class WeightedNode<T> extends PriorityQueueNode {
data: T;
@@ -417,6 +440,7 @@ declare abstract class Collider extends Component {
onRemovedFromEntity(): void;
onEnabled(): void;
onDisabled(): void;
update(): void;
}
declare class BoxCollider extends Collider {
width: number;
@@ -760,7 +784,7 @@ declare class Rectangle {
containsRect(value: Rectangle): boolean;
getHalfSize(): Vector2;
static fromMinMax(minX: number, minY: number, maxX: number, maxY: number): Rectangle;
getClosestPointOnRectangleBorderToPoint(point: Point): {
getClosestPointOnRectangleBorderToPoint(point: Vector2): {
res: Vector2;
edgeNormal: Vector2;
};
@@ -768,34 +792,6 @@ declare class Rectangle {
calculateBounds(parentPosition: Vector2, position: Vector2, origin: Vector2, scale: Vector2, rotation: number, width: number, height: number): void;
static rectEncompassingPoints(points: Vector2[]): Rectangle;
}
declare class Vector2 {
x: number;
y: number;
private static readonly unitYVector;
private static readonly unitXVector;
private static readonly unitVector2;
private static readonly zeroVector2;
static readonly zero: Vector2;
static readonly one: Vector2;
static readonly unitX: Vector2;
static readonly unitY: Vector2;
constructor(x?: number, y?: number);
static add(value1: Vector2, value2: Vector2): Vector2;
static divide(value1: Vector2, value2: Vector2): Vector2;
static multiply(value1: Vector2, value2: Vector2): Vector2;
static subtract(value1: Vector2, value2: Vector2): Vector2;
normalize(): void;
length(): number;
round(): Vector2;
static normalize(value: Vector2): Vector2;
static dot(value1: Vector2, value2: Vector2): number;
static distanceSquared(value1: Vector2, value2: Vector2): number;
static clamp(value1: Vector2, min: Vector2, max: Vector2): Vector2;
static lerp(value1: Vector2, value2: Vector2, amount: number): Vector2;
static transform(position: Vector2, matrix: Matrix2D): Vector2;
static distance(value1: Vector2, value2: Vector2): number;
static negate(value: Vector2): Vector2;
}
declare class Vector3 {
x: number;
y: number;
@@ -928,7 +924,7 @@ declare class ShapeCollisions {
static closestPointOnLine(lineA: Vector2, lineB: Vector2, closestTo: Vector2): Vector2;
static pointToPoly(point: Vector2, poly: Polygon): CollisionResult;
static circleToCircle(first: Circle, second: Circle): CollisionResult;
static boxToBox(first: Box, second: Box): false | CollisionResult;
static boxToBox(first: Box, second: Box): CollisionResult;
private static minkowskiDifference;
}
declare class SpatialHash {
@@ -1035,7 +1031,7 @@ declare class Pair<T> {
equals(other: Pair<T>): boolean;
}
declare class RectangleExt {
static union(first: Rectangle, point: Point): Rectangle;
static union(first: Rectangle, point: Vector2): Rectangle;
static unionR(value1: Rectangle, value2: Rectangle): Rectangle;
}
declare class Triangulator {

View File

@@ -361,10 +361,10 @@ var AStarNode = (function (_super) {
var AstarGridGraph = (function () {
function AstarGridGraph(width, height) {
this.dirs = [
new Point(1, 0),
new Point(0, -1),
new Point(-1, 0),
new Point(0, 1)
new Vector2(1, 0),
new Vector2(0, -1),
new Vector2(-1, 0),
new Vector2(0, 1)
];
this.walls = [];
this.weightedNodes = [];
@@ -387,7 +387,7 @@ var AstarGridGraph = (function () {
var _this = this;
this._neighbors.length = 0;
this.dirs.forEach(function (dir) {
var next = new Point(node.x + dir.x, node.y + dir.y);
var next = new Vector2(node.x + dir.x, node.y + dir.y);
if (_this.isNodeInBounds(next) && _this.isNodePassable(next))
_this._neighbors.push(next);
});
@@ -583,12 +583,113 @@ var UnweightedGraph = (function () {
};
return UnweightedGraph;
}());
var Point = (function () {
function Point(x, y) {
var Vector2 = (function () {
function Vector2(x, y) {
this.x = 0;
this.y = 0;
this.x = x ? x : 0;
this.y = y ? y : this.x;
}
return Point;
Object.defineProperty(Vector2, "zero", {
get: function () {
return Vector2.zeroVector2;
},
enumerable: true,
configurable: true
});
Object.defineProperty(Vector2, "one", {
get: function () {
return Vector2.unitVector2;
},
enumerable: true,
configurable: true
});
Object.defineProperty(Vector2, "unitX", {
get: function () {
return Vector2.unitXVector;
},
enumerable: true,
configurable: true
});
Object.defineProperty(Vector2, "unitY", {
get: function () {
return Vector2.unitYVector;
},
enumerable: true,
configurable: true
});
Vector2.add = function (value1, value2) {
var result = new Vector2(0, 0);
result.x = value1.x + value2.x;
result.y = value1.y + value2.y;
return result;
};
Vector2.divide = function (value1, value2) {
var result = new Vector2(0, 0);
result.x = value1.x / value2.x;
result.y = value1.y / value2.y;
return result;
};
Vector2.multiply = function (value1, value2) {
var result = new Vector2(0, 0);
result.x = value1.x * value2.x;
result.y = value1.y * value2.y;
return result;
};
Vector2.subtract = function (value1, value2) {
var result = new Vector2(0, 0);
result.x = value1.x - value2.x;
result.y = value1.y - value2.y;
return result;
};
Vector2.prototype.normalize = function () {
var val = 1 / Math.sqrt((this.x * this.x) + (this.y * this.y));
this.x *= val;
this.y *= val;
};
Vector2.prototype.length = function () {
return Math.sqrt((this.x * this.x) + (this.y * this.y));
};
Vector2.prototype.round = function () {
return new Vector2(Math.round(this.x), Math.round(this.y));
};
Vector2.normalize = function (value) {
var val = 1 / Math.sqrt((value.x * value.x) + (value.y * value.y));
value.x *= val;
value.y *= val;
return value;
};
Vector2.dot = function (value1, value2) {
return (value1.x * value2.x) + (value1.y * value2.y);
};
Vector2.distanceSquared = function (value1, value2) {
var v1 = value1.x - value2.x, v2 = value1.y - value2.y;
return (v1 * v1) + (v2 * v2);
};
Vector2.clamp = function (value1, min, max) {
return new Vector2(MathHelper.clamp(value1.x, min.x, max.x), MathHelper.clamp(value1.y, min.y, max.y));
};
Vector2.lerp = function (value1, value2, amount) {
return new Vector2(MathHelper.lerp(value1.x, value2.x, amount), MathHelper.lerp(value1.y, value2.y, amount));
};
Vector2.transform = function (position, matrix) {
return new Vector2((position.x * matrix.m11) + (position.y * matrix.m21), (position.x * matrix.m12) + (position.y * matrix.m22));
};
Vector2.distance = function (value1, value2) {
var v1 = value1.x - value2.x, v2 = value1.y - value2.y;
return Math.sqrt((v1 * v1) + (v2 * v2));
};
Vector2.negate = function (value) {
var result = new Vector2();
result.x = -value.x;
result.y = -value.y;
return result;
};
Vector2.unitYVector = new Vector2(0, 1);
Vector2.unitXVector = new Vector2(1, 0);
Vector2.unitVector2 = new Vector2(1, 1);
Vector2.zeroVector2 = new Vector2(0, 0);
return Vector2;
}());
var UnweightedGridGraph = (function () {
function UnweightedGridGraph(width, height, allowDiagonalSearch) {
@@ -609,7 +710,7 @@ var UnweightedGridGraph = (function () {
var _this = this;
this._neighbors.length = 0;
this._dirs.forEach(function (dir) {
var next = new Point(node.x + dir.x, node.y + dir.y);
var next = new Vector2(node.x + dir.x, node.y + dir.y);
if (_this.isNodeInBounds(next) && _this.isNodePassable(next))
_this._neighbors.push(next);
});
@@ -619,20 +720,20 @@ var UnweightedGridGraph = (function () {
return BreadthFirstPathfinder.search(this, start, goal);
};
UnweightedGridGraph.CARDINAL_DIRS = [
new Point(1, 0),
new Point(0, -1),
new Point(-1, 0),
new Point(0, -1)
new Vector2(1, 0),
new Vector2(0, -1),
new Vector2(-1, 0),
new Vector2(0, -1)
];
UnweightedGridGraph.COMPASS_DIRS = [
new Point(1, 0),
new Point(1, -1),
new Point(0, -1),
new Point(-1, -1),
new Point(-1, 0),
new Point(-1, 1),
new Point(0, 1),
new Point(1, 1),
new Vector2(1, 0),
new Vector2(1, -1),
new Vector2(0, -1),
new Vector2(-1, -1),
new Vector2(-1, 0),
new Vector2(-1, 1),
new Vector2(0, 1),
new Vector2(1, 1),
];
return UnweightedGridGraph;
}());
@@ -661,7 +762,7 @@ var WeightedGridGraph = (function () {
var _this = this;
this._neighbors.length = 0;
this._dirs.forEach(function (dir) {
var next = new Point(node.x + dir.x, node.y + dir.y);
var next = new Vector2(node.x + dir.x, node.y + dir.y);
if (_this.isNodeInBounds(next) && _this.isNodePassable(next))
_this._neighbors.push(next);
});
@@ -671,20 +772,20 @@ var WeightedGridGraph = (function () {
return this.weightedNodes.find(function (t) { return JSON.stringify(t) == JSON.stringify(to); }) ? this.weightedNodeWeight : this.defaultWeight;
};
WeightedGridGraph.CARDINAL_DIRS = [
new Point(1, 0),
new Point(0, -1),
new Point(-1, 0),
new Point(0, 1)
new Vector2(1, 0),
new Vector2(0, -1),
new Vector2(-1, 0),
new Vector2(0, 1)
];
WeightedGridGraph.COMPASS_DIRS = [
new Point(1, 0),
new Point(1, -1),
new Point(0, -1),
new Point(-1, -1),
new Point(-1, 0),
new Point(-1, 1),
new Point(0, 1),
new Point(1, 1),
new Vector2(1, 0),
new Vector2(1, -1),
new Vector2(0, -1),
new Vector2(-1, -1),
new Vector2(-1, 0),
new Vector2(-1, 1),
new Vector2(0, 1),
new Vector2(1, 1),
];
return WeightedGridGraph;
}());
@@ -1112,6 +1213,8 @@ var Scene = (function (_super) {
if (this.entityProcessors)
this.entityProcessors.end();
this.unload();
if (this.parent)
this.parent.removeChild(this);
};
Scene.prototype.onStart = function () {
return __awaiter(this, void 0, void 0, function () {
@@ -1930,6 +2033,13 @@ var Collider = (function (_super) {
Collider.prototype.onDisabled = function () {
this.unregisterColliderWithPhysicsSystem();
};
Collider.prototype.update = function () {
var spriteRenderer = this.entity.getComponent(SpriteRenderer);
if (spriteRenderer) {
this.bounds.x = spriteRenderer.x;
this.bounds.y = spriteRenderer.y;
}
};
return Collider;
}(Component));
var BoxCollider = (function (_super) {
@@ -3556,114 +3666,6 @@ var Rectangle = (function () {
};
return Rectangle;
}());
var Vector2 = (function () {
function Vector2(x, y) {
this.x = 0;
this.y = 0;
this.x = x ? x : 0;
this.y = y ? y : this.x;
}
Object.defineProperty(Vector2, "zero", {
get: function () {
return Vector2.zeroVector2;
},
enumerable: true,
configurable: true
});
Object.defineProperty(Vector2, "one", {
get: function () {
return Vector2.unitVector2;
},
enumerable: true,
configurable: true
});
Object.defineProperty(Vector2, "unitX", {
get: function () {
return Vector2.unitXVector;
},
enumerable: true,
configurable: true
});
Object.defineProperty(Vector2, "unitY", {
get: function () {
return Vector2.unitYVector;
},
enumerable: true,
configurable: true
});
Vector2.add = function (value1, value2) {
var result = new Vector2(0, 0);
result.x = value1.x + value2.x;
result.y = value1.y + value2.y;
return result;
};
Vector2.divide = function (value1, value2) {
var result = new Vector2(0, 0);
result.x = value1.x / value2.x;
result.y = value1.y / value2.y;
return result;
};
Vector2.multiply = function (value1, value2) {
var result = new Vector2(0, 0);
result.x = value1.x * value2.x;
result.y = value1.y * value2.y;
return result;
};
Vector2.subtract = function (value1, value2) {
var result = new Vector2(0, 0);
result.x = value1.x - value2.x;
result.y = value1.y - value2.y;
return result;
};
Vector2.prototype.normalize = function () {
var val = 1 / Math.sqrt((this.x * this.x) + (this.y * this.y));
this.x *= val;
this.y *= val;
};
Vector2.prototype.length = function () {
return Math.sqrt((this.x * this.x) + (this.y * this.y));
};
Vector2.prototype.round = function () {
return new Vector2(Math.round(this.x), Math.round(this.y));
};
Vector2.normalize = function (value) {
var val = 1 / Math.sqrt((value.x * value.x) + (value.y * value.y));
value.x *= val;
value.y *= val;
return value;
};
Vector2.dot = function (value1, value2) {
return (value1.x * value2.x) + (value1.y * value2.y);
};
Vector2.distanceSquared = function (value1, value2) {
var v1 = value1.x - value2.x, v2 = value1.y - value2.y;
return (v1 * v1) + (v2 * v2);
};
Vector2.clamp = function (value1, min, max) {
return new Vector2(MathHelper.clamp(value1.x, min.x, max.x), MathHelper.clamp(value1.y, min.y, max.y));
};
Vector2.lerp = function (value1, value2, amount) {
return new Vector2(MathHelper.lerp(value1.x, value2.x, amount), MathHelper.lerp(value1.y, value2.y, amount));
};
Vector2.transform = function (position, matrix) {
return new Vector2((position.x * matrix.m11) + (position.y * matrix.m21), (position.x * matrix.m12) + (position.y * matrix.m22));
};
Vector2.distance = function (value1, value2) {
var v1 = value1.x - value2.x, v2 = value1.y - value2.y;
return Math.sqrt((v1 * v1) + (v2 * v2));
};
Vector2.negate = function (value) {
var result = new Vector2();
result.x = -value.x;
result.y = -value.y;
return result;
};
Vector2.unitYVector = new Vector2(0, 1);
Vector2.unitXVector = new Vector2(1, 0);
Vector2.unitVector2 = new Vector2(1, 1);
Vector2.zeroVector2 = new Vector2(0, 0);
return Vector2;
}());
var Vector3 = (function () {
function Vector3(x, y, z) {
this.x = x;
@@ -4254,7 +4256,7 @@ var ShapeCollisions = (function () {
}
}
result.normal = translationAxis;
result.minimumTranslationVector = Vector2.multiply(new Vector2(-translationAxis), new Vector2(minIntervalDistance));
result.minimumTranslationVector = Vector2.multiply(new Vector2(-translationAxis.x, -translationAxis.y), new Vector2(minIntervalDistance));
return result;
};
ShapeCollisions.intervalDistance = function (minA, maxA, minB, maxB) {
@@ -4379,8 +4381,8 @@ var ShapeCollisions = (function () {
var minkowskiDiff = this.minkowskiDifference(first, second);
if (minkowskiDiff.contains(new Vector2(0, 0))) {
result.minimumTranslationVector = minkowskiDiff.getClosestPointOnBoundsToOrigin();
if (result.minimumTranslationVector == Vector2.zero)
return false;
if (result.minimumTranslationVector.x == 0 && result.minimumTranslationVector.y == 0)
return null;
result.normal = new Vector2(-result.minimumTranslationVector.x, -result.minimumTranslationVector.y);
result.normal.normalize();
return result;
@@ -4496,7 +4498,7 @@ var SpatialHash = (function () {
return cell;
};
SpatialHash.prototype.cellCoords = function (x, y) {
return new Point(Math.floor(x * this._inverseCellSize), Math.floor(y * this._inverseCellSize));
return new Vector2(Math.floor(x * this._inverseCellSize), Math.floor(y * this._inverseCellSize));
};
return SpatialHash;
}());

File diff suppressed because one or more lines are too long

View File

@@ -15,13 +15,13 @@ class MainScene extends Scene {
bg.addComponent(new PlayerController());
bg.addComponent(new Mover());
bg.addComponent(new BoxCollider());
bg.position = new Vector2(Math.random() * 200, Math.random() * 200);
bg.position = new Vector2(30, 30);
for (let i = 0; i < 100; i++) {
for (let i = 0; i < 1; i++) {
let sprite = new Sprite(RES.getRes("checkbox_select_disabled_png"));
let player2 = this.createEntity("player2");
player2.addComponent(new SpriteRenderer()).setSprite(sprite);
player2.position = new Vector2(Math.random() * 100 * i, Math.random() * 100 * i);
player2.position = new Vector2(10, 10);
player2.addComponent(new BoxCollider());
}
@@ -63,24 +63,24 @@ class MainScene extends Scene {
public dijkstraTest() {
let graph = new WeightedGridGraph(20, 20);
graph.weightedNodes.push(new Point(3, 3));
graph.weightedNodes.push(new Point(3, 4));
graph.weightedNodes.push(new Point(4, 3));
graph.weightedNodes.push(new Point(4, 4));
graph.weightedNodes.push(new Vector2(3, 3));
graph.weightedNodes.push(new Vector2(3, 4));
graph.weightedNodes.push(new Vector2(4, 3));
graph.weightedNodes.push(new Vector2(4, 4));
let path = graph.search(new Point(3, 4), new Point(15, 17));
let path = graph.search(new Vector2(3, 4), new Vector2(15, 17));
console.log(path);
}
public astarTest() {
let graph = new AstarGridGraph(20, 20);
graph.weightedNodes.push(new Point(3, 3));
graph.weightedNodes.push(new Point(3, 4));
graph.weightedNodes.push(new Point(4, 3));
graph.weightedNodes.push(new Point(4, 4));
graph.weightedNodes.push(new Vector2(3, 3));
graph.weightedNodes.push(new Vector2(3, 4));
graph.weightedNodes.push(new Vector2(4, 3));
graph.weightedNodes.push(new Vector2(4, 4));
let path = graph.search(new Point(3, 4), new Point(15, 17));
let path = graph.search(new Vector2(3, 4), new Vector2(15, 17));
console.log(path);
}
}

View File

@@ -34,23 +34,23 @@ class PlayerController extends Component {
return;
if (this.down){
let camera = SceneManager.scene.camera;
let moveLeft: number = 0;
let moveRight: number = 0;
let speed = 200;
let worldPos = Input.touchPosition;
if (worldPos.x < this.spriteRenderer.x){
moveLeft = -1;
} else if(worldPos.x > this.spriteRenderer.x){
moveLeft = 1;
}
// let camera = SceneManager.scene.camera;
// let moveLeft: number = 0;
// let moveRight: number = 0;
// let speed = 100;
// let worldPos = Input.touchPosition;
// if (worldPos.x < this.spriteRenderer.x){
// moveLeft = -1;
// } else if(worldPos.x > this.spriteRenderer.x){
// moveLeft = 1;
// }
if (worldPos.y < this.spriteRenderer.y){
moveRight = -1;
} else if(worldPos.y > this.spriteRenderer.y){
moveRight = 1;
}
this.mover.move(new Vector2(moveLeft * speed * Time.deltaTime, moveRight * speed * Time.deltaTime));
// if (worldPos.y < this.spriteRenderer.y){
// moveRight = -1;
// } else if(worldPos.y > this.spriteRenderer.y){
// moveRight = 1;
// }
this.mover.move(new Vector2(-1, -1));
}
}
}

View File

@@ -32,22 +32,22 @@ declare class AStarNode<T> extends PriorityQueueNode {
data: T;
constructor(data: T);
}
declare class AstarGridGraph implements IAstarGraph<Point> {
dirs: Point[];
walls: Point[];
weightedNodes: Point[];
declare class AstarGridGraph implements IAstarGraph<Vector2> {
dirs: Vector2[];
walls: Vector2[];
weightedNodes: Vector2[];
defaultWeight: number;
weightedNodeWeight: number;
private _width;
private _height;
private _neighbors;
constructor(width: number, height: number);
isNodeInBounds(node: Point): boolean;
isNodePassable(node: Point): boolean;
search(start: Point, goal: Point): Point[];
getNeighbors(node: Point): Point[];
cost(from: Point, to: Point): number;
heuristic(node: Point, goal: Point): number;
isNodeInBounds(node: Vector2): boolean;
isNodePassable(node: Vector2): boolean;
search(start: Vector2, goal: Vector2): Vector2[];
getNeighbors(node: Vector2): Vector2[];
cost(from: Vector2, to: Vector2): number;
heuristic(node: Vector2, goal: Vector2): number;
}
interface IAstarGraph<T> {
getNeighbors(node: T): Array<T>;
@@ -84,34 +84,57 @@ declare class UnweightedGraph<T> implements IUnweightedGraph<T> {
addEdgesForNode(node: T, edges: T[]): this;
getNeighbors(node: T): T[];
}
declare class Point {
declare class Vector2 {
x: number;
y: number;
private static readonly unitYVector;
private static readonly unitXVector;
private static readonly unitVector2;
private static readonly zeroVector2;
static readonly zero: Vector2;
static readonly one: Vector2;
static readonly unitX: Vector2;
static readonly unitY: Vector2;
constructor(x?: number, y?: number);
static add(value1: Vector2, value2: Vector2): Vector2;
static divide(value1: Vector2, value2: Vector2): Vector2;
static multiply(value1: Vector2, value2: Vector2): Vector2;
static subtract(value1: Vector2, value2: Vector2): Vector2;
normalize(): void;
length(): number;
round(): Vector2;
static normalize(value: Vector2): Vector2;
static dot(value1: Vector2, value2: Vector2): number;
static distanceSquared(value1: Vector2, value2: Vector2): number;
static clamp(value1: Vector2, min: Vector2, max: Vector2): Vector2;
static lerp(value1: Vector2, value2: Vector2, amount: number): Vector2;
static transform(position: Vector2, matrix: Matrix2D): Vector2;
static distance(value1: Vector2, value2: Vector2): number;
static negate(value: Vector2): Vector2;
}
declare class UnweightedGridGraph implements IUnweightedGraph<Point> {
declare class UnweightedGridGraph implements IUnweightedGraph<Vector2> {
private static readonly CARDINAL_DIRS;
private static readonly COMPASS_DIRS;
walls: Point[];
walls: Vector2[];
private _width;
private _hegiht;
private _dirs;
private _neighbors;
constructor(width: number, height: number, allowDiagonalSearch?: boolean);
isNodeInBounds(node: Point): boolean;
isNodePassable(node: Point): boolean;
getNeighbors(node: Point): Point[];
search(start: Point, goal: Point): Point[];
isNodeInBounds(node: Vector2): boolean;
isNodePassable(node: Vector2): boolean;
getNeighbors(node: Vector2): Vector2[];
search(start: Vector2, goal: Vector2): Vector2[];
}
interface IWeightedGraph<T> {
getNeighbors(node: T): T[];
cost(from: T, to: T): number;
}
declare class WeightedGridGraph implements IWeightedGraph<Point> {
static readonly CARDINAL_DIRS: Point[];
declare class WeightedGridGraph implements IWeightedGraph<Vector2> {
static readonly CARDINAL_DIRS: Vector2[];
private static readonly COMPASS_DIRS;
walls: Point[];
weightedNodes: Point[];
walls: Vector2[];
weightedNodes: Vector2[];
defaultWeight: number;
weightedNodeWeight: number;
private _width;
@@ -119,11 +142,11 @@ declare class WeightedGridGraph implements IWeightedGraph<Point> {
private _dirs;
private _neighbors;
constructor(width: number, height: number, allowDiagonalSearch?: boolean);
isNodeInBounds(node: Point): boolean;
isNodePassable(node: Point): boolean;
search(start: Point, goal: Point): Point[];
getNeighbors(node: Point): Point[];
cost(from: Point, to: Point): number;
isNodeInBounds(node: Vector2): boolean;
isNodePassable(node: Vector2): boolean;
search(start: Vector2, goal: Vector2): Vector2[];
getNeighbors(node: Vector2): Vector2[];
cost(from: Vector2, to: Vector2): number;
}
declare class WeightedNode<T> extends PriorityQueueNode {
data: T;
@@ -417,6 +440,7 @@ declare abstract class Collider extends Component {
onRemovedFromEntity(): void;
onEnabled(): void;
onDisabled(): void;
update(): void;
}
declare class BoxCollider extends Collider {
width: number;
@@ -760,7 +784,7 @@ declare class Rectangle {
containsRect(value: Rectangle): boolean;
getHalfSize(): Vector2;
static fromMinMax(minX: number, minY: number, maxX: number, maxY: number): Rectangle;
getClosestPointOnRectangleBorderToPoint(point: Point): {
getClosestPointOnRectangleBorderToPoint(point: Vector2): {
res: Vector2;
edgeNormal: Vector2;
};
@@ -768,34 +792,6 @@ declare class Rectangle {
calculateBounds(parentPosition: Vector2, position: Vector2, origin: Vector2, scale: Vector2, rotation: number, width: number, height: number): void;
static rectEncompassingPoints(points: Vector2[]): Rectangle;
}
declare class Vector2 {
x: number;
y: number;
private static readonly unitYVector;
private static readonly unitXVector;
private static readonly unitVector2;
private static readonly zeroVector2;
static readonly zero: Vector2;
static readonly one: Vector2;
static readonly unitX: Vector2;
static readonly unitY: Vector2;
constructor(x?: number, y?: number);
static add(value1: Vector2, value2: Vector2): Vector2;
static divide(value1: Vector2, value2: Vector2): Vector2;
static multiply(value1: Vector2, value2: Vector2): Vector2;
static subtract(value1: Vector2, value2: Vector2): Vector2;
normalize(): void;
length(): number;
round(): Vector2;
static normalize(value: Vector2): Vector2;
static dot(value1: Vector2, value2: Vector2): number;
static distanceSquared(value1: Vector2, value2: Vector2): number;
static clamp(value1: Vector2, min: Vector2, max: Vector2): Vector2;
static lerp(value1: Vector2, value2: Vector2, amount: number): Vector2;
static transform(position: Vector2, matrix: Matrix2D): Vector2;
static distance(value1: Vector2, value2: Vector2): number;
static negate(value: Vector2): Vector2;
}
declare class Vector3 {
x: number;
y: number;
@@ -928,7 +924,7 @@ declare class ShapeCollisions {
static closestPointOnLine(lineA: Vector2, lineB: Vector2, closestTo: Vector2): Vector2;
static pointToPoly(point: Vector2, poly: Polygon): CollisionResult;
static circleToCircle(first: Circle, second: Circle): CollisionResult;
static boxToBox(first: Box, second: Box): false | CollisionResult;
static boxToBox(first: Box, second: Box): CollisionResult;
private static minkowskiDifference;
}
declare class SpatialHash {
@@ -1035,7 +1031,7 @@ declare class Pair<T> {
equals(other: Pair<T>): boolean;
}
declare class RectangleExt {
static union(first: Rectangle, point: Point): Rectangle;
static union(first: Rectangle, point: Vector2): Rectangle;
static unionR(value1: Rectangle, value2: Rectangle): Rectangle;
}
declare class Triangulator {

View File

@@ -361,10 +361,10 @@ var AStarNode = (function (_super) {
var AstarGridGraph = (function () {
function AstarGridGraph(width, height) {
this.dirs = [
new Point(1, 0),
new Point(0, -1),
new Point(-1, 0),
new Point(0, 1)
new Vector2(1, 0),
new Vector2(0, -1),
new Vector2(-1, 0),
new Vector2(0, 1)
];
this.walls = [];
this.weightedNodes = [];
@@ -387,7 +387,7 @@ var AstarGridGraph = (function () {
var _this = this;
this._neighbors.length = 0;
this.dirs.forEach(function (dir) {
var next = new Point(node.x + dir.x, node.y + dir.y);
var next = new Vector2(node.x + dir.x, node.y + dir.y);
if (_this.isNodeInBounds(next) && _this.isNodePassable(next))
_this._neighbors.push(next);
});
@@ -583,12 +583,113 @@ var UnweightedGraph = (function () {
};
return UnweightedGraph;
}());
var Point = (function () {
function Point(x, y) {
var Vector2 = (function () {
function Vector2(x, y) {
this.x = 0;
this.y = 0;
this.x = x ? x : 0;
this.y = y ? y : this.x;
}
return Point;
Object.defineProperty(Vector2, "zero", {
get: function () {
return Vector2.zeroVector2;
},
enumerable: true,
configurable: true
});
Object.defineProperty(Vector2, "one", {
get: function () {
return Vector2.unitVector2;
},
enumerable: true,
configurable: true
});
Object.defineProperty(Vector2, "unitX", {
get: function () {
return Vector2.unitXVector;
},
enumerable: true,
configurable: true
});
Object.defineProperty(Vector2, "unitY", {
get: function () {
return Vector2.unitYVector;
},
enumerable: true,
configurable: true
});
Vector2.add = function (value1, value2) {
var result = new Vector2(0, 0);
result.x = value1.x + value2.x;
result.y = value1.y + value2.y;
return result;
};
Vector2.divide = function (value1, value2) {
var result = new Vector2(0, 0);
result.x = value1.x / value2.x;
result.y = value1.y / value2.y;
return result;
};
Vector2.multiply = function (value1, value2) {
var result = new Vector2(0, 0);
result.x = value1.x * value2.x;
result.y = value1.y * value2.y;
return result;
};
Vector2.subtract = function (value1, value2) {
var result = new Vector2(0, 0);
result.x = value1.x - value2.x;
result.y = value1.y - value2.y;
return result;
};
Vector2.prototype.normalize = function () {
var val = 1 / Math.sqrt((this.x * this.x) + (this.y * this.y));
this.x *= val;
this.y *= val;
};
Vector2.prototype.length = function () {
return Math.sqrt((this.x * this.x) + (this.y * this.y));
};
Vector2.prototype.round = function () {
return new Vector2(Math.round(this.x), Math.round(this.y));
};
Vector2.normalize = function (value) {
var val = 1 / Math.sqrt((value.x * value.x) + (value.y * value.y));
value.x *= val;
value.y *= val;
return value;
};
Vector2.dot = function (value1, value2) {
return (value1.x * value2.x) + (value1.y * value2.y);
};
Vector2.distanceSquared = function (value1, value2) {
var v1 = value1.x - value2.x, v2 = value1.y - value2.y;
return (v1 * v1) + (v2 * v2);
};
Vector2.clamp = function (value1, min, max) {
return new Vector2(MathHelper.clamp(value1.x, min.x, max.x), MathHelper.clamp(value1.y, min.y, max.y));
};
Vector2.lerp = function (value1, value2, amount) {
return new Vector2(MathHelper.lerp(value1.x, value2.x, amount), MathHelper.lerp(value1.y, value2.y, amount));
};
Vector2.transform = function (position, matrix) {
return new Vector2((position.x * matrix.m11) + (position.y * matrix.m21), (position.x * matrix.m12) + (position.y * matrix.m22));
};
Vector2.distance = function (value1, value2) {
var v1 = value1.x - value2.x, v2 = value1.y - value2.y;
return Math.sqrt((v1 * v1) + (v2 * v2));
};
Vector2.negate = function (value) {
var result = new Vector2();
result.x = -value.x;
result.y = -value.y;
return result;
};
Vector2.unitYVector = new Vector2(0, 1);
Vector2.unitXVector = new Vector2(1, 0);
Vector2.unitVector2 = new Vector2(1, 1);
Vector2.zeroVector2 = new Vector2(0, 0);
return Vector2;
}());
var UnweightedGridGraph = (function () {
function UnweightedGridGraph(width, height, allowDiagonalSearch) {
@@ -609,7 +710,7 @@ var UnweightedGridGraph = (function () {
var _this = this;
this._neighbors.length = 0;
this._dirs.forEach(function (dir) {
var next = new Point(node.x + dir.x, node.y + dir.y);
var next = new Vector2(node.x + dir.x, node.y + dir.y);
if (_this.isNodeInBounds(next) && _this.isNodePassable(next))
_this._neighbors.push(next);
});
@@ -619,20 +720,20 @@ var UnweightedGridGraph = (function () {
return BreadthFirstPathfinder.search(this, start, goal);
};
UnweightedGridGraph.CARDINAL_DIRS = [
new Point(1, 0),
new Point(0, -1),
new Point(-1, 0),
new Point(0, -1)
new Vector2(1, 0),
new Vector2(0, -1),
new Vector2(-1, 0),
new Vector2(0, -1)
];
UnweightedGridGraph.COMPASS_DIRS = [
new Point(1, 0),
new Point(1, -1),
new Point(0, -1),
new Point(-1, -1),
new Point(-1, 0),
new Point(-1, 1),
new Point(0, 1),
new Point(1, 1),
new Vector2(1, 0),
new Vector2(1, -1),
new Vector2(0, -1),
new Vector2(-1, -1),
new Vector2(-1, 0),
new Vector2(-1, 1),
new Vector2(0, 1),
new Vector2(1, 1),
];
return UnweightedGridGraph;
}());
@@ -661,7 +762,7 @@ var WeightedGridGraph = (function () {
var _this = this;
this._neighbors.length = 0;
this._dirs.forEach(function (dir) {
var next = new Point(node.x + dir.x, node.y + dir.y);
var next = new Vector2(node.x + dir.x, node.y + dir.y);
if (_this.isNodeInBounds(next) && _this.isNodePassable(next))
_this._neighbors.push(next);
});
@@ -671,20 +772,20 @@ var WeightedGridGraph = (function () {
return this.weightedNodes.find(function (t) { return JSON.stringify(t) == JSON.stringify(to); }) ? this.weightedNodeWeight : this.defaultWeight;
};
WeightedGridGraph.CARDINAL_DIRS = [
new Point(1, 0),
new Point(0, -1),
new Point(-1, 0),
new Point(0, 1)
new Vector2(1, 0),
new Vector2(0, -1),
new Vector2(-1, 0),
new Vector2(0, 1)
];
WeightedGridGraph.COMPASS_DIRS = [
new Point(1, 0),
new Point(1, -1),
new Point(0, -1),
new Point(-1, -1),
new Point(-1, 0),
new Point(-1, 1),
new Point(0, 1),
new Point(1, 1),
new Vector2(1, 0),
new Vector2(1, -1),
new Vector2(0, -1),
new Vector2(-1, -1),
new Vector2(-1, 0),
new Vector2(-1, 1),
new Vector2(0, 1),
new Vector2(1, 1),
];
return WeightedGridGraph;
}());
@@ -1112,6 +1213,8 @@ var Scene = (function (_super) {
if (this.entityProcessors)
this.entityProcessors.end();
this.unload();
if (this.parent)
this.parent.removeChild(this);
};
Scene.prototype.onStart = function () {
return __awaiter(this, void 0, void 0, function () {
@@ -1930,6 +2033,13 @@ var Collider = (function (_super) {
Collider.prototype.onDisabled = function () {
this.unregisterColliderWithPhysicsSystem();
};
Collider.prototype.update = function () {
var spriteRenderer = this.entity.getComponent(SpriteRenderer);
if (spriteRenderer) {
this.bounds.x = spriteRenderer.x;
this.bounds.y = spriteRenderer.y;
}
};
return Collider;
}(Component));
var BoxCollider = (function (_super) {
@@ -3556,114 +3666,6 @@ var Rectangle = (function () {
};
return Rectangle;
}());
var Vector2 = (function () {
function Vector2(x, y) {
this.x = 0;
this.y = 0;
this.x = x ? x : 0;
this.y = y ? y : this.x;
}
Object.defineProperty(Vector2, "zero", {
get: function () {
return Vector2.zeroVector2;
},
enumerable: true,
configurable: true
});
Object.defineProperty(Vector2, "one", {
get: function () {
return Vector2.unitVector2;
},
enumerable: true,
configurable: true
});
Object.defineProperty(Vector2, "unitX", {
get: function () {
return Vector2.unitXVector;
},
enumerable: true,
configurable: true
});
Object.defineProperty(Vector2, "unitY", {
get: function () {
return Vector2.unitYVector;
},
enumerable: true,
configurable: true
});
Vector2.add = function (value1, value2) {
var result = new Vector2(0, 0);
result.x = value1.x + value2.x;
result.y = value1.y + value2.y;
return result;
};
Vector2.divide = function (value1, value2) {
var result = new Vector2(0, 0);
result.x = value1.x / value2.x;
result.y = value1.y / value2.y;
return result;
};
Vector2.multiply = function (value1, value2) {
var result = new Vector2(0, 0);
result.x = value1.x * value2.x;
result.y = value1.y * value2.y;
return result;
};
Vector2.subtract = function (value1, value2) {
var result = new Vector2(0, 0);
result.x = value1.x - value2.x;
result.y = value1.y - value2.y;
return result;
};
Vector2.prototype.normalize = function () {
var val = 1 / Math.sqrt((this.x * this.x) + (this.y * this.y));
this.x *= val;
this.y *= val;
};
Vector2.prototype.length = function () {
return Math.sqrt((this.x * this.x) + (this.y * this.y));
};
Vector2.prototype.round = function () {
return new Vector2(Math.round(this.x), Math.round(this.y));
};
Vector2.normalize = function (value) {
var val = 1 / Math.sqrt((value.x * value.x) + (value.y * value.y));
value.x *= val;
value.y *= val;
return value;
};
Vector2.dot = function (value1, value2) {
return (value1.x * value2.x) + (value1.y * value2.y);
};
Vector2.distanceSquared = function (value1, value2) {
var v1 = value1.x - value2.x, v2 = value1.y - value2.y;
return (v1 * v1) + (v2 * v2);
};
Vector2.clamp = function (value1, min, max) {
return new Vector2(MathHelper.clamp(value1.x, min.x, max.x), MathHelper.clamp(value1.y, min.y, max.y));
};
Vector2.lerp = function (value1, value2, amount) {
return new Vector2(MathHelper.lerp(value1.x, value2.x, amount), MathHelper.lerp(value1.y, value2.y, amount));
};
Vector2.transform = function (position, matrix) {
return new Vector2((position.x * matrix.m11) + (position.y * matrix.m21), (position.x * matrix.m12) + (position.y * matrix.m22));
};
Vector2.distance = function (value1, value2) {
var v1 = value1.x - value2.x, v2 = value1.y - value2.y;
return Math.sqrt((v1 * v1) + (v2 * v2));
};
Vector2.negate = function (value) {
var result = new Vector2();
result.x = -value.x;
result.y = -value.y;
return result;
};
Vector2.unitYVector = new Vector2(0, 1);
Vector2.unitXVector = new Vector2(1, 0);
Vector2.unitVector2 = new Vector2(1, 1);
Vector2.zeroVector2 = new Vector2(0, 0);
return Vector2;
}());
var Vector3 = (function () {
function Vector3(x, y, z) {
this.x = x;
@@ -4254,7 +4256,7 @@ var ShapeCollisions = (function () {
}
}
result.normal = translationAxis;
result.minimumTranslationVector = Vector2.multiply(new Vector2(-translationAxis), new Vector2(minIntervalDistance));
result.minimumTranslationVector = Vector2.multiply(new Vector2(-translationAxis.x, -translationAxis.y), new Vector2(minIntervalDistance));
return result;
};
ShapeCollisions.intervalDistance = function (minA, maxA, minB, maxB) {
@@ -4379,8 +4381,8 @@ var ShapeCollisions = (function () {
var minkowskiDiff = this.minkowskiDifference(first, second);
if (minkowskiDiff.contains(new Vector2(0, 0))) {
result.minimumTranslationVector = minkowskiDiff.getClosestPointOnBoundsToOrigin();
if (result.minimumTranslationVector == Vector2.zero)
return false;
if (result.minimumTranslationVector.x == 0 && result.minimumTranslationVector.y == 0)
return null;
result.normal = new Vector2(-result.minimumTranslationVector.x, -result.minimumTranslationVector.y);
result.normal.normalize();
return result;
@@ -4496,7 +4498,7 @@ var SpatialHash = (function () {
return cell;
};
SpatialHash.prototype.cellCoords = function (x, y) {
return new Point(Math.floor(x * this._inverseCellSize), Math.floor(y * this._inverseCellSize));
return new Vector2(Math.floor(x * this._inverseCellSize), Math.floor(y * this._inverseCellSize));
};
return SpatialHash;
}());

File diff suppressed because one or more lines are too long

View File

@@ -2,22 +2,22 @@
* 基本静态网格图与A*一起使用
* 将walls添加到walls HashSet并将加权节点添加到weightedNodes
*/
class AstarGridGraph implements IAstarGraph<Point> {
public dirs: Point[] = [
new Point(1, 0),
new Point(0, -1),
new Point(-1, 0),
new Point(0, 1)
class AstarGridGraph implements IAstarGraph<Vector2> {
public dirs: Vector2[] = [
new Vector2(1, 0),
new Vector2(0, -1),
new Vector2(-1, 0),
new Vector2(0, 1)
];
public walls: Point[] = [];
public weightedNodes: Point[] = [];
public walls: Vector2[] = [];
public weightedNodes: Vector2[] = [];
public defaultWeight: number = 1;
public weightedNodeWeight = 5;
private _width;
private _height;
private _neighbors: Point[] = new Array(4);
private _neighbors: Vector2[] = new Array(4);
constructor(width: number, height: number){
this._width = width;
@@ -28,7 +28,7 @@ class AstarGridGraph implements IAstarGraph<Point> {
* 确保节点在网格图的边界内
* @param node
*/
public isNodeInBounds(node: Point): boolean {
public isNodeInBounds(node: Vector2): boolean {
return 0 <= node.x && node.x < this._width && 0 <= node.y && node.y < this._height;
}
@@ -36,19 +36,19 @@ class AstarGridGraph implements IAstarGraph<Point> {
* 检查节点是否可以通过。墙壁是不可逾越的。
* @param node
*/
public isNodePassable(node: Point): boolean {
public isNodePassable(node: Vector2): boolean {
return !this.walls.firstOrDefault(wall => JSON.stringify(wall) == JSON.stringify(node));
}
public search(start: Point, goal: Point){
public search(start: Vector2, goal: Vector2){
return AStarPathfinder.search(this, start, goal);
}
public getNeighbors(node: Point): Point[] {
public getNeighbors(node: Vector2): Vector2[] {
this._neighbors.length = 0;
this.dirs.forEach(dir => {
let next = new Point(node.x + dir.x, node.y + dir.y);
let next = new Vector2(node.x + dir.x, node.y + dir.y);
if (this.isNodeInBounds(next) && this.isNodePassable(next))
this._neighbors.push(next);
});
@@ -56,11 +56,11 @@ class AstarGridGraph implements IAstarGraph<Point> {
return this._neighbors;
}
public cost(from: Point, to: Point): number {
public cost(from: Vector2, to: Vector2): number {
return this.weightedNodes.find((p)=> JSON.stringify(p) == JSON.stringify(to)) ? this.weightedNodeWeight : this.defaultWeight;
}
public heuristic(node: Point, goal: Point) {
public heuristic(node: Vector2, goal: Vector2) {
return Math.abs(node.x - goal.x) + Math.abs(node.y - goal.y);
}

View File

@@ -1,33 +1,33 @@
///<reference path="../../../Math/Point.ts" />
///<reference path="../../../Math/Vector2.ts" />
/**
* 基本的未加权网格图形用于BreadthFirstPathfinder
*/
class UnweightedGridGraph implements IUnweightedGraph<Point> {
private static readonly CARDINAL_DIRS: Point[] = [
new Point(1, 0),
new Point(0, -1),
new Point(-1, 0),
new Point(0, -1)
class UnweightedGridGraph implements IUnweightedGraph<Vector2> {
private static readonly CARDINAL_DIRS: Vector2[] = [
new Vector2(1, 0),
new Vector2(0, -1),
new Vector2(-1, 0),
new Vector2(0, -1)
];
private static readonly COMPASS_DIRS = [
new Point(1, 0),
new Point(1, -1),
new Point(0, -1),
new Point(-1, -1),
new Point(-1, 0),
new Point(-1, 1),
new Point(0, 1),
new Point(1, 1),
new Vector2(1, 0),
new Vector2(1, -1),
new Vector2(0, -1),
new Vector2(-1, -1),
new Vector2(-1, 0),
new Vector2(-1, 1),
new Vector2(0, 1),
new Vector2(1, 1),
];
public walls: Point[] = [];
public walls: Vector2[] = [];
private _width: number;
private _hegiht: number;
private _dirs: Point[];
private _neighbors: Point[] = new Array(4);
private _dirs: Vector2[];
private _neighbors: Vector2[] = new Array(4);
constructor(width: number, height: number, allowDiagonalSearch: boolean = false) {
this._width = width;
@@ -35,19 +35,19 @@ class UnweightedGridGraph implements IUnweightedGraph<Point> {
this._dirs = allowDiagonalSearch ? UnweightedGridGraph.COMPASS_DIRS : UnweightedGridGraph.CARDINAL_DIRS;
}
public isNodeInBounds(node: Point): boolean {
public isNodeInBounds(node: Vector2): boolean {
return 0 <= node.x && node.x < this._width && 0 <= node.y && node.y < this._hegiht;
}
public isNodePassable(node: Point): boolean {
public isNodePassable(node: Vector2): boolean {
return !this.walls.firstOrDefault(wall => JSON.stringify(wall) == JSON.stringify(node));
}
public getNeighbors(node: Point) {
public getNeighbors(node: Vector2) {
this._neighbors.length = 0;
this._dirs.forEach(dir => {
let next = new Point(node.x + dir.x, node.y + dir.y);
let next = new Vector2(node.x + dir.x, node.y + dir.y);
if (this.isNodeInBounds(next) && this.isNodePassable(next))
this._neighbors.push(next);
});
@@ -55,7 +55,7 @@ class UnweightedGridGraph implements IUnweightedGraph<Point> {
return this._neighbors;
}
public search(start: Point, goal: Point): Point[] {
public search(start: Vector2, goal: Vector2): Vector2[] {
return BreadthFirstPathfinder.search(this, start, goal);
}
}

View File

@@ -1,35 +1,35 @@
///<reference path="../../../Math/Point.ts" />
///<reference path="../../../Math/Vector2.ts" />
/**
* 支持一种加权节点的基本网格图
*/
class WeightedGridGraph implements IWeightedGraph<Point> {
class WeightedGridGraph implements IWeightedGraph<Vector2> {
public static readonly CARDINAL_DIRS = [
new Point(1, 0),
new Point(0, -1),
new Point(-1, 0),
new Point(0, 1)
new Vector2(1, 0),
new Vector2(0, -1),
new Vector2(-1, 0),
new Vector2(0, 1)
];
private static readonly COMPASS_DIRS = [
new Point(1, 0),
new Point(1, -1),
new Point(0, -1),
new Point(-1, -1),
new Point(-1, 0),
new Point(-1, 1),
new Point(0, 1),
new Point(1, 1),
new Vector2(1, 0),
new Vector2(1, -1),
new Vector2(0, -1),
new Vector2(-1, -1),
new Vector2(-1, 0),
new Vector2(-1, 1),
new Vector2(0, 1),
new Vector2(1, 1),
];
public walls: Point[] = [];
public weightedNodes: Point[] = [];
public walls: Vector2[] = [];
public weightedNodes: Vector2[] = [];
public defaultWeight = 1;
public weightedNodeWeight = 5;
private _width: number;
private _height: number;
private _dirs: Point[];
private _neighbors: Point[] = new Array(4);
private _dirs: Vector2[];
private _neighbors: Vector2[] = new Array(4);
constructor(width: number, height: number, allowDiagonalSearch: boolean = false){
this._width = width;
@@ -37,23 +37,23 @@ class WeightedGridGraph implements IWeightedGraph<Point> {
this._dirs = allowDiagonalSearch ? WeightedGridGraph.COMPASS_DIRS : WeightedGridGraph.CARDINAL_DIRS;
}
public isNodeInBounds(node: Point){
public isNodeInBounds(node: Vector2){
return 0 <= node.x && node.x < this._width && 0 <= node.y && node.y < this._height;
}
public isNodePassable(node: Point): boolean {
public isNodePassable(node: Vector2): boolean {
return !this.walls.firstOrDefault(wall => JSON.stringify(wall) == JSON.stringify(node));
}
public search(start: Point, goal: Point){
public search(start: Vector2, goal: Vector2){
return WeightedPathfinder.search(this, start, goal);
}
public getNeighbors(node: Point): Point[]{
public getNeighbors(node: Vector2): Vector2[]{
this._neighbors.length = 0;
this._dirs.forEach(dir => {
let next = new Point(node.x + dir.x, node.y + dir.y);
let next = new Vector2(node.x + dir.x, node.y + dir.y);
if (this.isNodeInBounds(next) && this.isNodePassable(next))
this._neighbors.push(next);
});
@@ -61,7 +61,7 @@ class WeightedGridGraph implements IWeightedGraph<Point> {
return this._neighbors;
}
public cost(from: Point, to: Point): number{
public cost(from: Vector2, to: Vector2): number{
return this.weightedNodes.find(t => JSON.stringify(t) == JSON.stringify(to)) ? this.weightedNodeWeight : this.defaultWeight;
}
}

View File

@@ -59,7 +59,13 @@ abstract class Collider extends Component{
return this.shape.overlaps(other.shape);
}
/**
* 检查这个与运动应用的碰撞器(移动向量)是否与碰撞器碰撞。如果是这样将返回true并且结果将填充碰撞数据。
* @param collider
* @param motion
*/
public collidesWith(collider: Collider, motion: Vector2){
// 改变形状的位置,使它在移动后的位置,这样我们可以检查重叠
let oldPosition = this.shape.position;
this.shape.position = Vector2.add(this.shape.position, motion);
@@ -67,6 +73,7 @@ abstract class Collider extends Component{
if (result)
result.collider = collider;
// 将图形位置返回到检查前的位置
this.shape.position = oldPosition;
return result;
@@ -82,6 +89,7 @@ abstract class Collider extends Component{
if (renderable){
let renderbaleBounds = renderable.bounds;
// 这里我们需要大小*反尺度,因为当我们自动调整碰撞器的大小时,它需要没有缩放的渲染
let width = renderbaleBounds.width / this.entity.scale.x;
let height = renderbaleBounds.height / this.entity.scale.y;
@@ -90,6 +98,7 @@ abstract class Collider extends Component{
boxCollider.width = width;
boxCollider.height = height;
// 获取渲染的中心将其转移到本地坐标并使用它作为碰撞器的localOffset
this.localOffset = Vector2.subtract(renderbaleBounds.center, this.entity.position);
}
}
@@ -112,4 +121,13 @@ abstract class Collider extends Component{
public onDisabled(){
this.unregisterColliderWithPhysicsSystem();
}
public update(){
let spriteRenderer = this.entity.getComponent(SpriteRenderer);
// 将显示目标设置为碰撞盒
if (spriteRenderer){
this.bounds.x = spriteRenderer.x;
this.bounds.y = spriteRenderer.y;
}
}
}

View File

@@ -144,6 +144,9 @@ class Scene extends egret.DisplayObjectContainer {
this.entityProcessors.end();
this.unload();
if (this.parent)
this.parent.removeChild(this);
}
protected async onStart() {

View File

@@ -1,9 +0,0 @@
class Point {
public x: number;
public y: number;
constructor(x?: number, y?: number){
this.x = x ? x : 0;
this.y = y ? y : this.x;
}
}

View File

@@ -86,7 +86,7 @@ class Rectangle {
return new Rectangle(minX, minY, maxX - minX, maxY - minY);
}
public getClosestPointOnRectangleBorderToPoint(point: Point): { res: Vector2, edgeNormal: Vector2 } {
public getClosestPointOnRectangleBorderToPoint(point: Vector2): { res: Vector2, edgeNormal: Vector2 } {
let edgeNormal = new Vector2(0, 0);
let res = new Vector2(0, 0);

View File

@@ -168,6 +168,7 @@ class Polygon extends Shape {
}
public recalculateBounds(collider: Collider) {
// 如果我们没有旋转或不关心TRS我们使用localOffset作为中心我们会从那开始
this.center = collider.localOffset;
if (collider.shouldColliderScaleAndRotationWithTransform){
@@ -180,6 +181,8 @@ class Polygon extends Shape {
combinedMatrix = Matrix2D.multiply(combinedMatrix, tempMat);
hasUnitScale = false;
// 缩放偏移量并将其设置为中心。如果我们有旋转,它会在下面重置
let scaledOffset = Vector2.multiply(collider.localOffset, collider.entity.scale);
this.center = scaledOffset;
}
@@ -188,6 +191,8 @@ class Polygon extends Shape {
tempMat = Matrix2D.createRotation(collider.entity.rotation);
combinedMatrix = Matrix2D.multiply(combinedMatrix, tempMat);
// 为了处理偏移原点的旋转我们只需要将圆心在(0,0)附近移动我们的偏移使角度为0
// 我们还需要处理这里的比例所以我们先对偏移进行缩放以得到合适的长度。
let offsetAngle = Math.atan2(collider.localOffset.y, collider.localOffset.x) * MathHelper.Rad2Deg;
let offsetLength = hasUnitScale ? collider._localOffsetLength : (Vector2.multiply(collider.localOffset, collider.entity.scale)).length();
this.center = MathHelper.pointOnCirlce(Vector2.zero, offsetLength, MathHelper.toDegrees(collider.entity.rotation) + offsetAngle);
@@ -196,9 +201,11 @@ class Polygon extends Shape {
tempMat = Matrix2D.createTranslation(this._polygonCenter.x, this._polygonCenter.y);
combinedMatrix = Matrix2D.multiply(combinedMatrix, tempMat);
// 最后变换原始点
Vector2Ext.transform(this._originalPoints, combinedMatrix, this.points);
this.isUnrotated = collider.entity.rotation == 0;
// 如果旋转的话,我们只需要重建边的法线
if (collider._isRotationDirty)
this._areEdgeNormalsDirty = true;
}

View File

@@ -15,13 +15,17 @@ class ShapeCollisions {
let polygonOffset = Vector2.subtract(first.position, second.position);
let axis: Vector2;
// 循环穿过两个多边形的所有边
for (let edgeIndex = 0; edgeIndex < firstEdges.length + secondEdges.length; edgeIndex++) {
// 1. 找出当前多边形是否相交
// 多边形的归一化轴垂直于缓存给我们的当前边
if (edgeIndex < firstEdges.length) {
axis = firstEdges[edgeIndex];
} else {
axis = secondEdges[edgeIndex - firstEdges.length];
}
// 求多边形在当前轴上的投影
let minA = 0;
let minB = 0;
let maxA = 0;
@@ -34,17 +38,24 @@ class ShapeCollisions {
minB = tb.min;
maxB = tb.max;
// 将区间设为第二个多边形的空间。由轴上投影的位置差偏移。
let relativeIntervalOffset = Vector2.dot(polygonOffset, axis);
minA += relativeIntervalOffset;
maxA += relativeIntervalOffset;
// 检查多边形投影是否正在相交
intervalDist = this.intervalDistance(minA, maxA, minB, maxB);
if (intervalDist > 0)
isIntersecting = false;
// 对于多对多数据类型转换添加一个Vector2?参数称为deltaMovement。为了提高速度我们这里不使用它
// TODO: 现在找出多边形是否会相交。只要检查速度就行了
// 如果多边形不相交,也不会相交,退出循环
if (!isIntersecting)
return null;
// 检查当前间隔距离是否为最小值。如果是,则存储间隔距离和当前距离。这将用于计算最小平移向量
intervalDist = Math.abs(intervalDist);
if (intervalDist < minIntervalDistance) {
minIntervalDistance = intervalDist;
@@ -55,8 +66,9 @@ class ShapeCollisions {
}
}
// 利用最小平移向量对多边形进行推入。
result.normal = translationAxis;
result.minimumTranslationVector = Vector2.multiply(new Vector2(-translationAxis), new Vector2(minIntervalDistance));
result.minimumTranslationVector = Vector2.multiply(new Vector2(-translationAxis.x, -translationAxis.y), new Vector2(minIntervalDistance));
return result;
}
@@ -263,10 +275,11 @@ class ShapeCollisions {
let minkowskiDiff = this.minkowskiDifference(first, second);
if (minkowskiDiff.contains(new Vector2(0, 0))){
// 计算MTV。如果它是零我们就可以称它为非碰撞
result.minimumTranslationVector = minkowskiDiff.getClosestPointOnBoundsToOrigin();
if (result.minimumTranslationVector == Vector2.zero)
return false;
if (result.minimumTranslationVector.x == 0 && result.minimumTranslationVector.y == 0)
return null;
result.normal = new Vector2(-result.minimumTranslationVector.x, -result.minimumTranslationVector.y);
result.normal.normalize();
@@ -278,6 +291,8 @@ class ShapeCollisions {
}
private static minkowskiDifference(first: Box, second: Box){
// 我们需要第一个框的左上角
// 碰撞器只会修改运动的位置所以我们需要用位置来计算出运动是什么。
let positionOffset = Vector2.subtract(first.position, Vector2.add(first.bounds.location, Vector2.divide(first.bounds.size, new Vector2(2))));
let topLeft = Vector2.subtract(Vector2.add(first.bounds.location, positionOffset), second.bounds.max);
let fullSize = Vector2.add(first.bounds.size, second.bounds.size);

View File

@@ -122,8 +122,8 @@ class SpatialHash {
return cell;
}
private cellCoords(x: number, y: number): Point {
return new Point(Math.floor(x * this._inverseCellSize), Math.floor(y * this._inverseCellSize));
private cellCoords(x: number, y: number): Vector2 {
return new Vector2(Math.floor(x * this._inverseCellSize), Math.floor(y * this._inverseCellSize));
}
}

View File

@@ -1,5 +1,5 @@
class RectangleExt {
public static union(first: Rectangle, point: Point){
public static union(first: Rectangle, point: Vector2){
let rect = new Rectangle(point.x, point.y, 0, 0);
return this.unionR(first, rect);
}