新增事件发送接收器

This commit is contained in:
yhh
2020-06-15 12:16:23 +08:00
parent 16892eb7af
commit c3120d791f
15 changed files with 141 additions and 123 deletions

View File

@@ -576,7 +576,7 @@ declare class Rectangle {
declare class Vector2 { declare class Vector2 {
x: number; x: number;
y: number; y: number;
constructor(x: number, y?: number); constructor(x?: number, y?: number);
static add(value1: Vector2, value2: Vector2): Vector2; static add(value1: Vector2, value2: Vector2): Vector2;
static divide(value1: Vector2, value2: Vector2): Vector2; static divide(value1: Vector2, value2: Vector2): Vector2;
static multiply(value1: Vector2, value2: Vector2): Vector2; static multiply(value1: Vector2, value2: Vector2): Vector2;
@@ -675,18 +675,6 @@ 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;
} }
declare class Particle {
position: Vector2;
lastPosition: Vector2;
isPinned: boolean;
pinnedPosition: any;
acceleration: Vector2;
mass: number;
radius: number;
collidesWithColliders: boolean;
constructor(position: Vector2);
applyForce(force: Vector2): void;
}
declare class SpatialHash { declare class SpatialHash {
gridBounds: Rectangle; gridBounds: Rectangle;
private _raycastParser; private _raycastParser;
@@ -714,6 +702,13 @@ declare class NumberDictionary {
getAllObjects(): Collider[]; getAllObjects(): Collider[];
clear(): void; clear(): void;
} }
declare class Emitter<T> {
private _messageTable;
constructor();
addObserver(eventType: T, handler: Function): void;
removeObserver(eventType: T, handler: Function): void;
emit(eventType: T, data: any): void;
}
declare class Triangulator { declare class Triangulator {
triangleIndices: number[]; triangleIndices: number[];
private _triPrev; private _triPrev;

View File

@@ -2719,8 +2719,8 @@ var Vector2 = (function () {
function Vector2(x, y) { function Vector2(x, y) {
this.x = 0; this.x = 0;
this.y = 0; this.y = 0;
this.x = x; this.x = x ? x : 0;
this.y = y ? y : x; this.y = y ? y : this.x;
} }
Vector2.add = function (value1, value2) { Vector2.add = function (value1, value2) {
var result = new Vector2(0, 0); var result = new Vector2(0, 0);
@@ -3026,7 +3026,7 @@ var Polygon = (function (_super) {
var verts = new Vector2[vertCount]; var verts = new Vector2[vertCount];
for (var i = 0; i < vertCount; i++) { for (var i = 0; i < vertCount; i++) {
var a = 2 * Math.PI * (i / vertCount); var a = 2 * Math.PI * (i / vertCount);
verts[i] = new Vector2(Math.cos(a), Math.sign(a) * radius); verts[i] = new Vector2(Math.cos(a), Math.sin(a) * radius);
} }
return verts; return verts;
}; };
@@ -3086,6 +3086,12 @@ var ShapeCollisions = (function () {
ShapeCollisions.polygonToPolygon = function (first, second) { ShapeCollisions.polygonToPolygon = function (first, second) {
var result = new CollisionResult(); var result = new CollisionResult();
var isIntersecting = true; var isIntersecting = true;
var firstEdges = first.edgeNormals;
var secondEdges = second.edgeNormals;
var minIntervalDistance = Number.POSITIVE_INFINITY;
var translationAxis = new Vector2();
var polygonOffset = Vector2.subtract(first.position, second.position);
var axis;
}; };
ShapeCollisions.circleToPolygon = function (circle, polygon) { ShapeCollisions.circleToPolygon = function (circle, polygon) {
var result = new CollisionResult(); var result = new CollisionResult();
@@ -3156,22 +3162,6 @@ var ShapeCollisions = (function () {
}; };
return ShapeCollisions; return ShapeCollisions;
}()); }());
var Particle = (function () {
function Particle(position) {
this.position = new Vector2(0, 0);
this.lastPosition = new Vector2(0, 0);
this.acceleration = new Vector2(0, 0);
this.mass = 1;
this.radius = 0;
this.collidesWithColliders = true;
this.position = position;
this.lastPosition = position;
}
Particle.prototype.applyForce = function (force) {
this.acceleration = Vector2.add(this.acceleration, new Vector2(force.x / this.mass, force.y / this.mass));
};
return Particle;
}());
var SpatialHash = (function () { var SpatialHash = (function () {
function SpatialHash(cellSize) { function SpatialHash(cellSize) {
if (cellSize === void 0) { cellSize = 100; } if (cellSize === void 0) { cellSize = 100; }
@@ -3284,6 +3274,30 @@ var NumberDictionary = (function () {
}; };
return NumberDictionary; return NumberDictionary;
}()); }());
var Emitter = (function () {
function Emitter() {
this._messageTable = new Map();
}
Emitter.prototype.addObserver = function (eventType, handler) {
var list = this._messageTable.get(eventType);
if (!list) {
list = [];
this._messageTable.set(eventType, list);
}
if (list.contains(handler))
console.warn("您试图添加相同的观察者两次");
list.push(handler);
};
Emitter.prototype.removeObserver = function (eventType, handler) {
this._messageTable.get(eventType).remove(handler);
};
Emitter.prototype.emit = function (eventType, data) {
var list = this._messageTable.get(eventType);
for (var i = list.length - 1; i >= 0; i--)
list[i](data);
};
return Emitter;
}());
var Triangulator = (function () { var Triangulator = (function () {
function Triangulator() { function Triangulator() {
this.triangleIndices = []; this.triangleIndices = [];

File diff suppressed because one or more lines are too long

View File

@@ -15,9 +15,9 @@
"bin-debug/Main.js", "bin-debug/Main.js",
"bin-debug/Platform.js", "bin-debug/Platform.js",
"bin-debug/ThemeAdapter.js", "bin-debug/ThemeAdapter.js",
"bin-debug/game/CoreEmitterType.js",
"bin-debug/game/MainScene.js", "bin-debug/game/MainScene.js",
"bin-debug/game/SpawnerComponent.js", "bin-debug/game/SpawnerComponent.js",
"bin-debug/game/SpawnerSystem.js", "bin-debug/game/SpawnerSystem.js"
"bin-debug/game/VerletDemo.js"
] ]
} }

View File

@@ -29,6 +29,7 @@
class Main extends eui.UILayer { class Main extends eui.UILayer {
public static emitter: Emitter<CoreEmitterType>;
protected createChildren(): void { protected createChildren(): void {
super.createChildren(); super.createChildren();
@@ -51,10 +52,15 @@ class Main extends eui.UILayer {
egret.registerImplementation("eui.IAssetAdapter", assetAdapter); egret.registerImplementation("eui.IAssetAdapter", assetAdapter);
egret.registerImplementation("eui.IThemeAdapter", new ThemeAdapter()); egret.registerImplementation("eui.IThemeAdapter", new ThemeAdapter());
Main.emitter = new Emitter<CoreEmitterType>();
this.addEventListener(egret.Event.ENTER_FRAME, this.updateFrame, this);
this.runGame(); this.runGame();
} }
private updateFrame(evt: egret.Event){
Main.emitter.emit(CoreEmitterType.Update, evt);
}
private async runGame() { private async runGame() {
await this.loadResource(); await this.loadResource();
this.createGameScene(); this.createGameScene();
@@ -98,8 +104,11 @@ class Main extends eui.UILayer {
new Vector2(10, 10), new Vector2(10, 10),
new Vector2(0, 10), new Vector2(0, 10),
new Vector2(0, 0)])); new Vector2(0, 0)]));
player.addComponent(new VerletDemo());
player.addComponent(new SpawnComponent(EnemyType.worm)); player.addComponent(new SpawnComponent(EnemyType.worm));
// console.log(player.transform.position); // console.log(player.transform.position);
Main.emitter.addObserver(CoreEmitterType.Update, ()=>{
console.log("update emitter");
});
} }
} }

View File

@@ -0,0 +1,3 @@
enum CoreEmitterType {
Update,
}

View File

@@ -1,29 +0,0 @@
class VerletDemo extends RenderableComponent {
private _world: VerletWorld;
private _stage: egret.Stage;
protected getWidth(){
return this.entity.scene.stage.stageWidth;
}
protected getHeight(){
return this.entity.scene.stage.stageHeight;
}
public onAddedToEntity(){
this._stage = this.entity.scene.stage;
this._world = new VerletWorld(new Rectangle(0, 0, this.width, this.height));
this._world.addComposite(new Box(new Vector2(100, 100), 50, 20));
this._world.addComposite(new Box(new Vector2(10, 10), 200, 100));
}
public update(){
this._world.update();
this._world.debugRender(this._stage);
}
initialize() {
}
}

View File

@@ -576,7 +576,7 @@ declare class Rectangle {
declare class Vector2 { declare class Vector2 {
x: number; x: number;
y: number; y: number;
constructor(x: number, y?: number); constructor(x?: number, y?: number);
static add(value1: Vector2, value2: Vector2): Vector2; static add(value1: Vector2, value2: Vector2): Vector2;
static divide(value1: Vector2, value2: Vector2): Vector2; static divide(value1: Vector2, value2: Vector2): Vector2;
static multiply(value1: Vector2, value2: Vector2): Vector2; static multiply(value1: Vector2, value2: Vector2): Vector2;
@@ -675,18 +675,6 @@ 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;
} }
declare class Particle {
position: Vector2;
lastPosition: Vector2;
isPinned: boolean;
pinnedPosition: any;
acceleration: Vector2;
mass: number;
radius: number;
collidesWithColliders: boolean;
constructor(position: Vector2);
applyForce(force: Vector2): void;
}
declare class SpatialHash { declare class SpatialHash {
gridBounds: Rectangle; gridBounds: Rectangle;
private _raycastParser; private _raycastParser;
@@ -714,6 +702,13 @@ declare class NumberDictionary {
getAllObjects(): Collider[]; getAllObjects(): Collider[];
clear(): void; clear(): void;
} }
declare class Emitter<T> {
private _messageTable;
constructor();
addObserver(eventType: T, handler: Function): void;
removeObserver(eventType: T, handler: Function): void;
emit(eventType: T, data: any): void;
}
declare class Triangulator { declare class Triangulator {
triangleIndices: number[]; triangleIndices: number[];
private _triPrev; private _triPrev;

View File

@@ -2719,8 +2719,8 @@ var Vector2 = (function () {
function Vector2(x, y) { function Vector2(x, y) {
this.x = 0; this.x = 0;
this.y = 0; this.y = 0;
this.x = x; this.x = x ? x : 0;
this.y = y ? y : x; this.y = y ? y : this.x;
} }
Vector2.add = function (value1, value2) { Vector2.add = function (value1, value2) {
var result = new Vector2(0, 0); var result = new Vector2(0, 0);
@@ -3026,7 +3026,7 @@ var Polygon = (function (_super) {
var verts = new Vector2[vertCount]; var verts = new Vector2[vertCount];
for (var i = 0; i < vertCount; i++) { for (var i = 0; i < vertCount; i++) {
var a = 2 * Math.PI * (i / vertCount); var a = 2 * Math.PI * (i / vertCount);
verts[i] = new Vector2(Math.cos(a), Math.sign(a) * radius); verts[i] = new Vector2(Math.cos(a), Math.sin(a) * radius);
} }
return verts; return verts;
}; };
@@ -3086,6 +3086,12 @@ var ShapeCollisions = (function () {
ShapeCollisions.polygonToPolygon = function (first, second) { ShapeCollisions.polygonToPolygon = function (first, second) {
var result = new CollisionResult(); var result = new CollisionResult();
var isIntersecting = true; var isIntersecting = true;
var firstEdges = first.edgeNormals;
var secondEdges = second.edgeNormals;
var minIntervalDistance = Number.POSITIVE_INFINITY;
var translationAxis = new Vector2();
var polygonOffset = Vector2.subtract(first.position, second.position);
var axis;
}; };
ShapeCollisions.circleToPolygon = function (circle, polygon) { ShapeCollisions.circleToPolygon = function (circle, polygon) {
var result = new CollisionResult(); var result = new CollisionResult();
@@ -3156,22 +3162,6 @@ var ShapeCollisions = (function () {
}; };
return ShapeCollisions; return ShapeCollisions;
}()); }());
var Particle = (function () {
function Particle(position) {
this.position = new Vector2(0, 0);
this.lastPosition = new Vector2(0, 0);
this.acceleration = new Vector2(0, 0);
this.mass = 1;
this.radius = 0;
this.collidesWithColliders = true;
this.position = position;
this.lastPosition = position;
}
Particle.prototype.applyForce = function (force) {
this.acceleration = Vector2.add(this.acceleration, new Vector2(force.x / this.mass, force.y / this.mass));
};
return Particle;
}());
var SpatialHash = (function () { var SpatialHash = (function () {
function SpatialHash(cellSize) { function SpatialHash(cellSize) {
if (cellSize === void 0) { cellSize = 100; } if (cellSize === void 0) { cellSize = 100; }
@@ -3284,6 +3274,30 @@ var NumberDictionary = (function () {
}; };
return NumberDictionary; return NumberDictionary;
}()); }());
var Emitter = (function () {
function Emitter() {
this._messageTable = new Map();
}
Emitter.prototype.addObserver = function (eventType, handler) {
var list = this._messageTable.get(eventType);
if (!list) {
list = [];
this._messageTable.set(eventType, list);
}
if (list.contains(handler))
console.warn("您试图添加相同的观察者两次");
list.push(handler);
};
Emitter.prototype.removeObserver = function (eventType, handler) {
this._messageTable.get(eventType).remove(handler);
};
Emitter.prototype.emit = function (eventType, data) {
var list = this._messageTable.get(eventType);
for (var i = list.length - 1; i >= 0; i--)
list[i](data);
};
return Emitter;
}());
var Triangulator = (function () { var Triangulator = (function () {
function Triangulator() { function Triangulator() {
this.triangleIndices = []; this.triangleIndices = [];

File diff suppressed because one or more lines are too long

View File

@@ -8,9 +8,9 @@ class Vector2 {
* @param x 二维空间中的x坐标 * @param x 二维空间中的x坐标
* @param y 二维空间的y坐标 * @param y 二维空间的y坐标
*/ */
constructor(x: number, y?: number){ constructor(x? : number, y?: number){
this.x = x; this.x = x ? x : 0;
this.y = y ? y : x; this.y = y ? y : this.x;
} }
public static add(value1: Vector2, value2: Vector2){ public static add(value1: Vector2, value2: Vector2){

View File

@@ -120,7 +120,7 @@ class Polygon extends Shape {
for (let i = 0; i < vertCount; i++) { for (let i = 0; i < vertCount; i++) {
let a = 2 * Math.PI * (i / vertCount); let a = 2 * Math.PI * (i / vertCount);
verts[i] = new Vector2(Math.cos(a), Math.sign(a) * radius); verts[i] = new Vector2(Math.cos(a), Math.sin(a) * radius);
} }
return verts; return verts;

View File

@@ -2,7 +2,14 @@ class ShapeCollisions {
public static polygonToPolygon(first: Polygon, second: Polygon){ public static polygonToPolygon(first: Polygon, second: Polygon){
let result = new CollisionResult(); let result = new CollisionResult();
let isIntersecting = true; let isIntersecting = true;
// let firstEdges = first.ed
let firstEdges = first.edgeNormals;
let secondEdges = second.edgeNormals;
let minIntervalDistance = Number.POSITIVE_INFINITY;
let translationAxis = new Vector2();
let polygonOffset = Vector2.subtract(first.position, second.position);
let axis: Vector2;
} }
public static circleToPolygon(circle: Circle, polygon: Polygon){ public static circleToPolygon(circle: Circle, polygon: Polygon){

View File

@@ -1,19 +0,0 @@
class Particle {
public position: Vector2 = new Vector2(0, 0);
public lastPosition: Vector2 = new Vector2(0, 0);
public isPinned: boolean;
public pinnedPosition;
public acceleration: Vector2 = new Vector2(0, 0);
public mass: number = 1;
public radius: number = 0;
public collidesWithColliders = true;
constructor(position: Vector2){
this.position = position;
this.lastPosition = position;
}
public applyForce(force: Vector2){
this.acceleration = Vector2.add(this.acceleration, new Vector2(force.x / this.mass, force.y / this.mass));
}
}

View File

@@ -0,0 +1,29 @@
class Emitter<T> {
private _messageTable: Map<T, Function[]>;
constructor(){
this._messageTable = new Map<T, Function[]>();
}
public addObserver(eventType: T, handler: Function){
let list: Function[] = this._messageTable.get(eventType);
if (!list){
list = [];
this._messageTable.set(eventType, list);
}
if (list.contains(handler))
console.warn("您试图添加相同的观察者两次");
list.push(handler);
}
public removeObserver(eventType: T, handler: Function){
this._messageTable.get(eventType).remove(handler);
}
public emit(eventType: T, data: any){
let list: Function[] = this._messageTable.get(eventType);
for (let i = list.length - 1; i >= 0; i --)
list[i](data);
}
}