修复切换场景未移除问题

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
+53 -57
View File
@@ -32,22 +32,22 @@ declare class AStarNode<T> extends PriorityQueueNode {
data: T; data: T;
constructor(data: T); constructor(data: T);
} }
declare class AstarGridGraph implements IAstarGraph<Point> { declare class AstarGridGraph implements IAstarGraph<Vector2> {
dirs: Point[]; dirs: Vector2[];
walls: Point[]; walls: Vector2[];
weightedNodes: Point[]; weightedNodes: Vector2[];
defaultWeight: number; defaultWeight: number;
weightedNodeWeight: number; weightedNodeWeight: number;
private _width; private _width;
private _height; private _height;
private _neighbors; private _neighbors;
constructor(width: number, height: number); constructor(width: number, height: number);
isNodeInBounds(node: Point): boolean; isNodeInBounds(node: Vector2): boolean;
isNodePassable(node: Point): boolean; isNodePassable(node: Vector2): boolean;
search(start: Point, goal: Point): Point[]; search(start: Vector2, goal: Vector2): Vector2[];
getNeighbors(node: Point): Point[]; getNeighbors(node: Vector2): Vector2[];
cost(from: Point, to: Point): number; cost(from: Vector2, to: Vector2): number;
heuristic(node: Point, goal: Point): number; heuristic(node: Vector2, goal: Vector2): number;
} }
interface IAstarGraph<T> { interface IAstarGraph<T> {
getNeighbors(node: T): Array<T>; getNeighbors(node: T): Array<T>;
@@ -84,34 +84,57 @@ declare class UnweightedGraph<T> implements IUnweightedGraph<T> {
addEdgesForNode(node: T, edges: T[]): this; addEdgesForNode(node: T, edges: T[]): this;
getNeighbors(node: T): T[]; getNeighbors(node: T): T[];
} }
declare class Point { declare class Vector2 {
x: number; x: number;
y: 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); 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 CARDINAL_DIRS;
private static readonly COMPASS_DIRS; private static readonly COMPASS_DIRS;
walls: Point[]; walls: Vector2[];
private _width; private _width;
private _hegiht; private _hegiht;
private _dirs; private _dirs;
private _neighbors; private _neighbors;
constructor(width: number, height: number, allowDiagonalSearch?: boolean); constructor(width: number, height: number, allowDiagonalSearch?: boolean);
isNodeInBounds(node: Point): boolean; isNodeInBounds(node: Vector2): boolean;
isNodePassable(node: Point): boolean; isNodePassable(node: Vector2): boolean;
getNeighbors(node: Point): Point[]; getNeighbors(node: Vector2): Vector2[];
search(start: Point, goal: Point): Point[]; search(start: Vector2, goal: Vector2): Vector2[];
} }
interface IWeightedGraph<T> { interface IWeightedGraph<T> {
getNeighbors(node: T): T[]; getNeighbors(node: T): T[];
cost(from: T, to: T): number; cost(from: T, to: T): number;
} }
declare class WeightedGridGraph implements IWeightedGraph<Point> { declare class WeightedGridGraph implements IWeightedGraph<Vector2> {
static readonly CARDINAL_DIRS: Point[]; static readonly CARDINAL_DIRS: Vector2[];
private static readonly COMPASS_DIRS; private static readonly COMPASS_DIRS;
walls: Point[]; walls: Vector2[];
weightedNodes: Point[]; weightedNodes: Vector2[];
defaultWeight: number; defaultWeight: number;
weightedNodeWeight: number; weightedNodeWeight: number;
private _width; private _width;
@@ -119,11 +142,11 @@ declare class WeightedGridGraph implements IWeightedGraph<Point> {
private _dirs; private _dirs;
private _neighbors; private _neighbors;
constructor(width: number, height: number, allowDiagonalSearch?: boolean); constructor(width: number, height: number, allowDiagonalSearch?: boolean);
isNodeInBounds(node: Point): boolean; isNodeInBounds(node: Vector2): boolean;
isNodePassable(node: Point): boolean; isNodePassable(node: Vector2): boolean;
search(start: Point, goal: Point): Point[]; search(start: Vector2, goal: Vector2): Vector2[];
getNeighbors(node: Point): Point[]; getNeighbors(node: Vector2): Vector2[];
cost(from: Point, to: Point): number; cost(from: Vector2, to: Vector2): number;
} }
declare class WeightedNode<T> extends PriorityQueueNode { declare class WeightedNode<T> extends PriorityQueueNode {
data: T; data: T;
@@ -417,6 +440,7 @@ declare abstract class Collider extends Component {
onRemovedFromEntity(): void; onRemovedFromEntity(): void;
onEnabled(): void; onEnabled(): void;
onDisabled(): void; onDisabled(): void;
update(): void;
} }
declare class BoxCollider extends Collider { declare class BoxCollider extends Collider {
width: number; width: number;
@@ -760,7 +784,7 @@ declare class Rectangle {
containsRect(value: Rectangle): boolean; containsRect(value: Rectangle): boolean;
getHalfSize(): Vector2; getHalfSize(): Vector2;
static fromMinMax(minX: number, minY: number, maxX: number, maxY: number): Rectangle; static fromMinMax(minX: number, minY: number, maxX: number, maxY: number): Rectangle;
getClosestPointOnRectangleBorderToPoint(point: Point): { getClosestPointOnRectangleBorderToPoint(point: Vector2): {
res: Vector2; res: Vector2;
edgeNormal: 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; calculateBounds(parentPosition: Vector2, position: Vector2, origin: Vector2, scale: Vector2, rotation: number, width: number, height: number): void;
static rectEncompassingPoints(points: Vector2[]): Rectangle; 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 { declare class Vector3 {
x: number; x: number;
y: number; y: number;
@@ -928,7 +924,7 @@ declare class ShapeCollisions {
static closestPointOnLine(lineA: Vector2, lineB: Vector2, closestTo: Vector2): Vector2; static closestPointOnLine(lineA: Vector2, lineB: Vector2, closestTo: Vector2): Vector2;
static pointToPoly(point: Vector2, poly: Polygon): CollisionResult; static pointToPoly(point: Vector2, poly: Polygon): CollisionResult;
static circleToCircle(first: Circle, second: Circle): 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; private static minkowskiDifference;
} }
declare class SpatialHash { declare class SpatialHash {
@@ -1035,7 +1031,7 @@ declare class Pair<T> {
equals(other: Pair<T>): boolean; equals(other: Pair<T>): boolean;
} }
declare class RectangleExt { declare class RectangleExt {
static union(first: Rectangle, point: Point): Rectangle; static union(first: Rectangle, point: Vector2): Rectangle;
static unionR(value1: Rectangle, value2: Rectangle): Rectangle; static unionR(value1: Rectangle, value2: Rectangle): Rectangle;
} }
declare class Triangulator { declare class Triangulator {
+148 -146
View File
@@ -361,10 +361,10 @@ var AStarNode = (function (_super) {
var AstarGridGraph = (function () { var AstarGridGraph = (function () {
function AstarGridGraph(width, height) { function AstarGridGraph(width, height) {
this.dirs = [ this.dirs = [
new Point(1, 0), new Vector2(1, 0),
new Point(0, -1), new Vector2(0, -1),
new Point(-1, 0), new Vector2(-1, 0),
new Point(0, 1) new Vector2(0, 1)
]; ];
this.walls = []; this.walls = [];
this.weightedNodes = []; this.weightedNodes = [];
@@ -387,7 +387,7 @@ var AstarGridGraph = (function () {
var _this = this; var _this = this;
this._neighbors.length = 0; this._neighbors.length = 0;
this.dirs.forEach(function (dir) { 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)) if (_this.isNodeInBounds(next) && _this.isNodePassable(next))
_this._neighbors.push(next); _this._neighbors.push(next);
}); });
@@ -583,12 +583,113 @@ var UnweightedGraph = (function () {
}; };
return UnweightedGraph; return UnweightedGraph;
}()); }());
var Point = (function () { var Vector2 = (function () {
function Point(x, y) { function Vector2(x, y) {
this.x = 0;
this.y = 0;
this.x = x ? x : 0; this.x = x ? x : 0;
this.y = y ? y : this.x; 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 () { var UnweightedGridGraph = (function () {
function UnweightedGridGraph(width, height, allowDiagonalSearch) { function UnweightedGridGraph(width, height, allowDiagonalSearch) {
@@ -609,7 +710,7 @@ var UnweightedGridGraph = (function () {
var _this = this; var _this = this;
this._neighbors.length = 0; this._neighbors.length = 0;
this._dirs.forEach(function (dir) { 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)) if (_this.isNodeInBounds(next) && _this.isNodePassable(next))
_this._neighbors.push(next); _this._neighbors.push(next);
}); });
@@ -619,20 +720,20 @@ var UnweightedGridGraph = (function () {
return BreadthFirstPathfinder.search(this, start, goal); return BreadthFirstPathfinder.search(this, start, goal);
}; };
UnweightedGridGraph.CARDINAL_DIRS = [ UnweightedGridGraph.CARDINAL_DIRS = [
new Point(1, 0), new Vector2(1, 0),
new Point(0, -1), new Vector2(0, -1),
new Point(-1, 0), new Vector2(-1, 0),
new Point(0, -1) new Vector2(0, -1)
]; ];
UnweightedGridGraph.COMPASS_DIRS = [ UnweightedGridGraph.COMPASS_DIRS = [
new Point(1, 0), new Vector2(1, 0),
new Point(1, -1), new Vector2(1, -1),
new Point(0, -1), new Vector2(0, -1),
new Point(-1, -1), new Vector2(-1, -1),
new Point(-1, 0), new Vector2(-1, 0),
new Point(-1, 1), new Vector2(-1, 1),
new Point(0, 1), new Vector2(0, 1),
new Point(1, 1), new Vector2(1, 1),
]; ];
return UnweightedGridGraph; return UnweightedGridGraph;
}()); }());
@@ -661,7 +762,7 @@ var WeightedGridGraph = (function () {
var _this = this; var _this = this;
this._neighbors.length = 0; this._neighbors.length = 0;
this._dirs.forEach(function (dir) { 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)) if (_this.isNodeInBounds(next) && _this.isNodePassable(next))
_this._neighbors.push(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; return this.weightedNodes.find(function (t) { return JSON.stringify(t) == JSON.stringify(to); }) ? this.weightedNodeWeight : this.defaultWeight;
}; };
WeightedGridGraph.CARDINAL_DIRS = [ WeightedGridGraph.CARDINAL_DIRS = [
new Point(1, 0), new Vector2(1, 0),
new Point(0, -1), new Vector2(0, -1),
new Point(-1, 0), new Vector2(-1, 0),
new Point(0, 1) new Vector2(0, 1)
]; ];
WeightedGridGraph.COMPASS_DIRS = [ WeightedGridGraph.COMPASS_DIRS = [
new Point(1, 0), new Vector2(1, 0),
new Point(1, -1), new Vector2(1, -1),
new Point(0, -1), new Vector2(0, -1),
new Point(-1, -1), new Vector2(-1, -1),
new Point(-1, 0), new Vector2(-1, 0),
new Point(-1, 1), new Vector2(-1, 1),
new Point(0, 1), new Vector2(0, 1),
new Point(1, 1), new Vector2(1, 1),
]; ];
return WeightedGridGraph; return WeightedGridGraph;
}()); }());
@@ -1112,6 +1213,8 @@ var Scene = (function (_super) {
if (this.entityProcessors) if (this.entityProcessors)
this.entityProcessors.end(); this.entityProcessors.end();
this.unload(); this.unload();
if (this.parent)
this.parent.removeChild(this);
}; };
Scene.prototype.onStart = function () { Scene.prototype.onStart = function () {
return __awaiter(this, void 0, void 0, function () { return __awaiter(this, void 0, void 0, function () {
@@ -1930,6 +2033,13 @@ var Collider = (function (_super) {
Collider.prototype.onDisabled = function () { Collider.prototype.onDisabled = function () {
this.unregisterColliderWithPhysicsSystem(); 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; return Collider;
}(Component)); }(Component));
var BoxCollider = (function (_super) { var BoxCollider = (function (_super) {
@@ -3556,114 +3666,6 @@ var Rectangle = (function () {
}; };
return Rectangle; 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 () { var Vector3 = (function () {
function Vector3(x, y, z) { function Vector3(x, y, z) {
this.x = x; this.x = x;
@@ -4254,7 +4256,7 @@ var ShapeCollisions = (function () {
} }
} }
result.normal = translationAxis; 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; return result;
}; };
ShapeCollisions.intervalDistance = function (minA, maxA, minB, maxB) { ShapeCollisions.intervalDistance = function (minA, maxA, minB, maxB) {
@@ -4379,8 +4381,8 @@ var ShapeCollisions = (function () {
var minkowskiDiff = this.minkowskiDifference(first, second); var minkowskiDiff = this.minkowskiDifference(first, second);
if (minkowskiDiff.contains(new Vector2(0, 0))) { if (minkowskiDiff.contains(new Vector2(0, 0))) {
result.minimumTranslationVector = minkowskiDiff.getClosestPointOnBoundsToOrigin(); result.minimumTranslationVector = minkowskiDiff.getClosestPointOnBoundsToOrigin();
if (result.minimumTranslationVector == Vector2.zero) if (result.minimumTranslationVector.x == 0 && result.minimumTranslationVector.y == 0)
return false; return null;
result.normal = new Vector2(-result.minimumTranslationVector.x, -result.minimumTranslationVector.y); result.normal = new Vector2(-result.minimumTranslationVector.x, -result.minimumTranslationVector.y);
result.normal.normalize(); result.normal.normalize();
return result; return result;
@@ -4496,7 +4498,7 @@ var SpatialHash = (function () {
return cell; return cell;
}; };
SpatialHash.prototype.cellCoords = function (x, y) { 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; return SpatialHash;
}()); }());
File diff suppressed because one or more lines are too long
+13 -13
View File
@@ -15,13 +15,13 @@ class MainScene extends Scene {
bg.addComponent(new PlayerController()); bg.addComponent(new PlayerController());
bg.addComponent(new Mover()); bg.addComponent(new Mover());
bg.addComponent(new BoxCollider()); 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 sprite = new Sprite(RES.getRes("checkbox_select_disabled_png"));
let player2 = this.createEntity("player2"); let player2 = this.createEntity("player2");
player2.addComponent(new SpriteRenderer()).setSprite(sprite); 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()); player2.addComponent(new BoxCollider());
} }
@@ -63,24 +63,24 @@ class MainScene extends Scene {
public dijkstraTest() { public dijkstraTest() {
let graph = new WeightedGridGraph(20, 20); let graph = new WeightedGridGraph(20, 20);
graph.weightedNodes.push(new Point(3, 3)); graph.weightedNodes.push(new Vector2(3, 3));
graph.weightedNodes.push(new Point(3, 4)); graph.weightedNodes.push(new Vector2(3, 4));
graph.weightedNodes.push(new Point(4, 3)); graph.weightedNodes.push(new Vector2(4, 3));
graph.weightedNodes.push(new Point(4, 4)); 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); console.log(path);
} }
public astarTest() { public astarTest() {
let graph = new AstarGridGraph(20, 20); let graph = new AstarGridGraph(20, 20);
graph.weightedNodes.push(new Point(3, 3)); graph.weightedNodes.push(new Vector2(3, 3));
graph.weightedNodes.push(new Point(3, 4)); graph.weightedNodes.push(new Vector2(3, 4));
graph.weightedNodes.push(new Point(4, 3)); graph.weightedNodes.push(new Vector2(4, 3));
graph.weightedNodes.push(new Point(4, 4)); 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); console.log(path);
} }
} }
+16 -16
View File
@@ -34,23 +34,23 @@ class PlayerController extends Component {
return; return;
if (this.down){ if (this.down){
let camera = SceneManager.scene.camera; // let camera = SceneManager.scene.camera;
let moveLeft: number = 0; // let moveLeft: number = 0;
let moveRight: number = 0; // let moveRight: number = 0;
let speed = 200; // let speed = 100;
let worldPos = Input.touchPosition; // let worldPos = Input.touchPosition;
if (worldPos.x < this.spriteRenderer.x){ // if (worldPos.x < this.spriteRenderer.x){
moveLeft = -1; // moveLeft = -1;
} else if(worldPos.x > this.spriteRenderer.x){ // } else if(worldPos.x > this.spriteRenderer.x){
moveLeft = 1; // moveLeft = 1;
} // }
if (worldPos.y < this.spriteRenderer.y){ // if (worldPos.y < this.spriteRenderer.y){
moveRight = -1; // moveRight = -1;
} else if(worldPos.y > this.spriteRenderer.y){ // } else if(worldPos.y > this.spriteRenderer.y){
moveRight = 1; // moveRight = 1;
} // }
this.mover.move(new Vector2(moveLeft * speed * Time.deltaTime, moveRight * speed * Time.deltaTime)); this.mover.move(new Vector2(-1, -1));
} }
} }
} }
+53 -57
View File
@@ -32,22 +32,22 @@ declare class AStarNode<T> extends PriorityQueueNode {
data: T; data: T;
constructor(data: T); constructor(data: T);
} }
declare class AstarGridGraph implements IAstarGraph<Point> { declare class AstarGridGraph implements IAstarGraph<Vector2> {
dirs: Point[]; dirs: Vector2[];
walls: Point[]; walls: Vector2[];
weightedNodes: Point[]; weightedNodes: Vector2[];
defaultWeight: number; defaultWeight: number;
weightedNodeWeight: number; weightedNodeWeight: number;
private _width; private _width;
private _height; private _height;
private _neighbors; private _neighbors;
constructor(width: number, height: number); constructor(width: number, height: number);
isNodeInBounds(node: Point): boolean; isNodeInBounds(node: Vector2): boolean;
isNodePassable(node: Point): boolean; isNodePassable(node: Vector2): boolean;
search(start: Point, goal: Point): Point[]; search(start: Vector2, goal: Vector2): Vector2[];
getNeighbors(node: Point): Point[]; getNeighbors(node: Vector2): Vector2[];
cost(from: Point, to: Point): number; cost(from: Vector2, to: Vector2): number;
heuristic(node: Point, goal: Point): number; heuristic(node: Vector2, goal: Vector2): number;
} }
interface IAstarGraph<T> { interface IAstarGraph<T> {
getNeighbors(node: T): Array<T>; getNeighbors(node: T): Array<T>;
@@ -84,34 +84,57 @@ declare class UnweightedGraph<T> implements IUnweightedGraph<T> {
addEdgesForNode(node: T, edges: T[]): this; addEdgesForNode(node: T, edges: T[]): this;
getNeighbors(node: T): T[]; getNeighbors(node: T): T[];
} }
declare class Point { declare class Vector2 {
x: number; x: number;
y: 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); 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 CARDINAL_DIRS;
private static readonly COMPASS_DIRS; private static readonly COMPASS_DIRS;
walls: Point[]; walls: Vector2[];
private _width; private _width;
private _hegiht; private _hegiht;
private _dirs; private _dirs;
private _neighbors; private _neighbors;
constructor(width: number, height: number, allowDiagonalSearch?: boolean); constructor(width: number, height: number, allowDiagonalSearch?: boolean);
isNodeInBounds(node: Point): boolean; isNodeInBounds(node: Vector2): boolean;
isNodePassable(node: Point): boolean; isNodePassable(node: Vector2): boolean;
getNeighbors(node: Point): Point[]; getNeighbors(node: Vector2): Vector2[];
search(start: Point, goal: Point): Point[]; search(start: Vector2, goal: Vector2): Vector2[];
} }
interface IWeightedGraph<T> { interface IWeightedGraph<T> {
getNeighbors(node: T): T[]; getNeighbors(node: T): T[];
cost(from: T, to: T): number; cost(from: T, to: T): number;
} }
declare class WeightedGridGraph implements IWeightedGraph<Point> { declare class WeightedGridGraph implements IWeightedGraph<Vector2> {
static readonly CARDINAL_DIRS: Point[]; static readonly CARDINAL_DIRS: Vector2[];
private static readonly COMPASS_DIRS; private static readonly COMPASS_DIRS;
walls: Point[]; walls: Vector2[];
weightedNodes: Point[]; weightedNodes: Vector2[];
defaultWeight: number; defaultWeight: number;
weightedNodeWeight: number; weightedNodeWeight: number;
private _width; private _width;
@@ -119,11 +142,11 @@ declare class WeightedGridGraph implements IWeightedGraph<Point> {
private _dirs; private _dirs;
private _neighbors; private _neighbors;
constructor(width: number, height: number, allowDiagonalSearch?: boolean); constructor(width: number, height: number, allowDiagonalSearch?: boolean);
isNodeInBounds(node: Point): boolean; isNodeInBounds(node: Vector2): boolean;
isNodePassable(node: Point): boolean; isNodePassable(node: Vector2): boolean;
search(start: Point, goal: Point): Point[]; search(start: Vector2, goal: Vector2): Vector2[];
getNeighbors(node: Point): Point[]; getNeighbors(node: Vector2): Vector2[];
cost(from: Point, to: Point): number; cost(from: Vector2, to: Vector2): number;
} }
declare class WeightedNode<T> extends PriorityQueueNode { declare class WeightedNode<T> extends PriorityQueueNode {
data: T; data: T;
@@ -417,6 +440,7 @@ declare abstract class Collider extends Component {
onRemovedFromEntity(): void; onRemovedFromEntity(): void;
onEnabled(): void; onEnabled(): void;
onDisabled(): void; onDisabled(): void;
update(): void;
} }
declare class BoxCollider extends Collider { declare class BoxCollider extends Collider {
width: number; width: number;
@@ -760,7 +784,7 @@ declare class Rectangle {
containsRect(value: Rectangle): boolean; containsRect(value: Rectangle): boolean;
getHalfSize(): Vector2; getHalfSize(): Vector2;
static fromMinMax(minX: number, minY: number, maxX: number, maxY: number): Rectangle; static fromMinMax(minX: number, minY: number, maxX: number, maxY: number): Rectangle;
getClosestPointOnRectangleBorderToPoint(point: Point): { getClosestPointOnRectangleBorderToPoint(point: Vector2): {
res: Vector2; res: Vector2;
edgeNormal: 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; calculateBounds(parentPosition: Vector2, position: Vector2, origin: Vector2, scale: Vector2, rotation: number, width: number, height: number): void;
static rectEncompassingPoints(points: Vector2[]): Rectangle; 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 { declare class Vector3 {
x: number; x: number;
y: number; y: number;
@@ -928,7 +924,7 @@ declare class ShapeCollisions {
static closestPointOnLine(lineA: Vector2, lineB: Vector2, closestTo: Vector2): Vector2; static closestPointOnLine(lineA: Vector2, lineB: Vector2, closestTo: Vector2): Vector2;
static pointToPoly(point: Vector2, poly: Polygon): CollisionResult; static pointToPoly(point: Vector2, poly: Polygon): CollisionResult;
static circleToCircle(first: Circle, second: Circle): 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; private static minkowskiDifference;
} }
declare class SpatialHash { declare class SpatialHash {
@@ -1035,7 +1031,7 @@ declare class Pair<T> {
equals(other: Pair<T>): boolean; equals(other: Pair<T>): boolean;
} }
declare class RectangleExt { declare class RectangleExt {
static union(first: Rectangle, point: Point): Rectangle; static union(first: Rectangle, point: Vector2): Rectangle;
static unionR(value1: Rectangle, value2: Rectangle): Rectangle; static unionR(value1: Rectangle, value2: Rectangle): Rectangle;
} }
declare class Triangulator { declare class Triangulator {
+148 -146
View File
@@ -361,10 +361,10 @@ var AStarNode = (function (_super) {
var AstarGridGraph = (function () { var AstarGridGraph = (function () {
function AstarGridGraph(width, height) { function AstarGridGraph(width, height) {
this.dirs = [ this.dirs = [
new Point(1, 0), new Vector2(1, 0),
new Point(0, -1), new Vector2(0, -1),
new Point(-1, 0), new Vector2(-1, 0),
new Point(0, 1) new Vector2(0, 1)
]; ];
this.walls = []; this.walls = [];
this.weightedNodes = []; this.weightedNodes = [];
@@ -387,7 +387,7 @@ var AstarGridGraph = (function () {
var _this = this; var _this = this;
this._neighbors.length = 0; this._neighbors.length = 0;
this.dirs.forEach(function (dir) { 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)) if (_this.isNodeInBounds(next) && _this.isNodePassable(next))
_this._neighbors.push(next); _this._neighbors.push(next);
}); });
@@ -583,12 +583,113 @@ var UnweightedGraph = (function () {
}; };
return UnweightedGraph; return UnweightedGraph;
}()); }());
var Point = (function () { var Vector2 = (function () {
function Point(x, y) { function Vector2(x, y) {
this.x = 0;
this.y = 0;
this.x = x ? x : 0; this.x = x ? x : 0;
this.y = y ? y : this.x; 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 () { var UnweightedGridGraph = (function () {
function UnweightedGridGraph(width, height, allowDiagonalSearch) { function UnweightedGridGraph(width, height, allowDiagonalSearch) {
@@ -609,7 +710,7 @@ var UnweightedGridGraph = (function () {
var _this = this; var _this = this;
this._neighbors.length = 0; this._neighbors.length = 0;
this._dirs.forEach(function (dir) { 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)) if (_this.isNodeInBounds(next) && _this.isNodePassable(next))
_this._neighbors.push(next); _this._neighbors.push(next);
}); });
@@ -619,20 +720,20 @@ var UnweightedGridGraph = (function () {
return BreadthFirstPathfinder.search(this, start, goal); return BreadthFirstPathfinder.search(this, start, goal);
}; };
UnweightedGridGraph.CARDINAL_DIRS = [ UnweightedGridGraph.CARDINAL_DIRS = [
new Point(1, 0), new Vector2(1, 0),
new Point(0, -1), new Vector2(0, -1),
new Point(-1, 0), new Vector2(-1, 0),
new Point(0, -1) new Vector2(0, -1)
]; ];
UnweightedGridGraph.COMPASS_DIRS = [ UnweightedGridGraph.COMPASS_DIRS = [
new Point(1, 0), new Vector2(1, 0),
new Point(1, -1), new Vector2(1, -1),
new Point(0, -1), new Vector2(0, -1),
new Point(-1, -1), new Vector2(-1, -1),
new Point(-1, 0), new Vector2(-1, 0),
new Point(-1, 1), new Vector2(-1, 1),
new Point(0, 1), new Vector2(0, 1),
new Point(1, 1), new Vector2(1, 1),
]; ];
return UnweightedGridGraph; return UnweightedGridGraph;
}()); }());
@@ -661,7 +762,7 @@ var WeightedGridGraph = (function () {
var _this = this; var _this = this;
this._neighbors.length = 0; this._neighbors.length = 0;
this._dirs.forEach(function (dir) { 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)) if (_this.isNodeInBounds(next) && _this.isNodePassable(next))
_this._neighbors.push(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; return this.weightedNodes.find(function (t) { return JSON.stringify(t) == JSON.stringify(to); }) ? this.weightedNodeWeight : this.defaultWeight;
}; };
WeightedGridGraph.CARDINAL_DIRS = [ WeightedGridGraph.CARDINAL_DIRS = [
new Point(1, 0), new Vector2(1, 0),
new Point(0, -1), new Vector2(0, -1),
new Point(-1, 0), new Vector2(-1, 0),
new Point(0, 1) new Vector2(0, 1)
]; ];
WeightedGridGraph.COMPASS_DIRS = [ WeightedGridGraph.COMPASS_DIRS = [
new Point(1, 0), new Vector2(1, 0),
new Point(1, -1), new Vector2(1, -1),
new Point(0, -1), new Vector2(0, -1),
new Point(-1, -1), new Vector2(-1, -1),
new Point(-1, 0), new Vector2(-1, 0),
new Point(-1, 1), new Vector2(-1, 1),
new Point(0, 1), new Vector2(0, 1),
new Point(1, 1), new Vector2(1, 1),
]; ];
return WeightedGridGraph; return WeightedGridGraph;
}()); }());
@@ -1112,6 +1213,8 @@ var Scene = (function (_super) {
if (this.entityProcessors) if (this.entityProcessors)
this.entityProcessors.end(); this.entityProcessors.end();
this.unload(); this.unload();
if (this.parent)
this.parent.removeChild(this);
}; };
Scene.prototype.onStart = function () { Scene.prototype.onStart = function () {
return __awaiter(this, void 0, void 0, function () { return __awaiter(this, void 0, void 0, function () {
@@ -1930,6 +2033,13 @@ var Collider = (function (_super) {
Collider.prototype.onDisabled = function () { Collider.prototype.onDisabled = function () {
this.unregisterColliderWithPhysicsSystem(); 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; return Collider;
}(Component)); }(Component));
var BoxCollider = (function (_super) { var BoxCollider = (function (_super) {
@@ -3556,114 +3666,6 @@ var Rectangle = (function () {
}; };
return Rectangle; 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 () { var Vector3 = (function () {
function Vector3(x, y, z) { function Vector3(x, y, z) {
this.x = x; this.x = x;
@@ -4254,7 +4256,7 @@ var ShapeCollisions = (function () {
} }
} }
result.normal = translationAxis; 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; return result;
}; };
ShapeCollisions.intervalDistance = function (minA, maxA, minB, maxB) { ShapeCollisions.intervalDistance = function (minA, maxA, minB, maxB) {
@@ -4379,8 +4381,8 @@ var ShapeCollisions = (function () {
var minkowskiDiff = this.minkowskiDifference(first, second); var minkowskiDiff = this.minkowskiDifference(first, second);
if (minkowskiDiff.contains(new Vector2(0, 0))) { if (minkowskiDiff.contains(new Vector2(0, 0))) {
result.minimumTranslationVector = minkowskiDiff.getClosestPointOnBoundsToOrigin(); result.minimumTranslationVector = minkowskiDiff.getClosestPointOnBoundsToOrigin();
if (result.minimumTranslationVector == Vector2.zero) if (result.minimumTranslationVector.x == 0 && result.minimumTranslationVector.y == 0)
return false; return null;
result.normal = new Vector2(-result.minimumTranslationVector.x, -result.minimumTranslationVector.y); result.normal = new Vector2(-result.minimumTranslationVector.x, -result.minimumTranslationVector.y);
result.normal.normalize(); result.normal.normalize();
return result; return result;
@@ -4496,7 +4498,7 @@ var SpatialHash = (function () {
return cell; return cell;
}; };
SpatialHash.prototype.cellCoords = function (x, y) { 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; return SpatialHash;
}()); }());
+1 -1
View File
File diff suppressed because one or more lines are too long
@@ -2,22 +2,22 @@
* A*使 * A*使
* walls添加到walls HashSetweightedNodes * walls添加到walls HashSetweightedNodes
*/ */
class AstarGridGraph implements IAstarGraph<Point> { class AstarGridGraph implements IAstarGraph<Vector2> {
public dirs: Point[] = [ public dirs: Vector2[] = [
new Point(1, 0), new Vector2(1, 0),
new Point(0, -1), new Vector2(0, -1),
new Point(-1, 0), new Vector2(-1, 0),
new Point(0, 1) new Vector2(0, 1)
]; ];
public walls: Point[] = []; public walls: Vector2[] = [];
public weightedNodes: Point[] = []; public weightedNodes: Vector2[] = [];
public defaultWeight: number = 1; public defaultWeight: number = 1;
public weightedNodeWeight = 5; public weightedNodeWeight = 5;
private _width; private _width;
private _height; private _height;
private _neighbors: Point[] = new Array(4); private _neighbors: Vector2[] = new Array(4);
constructor(width: number, height: number){ constructor(width: number, height: number){
this._width = width; this._width = width;
@@ -28,7 +28,7 @@ class AstarGridGraph implements IAstarGraph<Point> {
* *
* @param node * @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; 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 * @param node
*/ */
public isNodePassable(node: Point): boolean { public isNodePassable(node: Vector2): boolean {
return !this.walls.firstOrDefault(wall => JSON.stringify(wall) == JSON.stringify(node)); 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); return AStarPathfinder.search(this, start, goal);
} }
public getNeighbors(node: Point): Point[] { public getNeighbors(node: Vector2): Vector2[] {
this._neighbors.length = 0; this._neighbors.length = 0;
this.dirs.forEach(dir => { 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)) if (this.isNodeInBounds(next) && this.isNodePassable(next))
this._neighbors.push(next); this._neighbors.push(next);
}); });
@@ -56,11 +56,11 @@ class AstarGridGraph implements IAstarGraph<Point> {
return this._neighbors; 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; 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); return Math.abs(node.x - goal.x) + Math.abs(node.y - goal.y);
} }
@@ -1,33 +1,33 @@
///<reference path="../../../Math/Point.ts" /> ///<reference path="../../../Math/Vector2.ts" />
/** /**
* BreadthFirstPathfinder * BreadthFirstPathfinder
*/ */
class UnweightedGridGraph implements IUnweightedGraph<Point> { class UnweightedGridGraph implements IUnweightedGraph<Vector2> {
private static readonly CARDINAL_DIRS: Point[] = [ private static readonly CARDINAL_DIRS: Vector2[] = [
new Point(1, 0), new Vector2(1, 0),
new Point(0, -1), new Vector2(0, -1),
new Point(-1, 0), new Vector2(-1, 0),
new Point(0, -1) new Vector2(0, -1)
]; ];
private static readonly COMPASS_DIRS = [ private static readonly COMPASS_DIRS = [
new Point(1, 0), new Vector2(1, 0),
new Point(1, -1), new Vector2(1, -1),
new Point(0, -1), new Vector2(0, -1),
new Point(-1, -1), new Vector2(-1, -1),
new Point(-1, 0), new Vector2(-1, 0),
new Point(-1, 1), new Vector2(-1, 1),
new Point(0, 1), new Vector2(0, 1),
new Point(1, 1), new Vector2(1, 1),
]; ];
public walls: Point[] = []; public walls: Vector2[] = [];
private _width: number; private _width: number;
private _hegiht: number; private _hegiht: number;
private _dirs: Point[]; private _dirs: Vector2[];
private _neighbors: Point[] = new Array(4); private _neighbors: Vector2[] = new Array(4);
constructor(width: number, height: number, allowDiagonalSearch: boolean = false) { constructor(width: number, height: number, allowDiagonalSearch: boolean = false) {
this._width = width; this._width = width;
@@ -35,19 +35,19 @@ class UnweightedGridGraph implements IUnweightedGraph<Point> {
this._dirs = allowDiagonalSearch ? UnweightedGridGraph.COMPASS_DIRS : UnweightedGridGraph.CARDINAL_DIRS; 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; 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)); return !this.walls.firstOrDefault(wall => JSON.stringify(wall) == JSON.stringify(node));
} }
public getNeighbors(node: Point) { public getNeighbors(node: Vector2) {
this._neighbors.length = 0; this._neighbors.length = 0;
this._dirs.forEach(dir => { 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)) if (this.isNodeInBounds(next) && this.isNodePassable(next))
this._neighbors.push(next); this._neighbors.push(next);
}); });
@@ -55,7 +55,7 @@ class UnweightedGridGraph implements IUnweightedGraph<Point> {
return this._neighbors; return this._neighbors;
} }
public search(start: Point, goal: Point): Point[] { public search(start: Vector2, goal: Vector2): Vector2[] {
return BreadthFirstPathfinder.search(this, start, goal); return BreadthFirstPathfinder.search(this, start, goal);
} }
} }
@@ -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 = [ public static readonly CARDINAL_DIRS = [
new Point(1, 0), new Vector2(1, 0),
new Point(0, -1), new Vector2(0, -1),
new Point(-1, 0), new Vector2(-1, 0),
new Point(0, 1) new Vector2(0, 1)
]; ];
private static readonly COMPASS_DIRS = [ private static readonly COMPASS_DIRS = [
new Point(1, 0), new Vector2(1, 0),
new Point(1, -1), new Vector2(1, -1),
new Point(0, -1), new Vector2(0, -1),
new Point(-1, -1), new Vector2(-1, -1),
new Point(-1, 0), new Vector2(-1, 0),
new Point(-1, 1), new Vector2(-1, 1),
new Point(0, 1), new Vector2(0, 1),
new Point(1, 1), new Vector2(1, 1),
]; ];
public walls: Point[] = []; public walls: Vector2[] = [];
public weightedNodes: Point[] = []; public weightedNodes: Vector2[] = [];
public defaultWeight = 1; public defaultWeight = 1;
public weightedNodeWeight = 5; public weightedNodeWeight = 5;
private _width: number; private _width: number;
private _height: number; private _height: number;
private _dirs: Point[]; private _dirs: Vector2[];
private _neighbors: Point[] = new Array(4); private _neighbors: Vector2[] = new Array(4);
constructor(width: number, height: number, allowDiagonalSearch: boolean = false){ constructor(width: number, height: number, allowDiagonalSearch: boolean = false){
this._width = width; this._width = width;
@@ -37,23 +37,23 @@ class WeightedGridGraph implements IWeightedGraph<Point> {
this._dirs = allowDiagonalSearch ? WeightedGridGraph.COMPASS_DIRS : WeightedGridGraph.CARDINAL_DIRS; 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; 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)); 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); return WeightedPathfinder.search(this, start, goal);
} }
public getNeighbors(node: Point): Point[]{ public getNeighbors(node: Vector2): Vector2[]{
this._neighbors.length = 0; this._neighbors.length = 0;
this._dirs.forEach(dir => { 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)) if (this.isNodeInBounds(next) && this.isNodePassable(next))
this._neighbors.push(next); this._neighbors.push(next);
}); });
@@ -61,7 +61,7 @@ class WeightedGridGraph implements IWeightedGraph<Point> {
return this._neighbors; 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; return this.weightedNodes.find(t => JSON.stringify(t) == JSON.stringify(to)) ? this.weightedNodeWeight : this.defaultWeight;
} }
} }
@@ -59,7 +59,13 @@ abstract class Collider extends Component{
return this.shape.overlaps(other.shape); return this.shape.overlaps(other.shape);
} }
/**
* ()true
* @param collider
* @param motion
*/
public collidesWith(collider: Collider, motion: Vector2){ public collidesWith(collider: Collider, motion: Vector2){
// 改变形状的位置,使它在移动后的位置,这样我们可以检查重叠
let oldPosition = this.shape.position; let oldPosition = this.shape.position;
this.shape.position = Vector2.add(this.shape.position, motion); this.shape.position = Vector2.add(this.shape.position, motion);
@@ -67,6 +73,7 @@ abstract class Collider extends Component{
if (result) if (result)
result.collider = collider; result.collider = collider;
// 将图形位置返回到检查前的位置
this.shape.position = oldPosition; this.shape.position = oldPosition;
return result; return result;
@@ -82,6 +89,7 @@ abstract class Collider extends Component{
if (renderable){ if (renderable){
let renderbaleBounds = renderable.bounds; let renderbaleBounds = renderable.bounds;
// 这里我们需要大小*反尺度,因为当我们自动调整碰撞器的大小时,它需要没有缩放的渲染
let width = renderbaleBounds.width / this.entity.scale.x; let width = renderbaleBounds.width / this.entity.scale.x;
let height = renderbaleBounds.height / this.entity.scale.y; let height = renderbaleBounds.height / this.entity.scale.y;
@@ -90,6 +98,7 @@ abstract class Collider extends Component{
boxCollider.width = width; boxCollider.width = width;
boxCollider.height = height; boxCollider.height = height;
// 获取渲染的中心,将其转移到本地坐标,并使用它作为碰撞器的localOffset
this.localOffset = Vector2.subtract(renderbaleBounds.center, this.entity.position); this.localOffset = Vector2.subtract(renderbaleBounds.center, this.entity.position);
} }
} }
@@ -112,4 +121,13 @@ abstract class Collider extends Component{
public onDisabled(){ public onDisabled(){
this.unregisterColliderWithPhysicsSystem(); this.unregisterColliderWithPhysicsSystem();
} }
public update(){
let spriteRenderer = this.entity.getComponent(SpriteRenderer);
// 将显示目标设置为碰撞盒
if (spriteRenderer){
this.bounds.x = spriteRenderer.x;
this.bounds.y = spriteRenderer.y;
}
}
} }
+3
View File
@@ -144,6 +144,9 @@ class Scene extends egret.DisplayObjectContainer {
this.entityProcessors.end(); this.entityProcessors.end();
this.unload(); this.unload();
if (this.parent)
this.parent.removeChild(this);
} }
protected async onStart() { protected async onStart() {
-9
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;
}
}
+1 -1
View File
@@ -86,7 +86,7 @@ class Rectangle {
return new Rectangle(minX, minY, maxX - minX, maxY - minY); 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 edgeNormal = new Vector2(0, 0);
let res = new Vector2(0, 0); let res = new Vector2(0, 0);
+7
View File
@@ -168,6 +168,7 @@ class Polygon extends Shape {
} }
public recalculateBounds(collider: Collider) { public recalculateBounds(collider: Collider) {
// 如果我们没有旋转或不关心TRS我们使用localOffset作为中心,我们会从那开始
this.center = collider.localOffset; this.center = collider.localOffset;
if (collider.shouldColliderScaleAndRotationWithTransform){ if (collider.shouldColliderScaleAndRotationWithTransform){
@@ -180,6 +181,8 @@ class Polygon extends Shape {
combinedMatrix = Matrix2D.multiply(combinedMatrix, tempMat); combinedMatrix = Matrix2D.multiply(combinedMatrix, tempMat);
hasUnitScale = false; hasUnitScale = false;
// 缩放偏移量并将其设置为中心。如果我们有旋转,它会在下面重置
let scaledOffset = Vector2.multiply(collider.localOffset, collider.entity.scale); let scaledOffset = Vector2.multiply(collider.localOffset, collider.entity.scale);
this.center = scaledOffset; this.center = scaledOffset;
} }
@@ -188,6 +191,8 @@ class Polygon extends Shape {
tempMat = Matrix2D.createRotation(collider.entity.rotation); tempMat = Matrix2D.createRotation(collider.entity.rotation);
combinedMatrix = Matrix2D.multiply(combinedMatrix, tempMat); combinedMatrix = Matrix2D.multiply(combinedMatrix, tempMat);
// 为了处理偏移原点的旋转我们只需要将圆心在(0,0)附近移动我们的偏移使角度为0
// 我们还需要处理这里的比例所以我们先对偏移进行缩放以得到合适的长度。
let offsetAngle = Math.atan2(collider.localOffset.y, collider.localOffset.x) * MathHelper.Rad2Deg; 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(); 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); 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); tempMat = Matrix2D.createTranslation(this._polygonCenter.x, this._polygonCenter.y);
combinedMatrix = Matrix2D.multiply(combinedMatrix, tempMat); combinedMatrix = Matrix2D.multiply(combinedMatrix, tempMat);
// 最后变换原始点
Vector2Ext.transform(this._originalPoints, combinedMatrix, this.points); Vector2Ext.transform(this._originalPoints, combinedMatrix, this.points);
this.isUnrotated = collider.entity.rotation == 0; this.isUnrotated = collider.entity.rotation == 0;
// 如果旋转的话,我们只需要重建边的法线
if (collider._isRotationDirty) if (collider._isRotationDirty)
this._areEdgeNormalsDirty = true; this._areEdgeNormalsDirty = true;
} }
@@ -15,13 +15,17 @@ class ShapeCollisions {
let polygonOffset = Vector2.subtract(first.position, second.position); let polygonOffset = Vector2.subtract(first.position, second.position);
let axis: Vector2; let axis: Vector2;
// 循环穿过两个多边形的所有边
for (let edgeIndex = 0; edgeIndex < firstEdges.length + secondEdges.length; edgeIndex++) { for (let edgeIndex = 0; edgeIndex < firstEdges.length + secondEdges.length; edgeIndex++) {
// 1. 找出当前多边形是否相交
// 多边形的归一化轴垂直于缓存给我们的当前边
if (edgeIndex < firstEdges.length) { if (edgeIndex < firstEdges.length) {
axis = firstEdges[edgeIndex]; axis = firstEdges[edgeIndex];
} else { } else {
axis = secondEdges[edgeIndex - firstEdges.length]; axis = secondEdges[edgeIndex - firstEdges.length];
} }
// 求多边形在当前轴上的投影
let minA = 0; let minA = 0;
let minB = 0; let minB = 0;
let maxA = 0; let maxA = 0;
@@ -34,17 +38,24 @@ class ShapeCollisions {
minB = tb.min; minB = tb.min;
maxB = tb.max; maxB = tb.max;
// 将区间设为第二个多边形的空间。由轴上投影的位置差偏移。
let relativeIntervalOffset = Vector2.dot(polygonOffset, axis); let relativeIntervalOffset = Vector2.dot(polygonOffset, axis);
minA += relativeIntervalOffset; minA += relativeIntervalOffset;
maxA += relativeIntervalOffset; maxA += relativeIntervalOffset;
// 检查多边形投影是否正在相交
intervalDist = this.intervalDistance(minA, maxA, minB, maxB); intervalDist = this.intervalDistance(minA, maxA, minB, maxB);
if (intervalDist > 0) if (intervalDist > 0)
isIntersecting = false; isIntersecting = false;
// 对于多对多数据类型转换,添加一个Vector2?参数称为deltaMovement。为了提高速度,我们这里不使用它
// TODO: 现在找出多边形是否会相交。只要检查速度就行了
// 如果多边形不相交,也不会相交,退出循环
if (!isIntersecting) if (!isIntersecting)
return null; return null;
// 检查当前间隔距离是否为最小值。如果是,则存储间隔距离和当前距离。这将用于计算最小平移向量
intervalDist = Math.abs(intervalDist); intervalDist = Math.abs(intervalDist);
if (intervalDist < minIntervalDistance) { if (intervalDist < minIntervalDistance) {
minIntervalDistance = intervalDist; minIntervalDistance = intervalDist;
@@ -55,8 +66,9 @@ class ShapeCollisions {
} }
} }
// 利用最小平移向量对多边形进行推入。
result.normal = translationAxis; 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; return result;
} }
@@ -263,10 +275,11 @@ class ShapeCollisions {
let minkowskiDiff = this.minkowskiDifference(first, second); let minkowskiDiff = this.minkowskiDifference(first, second);
if (minkowskiDiff.contains(new Vector2(0, 0))){ if (minkowskiDiff.contains(new Vector2(0, 0))){
// 计算MTV。如果它是零,我们就可以称它为非碰撞
result.minimumTranslationVector = minkowskiDiff.getClosestPointOnBoundsToOrigin(); result.minimumTranslationVector = minkowskiDiff.getClosestPointOnBoundsToOrigin();
if (result.minimumTranslationVector == Vector2.zero) if (result.minimumTranslationVector.x == 0 && result.minimumTranslationVector.y == 0)
return false; return null;
result.normal = new Vector2(-result.minimumTranslationVector.x, -result.minimumTranslationVector.y); result.normal = new Vector2(-result.minimumTranslationVector.x, -result.minimumTranslationVector.y);
result.normal.normalize(); result.normal.normalize();
@@ -278,6 +291,8 @@ class ShapeCollisions {
} }
private static minkowskiDifference(first: Box, second: Box){ 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 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 topLeft = Vector2.subtract(Vector2.add(first.bounds.location, positionOffset), second.bounds.max);
let fullSize = Vector2.add(first.bounds.size, second.bounds.size); let fullSize = Vector2.add(first.bounds.size, second.bounds.size);
+2 -2
View File
@@ -122,8 +122,8 @@ class SpatialHash {
return cell; return cell;
} }
private cellCoords(x: number, y: number): Point { private cellCoords(x: number, y: number): Vector2 {
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));
} }
} }
+1 -1
View File
@@ -1,5 +1,5 @@
class RectangleExt { 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); let rect = new Rectangle(point.x, point.y, 0, 0);
return this.unionR(first, rect); return this.unionR(first, rect);
} }