Merge pull request #2 from esengine/develop_scenetransition

新增sceneTransition 用于场景过渡
This commit is contained in:
YHH
2020-06-22 15:28:45 +08:00
committed by GitHub
24 changed files with 2268 additions and 232 deletions

View File

@@ -167,7 +167,7 @@ declare class Entity {
readonly components: ComponentList;
private _updateOrder;
private _enabled;
private _isDestoryed;
_isDestoryed: boolean;
private _tag;
componentBits: BitSet;
parent: Transform;
@@ -211,10 +211,12 @@ declare class Scene extends egret.DisplayObjectContainer {
camera: Camera;
readonly entities: EntityList;
readonly renderableComponents: RenderableComponentList;
readonly content: ContentManager;
private _projectionMatrix;
private _transformMatrix;
private _matrixTransformMatrix;
private _renderers;
private _didSceneBegin;
readonly entityProcessors: EntityProcessorList;
constructor(displayObject: egret.DisplayObject);
createEntity(name: string): Entity;
@@ -224,25 +226,29 @@ declare class Scene extends egret.DisplayObjectContainer {
addEntityProcessor(processor: EntitySystem): EntitySystem;
removeEntityProcessor(processor: EntitySystem): void;
getEntityProcessor<T extends EntitySystem>(): T;
setActive(): Scene;
addRenderer<T extends Renderer>(renderer: T): T;
getRenderer<T extends Renderer>(type: any): T;
removeRenderer(renderer: Renderer): void;
initialize(): void;
onActive(): void;
onDeactive(): void;
begin(): void;
end(): void;
protected onStart(): void;
protected onActive(): void;
protected onDeactive(): void;
protected unload(): void;
update(): void;
render(): void;
prepRenderState(): void;
destory(): void;
}
declare class SceneManager {
private static _loadedScenes;
private static _lastScene;
private static _activeScene;
static createScene(name: string, scene: Scene): Scene;
static setActiveScene(scene: Scene): Scene;
static getActiveScene(): Scene;
private static _scene;
private static _nextScene;
static sceneTransition: SceneTransition;
static stage: egret.Stage;
constructor(stage: egret.Stage);
static scene: Scene;
static initialize(stage: egret.Stage): void;
static update(): void;
static render(): void;
static startSceneTransition<T extends SceneTransition>(sceneTransition: T): T;
}
declare enum DirtyType {
clean = 0,
@@ -428,6 +434,7 @@ declare class SpriteRenderer extends RenderableComponent {
setColor(color: number): void;
isVisibleFromCamera(camera: Camera): boolean;
render(camera: Camera): void;
onRemovedFromEntity(): void;
}
interface ITriggerListener {
onTriggerEnter(other: Collider, local: Collider): any;
@@ -608,6 +615,7 @@ declare abstract class Renderer {
onAddedToScene(scene: Scene): void;
protected beginRender(cam: Camera): void;
abstract render(scene: Scene): any;
unload(): void;
protected renderAfterStateCheck(renderable: IRenderable, cam: Camera): void;
}
declare class DefaultRenderer extends Renderer {
@@ -623,6 +631,32 @@ interface IRenderable {
declare class ScreenSpaceRenderer extends Renderer {
render(scene: Scene): void;
}
declare abstract class SceneTransition {
private _hasPreviousSceneRender;
loadsNewScene: boolean;
isNewSceneLoaded: boolean;
protected sceneLoadAction: Function;
onScreenObscured: Function;
onTransitionCompleted: Function;
readonly hasPreviousSceneRender: boolean;
constructor(sceneLoadAction: Function);
preRender(): void;
render(): void;
onBeginTransition(): void;
protected transitionComplete(): void;
protected loadNextScene(): void;
}
declare class FadeTransition extends SceneTransition {
fadeToColor: number;
fadeOutDuration: number;
fadeEaseType: Function;
delayBeforeFadeInDuration: number;
private _mask;
private _alpha;
constructor(sceneLoadAction: Function);
onBeginTransition(): void;
render(): void;
}
declare class Flags {
static isFlagSet(self: number, flag: number): boolean;
static isUnshiftedFlagSet(self: number, flag: number): boolean;
@@ -759,6 +793,7 @@ declare class Physics {
static spatialHashCellSize: number;
static readonly allLayers: number;
static reset(): void;
static clear(): void;
static overlapCircleAll(center: Vector2, randius: number, results: any[], layerMask?: number): number;
static boxcastBroadphase(rect: Rectangle, layerMask?: number): Collider[];
static boxcastBroadphaseExcludingSelf(collider: Collider, rect: Rectangle, layerMask?: number): Collider[];
@@ -787,7 +822,7 @@ declare class Polygon extends Shape {
constructor(points: Vector2[], isBox?: boolean);
private buildEdgeNormals;
setPoints(points: Vector2[]): void;
collidesWithShape(other: Shape): CollisionResult;
collidesWithShape(other: Shape): any;
recalculateCenterAndEdgeNormals(): void;
overlaps(other: Shape): any;
static findPolygonCenter(points: Vector2[]): Vector2;
@@ -850,6 +885,7 @@ declare class SpatialHash {
constructor(cellSize?: number);
remove(collider: Collider): void;
register(collider: Collider): void;
clear(): void;
overlapCircle(circleCenter: Vector2, radius: number, results: Collider[], layerMask: any): number;
aabbBroadphase(bounds: Rectangle, excludeCollider: Collider, layerMask: number): Collider[];
private cellAtPosition;
@@ -866,6 +902,11 @@ declare class NumberDictionary {
tryGetValue(x: number, y: number): Collider[];
clear(): void;
}
declare class ContentManager {
protected loadedAssets: Map<string, any>;
load(name: string, local?: boolean): Promise<any>;
dispose(): void;
}
declare class Emitter<T> {
private _messageTable;
constructor();
@@ -908,7 +949,7 @@ declare class Input {
static readonly totalTouchCount: number;
static readonly gameTouchs: TouchState[];
static readonly touchPositionDelta: Vector2;
static initialize(): void;
static initialize(stage: egret.Stage): void;
private static initTouchCache;
private static touchBegin;
private static touchMove;

View File

@@ -1059,7 +1059,7 @@ var Entity = (function () {
};
Entity.prototype.onRemovedFromScene = function () {
if (this._isDestoryed)
this.components.remove;
this.components.removeAllComponents();
};
Entity.prototype.onTransformChanged = function (comp) {
this.components.onEntityTransformChanged(comp);
@@ -1085,9 +1085,9 @@ var Scene = (function (_super) {
_this.entityProcessors = new EntityProcessorList();
_this.renderableComponents = new RenderableComponentList();
_this.entities = new EntityList(_this);
_this.content = new ContentManager();
_this.addEventListener(egret.Event.ACTIVATE, _this.onActive, _this);
_this.addEventListener(egret.Event.DEACTIVATE, _this.onDeactive, _this);
_this.addEventListener(egret.Event.ENTER_FRAME, _this.update, _this);
return _this;
}
Scene.prototype.createEntity = function (name) {
@@ -1121,10 +1121,6 @@ var Scene = (function (_super) {
Scene.prototype.getEntityProcessor = function () {
return this.entityProcessors.getProcessor();
};
Scene.prototype.setActive = function () {
SceneManager.setActiveScene(this);
return this;
};
Scene.prototype.addRenderer = function (renderer) {
this._renderers.push(renderer);
this._renderers.sort();
@@ -1141,28 +1137,43 @@ var Scene = (function (_super) {
Scene.prototype.removeRenderer = function (renderer) {
this._renderers.remove(renderer);
};
Scene.prototype.initialize = function () {
Scene.prototype.begin = function () {
if (this._renderers.length == 0) {
this.addRenderer(new DefaultRenderer());
console.warn("场景开始时没有渲染器 自动添加DefaultRenderer以保证能够正常渲染");
}
this.camera = this.createEntity("camera").getOrCreateComponent(new Camera());
Physics.reset();
Input.initialize();
if (this.entityProcessors)
this.entityProcessors.begin();
this.camera.onSceneSizeChanged(this.stage.stageWidth, this.stage.stageHeight);
this._didSceneBegin = true;
this.onStart();
};
Scene.prototype.end = function () {
this._didSceneBegin = false;
this.removeEventListener(egret.Event.DEACTIVATE, this.onDeactive, this);
this.removeEventListener(egret.Event.ACTIVATE, this.onActive, this);
for (var i = 0; i < this._renderers.length; i++) {
this._renderers[i].unload();
}
this.entities.removeAllEntities();
Physics.clear();
this.camera.destory();
this.camera = null;
this.content.dispose();
if (this.entityProcessors)
this.entityProcessors.end();
this.unload();
};
Scene.prototype.onStart = function () {
};
Scene.prototype.onActive = function () {
};
Scene.prototype.onDeactive = function () {
};
Scene.prototype.unload = function () { };
Scene.prototype.update = function () {
Time.update(egret.getTimer());
for (var i = GlobalManager.globalManagers.length - 1; i >= 0; i--) {
if (GlobalManager.globalManagers[i].enabled)
GlobalManager.globalManagers[i].update();
}
this.entities.updateLists();
if (this.entityProcessors)
this.entityProcessors.update();
@@ -1170,7 +1181,6 @@ var Scene = (function (_super) {
if (this.entityProcessors)
this.entityProcessors.lateUpdate();
this.renderableComponents.updateList();
this.render();
};
Scene.prototype.render = function () {
for (var i = 0; i < this._renderers.length; i++) {
@@ -1180,44 +1190,84 @@ var Scene = (function (_super) {
this._renderers[i].render(this);
}
};
Scene.prototype.prepRenderState = function () {
this._projectionMatrix.m11 = 2 / this.stage.stageWidth;
this._projectionMatrix.m22 = -2 / this.stage.stageHeight;
this._transformMatrix = this.camera.transformMatrix;
this._matrixTransformMatrix = Matrix2D.multiply(this._transformMatrix, this._projectionMatrix);
};
Scene.prototype.destory = function () {
this.removeEventListener(egret.Event.DEACTIVATE, this.onDeactive, this);
this.removeEventListener(egret.Event.ACTIVATE, this.onActive, this);
this.camera.destory();
this.camera = null;
this.entities.removeAllEntities();
};
return Scene;
}(egret.DisplayObjectContainer));
var SceneManager = (function () {
function SceneManager() {
function SceneManager(stage) {
stage.addEventListener(egret.Event.ENTER_FRAME, SceneManager.update, this);
SceneManager.stage = stage;
SceneManager.initialize(stage);
}
SceneManager.createScene = function (name, scene) {
scene.name = name;
this._loadedScenes.set(name, scene);
return scene;
Object.defineProperty(SceneManager, "scene", {
get: function () {
return this._scene;
},
set: function (value) {
if (!value)
throw new Error("场景不能为空");
if (this._scene == null) {
this._scene = value;
this._scene.begin();
}
else {
this._nextScene = value;
}
},
enumerable: true,
configurable: true
});
SceneManager.initialize = function (stage) {
Input.initialize(stage);
};
SceneManager.setActiveScene = function (scene) {
if (this._activeScene) {
if (this._activeScene == scene)
return;
this._lastScene = this._activeScene;
this._activeScene.destory();
SceneManager.update = function () {
Time.update(egret.getTimer());
if (SceneManager._scene) {
for (var i = GlobalManager.globalManagers.length - 1; i >= 0; i--) {
if (GlobalManager.globalManagers[i].enabled)
GlobalManager.globalManagers[i].update();
}
if (!SceneManager.sceneTransition ||
(SceneManager.sceneTransition && (!SceneManager.sceneTransition.loadsNewScene || SceneManager.sceneTransition.isNewSceneLoaded))) {
SceneManager._scene.update();
}
if (SceneManager._nextScene) {
SceneManager._scene.end();
for (var i = 0; i < SceneManager._scene.entities.buffer.length; i++) {
var entity = SceneManager._scene.entities.buffer[i];
entity.destory();
}
SceneManager._scene = SceneManager._nextScene;
SceneManager._nextScene = null;
SceneManager._scene.begin();
}
}
this._activeScene = scene;
this._activeScene.initialize();
return scene;
SceneManager.render();
};
SceneManager.getActiveScene = function () {
return this._activeScene;
SceneManager.render = function () {
if (this.sceneTransition) {
this.sceneTransition.preRender();
if (this._scene && !this.sceneTransition.hasPreviousSceneRender) {
this.scene.render();
this.sceneTransition.onBeginTransition();
}
else if (this.sceneTransition) {
if (this._scene && this.sceneTransition.isNewSceneLoaded) {
this._scene.render();
}
this.sceneTransition.render();
}
}
else if (this.scene) {
this.scene.render();
}
};
SceneManager.startSceneTransition = function (sceneTransition) {
if (this.sceneTransition) {
throw new Error("在前一个场景完成之前,不能开始一个新的场景转换。");
}
this.sceneTransition = sceneTransition;
return sceneTransition;
};
SceneManager._loadedScenes = new Map();
return SceneManager;
}());
var DirtyType;
@@ -2051,11 +2101,15 @@ var SpriteRenderer = (function (_super) {
return;
this._bitmap.x = this.entity.transform.position.x - camera.transform.position.x + camera.origin.x;
this._bitmap.y = this.entity.transform.position.y - camera.transform.position.y + camera.origin.y;
this._bitmap.rotation = this.entity.transform.rotation;
this._bitmap.rotation = this.entity.transform.rotation + camera.transform.rotation;
this._bitmap.anchorOffsetX = this._origin.x;
this._bitmap.anchorOffsetY = this._origin.y;
this._bitmap.scaleX = this.entity.transform.scale.x;
this._bitmap.scaleY = this.entity.transform.scale.y;
this._bitmap.scaleX = this.entity.transform.scale.x * camera.transform.scale.x;
this._bitmap.scaleY = this.entity.transform.scale.y * camera.transform.scale.y;
};
SpriteRenderer.prototype.onRemovedFromEntity = function () {
if (this._bitmap)
this.stage.removeChild(this._bitmap);
};
return SpriteRenderer;
}(RenderableComponent));
@@ -2727,6 +2781,8 @@ var EntityList = (function () {
this._entitiesToAdded.length = 0;
this.updateLists();
for (var i = 0; i < this._entities.length; i++) {
this._entities[i]._isDestoryed = true;
this._entities[i].onRemovedFromScene();
this._entities[i].scene = null;
}
this._entities.length = 0;
@@ -2899,11 +2955,12 @@ var Renderer = (function () {
Renderer.prototype.onAddedToScene = function (scene) { };
Renderer.prototype.beginRender = function (cam) {
cam.transform.updateTransform();
var entities = SceneManager.getActiveScene().entities;
var entities = SceneManager.scene.entities;
for (var i = 0; i < entities.buffer.length; i++) {
entities.buffer[i].transform.updateTransform();
}
};
Renderer.prototype.unload = function () { };
Renderer.prototype.renderAfterStateCheck = function (renderable, cam) {
renderable.render(cam);
};
@@ -2934,6 +2991,82 @@ var ScreenSpaceRenderer = (function (_super) {
};
return ScreenSpaceRenderer;
}(Renderer));
var SceneTransition = (function () {
function SceneTransition(sceneLoadAction) {
this.sceneLoadAction = sceneLoadAction;
this.loadsNewScene = sceneLoadAction != null;
}
Object.defineProperty(SceneTransition.prototype, "hasPreviousSceneRender", {
get: function () {
if (!this._hasPreviousSceneRender) {
this._hasPreviousSceneRender = true;
return false;
}
return true;
},
enumerable: true,
configurable: true
});
SceneTransition.prototype.preRender = function () { };
SceneTransition.prototype.render = function () {
};
SceneTransition.prototype.onBeginTransition = function () {
this.loadNextScene();
this.transitionComplete();
};
SceneTransition.prototype.transitionComplete = function () {
SceneManager.sceneTransition = null;
if (this.onTransitionCompleted) {
this.onTransitionCompleted();
}
};
SceneTransition.prototype.loadNextScene = function () {
if (this.onScreenObscured)
this.onScreenObscured();
if (!this.loadsNewScene) {
this.isNewSceneLoaded = true;
}
SceneManager.scene = this.sceneLoadAction();
this.isNewSceneLoaded = true;
};
return SceneTransition;
}());
var FadeTransition = (function (_super) {
__extends(FadeTransition, _super);
function FadeTransition(sceneLoadAction) {
var _this = _super.call(this, sceneLoadAction) || this;
_this.fadeToColor = 0x000000;
_this.fadeOutDuration = 0.4;
_this.fadeEaseType = egret.Ease.quadInOut;
_this.delayBeforeFadeInDuration = 0.1;
_this._alpha = 0;
_this._mask = new egret.Shape();
return _this;
}
FadeTransition.prototype.onBeginTransition = function () {
var _this = this;
this._mask.graphics.beginFill(this.fadeToColor, 1);
this._mask.graphics.drawRect(0, 0, SceneManager.stage.stageWidth, SceneManager.stage.stageHeight);
this._mask.graphics.endFill();
SceneManager.stage.addChild(this._mask);
egret.Tween.get(this).to({ _alpha: 1 }, this.fadeOutDuration * 1000, this.fadeEaseType)
.call(function () {
_this.loadNextScene();
}).wait(this.delayBeforeFadeInDuration).call(function () {
egret.Tween.get(_this).to({ _alpha: 0 }, _this.fadeOutDuration * 1000, _this.fadeEaseType).call(function () {
_this.transitionComplete();
SceneManager.stage.removeChild(_this._mask);
});
});
};
FadeTransition.prototype.render = function () {
this._mask.graphics.clear();
this._mask.graphics.beginFill(this.fadeToColor, this._alpha);
this._mask.graphics.drawRect(0, 0, SceneManager.stage.stageWidth, SceneManager.stage.stageHeight);
this._mask.graphics.endFill();
};
return FadeTransition;
}(SceneTransition));
var Flags = (function () {
function Flags() {
}
@@ -3662,6 +3795,9 @@ var Physics = (function () {
Physics.reset = function () {
this._spatialHash = new SpatialHash(this.spatialHashCellSize);
};
Physics.clear = function () {
this._spatialHash.clear();
};
Physics.overlapCircleAll = function (center, randius, results, layerMask) {
if (layerMask === void 0) { layerMask = -1; }
return this._spatialHash.overlapCircle(center, randius, results, layerMask);
@@ -3739,8 +3875,7 @@ var Polygon = (function (_super) {
Polygon.prototype.collidesWithShape = function (other) {
var result = new CollisionResult();
if (other instanceof Polygon) {
result = ShapeCollisions.polygonToPolygon(this, other);
return result;
return ShapeCollisions.polygonToPolygon(this, other);
}
if (other instanceof Circle) {
result = ShapeCollisions.circleToPolygon(other, this);
@@ -4163,6 +4298,9 @@ var SpatialHash = (function () {
}
}
};
SpatialHash.prototype.clear = function () {
this._cellDict.clear();
};
SpatialHash.prototype.overlapCircle = function (circleCenter, radius, results, layerMask) {
var bounds = new Rectangle(circleCenter.x - radius, circleCenter.y - radius, radius * 2, radius * 2);
this._overlapTestCircle.radius = radius;
@@ -4256,6 +4394,48 @@ var NumberDictionary = (function () {
};
return NumberDictionary;
}());
var ContentManager = (function () {
function ContentManager() {
this.loadedAssets = new Map();
}
ContentManager.prototype.load = function (name, local) {
var _this = this;
if (local === void 0) { local = true; }
return new Promise(function (resolve, reject) {
var res = _this.loadedAssets.get(name);
if (res) {
resolve(res);
return;
}
if (local) {
RES.getResAsync(name).then(function (data) {
_this.loadedAssets.set(name, data);
resolve(data);
}).catch(function (err) {
console.error("资源加载错误:", name, err);
reject(err);
});
}
else {
RES.getResByUrl(name).then(function (data) {
_this.loadedAssets.set(name, data);
resolve(data);
}).catch(function (err) {
console.error("资源加载错误:", name, err);
reject(err);
});
}
});
};
ContentManager.prototype.dispose = function () {
this.loadedAssets.forEach(function (value) {
var assetsToRemove = value;
assetsToRemove.dispose();
});
this.loadedAssets.clear();
};
return ContentManager;
}());
var Emitter = (function () {
function Emitter() {
this._messageTable = new Map();
@@ -4354,6 +4534,8 @@ var Input = (function () {
}
Object.defineProperty(Input, "touchPosition", {
get: function () {
if (!this._gameTouchs[0])
return Vector2.zero;
return this._gameTouchs[0].position;
},
enumerable: true,
@@ -4402,11 +4584,11 @@ var Input = (function () {
enumerable: true,
configurable: true
});
Input.initialize = function () {
Input.initialize = function (stage) {
if (this._init)
return;
this._init = true;
this._stage = SceneManager.getActiveScene().stage;
this._stage = stage;
this._stage.addEventListener(egret.TouchEvent.TOUCH_BEGIN, this.touchBegin, this);
this._stage.addEventListener(egret.TouchEvent.TOUCH_MOVE, this.touchMove, this);
this._stage.addEventListener(egret.TouchEvent.TOUCH_END, this.touchEnd, this);

File diff suppressed because one or more lines are too long

View File

@@ -30,6 +30,7 @@
class Main extends eui.UILayer {
public static emitter: Emitter<CoreEmitterType>;
public static manager: SceneManager;
protected createChildren(): void {
super.createChildren();
@@ -52,6 +53,7 @@ class Main extends eui.UILayer {
egret.registerImplementation("eui.IAssetAdapter", assetAdapter);
egret.registerImplementation("eui.IThemeAdapter", new ThemeAdapter());
Main.manager = new SceneManager(this.stage);
Main.emitter = new Emitter<CoreEmitterType>();
this.addEventListener(egret.Event.ENTER_FRAME, this.updateFrame, this);
this.runGame();
@@ -97,26 +99,20 @@ class Main extends eui.UILayer {
* Create scene interface
*/
protected createGameScene(): void {
let sprite = new Sprite(RES.getRes("checkbox_select_disabled_png"));
let scene = SceneManager.createScene("main", new MainScene(this)).setActive();
let player = scene.createEntity("player");
player.addComponent(new SpriteRenderer()).setSprite(sprite).setColor(0xFF0000);
player.addComponent(new SpawnComponent(EnemyType.worm));
player.addComponent(new Mover());
player.addComponent(new PlayerController());
player.addComponent(new FollowCamera(player));
player.addComponent(new BoxCollider());
for (let i = 0; i < 20; i ++){
let sprite = new Sprite(RES.getRes("checkbox_select_disabled_png"));
let player2 = scene.createEntity("player2");
player2.addComponent(new SpriteRenderer()).setSprite(sprite);
player2.transform.position = new Vector2(Math.random() * 100 * i, Math.random() * 100 * i);
player2.addComponent(new BoxCollider());
}
SceneManager.scene = new MainScene(this);
// Main.emitter.addObserver(CoreEmitterType.Update, ()=>{
// console.log("update emitter");
// });
let button = new eui.Button();
button.label = "切换场景";
this.stage.addChild(button);
button.addEventListener(egret.TouchEvent.TOUCH_TAP, ()=>{
SceneManager.startSceneTransition(new FadeTransition(()=>{
return new MainScene(this);
}));
}, this);
}
}

View File

@@ -8,6 +8,33 @@ class MainScene extends Scene {
this.breadthfirstTest();
}
public onStart(){
this.content.load("http://www.hyuan.org/123.jpeg", false).then((data)=>{
console.log(data);
});
this.camera.setZoom(0.5);
let sprite = new Sprite(RES.getRes("checkbox_select_disabled_png"));
let player = this.createEntity("player");
player.addComponent(new SpriteRenderer()).setSprite(sprite).setColor(0xFF0000);
player.addComponent(new SpawnComponent(EnemyType.worm));
player.addComponent(new Mover());
player.addComponent(new PlayerController());
player.addComponent(new FollowCamera(player));
player.addComponent(new BoxCollider());
for (let i = 0; i < 20; i ++){
let sprite = new Sprite(RES.getRes("checkbox_select_disabled_png"));
let player2 = this.createEntity("player2");
player2.addComponent(new SpriteRenderer()).setSprite(sprite);
player2.transform.position = new Vector2(Math.random() * 100 * i, Math.random() * 100 * i);
player2.addComponent(new BoxCollider());
}
}
public breadthfirstTest(){
let graph = new UnweightedGraph<string>();

View File

@@ -27,7 +27,7 @@ class PlayerController extends Component {
return;
if (this.down){
let camera = SceneManager.getActiveScene().camera;
let camera = SceneManager.scene.camera;
let worldVec = camera.screenToWorldPoint(this.touchPoint);
this.mover.move(Input.touchPositionDelta);
console.log(Input.touchPositionDelta);

View File

@@ -167,7 +167,7 @@ declare class Entity {
readonly components: ComponentList;
private _updateOrder;
private _enabled;
private _isDestoryed;
_isDestoryed: boolean;
private _tag;
componentBits: BitSet;
parent: Transform;
@@ -211,10 +211,12 @@ declare class Scene extends egret.DisplayObjectContainer {
camera: Camera;
readonly entities: EntityList;
readonly renderableComponents: RenderableComponentList;
readonly content: ContentManager;
private _projectionMatrix;
private _transformMatrix;
private _matrixTransformMatrix;
private _renderers;
private _didSceneBegin;
readonly entityProcessors: EntityProcessorList;
constructor(displayObject: egret.DisplayObject);
createEntity(name: string): Entity;
@@ -224,25 +226,29 @@ declare class Scene extends egret.DisplayObjectContainer {
addEntityProcessor(processor: EntitySystem): EntitySystem;
removeEntityProcessor(processor: EntitySystem): void;
getEntityProcessor<T extends EntitySystem>(): T;
setActive(): Scene;
addRenderer<T extends Renderer>(renderer: T): T;
getRenderer<T extends Renderer>(type: any): T;
removeRenderer(renderer: Renderer): void;
initialize(): void;
onActive(): void;
onDeactive(): void;
begin(): void;
end(): void;
protected onStart(): void;
protected onActive(): void;
protected onDeactive(): void;
protected unload(): void;
update(): void;
render(): void;
prepRenderState(): void;
destory(): void;
}
declare class SceneManager {
private static _loadedScenes;
private static _lastScene;
private static _activeScene;
static createScene(name: string, scene: Scene): Scene;
static setActiveScene(scene: Scene): Scene;
static getActiveScene(): Scene;
private static _scene;
private static _nextScene;
static sceneTransition: SceneTransition;
static stage: egret.Stage;
constructor(stage: egret.Stage);
static scene: Scene;
static initialize(stage: egret.Stage): void;
static update(): void;
static render(): void;
static startSceneTransition<T extends SceneTransition>(sceneTransition: T): T;
}
declare enum DirtyType {
clean = 0,
@@ -428,6 +434,7 @@ declare class SpriteRenderer extends RenderableComponent {
setColor(color: number): void;
isVisibleFromCamera(camera: Camera): boolean;
render(camera: Camera): void;
onRemovedFromEntity(): void;
}
interface ITriggerListener {
onTriggerEnter(other: Collider, local: Collider): any;
@@ -608,6 +615,7 @@ declare abstract class Renderer {
onAddedToScene(scene: Scene): void;
protected beginRender(cam: Camera): void;
abstract render(scene: Scene): any;
unload(): void;
protected renderAfterStateCheck(renderable: IRenderable, cam: Camera): void;
}
declare class DefaultRenderer extends Renderer {
@@ -623,6 +631,32 @@ interface IRenderable {
declare class ScreenSpaceRenderer extends Renderer {
render(scene: Scene): void;
}
declare abstract class SceneTransition {
private _hasPreviousSceneRender;
loadsNewScene: boolean;
isNewSceneLoaded: boolean;
protected sceneLoadAction: Function;
onScreenObscured: Function;
onTransitionCompleted: Function;
readonly hasPreviousSceneRender: boolean;
constructor(sceneLoadAction: Function);
preRender(): void;
render(): void;
onBeginTransition(): void;
protected transitionComplete(): void;
protected loadNextScene(): void;
}
declare class FadeTransition extends SceneTransition {
fadeToColor: number;
fadeOutDuration: number;
fadeEaseType: Function;
delayBeforeFadeInDuration: number;
private _mask;
private _alpha;
constructor(sceneLoadAction: Function);
onBeginTransition(): void;
render(): void;
}
declare class Flags {
static isFlagSet(self: number, flag: number): boolean;
static isUnshiftedFlagSet(self: number, flag: number): boolean;
@@ -759,6 +793,7 @@ declare class Physics {
static spatialHashCellSize: number;
static readonly allLayers: number;
static reset(): void;
static clear(): void;
static overlapCircleAll(center: Vector2, randius: number, results: any[], layerMask?: number): number;
static boxcastBroadphase(rect: Rectangle, layerMask?: number): Collider[];
static boxcastBroadphaseExcludingSelf(collider: Collider, rect: Rectangle, layerMask?: number): Collider[];
@@ -787,7 +822,7 @@ declare class Polygon extends Shape {
constructor(points: Vector2[], isBox?: boolean);
private buildEdgeNormals;
setPoints(points: Vector2[]): void;
collidesWithShape(other: Shape): CollisionResult;
collidesWithShape(other: Shape): any;
recalculateCenterAndEdgeNormals(): void;
overlaps(other: Shape): any;
static findPolygonCenter(points: Vector2[]): Vector2;
@@ -850,6 +885,7 @@ declare class SpatialHash {
constructor(cellSize?: number);
remove(collider: Collider): void;
register(collider: Collider): void;
clear(): void;
overlapCircle(circleCenter: Vector2, radius: number, results: Collider[], layerMask: any): number;
aabbBroadphase(bounds: Rectangle, excludeCollider: Collider, layerMask: number): Collider[];
private cellAtPosition;
@@ -866,6 +902,11 @@ declare class NumberDictionary {
tryGetValue(x: number, y: number): Collider[];
clear(): void;
}
declare class ContentManager {
protected loadedAssets: Map<string, any>;
load(name: string, local?: boolean): Promise<any>;
dispose(): void;
}
declare class Emitter<T> {
private _messageTable;
constructor();
@@ -908,7 +949,7 @@ declare class Input {
static readonly totalTouchCount: number;
static readonly gameTouchs: TouchState[];
static readonly touchPositionDelta: Vector2;
static initialize(): void;
static initialize(stage: egret.Stage): void;
private static initTouchCache;
private static touchBegin;
private static touchMove;

View File

@@ -1059,7 +1059,7 @@ var Entity = (function () {
};
Entity.prototype.onRemovedFromScene = function () {
if (this._isDestoryed)
this.components.remove;
this.components.removeAllComponents();
};
Entity.prototype.onTransformChanged = function (comp) {
this.components.onEntityTransformChanged(comp);
@@ -1085,9 +1085,9 @@ var Scene = (function (_super) {
_this.entityProcessors = new EntityProcessorList();
_this.renderableComponents = new RenderableComponentList();
_this.entities = new EntityList(_this);
_this.content = new ContentManager();
_this.addEventListener(egret.Event.ACTIVATE, _this.onActive, _this);
_this.addEventListener(egret.Event.DEACTIVATE, _this.onDeactive, _this);
_this.addEventListener(egret.Event.ENTER_FRAME, _this.update, _this);
return _this;
}
Scene.prototype.createEntity = function (name) {
@@ -1121,10 +1121,6 @@ var Scene = (function (_super) {
Scene.prototype.getEntityProcessor = function () {
return this.entityProcessors.getProcessor();
};
Scene.prototype.setActive = function () {
SceneManager.setActiveScene(this);
return this;
};
Scene.prototype.addRenderer = function (renderer) {
this._renderers.push(renderer);
this._renderers.sort();
@@ -1141,28 +1137,43 @@ var Scene = (function (_super) {
Scene.prototype.removeRenderer = function (renderer) {
this._renderers.remove(renderer);
};
Scene.prototype.initialize = function () {
Scene.prototype.begin = function () {
if (this._renderers.length == 0) {
this.addRenderer(new DefaultRenderer());
console.warn("场景开始时没有渲染器 自动添加DefaultRenderer以保证能够正常渲染");
}
this.camera = this.createEntity("camera").getOrCreateComponent(new Camera());
Physics.reset();
Input.initialize();
if (this.entityProcessors)
this.entityProcessors.begin();
this.camera.onSceneSizeChanged(this.stage.stageWidth, this.stage.stageHeight);
this._didSceneBegin = true;
this.onStart();
};
Scene.prototype.end = function () {
this._didSceneBegin = false;
this.removeEventListener(egret.Event.DEACTIVATE, this.onDeactive, this);
this.removeEventListener(egret.Event.ACTIVATE, this.onActive, this);
for (var i = 0; i < this._renderers.length; i++) {
this._renderers[i].unload();
}
this.entities.removeAllEntities();
Physics.clear();
this.camera.destory();
this.camera = null;
this.content.dispose();
if (this.entityProcessors)
this.entityProcessors.end();
this.unload();
};
Scene.prototype.onStart = function () {
};
Scene.prototype.onActive = function () {
};
Scene.prototype.onDeactive = function () {
};
Scene.prototype.unload = function () { };
Scene.prototype.update = function () {
Time.update(egret.getTimer());
for (var i = GlobalManager.globalManagers.length - 1; i >= 0; i--) {
if (GlobalManager.globalManagers[i].enabled)
GlobalManager.globalManagers[i].update();
}
this.entities.updateLists();
if (this.entityProcessors)
this.entityProcessors.update();
@@ -1170,7 +1181,6 @@ var Scene = (function (_super) {
if (this.entityProcessors)
this.entityProcessors.lateUpdate();
this.renderableComponents.updateList();
this.render();
};
Scene.prototype.render = function () {
for (var i = 0; i < this._renderers.length; i++) {
@@ -1180,44 +1190,84 @@ var Scene = (function (_super) {
this._renderers[i].render(this);
}
};
Scene.prototype.prepRenderState = function () {
this._projectionMatrix.m11 = 2 / this.stage.stageWidth;
this._projectionMatrix.m22 = -2 / this.stage.stageHeight;
this._transformMatrix = this.camera.transformMatrix;
this._matrixTransformMatrix = Matrix2D.multiply(this._transformMatrix, this._projectionMatrix);
};
Scene.prototype.destory = function () {
this.removeEventListener(egret.Event.DEACTIVATE, this.onDeactive, this);
this.removeEventListener(egret.Event.ACTIVATE, this.onActive, this);
this.camera.destory();
this.camera = null;
this.entities.removeAllEntities();
};
return Scene;
}(egret.DisplayObjectContainer));
var SceneManager = (function () {
function SceneManager() {
function SceneManager(stage) {
stage.addEventListener(egret.Event.ENTER_FRAME, SceneManager.update, this);
SceneManager.stage = stage;
SceneManager.initialize(stage);
}
SceneManager.createScene = function (name, scene) {
scene.name = name;
this._loadedScenes.set(name, scene);
return scene;
Object.defineProperty(SceneManager, "scene", {
get: function () {
return this._scene;
},
set: function (value) {
if (!value)
throw new Error("场景不能为空");
if (this._scene == null) {
this._scene = value;
this._scene.begin();
}
else {
this._nextScene = value;
}
},
enumerable: true,
configurable: true
});
SceneManager.initialize = function (stage) {
Input.initialize(stage);
};
SceneManager.setActiveScene = function (scene) {
if (this._activeScene) {
if (this._activeScene == scene)
return;
this._lastScene = this._activeScene;
this._activeScene.destory();
SceneManager.update = function () {
Time.update(egret.getTimer());
if (SceneManager._scene) {
for (var i = GlobalManager.globalManagers.length - 1; i >= 0; i--) {
if (GlobalManager.globalManagers[i].enabled)
GlobalManager.globalManagers[i].update();
}
if (!SceneManager.sceneTransition ||
(SceneManager.sceneTransition && (!SceneManager.sceneTransition.loadsNewScene || SceneManager.sceneTransition.isNewSceneLoaded))) {
SceneManager._scene.update();
}
if (SceneManager._nextScene) {
SceneManager._scene.end();
for (var i = 0; i < SceneManager._scene.entities.buffer.length; i++) {
var entity = SceneManager._scene.entities.buffer[i];
entity.destory();
}
SceneManager._scene = SceneManager._nextScene;
SceneManager._nextScene = null;
SceneManager._scene.begin();
}
}
this._activeScene = scene;
this._activeScene.initialize();
return scene;
SceneManager.render();
};
SceneManager.getActiveScene = function () {
return this._activeScene;
SceneManager.render = function () {
if (this.sceneTransition) {
this.sceneTransition.preRender();
if (this._scene && !this.sceneTransition.hasPreviousSceneRender) {
this.scene.render();
this.sceneTransition.onBeginTransition();
}
else if (this.sceneTransition) {
if (this._scene && this.sceneTransition.isNewSceneLoaded) {
this._scene.render();
}
this.sceneTransition.render();
}
}
else if (this.scene) {
this.scene.render();
}
};
SceneManager.startSceneTransition = function (sceneTransition) {
if (this.sceneTransition) {
throw new Error("在前一个场景完成之前,不能开始一个新的场景转换。");
}
this.sceneTransition = sceneTransition;
return sceneTransition;
};
SceneManager._loadedScenes = new Map();
return SceneManager;
}());
var DirtyType;
@@ -2051,11 +2101,15 @@ var SpriteRenderer = (function (_super) {
return;
this._bitmap.x = this.entity.transform.position.x - camera.transform.position.x + camera.origin.x;
this._bitmap.y = this.entity.transform.position.y - camera.transform.position.y + camera.origin.y;
this._bitmap.rotation = this.entity.transform.rotation;
this._bitmap.rotation = this.entity.transform.rotation + camera.transform.rotation;
this._bitmap.anchorOffsetX = this._origin.x;
this._bitmap.anchorOffsetY = this._origin.y;
this._bitmap.scaleX = this.entity.transform.scale.x;
this._bitmap.scaleY = this.entity.transform.scale.y;
this._bitmap.scaleX = this.entity.transform.scale.x * camera.transform.scale.x;
this._bitmap.scaleY = this.entity.transform.scale.y * camera.transform.scale.y;
};
SpriteRenderer.prototype.onRemovedFromEntity = function () {
if (this._bitmap)
this.stage.removeChild(this._bitmap);
};
return SpriteRenderer;
}(RenderableComponent));
@@ -2727,6 +2781,8 @@ var EntityList = (function () {
this._entitiesToAdded.length = 0;
this.updateLists();
for (var i = 0; i < this._entities.length; i++) {
this._entities[i]._isDestoryed = true;
this._entities[i].onRemovedFromScene();
this._entities[i].scene = null;
}
this._entities.length = 0;
@@ -2899,11 +2955,12 @@ var Renderer = (function () {
Renderer.prototype.onAddedToScene = function (scene) { };
Renderer.prototype.beginRender = function (cam) {
cam.transform.updateTransform();
var entities = SceneManager.getActiveScene().entities;
var entities = SceneManager.scene.entities;
for (var i = 0; i < entities.buffer.length; i++) {
entities.buffer[i].transform.updateTransform();
}
};
Renderer.prototype.unload = function () { };
Renderer.prototype.renderAfterStateCheck = function (renderable, cam) {
renderable.render(cam);
};
@@ -2934,6 +2991,82 @@ var ScreenSpaceRenderer = (function (_super) {
};
return ScreenSpaceRenderer;
}(Renderer));
var SceneTransition = (function () {
function SceneTransition(sceneLoadAction) {
this.sceneLoadAction = sceneLoadAction;
this.loadsNewScene = sceneLoadAction != null;
}
Object.defineProperty(SceneTransition.prototype, "hasPreviousSceneRender", {
get: function () {
if (!this._hasPreviousSceneRender) {
this._hasPreviousSceneRender = true;
return false;
}
return true;
},
enumerable: true,
configurable: true
});
SceneTransition.prototype.preRender = function () { };
SceneTransition.prototype.render = function () {
};
SceneTransition.prototype.onBeginTransition = function () {
this.loadNextScene();
this.transitionComplete();
};
SceneTransition.prototype.transitionComplete = function () {
SceneManager.sceneTransition = null;
if (this.onTransitionCompleted) {
this.onTransitionCompleted();
}
};
SceneTransition.prototype.loadNextScene = function () {
if (this.onScreenObscured)
this.onScreenObscured();
if (!this.loadsNewScene) {
this.isNewSceneLoaded = true;
}
SceneManager.scene = this.sceneLoadAction();
this.isNewSceneLoaded = true;
};
return SceneTransition;
}());
var FadeTransition = (function (_super) {
__extends(FadeTransition, _super);
function FadeTransition(sceneLoadAction) {
var _this = _super.call(this, sceneLoadAction) || this;
_this.fadeToColor = 0x000000;
_this.fadeOutDuration = 0.4;
_this.fadeEaseType = egret.Ease.quadInOut;
_this.delayBeforeFadeInDuration = 0.1;
_this._alpha = 0;
_this._mask = new egret.Shape();
return _this;
}
FadeTransition.prototype.onBeginTransition = function () {
var _this = this;
this._mask.graphics.beginFill(this.fadeToColor, 1);
this._mask.graphics.drawRect(0, 0, SceneManager.stage.stageWidth, SceneManager.stage.stageHeight);
this._mask.graphics.endFill();
SceneManager.stage.addChild(this._mask);
egret.Tween.get(this).to({ _alpha: 1 }, this.fadeOutDuration * 1000, this.fadeEaseType)
.call(function () {
_this.loadNextScene();
}).wait(this.delayBeforeFadeInDuration).call(function () {
egret.Tween.get(_this).to({ _alpha: 0 }, _this.fadeOutDuration * 1000, _this.fadeEaseType).call(function () {
_this.transitionComplete();
SceneManager.stage.removeChild(_this._mask);
});
});
};
FadeTransition.prototype.render = function () {
this._mask.graphics.clear();
this._mask.graphics.beginFill(this.fadeToColor, this._alpha);
this._mask.graphics.drawRect(0, 0, SceneManager.stage.stageWidth, SceneManager.stage.stageHeight);
this._mask.graphics.endFill();
};
return FadeTransition;
}(SceneTransition));
var Flags = (function () {
function Flags() {
}
@@ -3662,6 +3795,9 @@ var Physics = (function () {
Physics.reset = function () {
this._spatialHash = new SpatialHash(this.spatialHashCellSize);
};
Physics.clear = function () {
this._spatialHash.clear();
};
Physics.overlapCircleAll = function (center, randius, results, layerMask) {
if (layerMask === void 0) { layerMask = -1; }
return this._spatialHash.overlapCircle(center, randius, results, layerMask);
@@ -3739,8 +3875,7 @@ var Polygon = (function (_super) {
Polygon.prototype.collidesWithShape = function (other) {
var result = new CollisionResult();
if (other instanceof Polygon) {
result = ShapeCollisions.polygonToPolygon(this, other);
return result;
return ShapeCollisions.polygonToPolygon(this, other);
}
if (other instanceof Circle) {
result = ShapeCollisions.circleToPolygon(other, this);
@@ -4163,6 +4298,9 @@ var SpatialHash = (function () {
}
}
};
SpatialHash.prototype.clear = function () {
this._cellDict.clear();
};
SpatialHash.prototype.overlapCircle = function (circleCenter, radius, results, layerMask) {
var bounds = new Rectangle(circleCenter.x - radius, circleCenter.y - radius, radius * 2, radius * 2);
this._overlapTestCircle.radius = radius;
@@ -4256,6 +4394,48 @@ var NumberDictionary = (function () {
};
return NumberDictionary;
}());
var ContentManager = (function () {
function ContentManager() {
this.loadedAssets = new Map();
}
ContentManager.prototype.load = function (name, local) {
var _this = this;
if (local === void 0) { local = true; }
return new Promise(function (resolve, reject) {
var res = _this.loadedAssets.get(name);
if (res) {
resolve(res);
return;
}
if (local) {
RES.getResAsync(name).then(function (data) {
_this.loadedAssets.set(name, data);
resolve(data);
}).catch(function (err) {
console.error("资源加载错误:", name, err);
reject(err);
});
}
else {
RES.getResByUrl(name).then(function (data) {
_this.loadedAssets.set(name, data);
resolve(data);
}).catch(function (err) {
console.error("资源加载错误:", name, err);
reject(err);
});
}
});
};
ContentManager.prototype.dispose = function () {
this.loadedAssets.forEach(function (value) {
var assetsToRemove = value;
assetsToRemove.dispose();
});
this.loadedAssets.clear();
};
return ContentManager;
}());
var Emitter = (function () {
function Emitter() {
this._messageTable = new Map();
@@ -4354,6 +4534,8 @@ var Input = (function () {
}
Object.defineProperty(Input, "touchPosition", {
get: function () {
if (!this._gameTouchs[0])
return Vector2.zero;
return this._gameTouchs[0].position;
},
enumerable: true,
@@ -4402,11 +4584,11 @@ var Input = (function () {
enumerable: true,
configurable: true
});
Input.initialize = function () {
Input.initialize = function (stage) {
if (this._init)
return;
this._init = true;
this._stage = SceneManager.getActiveScene().stage;
this._stage = stage;
this._stage.addEventListener(egret.TouchEvent.TOUCH_BEGIN, this.touchBegin, this);
this._stage.addEventListener(egret.TouchEvent.TOUCH_MOVE, this.touchMove, this);
this._stage.addEventListener(egret.TouchEvent.TOUCH_END, this.touchEnd, this);

File diff suppressed because one or more lines are too long

1350
source/lib/tween.d.ts vendored Normal file

File diff suppressed because it is too large Load Diff

View File

@@ -76,10 +76,15 @@ class SpriteRenderer extends RenderableComponent {
this._bitmap.x = this.entity.transform.position.x - camera.transform.position.x + camera.origin.x;
this._bitmap.y = this.entity.transform.position.y - camera.transform.position.y + camera.origin.y;
this._bitmap.rotation = this.entity.transform.rotation;
this._bitmap.rotation = this.entity.transform.rotation + camera.transform.rotation;
this._bitmap.anchorOffsetX = this._origin.x;
this._bitmap.anchorOffsetY = this._origin.y;
this._bitmap.scaleX = this.entity.transform.scale.x;
this._bitmap.scaleY = this.entity.transform.scale.y;
this._bitmap.scaleX = this.entity.transform.scale.x * camera.transform.scale.x;
this._bitmap.scaleY = this.entity.transform.scale.y * camera.transform.scale.y;
}
public onRemovedFromEntity(){
if (this._bitmap)
this.stage.removeChild(this._bitmap);
}
}

View File

@@ -11,7 +11,7 @@ class Entity {
public readonly components: ComponentList;
private _updateOrder: number = 0;
private _enabled: boolean = true;
private _isDestoryed: boolean;
public _isDestoryed: boolean;
private _tag: number = 0;
public componentBits: BitSet;
@@ -253,7 +253,7 @@ class Entity {
public onRemovedFromScene(){
if (this._isDestoryed)
this.components.remove
this.components.removeAllComponents();
}
public onTransformChanged(comp: ComponentTransform){

View File

@@ -3,11 +3,13 @@ class Scene extends egret.DisplayObjectContainer {
public camera: Camera;
public readonly entities: EntityList;
public readonly renderableComponents: RenderableComponentList;
public readonly content: ContentManager;
private _projectionMatrix: Matrix2D;
private _transformMatrix: Matrix2D;
private _matrixTransformMatrix: Matrix2D;
private _renderers: Renderer[] = [];
private _didSceneBegin;
public readonly entityProcessors: EntityProcessorList;
@@ -18,10 +20,10 @@ class Scene extends egret.DisplayObjectContainer {
this.entityProcessors = new EntityProcessorList();
this.renderableComponents = new RenderableComponentList();
this.entities = new EntityList(this);
this.content = new ContentManager();
this.addEventListener(egret.Event.ACTIVATE, this.onActive, this);
this.addEventListener(egret.Event.DEACTIVATE, this.onDeactive, this);
this.addEventListener(egret.Event.ENTER_FRAME, this.update, this);
}
public createEntity(name: string) {
@@ -68,12 +70,6 @@ class Scene extends egret.DisplayObjectContainer {
return this.entityProcessors.getProcessor<T>();
}
public setActive(): Scene {
SceneManager.setActiveScene(this);
return this;
}
public addRenderer<T extends Renderer>(renderer: T) {
this._renderers.push(renderer);
this._renderers.sort();
@@ -96,8 +92,7 @@ class Scene extends egret.DisplayObjectContainer {
this._renderers.remove(renderer);
}
/** 初始化场景 */
public initialize() {
public begin(){
if (this._renderers.length == 0) {
this.addRenderer(new DefaultRenderer());
console.warn("场景开始时没有渲染器 自动添加DefaultRenderer以保证能够正常渲染");
@@ -106,32 +101,56 @@ class Scene extends egret.DisplayObjectContainer {
this.camera = this.createEntity("camera").getOrCreateComponent(new Camera());
Physics.reset();
Input.initialize();
if (this.entityProcessors)
this.entityProcessors.begin();
this.camera.onSceneSizeChanged(this.stage.stageWidth, this.stage.stageHeight);
this._didSceneBegin = true;
this.onStart();
}
public end(){
this._didSceneBegin = false;
this.removeEventListener(egret.Event.DEACTIVATE, this.onDeactive, this);
this.removeEventListener(egret.Event.ACTIVATE, this.onActive, this);
for (let i = 0; i < this._renderers.length; i ++){
this._renderers[i].unload();
}
this.entities.removeAllEntities();
Physics.clear();
this.camera.destory();
this.camera = null;
this.content.dispose();
if (this.entityProcessors)
this.entityProcessors.end();
this.unload();
}
protected onStart(){
}
/** 场景激活 */
public onActive() {
protected onActive() {
}
/** 场景失去焦点 */
public onDeactive() {
protected onDeactive() {
}
protected unload(){ }
public update() {
Time.update(egret.getTimer());
for (let i = GlobalManager.globalManagers.length - 1; i >= 0; i--) {
if (GlobalManager.globalManagers[i].enabled)
GlobalManager.globalManagers[i].update();
}
this.entities.updateLists();
if (this.entityProcessors)
@@ -143,7 +162,6 @@ class Scene extends egret.DisplayObjectContainer {
this.entityProcessors.lateUpdate();
this.renderableComponents.updateList();
this.render();
}
public render(){
@@ -154,22 +172,4 @@ class Scene extends egret.DisplayObjectContainer {
this._renderers[i].render(this);
}
}
public prepRenderState() {
this._projectionMatrix.m11 = 2 / this.stage.stageWidth;
this._projectionMatrix.m22 = -2 / this.stage.stageHeight;
this._transformMatrix = this.camera.transformMatrix;
this._matrixTransformMatrix = Matrix2D.multiply(this._transformMatrix, this._projectionMatrix);
}
public destory() {
this.removeEventListener(egret.Event.DEACTIVATE, this.onDeactive, this);
this.removeEventListener(egret.Event.ACTIVATE, this.onActive, this);
this.camera.destory();
this.camera = null;
this.entities.removeAllEntities();
}
}

View File

@@ -1,40 +1,97 @@
/** 运行时的场景管理。 */
class SceneManager {
private static _loadedScenes: Map<string, Scene> = new Map();
/** 上一个场景 */
private static _lastScene: Scene;
/** 当前激活的场景 */
private static _activeScene: Scene;
private static _scene: Scene;
private static _nextScene: Scene;
public static sceneTransition: SceneTransition;
public static stage: egret.Stage;
/**
* 使用给定的名称在运行时创建一个空的新场景。
* 新场景将与当前打开的任何现有场景一起被添加到层次结构中。
* 这个函数用于在运行时创建场景。
* @param name
* @param scene
*/
public static createScene(name: string, scene: Scene){
scene.name = name;
this._loadedScenes.set(name, scene);
return scene;
constructor(stage: egret.Stage) {
stage.addEventListener(egret.Event.ENTER_FRAME, SceneManager.update, this);
SceneManager.stage = stage;
SceneManager.initialize(stage);
}
public static setActiveScene(scene: Scene){
if (this._activeScene){
// 如果场景相同则不进行切换
if (this._activeScene == scene)
return;
public static get scene() {
return this._scene;
}
public static set scene(value: Scene) {
if (!value)
throw new Error("场景不能为空");
this._lastScene = this._activeScene;
this._activeScene.destory();
if (this._scene == null) {
this._scene = value;
this._scene.begin();
} else {
this._nextScene = value;
}
}
public static initialize(stage: egret.Stage) {
Input.initialize(stage);
}
public static update() {
Time.update(egret.getTimer());
if (SceneManager._scene) {
for (let i = GlobalManager.globalManagers.length - 1; i >= 0; i--) {
if (GlobalManager.globalManagers[i].enabled)
GlobalManager.globalManagers[i].update();
}
if (!SceneManager.sceneTransition ||
(SceneManager.sceneTransition && (!SceneManager.sceneTransition.loadsNewScene || SceneManager.sceneTransition.isNewSceneLoaded))) {
SceneManager._scene.update();
}
if (SceneManager._nextScene) {
SceneManager._scene.end();
for (let i = 0; i < SceneManager._scene.entities.buffer.length; i++) {
let entity = SceneManager._scene.entities.buffer[i];
entity.destory();
}
SceneManager._scene = SceneManager._nextScene;
SceneManager._nextScene = null;
SceneManager._scene.begin();
}
}
this._activeScene = scene;
this._activeScene.initialize();
return scene;
SceneManager.render();
}
public static getActiveScene(){
return this._activeScene;
public static render() {
if (this.sceneTransition){
this.sceneTransition.preRender();
if (this._scene && !this.sceneTransition.hasPreviousSceneRender){
this.scene.render();
this.sceneTransition.onBeginTransition();
} else if (this.sceneTransition) {
if (this._scene && this.sceneTransition.isNewSceneLoaded) {
this._scene.render();
}
this.sceneTransition.render();
}
} else if (this.scene) {
this.scene.render();
}
}
/**
* 临时运行SceneTransition允许一个场景过渡到另一个平滑的自定义效果。
* @param sceneTransition
*/
public static startSceneTransition<T extends SceneTransition>(sceneTransition: T): T {
if (this.sceneTransition) {
throw new Error("在前一个场景完成之前,不能开始一个新的场景转换。");
}
this.sceneTransition = sceneTransition;
return sceneTransition;
}
}

View File

@@ -82,6 +82,8 @@ class EntityList{
this.updateLists();
for (let i = 0; i < this._entities.length; i ++){
this._entities[i]._isDestoryed = true;
this._entities[i].onRemovedFromScene();
this._entities[i].scene = null;
}

View File

@@ -18,7 +18,7 @@ abstract class Renderer {
protected beginRender(cam: Camera){
cam.transform.updateTransform();
let entities = SceneManager.getActiveScene().entities;
let entities = SceneManager.scene.entities;
for (let i = 0; i < entities.buffer.length; i ++){
entities.buffer[i].transform.updateTransform();
}
@@ -29,6 +29,8 @@ abstract class Renderer {
* @param scene
*/
public abstract render(scene: Scene);
public unload(){ }
/**
*

View File

@@ -0,0 +1,38 @@
///<reference path="./SceneTransition.ts"/>
class FadeTransition extends SceneTransition {
public fadeToColor: number = 0x000000;
public fadeOutDuration = 0.4;
public fadeEaseType: Function = egret.Ease.quadInOut;
public delayBeforeFadeInDuration = 0.1;
private _mask: egret.Shape;
private _alpha: number = 0;
constructor(sceneLoadAction: Function) {
super(sceneLoadAction);
this._mask = new egret.Shape();
}
public onBeginTransition() {
this._mask.graphics.beginFill(this.fadeToColor, 1);
this._mask.graphics.drawRect(0, 0, SceneManager.stage.stageWidth, SceneManager.stage.stageHeight);
this._mask.graphics.endFill();
SceneManager.stage.addChild(this._mask);
egret.Tween.get(this).to({ _alpha: 1}, this.fadeOutDuration * 1000, this.fadeEaseType)
.call(() => {
this.loadNextScene();
}).wait(this.delayBeforeFadeInDuration).call(() => {
egret.Tween.get(this).to({ _alpha: 0 }, this.fadeOutDuration * 1000, this.fadeEaseType).call(() => {
this.transitionComplete();
SceneManager.stage.removeChild(this._mask);
});
});
}
public render(){
this._mask.graphics.clear();
this._mask.graphics.beginFill(this.fadeToColor, this._alpha);
this._mask.graphics.drawRect(0, 0, SceneManager.stage.stageWidth, SceneManager.stage.stageHeight);
this._mask.graphics.endFill();
}
}

View File

@@ -0,0 +1,64 @@
/**
* SceneTransition用于从一个场景过渡到另一个场景或在一个有效果的场景中过渡
*/
abstract class SceneTransition {
private _hasPreviousSceneRender: boolean;
/** 是否加载新场景的标志 */
public loadsNewScene: boolean;
/**
* 将此用于两个部分的转换。例如淡出会先淡出到黑色然后当isNewSceneLoaded为true它会淡出。
* 对于场景过渡isNewSceneLoaded应该在中点设置为true这就标识一个新的场景被加载了。
*/
public isNewSceneLoaded: boolean;
/** 返回新加载场景的函数 */
protected sceneLoadAction: Function;
/** 在loadNextScene执行时调用。这在进行场景间过渡时很有用这样你就知道什么时候可以更多地使用相机或者重置任何实体 */
public onScreenObscured: Function;
/** 当转换完成执行时调用,以便可以调用其他工作,比如启动另一个转换。 */
public onTransitionCompleted: Function;
public get hasPreviousSceneRender(){
if (!this._hasPreviousSceneRender){
this._hasPreviousSceneRender = true;
return false;
}
return true;
}
constructor(sceneLoadAction: Function) {
this.sceneLoadAction = sceneLoadAction;
this.loadsNewScene = sceneLoadAction != null;
}
public preRender() { }
public render() {
}
public onBeginTransition() {
this.loadNextScene();
this.transitionComplete();
}
protected transitionComplete() {
SceneManager.sceneTransition = null;
if (this.onTransitionCompleted) {
this.onTransitionCompleted();
}
}
protected loadNextScene() {
if (this.onScreenObscured)
this.onScreenObscured();
if (!this.loadsNewScene) {
this.isNewSceneLoaded = true;
}
SceneManager.scene = this.sceneLoadAction();
this.isNewSceneLoaded = true;
}
}

View File

@@ -9,6 +9,10 @@ class Physics {
this._spatialHash = new SpatialHash(this.spatialHashCellSize);
}
public static clear(){
this._spatialHash.clear();
}
public static overlapCircleAll(center: Vector2, randius: number, results: any[], layerMask = -1){
return this._spatialHash.overlapCircle(center, randius, results, layerMask);
}

View File

@@ -53,10 +53,8 @@ class Polygon extends Shape {
public collidesWithShape(other: Shape){
let result = new CollisionResult();
if (other instanceof Polygon){
result = ShapeCollisions.polygonToPolygon(this, other);
return result;
return ShapeCollisions.polygonToPolygon(this, other);
}
if (other instanceof Circle){
result = ShapeCollisions.circleToPolygon(other, this);

View File

@@ -52,6 +52,10 @@ class SpatialHash {
}
}
public clear(){
this._cellDict.clear();
}
public overlapCircle(circleCenter: Vector2, radius: number, results: Collider[], layerMask) {
let bounds = new Rectangle(circleCenter.x - radius, circleCenter.y - radius, radius * 2, radius * 2);

View File

@@ -0,0 +1,41 @@
class ContentManager {
protected loadedAssets: Map<string, any> = new Map<string, any>();
/** 异步加载资源 */
public load(name: string, local: boolean = true): Promise<any> {
return new Promise((resolve, reject) => {
let res = this.loadedAssets.get(name);
if (res) {
resolve(res);
return;
}
if (local) {
RES.getResAsync(name).then((data) => {
this.loadedAssets.set(name, data);
resolve(data);
}).catch((err) => {
console.error("资源加载错误:", name, err);
reject(err);
});
} else {
RES.getResByUrl(name).then((data) => {
this.loadedAssets.set(name, data);
resolve(data);
}).catch((err) => {
console.error("资源加载错误:", name, err);
reject(err);
});
}
})
}
public dispose(){
this.loadedAssets.forEach(value => {
let assetsToRemove = value;
assetsToRemove.dispose();
});
this.loadedAssets.clear();
}
}

View File

@@ -6,7 +6,7 @@ class TouchState {
public get position(){
return new Vector2(this.x, this.y);
}
public reset(){
this.x = 0;
this.y = 0;
@@ -26,6 +26,8 @@ class Input {
private static _totalTouchCount: number = 0;
/** 返回第一个触摸点的坐标 */
public static get touchPosition(){
if (!this._gameTouchs[0])
return Vector2.zero;
return this._gameTouchs[0].position;
}
/** 获取最大触摸数 */
@@ -65,12 +67,12 @@ class Input {
return delta;
}
public static initialize(){
public static initialize(stage: egret.Stage){
if (this._init)
return;
this._init = true;
this._stage = SceneManager.getActiveScene().stage;
this._stage = stage;
this._stage.addEventListener(egret.TouchEvent.TOUCH_BEGIN, this.touchBegin, this);
this._stage.addEventListener(egret.TouchEvent.TOUCH_MOVE, this.touchMove, this);
this._stage.addEventListener(egret.TouchEvent.TOUCH_END, this.touchEnd, this);