demo 更新
This commit is contained in:
19
demo/src/Fgui/common/UI_com_tips.ts
Normal file
19
demo/src/Fgui/common/UI_com_tips.ts
Normal file
@@ -0,0 +1,19 @@
|
||||
/** This is an automatically generated class by FairyGUI. Please do not modify it. **/
|
||||
|
||||
module FUI.common {
|
||||
|
||||
export class UI_com_tips extends fairygui.GComponent {
|
||||
public m_content:fairygui.GTextField;
|
||||
public static URL:string = "ui://minsdstak7mf0";
|
||||
|
||||
public static createInstance():UI_com_tips {
|
||||
return <UI_com_tips>(fairygui.UIPackage.createObject("common", "com_tips"));
|
||||
}
|
||||
|
||||
protected constructFromXML(xml:any):void {
|
||||
super.constructFromXML(xml);
|
||||
|
||||
this.m_content = <fairygui.GTextField>(this.getChildAt(1));
|
||||
}
|
||||
}
|
||||
}
|
||||
10
demo/src/Fgui/common/commonBinder.ts
Normal file
10
demo/src/Fgui/common/commonBinder.ts
Normal file
@@ -0,0 +1,10 @@
|
||||
/** This is an automatically generated class by FairyGUI. Please do not modify it. **/
|
||||
|
||||
module FUI.common {
|
||||
|
||||
export class commonBinder {
|
||||
public static bindAll():void {
|
||||
fairygui.UIObjectFactory.setPackageItemExtension(UI_com_tips.URL, UI_com_tips);
|
||||
}
|
||||
}
|
||||
}
|
||||
21
demo/src/Fgui/loading/UI_View_loading.ts
Normal file
21
demo/src/Fgui/loading/UI_View_loading.ts
Normal file
@@ -0,0 +1,21 @@
|
||||
/** This is an automatically generated class by FairyGUI. Please do not modify it. **/
|
||||
|
||||
module FUI.loading {
|
||||
|
||||
export class UI_View_loading extends fairygui.GComponent {
|
||||
public m_title:fairygui.GTextField;
|
||||
public m_pg_loading:fairygui.GProgressBar;
|
||||
public static URL:string = "ui://mk2d64e7r5ro0";
|
||||
|
||||
public static createInstance():UI_View_loading {
|
||||
return <UI_View_loading>(fairygui.UIPackage.createObject("loading", "View_loading"));
|
||||
}
|
||||
|
||||
protected constructFromXML(xml:any):void {
|
||||
super.constructFromXML(xml);
|
||||
|
||||
this.m_title = <fairygui.GTextField>(this.getChildAt(1));
|
||||
this.m_pg_loading = <fairygui.GProgressBar>(this.getChildAt(2));
|
||||
}
|
||||
}
|
||||
}
|
||||
10
demo/src/Fgui/loading/loadingBinder.ts
Normal file
10
demo/src/Fgui/loading/loadingBinder.ts
Normal file
@@ -0,0 +1,10 @@
|
||||
/** This is an automatically generated class by FairyGUI. Please do not modify it. **/
|
||||
|
||||
module FUI.loading {
|
||||
|
||||
export class loadingBinder {
|
||||
public static bindAll():void {
|
||||
fairygui.UIObjectFactory.setPackageItemExtension(UI_View_loading.URL, UI_View_loading);
|
||||
}
|
||||
}
|
||||
}
|
||||
19
demo/src/Fgui/sc/UI_View_sc.ts
Normal file
19
demo/src/Fgui/sc/UI_View_sc.ts
Normal file
@@ -0,0 +1,19 @@
|
||||
/** This is an automatically generated class by FairyGUI. Please do not modify it. **/
|
||||
|
||||
module FUI.sc {
|
||||
|
||||
export class UI_View_sc extends fairygui.GComponent {
|
||||
public m_list_sc:fairygui.GList;
|
||||
public static URL:string = "ui://m4sln17ak7mf0";
|
||||
|
||||
public static createInstance():UI_View_sc {
|
||||
return <UI_View_sc>(fairygui.UIPackage.createObject("sc", "View_sc"));
|
||||
}
|
||||
|
||||
protected constructFromXML(xml:any):void {
|
||||
super.constructFromXML(xml);
|
||||
|
||||
this.m_list_sc = <fairygui.GList>(this.getChildAt(0));
|
||||
}
|
||||
}
|
||||
}
|
||||
19
demo/src/Fgui/sc/UI_btn_sc.ts
Normal file
19
demo/src/Fgui/sc/UI_btn_sc.ts
Normal file
@@ -0,0 +1,19 @@
|
||||
/** This is an automatically generated class by FairyGUI. Please do not modify it. **/
|
||||
|
||||
module FUI.sc {
|
||||
|
||||
export class UI_btn_sc extends fairygui.GButton {
|
||||
public m_name:fairygui.GTextField;
|
||||
public static URL:string = "ui://m4sln17ak7mf2";
|
||||
|
||||
public static createInstance():UI_btn_sc {
|
||||
return <UI_btn_sc>(fairygui.UIPackage.createObject("sc", "btn_sc"));
|
||||
}
|
||||
|
||||
protected constructFromXML(xml:any):void {
|
||||
super.constructFromXML(xml);
|
||||
|
||||
this.m_name = <fairygui.GTextField>(this.getChildAt(3));
|
||||
}
|
||||
}
|
||||
}
|
||||
11
demo/src/Fgui/sc/scBinder.ts
Normal file
11
demo/src/Fgui/sc/scBinder.ts
Normal file
@@ -0,0 +1,11 @@
|
||||
/** This is an automatically generated class by FairyGUI. Please do not modify it. **/
|
||||
|
||||
module FUI.sc {
|
||||
|
||||
export class scBinder {
|
||||
public static bindAll():void {
|
||||
fairygui.UIObjectFactory.setPackageItemExtension(UI_View_sc.URL, UI_View_sc);
|
||||
fairygui.UIObjectFactory.setPackageItemExtension(UI_btn_sc.URL, UI_btn_sc);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,51 +0,0 @@
|
||||
//////////////////////////////////////////////////////////////////////////////////////
|
||||
//
|
||||
// Copyright (c) 2014-present, Egret Technology.
|
||||
// All rights reserved.
|
||||
// Redistribution and use in source and binary forms, with or without
|
||||
// modification, are permitted provided that the following conditions are met:
|
||||
//
|
||||
// * Redistributions of source code must retain the above copyright
|
||||
// notice, this list of conditions and the following disclaimer.
|
||||
// * Redistributions in binary form must reproduce the above copyright
|
||||
// notice, this list of conditions and the following disclaimer in the
|
||||
// documentation and/or other materials provided with the distribution.
|
||||
// * Neither the name of the Egret nor the
|
||||
// names of its contributors may be used to endorse or promote products
|
||||
// derived from this software without specific prior written permission.
|
||||
//
|
||||
// THIS SOFTWARE IS PROVIDED BY EGRET AND CONTRIBUTORS "AS IS" AND ANY EXPRESS
|
||||
// OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
||||
// OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
||||
// IN NO EVENT SHALL EGRET AND CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
|
||||
// INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
||||
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;LOSS OF USE, DATA,
|
||||
// OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
|
||||
// LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
|
||||
// NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
|
||||
// EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
//
|
||||
//////////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
class LoadingUI extends egret.Sprite implements RES.PromiseTaskReporter {
|
||||
|
||||
public constructor() {
|
||||
super();
|
||||
this.createView();
|
||||
}
|
||||
|
||||
private textField: egret.TextField;
|
||||
|
||||
private createView(): void {
|
||||
this.textField = new egret.TextField();
|
||||
this.addChild(this.textField);
|
||||
this.textField.y = 300;
|
||||
this.textField.width = 480;
|
||||
this.textField.height = 100;
|
||||
this.textField.textAlign = "center";
|
||||
}
|
||||
|
||||
public onProgress(current: number, total: number): void {
|
||||
this.textField.text = `Loading...${current}/${total}`;
|
||||
}
|
||||
}
|
||||
@@ -27,36 +27,42 @@
|
||||
//
|
||||
//////////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
import LoadingView = loading.LoadingView;
|
||||
|
||||
class Main extends es.Core {
|
||||
protected initialize() {
|
||||
this.runGame();
|
||||
try {
|
||||
this.runGame();
|
||||
} catch(err) {
|
||||
console.error(err);
|
||||
}
|
||||
}
|
||||
|
||||
private runGame() {
|
||||
egret.ImageLoader.crossOrigin = "anonymous";
|
||||
this.initUIConfig();
|
||||
this.loadResource();
|
||||
this.initGameControl();
|
||||
}
|
||||
|
||||
private initUIConfig(){
|
||||
LayerManager.getInstance().init(this.stage);
|
||||
FguiUtils.packageNamespace = FUI;
|
||||
fairygui.UIConfig.defaultFont = "黑体";
|
||||
fairygui.UIConfig.bringWindowToFrontOnClick = false;
|
||||
this.stage.addChild(fgui.GRoot.inst.displayListContainer);
|
||||
}
|
||||
|
||||
private loadResource() {
|
||||
const loadingView = new LoadingUI();
|
||||
this.stage.addChild(loadingView);
|
||||
RES.loadConfig("resource/default.res.json", "resource/").then(()=>{
|
||||
RES.loadGroup("preload", 0, loadingView).then(()=>{
|
||||
this.stage.removeChild(loadingView);
|
||||
this.createGameScene();
|
||||
}).catch(err => {
|
||||
console.error(err);
|
||||
});
|
||||
EventManager.getInstance().dispatch(loading.LoadingEvents.OPENVIEW);
|
||||
}).catch(err =>{
|
||||
console.error(err);
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* 创建场景界面
|
||||
* Create scene interface
|
||||
*/
|
||||
protected createGameScene(): void {
|
||||
es.Core.scene = new scene.MainScene();
|
||||
private initGameControl(){
|
||||
loading.LoadingControl.getInstance();
|
||||
sc.ScControl.getInstance();
|
||||
}
|
||||
}
|
||||
|
||||
35
demo/src/SampleHelpers/SampleScene.ts
Normal file
35
demo/src/SampleHelpers/SampleScene.ts
Normal file
@@ -0,0 +1,35 @@
|
||||
module samples {
|
||||
import ScreenSpaceRenderer = es.ScreenSpaceRenderer;
|
||||
import RenderLayerExcludeRenderer = es.RenderLayerExcludeRenderer;
|
||||
import SpriteRenderer = es.SpriteRenderer;
|
||||
|
||||
export class SampleScene extends es.Scene {
|
||||
public static readonly screenSpaceRenderLayer = 999;
|
||||
public canvas: SpriteRenderer;
|
||||
|
||||
public static _needsFullRender: boolean;
|
||||
public _screenSpaceRenderer: ScreenSpaceRenderer;
|
||||
|
||||
constructor(addExcludeRenderer: boolean = true, needsFullRender: boolean = false){
|
||||
super();
|
||||
SampleScene._needsFullRender = needsFullRender;
|
||||
|
||||
if (needsFullRender){
|
||||
this._screenSpaceRenderer = this.addRenderer(new ScreenSpaceRenderer(100, SampleScene.screenSpaceRenderLayer));
|
||||
this._screenSpaceRenderer.shouldDebugRender = false;
|
||||
}else{
|
||||
this.addRenderer(new ScreenSpaceRenderer(100, SampleScene.screenSpaceRenderLayer));
|
||||
}
|
||||
|
||||
if (addExcludeRenderer)
|
||||
this.addRenderer(new RenderLayerExcludeRenderer(0, SampleScene.screenSpaceRenderLayer));
|
||||
|
||||
this.canvas = this.createEntity("ui").addComponent(new SpriteRenderer());
|
||||
this.canvas.renderLayer = SampleScene.screenSpaceRenderLayer;
|
||||
this.setupSceneSelector();
|
||||
}
|
||||
|
||||
public setupSceneSelector(){
|
||||
}
|
||||
}
|
||||
}
|
||||
21
demo/src/Scenes/Animated Tiles/AnimatedTilesScene.ts
Normal file
21
demo/src/Scenes/Animated Tiles/AnimatedTilesScene.ts
Normal file
@@ -0,0 +1,21 @@
|
||||
module samples {
|
||||
export class AnimatedTilesScene extends SampleScene {
|
||||
constructor(){
|
||||
super(true, true);
|
||||
}
|
||||
|
||||
public initialize(){
|
||||
super.initialize();
|
||||
|
||||
let tiledEntity = this.createEntity("tiled-map-entity");
|
||||
try {
|
||||
es.TiledMapLoader.loadTmxMap(new es.TmxMap(), "orthogonal-outside_json").then(map => {
|
||||
tiledEntity.addComponent(new es.TiledMapRenderer(map));
|
||||
manager.AlterManager.alter_tips("Tiled tiles场景加载成功");
|
||||
});
|
||||
} catch (err){
|
||||
console.error(err);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
8
demo/src/Scenes/Empty Scene/BasicScene.ts
Normal file
8
demo/src/Scenes/Empty Scene/BasicScene.ts
Normal file
@@ -0,0 +1,8 @@
|
||||
module samples {
|
||||
export class BasicScene extends SampleScene {
|
||||
public async onStart() {
|
||||
super.onStart();
|
||||
manager.AlterManager.alter_tips("空白场景加载成功");
|
||||
}
|
||||
}
|
||||
}
|
||||
25
demo/src/UI/PopManager.ts
Normal file
25
demo/src/UI/PopManager.ts
Normal file
@@ -0,0 +1,25 @@
|
||||
module manager {
|
||||
export class AlterManager {
|
||||
public static alter_tips(txt: string){
|
||||
let item_tip = FUI.common.UI_com_tips.createInstance();
|
||||
if (!item_tip)
|
||||
return;
|
||||
item_tip.m_content.text = txt;
|
||||
item_tip.ensureBoundsCorrect();
|
||||
item_tip.x = es.Core.Instance.stage.stageWidth / 2 - item_tip.width / 2;
|
||||
item_tip.y = es.Core.Instance.stage.stageHeight / 2 - item_tip.height / 2;
|
||||
LayerManager.getInstance().tipsLayer.addChild(item_tip.displayObject);
|
||||
|
||||
item_tip.alpha = 0;
|
||||
let originY = item_tip.y;
|
||||
egret.Tween.get(item_tip).to({alpha: 1, y: originY - 100}, 700, egret.Ease.cubicInOut).wait(1000)
|
||||
.to({alpha: 0, y: originY - 200}, 700, egret.Ease.cubicInOut).call(()=>{
|
||||
egret.Tween.removeTweens(item_tip.displayObject);
|
||||
if (item_tip.displayObject && item_tip.displayObject.parent)
|
||||
item_tip.displayObject.parent.removeChild(item_tip.displayObject);
|
||||
item_tip.dispose();
|
||||
item_tip = null;
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
27
demo/src/UI/loading/LoadingControl.ts
Normal file
27
demo/src/UI/loading/LoadingControl.ts
Normal file
@@ -0,0 +1,27 @@
|
||||
module loading {
|
||||
export class LoadingControl {
|
||||
private _eventManager: EventManager;
|
||||
private _viewManager: ViewManager;
|
||||
private static single: LoadingControl;
|
||||
public static getInstance(){
|
||||
if (!this.single) this.single = new LoadingControl();
|
||||
|
||||
return this.single;
|
||||
}
|
||||
|
||||
constructor(){
|
||||
this.addEvents();
|
||||
}
|
||||
|
||||
private addEvents(){
|
||||
this._eventManager = EventManager.getInstance();
|
||||
this._viewManager = ViewManager.getInstance();
|
||||
// 事件监听
|
||||
this._eventManager.addListener(LoadingEvents.OPENVIEW, this.openView, this);
|
||||
}
|
||||
|
||||
private openView(){
|
||||
this._viewManager.openView(LoadingView);
|
||||
}
|
||||
}
|
||||
}
|
||||
5
demo/src/UI/loading/LoadingEvents.ts
Normal file
5
demo/src/UI/loading/LoadingEvents.ts
Normal file
@@ -0,0 +1,5 @@
|
||||
module loading {
|
||||
export module LoadingEvents {
|
||||
export const OPENVIEW = Symbol();
|
||||
}
|
||||
}
|
||||
59
demo/src/UI/loading/LoadingView.ts
Normal file
59
demo/src/UI/loading/LoadingView.ts
Normal file
@@ -0,0 +1,59 @@
|
||||
module loading {
|
||||
import commonBinder = FUI.common.commonBinder;
|
||||
import ResourceInfo = RES.ResourceInfo;
|
||||
|
||||
export class LoadingView extends BaseView implements RES.PromiseTaskReporter {
|
||||
private _ui: FUI.loading.UI_View_loading;
|
||||
private _loadGroup = ["preload", "common"];
|
||||
private _maxProgress = 0;
|
||||
private _currentProgress = 0;
|
||||
|
||||
constructor() {
|
||||
super("loading");
|
||||
}
|
||||
|
||||
public init() {
|
||||
this._ui = this._fuiView = FUI.loading.UI_View_loading.createInstance();
|
||||
this.addChild(this._ui.displayObject);
|
||||
super.init();
|
||||
}
|
||||
|
||||
public async show() {
|
||||
this._ui.m_pg_loading.value = 0;
|
||||
for (let i = 0; i < this._loadGroup.length; i ++){
|
||||
this._maxProgress += RES.getGroupByName(this._loadGroup[i]).length;
|
||||
}
|
||||
this._ui.m_pg_loading.max = this._maxProgress;
|
||||
for (let i = 0; i < this._loadGroup.length; i ++){
|
||||
await RES.loadGroup(this._loadGroup[i], 0, this);
|
||||
}
|
||||
}
|
||||
|
||||
public onProgress(current: number, total: number, resItem: ResourceInfo): void {
|
||||
this._ui.m_pg_loading.tweenValue(this._currentProgress++, 1);
|
||||
if (this._currentProgress == this._maxProgress) {
|
||||
if (resItem.name == "common"){
|
||||
fairygui.UIPackage.addPackage(resItem.name);
|
||||
commonBinder.bindAll();
|
||||
}
|
||||
egret.setTimeout(() => {
|
||||
this.openMainScene();
|
||||
}, this, 1000)
|
||||
}
|
||||
}
|
||||
|
||||
public openMainScene() {
|
||||
es.Core.scene = new samples.BasicScene();
|
||||
EventManager.getInstance().dispatch(sc.ScEvents.OPENVIEW);
|
||||
this.close();
|
||||
}
|
||||
|
||||
public destroy() {
|
||||
if (this._ui) {
|
||||
this._ui.dispose();
|
||||
this._ui = null;
|
||||
}
|
||||
super.destroy();
|
||||
}
|
||||
}
|
||||
}
|
||||
101
demo/src/UI/mvc/BaseView.ts
Normal file
101
demo/src/UI/mvc/BaseView.ts
Normal file
@@ -0,0 +1,101 @@
|
||||
class BaseView extends egret.DisplayObjectContainer {
|
||||
private _name: string;
|
||||
public get name(){
|
||||
return this._name;
|
||||
}
|
||||
public set name(value: string){
|
||||
this._name = value;
|
||||
}
|
||||
public viewLayer: viewLayer = viewLayer.pop;
|
||||
/** 是否可以被销毁 */
|
||||
protected _destoryChildren: boolean;
|
||||
protected _isDispose: boolean;
|
||||
/** fui界面 */
|
||||
protected _fuiView: fairygui.GComponent;
|
||||
/** 是否已经显示 */
|
||||
protected _isShow: boolean;
|
||||
/** 视图数据 */
|
||||
protected _showData: any;
|
||||
|
||||
constructor(name?: string){
|
||||
super();
|
||||
this._isDispose = false;
|
||||
if (name){
|
||||
this.name = name;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 当界面被初始化时调用
|
||||
*/
|
||||
public init(){
|
||||
this._destoryChildren = true;
|
||||
}
|
||||
|
||||
/**
|
||||
* 当界面大小被改变时
|
||||
*/
|
||||
public resize(){
|
||||
if (this._fuiView && this._isShow){
|
||||
this._fuiView.width = LayerManager.getInstance().stage.stageWidth;
|
||||
this._fuiView.height = LayerManager.getInstance().stage.stageHeight;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @param showData
|
||||
*/
|
||||
public baseShow(showData?: any){
|
||||
this._isShow = true;
|
||||
this._showData = showData;
|
||||
egret.callLater(this.resize, this);
|
||||
}
|
||||
|
||||
/**
|
||||
* 打开界面时调用
|
||||
*/
|
||||
public show() {
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* 当界面关闭时调用
|
||||
*/
|
||||
public close() {
|
||||
this._isShow = false;
|
||||
ViewManager.getInstance().closeView(this);
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @param showData
|
||||
*/
|
||||
public baseRefresh(showData?: any){
|
||||
this._showData = showData;
|
||||
}
|
||||
|
||||
/**
|
||||
* 当界面刷新时调用
|
||||
*/
|
||||
public refresh(showData?: any){
|
||||
this._showData = showData;
|
||||
}
|
||||
|
||||
/**
|
||||
* 当界面销毁时调用
|
||||
*/
|
||||
public destroy(){
|
||||
this._isDispose = true;
|
||||
if (this.parent)
|
||||
this.parent.removeChild(this);
|
||||
|
||||
if (this._destoryChildren){
|
||||
this._fuiView = null;
|
||||
while (this.numChildren > 0){
|
||||
this.removeChildAt(0);
|
||||
}
|
||||
this._destoryChildren = false;
|
||||
}
|
||||
}
|
||||
}
|
||||
104
demo/src/UI/mvc/EventManager.ts
Normal file
104
demo/src/UI/mvc/EventManager.ts
Normal file
@@ -0,0 +1,104 @@
|
||||
class EventManager {
|
||||
private static _single: EventManager;
|
||||
private _dict: any;
|
||||
private _eventList: EventVo[];
|
||||
|
||||
public static getInstance() {
|
||||
if (!this._single) this._single = new EventManager();
|
||||
return this._single;
|
||||
}
|
||||
|
||||
constructor() {
|
||||
this._dict = {};
|
||||
this._eventList = [];
|
||||
}
|
||||
|
||||
/**
|
||||
* 添加消息监听
|
||||
* @param type
|
||||
* @param listener
|
||||
* @param thisObj
|
||||
*/
|
||||
public addListener(type: string | symbol, listener: Function, thisObj: any): void {
|
||||
var arr: any[] = this._dict[type];
|
||||
if (arr == null) {
|
||||
arr = [];
|
||||
this._dict[type] = arr;
|
||||
}
|
||||
var i: number, len: number = arr.length;
|
||||
for (i = 0; i < len; i++) {
|
||||
if (arr[i][0] == listener && arr[i][1] == thisObj) return;
|
||||
}
|
||||
arr.push([listener, thisObj]);
|
||||
}
|
||||
|
||||
/**
|
||||
* 移除监听
|
||||
* @param type
|
||||
* @param listener
|
||||
* @param thisObj
|
||||
*/
|
||||
public removeListener(type: string | symbol, listener: Function, thisObj: any): void {
|
||||
var arr: any[] = this._dict[type];
|
||||
if (arr == null) return;
|
||||
|
||||
var i: number, len: number = Array.length;
|
||||
for (i = 0; i < len; i++) {
|
||||
if (arr[i][0] == listener && arr[i][1] == thisObj) {
|
||||
arr.splice(i, 1);
|
||||
i--;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (arr.length == 0) {
|
||||
delete this._dict[type];
|
||||
this._dict[type] = null;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 事件派发
|
||||
* @param type
|
||||
* @param param
|
||||
*/
|
||||
public dispatch(type: string | symbol, ...param: any[]): void {
|
||||
if (this._dict[type] == null) return;
|
||||
|
||||
var vo: EventVo = new EventVo();
|
||||
vo.type = type;
|
||||
vo.param = param;
|
||||
|
||||
this.dealMsg(vo);
|
||||
}
|
||||
|
||||
/**
|
||||
* 事件处理
|
||||
* @param vo
|
||||
*/
|
||||
private dealMsg(vo: EventVo): void {
|
||||
var listeners: Array<any> = this._dict[vo.type];
|
||||
if (listeners && listeners.length > 0) {
|
||||
var i: number = 0, len: number = listeners.length, listener: Array<any> = null;
|
||||
while (i < len) {
|
||||
listener = listeners[i];
|
||||
listener[0].apply(listener[1], vo.param);
|
||||
if (listeners.length != len) {
|
||||
len = listeners.length;
|
||||
i--;
|
||||
}
|
||||
i++;
|
||||
}
|
||||
}
|
||||
vo.dispose();
|
||||
}
|
||||
}
|
||||
|
||||
class EventVo {
|
||||
public type: string | symbol;
|
||||
public param: any[];
|
||||
public dispose() {
|
||||
this.type = null;
|
||||
this.param = null;
|
||||
}
|
||||
}
|
||||
389
demo/src/UI/mvc/Extension.ts
Normal file
389
demo/src/UI/mvc/Extension.ts
Normal file
@@ -0,0 +1,389 @@
|
||||
declare interface String {
|
||||
/**
|
||||
* 去除字符串头部空格或指定字符
|
||||
* @param c 指定字符(字符串)
|
||||
*/
|
||||
trimStart(c);
|
||||
/**
|
||||
* 去除字符串尾部空格或指定字符
|
||||
* @param c 指定字符(字符串)
|
||||
*/
|
||||
trimEnd(c);
|
||||
}
|
||||
|
||||
String.prototype.trimStart = function (c) {
|
||||
function trimStart(str, c) {
|
||||
let regExp;
|
||||
if (!c) {
|
||||
regExp = new RegExp(/^\s*/);
|
||||
} else {
|
||||
regExp = new RegExp("^" + c);
|
||||
}
|
||||
|
||||
return str.replace(regExp, "");
|
||||
}
|
||||
|
||||
return trimStart(this, c);
|
||||
};
|
||||
|
||||
String.prototype.trimEnd = function (c) {
|
||||
function trimEnd(str, c) {
|
||||
let regExp;
|
||||
if (!c) {
|
||||
regExp = new RegExp(/\s*$/);
|
||||
} else {
|
||||
regExp = new RegExp(c + "$");
|
||||
}
|
||||
|
||||
return str.replace(regExp, "");
|
||||
}
|
||||
|
||||
return trimEnd(this, c);
|
||||
};
|
||||
|
||||
declare interface Array<T> {
|
||||
/**
|
||||
* 获取满足表达式的数组元素索引
|
||||
* @param predicate 表达式
|
||||
*/
|
||||
findIndex(predicate: Function): number;
|
||||
/**
|
||||
* 是否存在满足表达式的数组元素
|
||||
* @param predicate 表达式
|
||||
*/
|
||||
any(predicate: Function): boolean;
|
||||
/**
|
||||
* 获取满足表达式的第一个或默认数组元素
|
||||
* @param predicate 表达式
|
||||
*/
|
||||
firstOrDefault(predicate: Function): T;
|
||||
/**
|
||||
* 获取满足表达式的第一个数组元素
|
||||
* @param predicate 表达式
|
||||
*/
|
||||
find(predicate: Function): T;
|
||||
/**
|
||||
* 筛选满足表达式的数组元素
|
||||
* @param predicate 表达式
|
||||
*/
|
||||
where(predicate: Function): Array<T>;
|
||||
/**
|
||||
* 获取满足表达式的数组元素的计数
|
||||
* @param predicate 表达式
|
||||
*/
|
||||
count(predicate: Function): number;
|
||||
/**
|
||||
* 获取满足表达式的数组元素的数组
|
||||
* @param predicate 表达式
|
||||
*/
|
||||
findAll(predicate: Function): Array<T>;
|
||||
/**
|
||||
* 移除满足表达式的数组元素
|
||||
* @param predicate 表达式
|
||||
*/
|
||||
removeAll(predicate: Function): void;
|
||||
/**
|
||||
* 移除数组元素
|
||||
* @param element 数组元素
|
||||
*/
|
||||
remove(element): boolean;
|
||||
/**
|
||||
* 移除特定索引数组元素
|
||||
* @param index 索引
|
||||
*/
|
||||
removeAt(index): void;
|
||||
/**
|
||||
* 移除范围数组元素
|
||||
* @param index 开始索引
|
||||
* @param count 删除的个数
|
||||
*/
|
||||
removeRange(index, count): void;
|
||||
/**
|
||||
* 获取通过选择器转换的数组
|
||||
* @param selector 选择器
|
||||
*/
|
||||
select(selector: Function): Array<T>;
|
||||
/**
|
||||
* 排序(升序)
|
||||
* @param keySelector key选择器
|
||||
* @param comparer 比较器
|
||||
*/
|
||||
orderBy(keySelector: Function, comparer: Function): Array<T>;
|
||||
/**
|
||||
* 排序(降序)
|
||||
* @param keySelector key选择器
|
||||
* @param comparer 比较器
|
||||
*/
|
||||
orderByDescending(keySelector: Function, comparer: Function): Array<T>;
|
||||
/**
|
||||
* 分组
|
||||
* @param keySelector key选择器
|
||||
*/
|
||||
groupBy(keySelector: Function): Array<T>;
|
||||
/**
|
||||
* 求和
|
||||
* @param selector 选择器
|
||||
*/
|
||||
sum(selector);
|
||||
}
|
||||
|
||||
Array.prototype.findIndex = function (predicate) {
|
||||
function findIndex(array, predicate) {
|
||||
for (let i = 0, len = array.length; i < len; i++) {
|
||||
if (predicate.call(arguments[2], array[i], i, array)) {
|
||||
return i;
|
||||
}
|
||||
}
|
||||
|
||||
return -1;
|
||||
}
|
||||
|
||||
return findIndex(this, predicate);
|
||||
};
|
||||
|
||||
Array.prototype.any = function (predicate) {
|
||||
function any(array, predicate) {
|
||||
return array.findIndex(predicate) > -1;
|
||||
}
|
||||
|
||||
return any(this, predicate);
|
||||
};
|
||||
|
||||
Array.prototype.firstOrDefault = function (predicate) {
|
||||
function firstOrDefault(array, predicate) {
|
||||
let index = array.findIndex(predicate);
|
||||
return index == -1 ? null : array[index];
|
||||
}
|
||||
|
||||
return firstOrDefault(this, predicate);
|
||||
};
|
||||
|
||||
Array.prototype.find = function (predicate) {
|
||||
function find(array, predicate) {
|
||||
return array.firstOrDefault(predicate);
|
||||
}
|
||||
|
||||
return find(this, predicate);
|
||||
};
|
||||
|
||||
Array.prototype.where = function (predicate) {
|
||||
function where(array, predicate) {
|
||||
if (typeof array.reduce === "function") {
|
||||
return array.reduce(function (ret, element, index) {
|
||||
if (predicate.call(arguments[2], element, index, array)) {
|
||||
ret.push(element);
|
||||
}
|
||||
|
||||
return ret;
|
||||
}, []);
|
||||
} else {
|
||||
let ret = [];
|
||||
for (let i = 0, len = array.length; i < len; i++) {
|
||||
let element = array[i];
|
||||
if (predicate.call(arguments[2], element, i, array)) {
|
||||
ret.push(element);
|
||||
}
|
||||
}
|
||||
|
||||
return ret;
|
||||
}
|
||||
}
|
||||
|
||||
return where(this, predicate);
|
||||
};
|
||||
|
||||
Array.prototype.count = function (predicate) {
|
||||
function count(array, predicate) {
|
||||
return array.where(predicate).length;
|
||||
}
|
||||
|
||||
return count(this, predicate);
|
||||
};
|
||||
|
||||
Array.prototype.findAll = function (predicate) {
|
||||
function findAll(array, predicate) {
|
||||
return array.where(predicate);
|
||||
}
|
||||
|
||||
return findAll(this, predicate);
|
||||
};
|
||||
|
||||
Array.prototype.removeAll = function (predicate) {
|
||||
function removeAll(array, predicate) {
|
||||
let index;
|
||||
do {
|
||||
index = array.findIndex(predicate);
|
||||
if (index >= 0) {
|
||||
array.splice(index, 1);
|
||||
}
|
||||
} while (index >= 0);
|
||||
}
|
||||
|
||||
removeAll(this, predicate);
|
||||
};
|
||||
|
||||
Array.prototype.remove = function (element) {
|
||||
function remove(array, element) {
|
||||
let index = array.findIndex(function (x) {
|
||||
return x === element;
|
||||
});
|
||||
|
||||
if (index >= 0) {
|
||||
array.splice(index, 1);
|
||||
return true;
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
return remove(this, element);
|
||||
};
|
||||
|
||||
Array.prototype.removeAt = function (index) {
|
||||
function removeAt(array, index) {
|
||||
array.splice(index, 1);
|
||||
}
|
||||
|
||||
return removeAt(this, index);
|
||||
};
|
||||
|
||||
Array.prototype.removeRange = function (index, count) {
|
||||
function removeRange(array, index, count) {
|
||||
array.splice(index, count);
|
||||
}
|
||||
|
||||
return removeRange(this, index, count);
|
||||
};
|
||||
|
||||
Array.prototype.select = function (selector) {
|
||||
function select(array, selector) {
|
||||
if (typeof array.reduce === "function") {
|
||||
return array.reduce(function (ret, element, index) {
|
||||
ret.push(selector.call(arguments[2], element, index, array));
|
||||
return ret;
|
||||
}, []);
|
||||
} else {
|
||||
let ret = [];
|
||||
for (let i = 0, len = array.length; i < len; i++) {
|
||||
ret.push(selector.call(arguments[2], array[i], i, array));
|
||||
}
|
||||
|
||||
return ret;
|
||||
}
|
||||
}
|
||||
|
||||
return select(this, selector);
|
||||
};
|
||||
|
||||
Array.prototype.orderBy = function (keySelector, comparer) {
|
||||
function orderBy(array, keySelector, comparer) {
|
||||
array.sort(function (x, y) {
|
||||
let v1 = keySelector(x);
|
||||
let v2 = keySelector(y);
|
||||
if (comparer) {
|
||||
return comparer(v1, v2);
|
||||
} else {
|
||||
return v1 > v2 ? 1 : -1;
|
||||
}
|
||||
});
|
||||
|
||||
return array;
|
||||
}
|
||||
|
||||
return orderBy(this, keySelector, comparer);
|
||||
};
|
||||
|
||||
Array.prototype.orderByDescending = function (keySelector, comparer) {
|
||||
function orderByDescending(array, keySelector, comparer) {
|
||||
array.sort(function (x, y) {
|
||||
let v1 = keySelector(x);
|
||||
let v2 = keySelector(y);
|
||||
if (comparer) {
|
||||
return -comparer(v1, v2);
|
||||
} else {
|
||||
return v1 < v2 ? 1 : -1;
|
||||
}
|
||||
});
|
||||
|
||||
return array;
|
||||
}
|
||||
|
||||
return orderByDescending(this, keySelector, comparer);
|
||||
};
|
||||
|
||||
Array.prototype.groupBy = function (keySelector) {
|
||||
function groupBy(array, keySelector) {
|
||||
if (typeof array.reduce === "function") {
|
||||
let keys = [];
|
||||
return array.reduce(function (groups, element, index) {
|
||||
let key = JSON.stringify(
|
||||
keySelector.call(arguments[1], element, index, array)
|
||||
);
|
||||
let index2 = keys.findIndex(function (x) {
|
||||
return x === key;
|
||||
});
|
||||
|
||||
if (index2 < 0) {
|
||||
index2 = keys.push(key) - 1;
|
||||
}
|
||||
|
||||
if (!groups[index2]) {
|
||||
groups[index2] = [];
|
||||
}
|
||||
|
||||
groups[index2].push(element);
|
||||
return groups;
|
||||
}, []);
|
||||
} else {
|
||||
let groups = [];
|
||||
let keys = [];
|
||||
for (let i = 0, len = array.length; i < len; i++) {
|
||||
let key = JSON.stringify(
|
||||
keySelector.call(arguments[1], array[i], i, array)
|
||||
);
|
||||
let index = keys.findIndex(function (x) {
|
||||
return x === key;
|
||||
});
|
||||
|
||||
if (index < 0) {
|
||||
index = keys.push(key) - 1;
|
||||
}
|
||||
|
||||
if (!groups[index]) {
|
||||
groups[index] = [];
|
||||
}
|
||||
|
||||
groups[index].push(array[i]);
|
||||
}
|
||||
|
||||
return groups;
|
||||
}
|
||||
}
|
||||
|
||||
return groupBy(this, keySelector);
|
||||
};
|
||||
|
||||
Array.prototype.sum = function (selector) {
|
||||
function sum(array, selector) {
|
||||
let ret;
|
||||
for (let i = 0, len = array.length; i < len; i++) {
|
||||
if (i == 0) {
|
||||
if (selector) {
|
||||
ret = selector.call(arguments[2], array[i], i, array);
|
||||
} else {
|
||||
ret = array[i];
|
||||
}
|
||||
} else {
|
||||
if (selector) {
|
||||
ret += selector.call(arguments[2], array[i], i, array);
|
||||
} else {
|
||||
ret += array[i];
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
return sum(this, selector);
|
||||
};
|
||||
25
demo/src/UI/mvc/FguiUtils.ts
Normal file
25
demo/src/UI/mvc/FguiUtils.ts
Normal file
@@ -0,0 +1,25 @@
|
||||
class FguiUtils {
|
||||
/** 包的命名空间 */
|
||||
public static packageNamespace: any;
|
||||
/** 加载fgui资源 */
|
||||
public static load(name: string): Promise<any> {
|
||||
return new Promise((resolve, reject) => {
|
||||
let existPkg = fairygui.UIPackage.getByName(name);
|
||||
if (existPkg) {
|
||||
resolve();
|
||||
}
|
||||
|
||||
RES.loadGroup(name, 0).then(()=>{
|
||||
fairygui.UIPackage.addPackage(name);
|
||||
if (this.packageNamespace[name][name + "Binder"])
|
||||
this.packageNamespace[name][name + "Binder"].bindAll();
|
||||
|
||||
resolve();
|
||||
}).catch(err => {
|
||||
console.error("loadfgui error:" + err);
|
||||
reject();
|
||||
});
|
||||
});
|
||||
|
||||
}
|
||||
}
|
||||
46
demo/src/UI/mvc/LayerManager.ts
Normal file
46
demo/src/UI/mvc/LayerManager.ts
Normal file
@@ -0,0 +1,46 @@
|
||||
/**
|
||||
* 视图类型
|
||||
*/
|
||||
enum viewLayer {
|
||||
pop,
|
||||
}
|
||||
|
||||
/**
|
||||
* 视图管理器
|
||||
*/
|
||||
class LayerManager {
|
||||
private static _single: LayerManager;
|
||||
public static getInstance(): LayerManager{
|
||||
if (!this._single) this._single = new LayerManager();
|
||||
|
||||
return this._single;
|
||||
}
|
||||
|
||||
private _stage: egret.Stage;
|
||||
/** 弹窗层 */
|
||||
public popLayer: egret.DisplayObjectContainer;
|
||||
public tipsLayer: egret.DisplayObjectContainer;
|
||||
/** 获取舞台 */
|
||||
public get stage(){
|
||||
return this._stage;
|
||||
}
|
||||
|
||||
public init(stage: egret.Stage){
|
||||
this._stage = stage;
|
||||
|
||||
this.popLayer = new egret.DisplayObjectContainer();
|
||||
this.tipsLayer = new egret.DisplayObjectContainer();
|
||||
|
||||
this._stage.addChild(this.popLayer);
|
||||
this._stage.addChild(this.tipsLayer);
|
||||
}
|
||||
|
||||
/**
|
||||
* 添加到layer上
|
||||
* @param view
|
||||
*/
|
||||
public addLayerToView(view: BaseView){
|
||||
let layerName = viewLayer[view.viewLayer] + "Layer";
|
||||
this[layerName].addChild(view);
|
||||
}
|
||||
}
|
||||
125
demo/src/UI/mvc/ViewManager.ts
Normal file
125
demo/src/UI/mvc/ViewManager.ts
Normal file
@@ -0,0 +1,125 @@
|
||||
/** 窗口控制器 */
|
||||
class ViewManager {
|
||||
private static _single: ViewManager;
|
||||
/** 已打开的列表 存储视图实例 */
|
||||
private _openDic: BaseView[] = [];
|
||||
/** 跳过关闭的列表 用于主视图 存储视图类型 */
|
||||
private _skipCloseDic = [];
|
||||
/** 层级管理器 */
|
||||
private _layerManager: LayerManager;
|
||||
|
||||
public static getInstance(): ViewManager{
|
||||
if (!this._single) this._single = new ViewManager();
|
||||
|
||||
return this._single;
|
||||
}
|
||||
|
||||
constructor(){
|
||||
this._layerManager = LayerManager.getInstance();
|
||||
}
|
||||
|
||||
/**
|
||||
* 打开界面
|
||||
* @param viewCls 视图类型
|
||||
* @param viewData 视图数据
|
||||
*/
|
||||
public openView(viewCls, viewData?: any): Promise<any>{
|
||||
return new Promise((resolve, reject)=>{
|
||||
let newView: BaseView = this.getView<BaseView>(viewCls);
|
||||
if (!newView) newView = new viewCls();
|
||||
|
||||
if (this.isOpenView(viewCls)){
|
||||
newView.refresh(viewData);
|
||||
return;
|
||||
}
|
||||
|
||||
this._openDic.push(newView);
|
||||
FguiUtils.load(newView.name).then(()=>{
|
||||
newView.init();
|
||||
newView.baseShow(viewData);
|
||||
newView.show();
|
||||
|
||||
this._layerManager.addLayerToView(newView);
|
||||
resolve();
|
||||
}).catch(()=>{
|
||||
this._openDic.remove(newView);
|
||||
newView.destroy();
|
||||
newView = null;
|
||||
reject();
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* 根据视图关闭
|
||||
* @param view 视图实例
|
||||
*/
|
||||
public closeView(view: BaseView){
|
||||
let wantToCloseView = this._openDic.firstOrDefault(a => {
|
||||
return a == view;
|
||||
})
|
||||
if (wantToCloseView) {
|
||||
this._openDic.remove(wantToCloseView);
|
||||
|
||||
wantToCloseView.destroy();
|
||||
wantToCloseView = null;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 关闭所有视图
|
||||
*/
|
||||
public closeAll(){
|
||||
for (let z = 0; z < this._openDic.length; z++) {
|
||||
let element = this._openDic[z];
|
||||
let canClose = true;
|
||||
for (let i = 0; i < this._skipCloseDic.length; i ++){
|
||||
if (element == this.getView(this._skipCloseDic[i])){
|
||||
canClose = false;
|
||||
}
|
||||
}
|
||||
if (canClose) {
|
||||
this.closeView(element);
|
||||
z--;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 根据视图类型关闭窗口
|
||||
* @param viewCls 视图类型
|
||||
*/
|
||||
public closeViewByCls(viewCls){
|
||||
let wantToCloseView = this._openDic.firstOrDefault(a => {
|
||||
return a instanceof viewCls;
|
||||
})
|
||||
if (wantToCloseView) {
|
||||
this._openDic.remove(wantToCloseView);
|
||||
wantToCloseView.destroy();
|
||||
wantToCloseView = null;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 根据视图类型获得窗口
|
||||
* 如果窗口未打开则返回null
|
||||
* @param viewCls 视图类型
|
||||
*/
|
||||
public getView<T>(viewCls: new () => T): T{
|
||||
let result: any = this._openDic.firstOrDefault(a => {
|
||||
return a instanceof viewCls;
|
||||
});
|
||||
|
||||
return result as T;
|
||||
}
|
||||
|
||||
/**
|
||||
* 根据视图类型获得窗口是否打开
|
||||
* @param viewCls
|
||||
*/
|
||||
public isOpenView(viewCls: any): boolean {
|
||||
return this._openDic.findIndex(a => {
|
||||
return a instanceof viewCls;
|
||||
}) != -1;
|
||||
}
|
||||
}
|
||||
27
demo/src/UI/sc/ScControl.ts
Normal file
27
demo/src/UI/sc/ScControl.ts
Normal file
@@ -0,0 +1,27 @@
|
||||
module sc {
|
||||
export class ScControl {
|
||||
private _eventManager: EventManager;
|
||||
private _viewManager: ViewManager;
|
||||
private static single: ScControl;
|
||||
public static getInstance(){
|
||||
if (!this.single) this.single = new ScControl();
|
||||
|
||||
return this.single;
|
||||
}
|
||||
|
||||
constructor(){
|
||||
this.addEvents();
|
||||
}
|
||||
|
||||
private addEvents(){
|
||||
this._eventManager = EventManager.getInstance();
|
||||
this._viewManager = ViewManager.getInstance();
|
||||
// 事件监听
|
||||
this._eventManager.addListener(ScEvents.OPENVIEW, this.openView, this);
|
||||
}
|
||||
|
||||
private openView(){
|
||||
this._viewManager.openView(ScView);
|
||||
}
|
||||
}
|
||||
}
|
||||
5
demo/src/UI/sc/ScEvents.ts
Normal file
5
demo/src/UI/sc/ScEvents.ts
Normal file
@@ -0,0 +1,5 @@
|
||||
module sc {
|
||||
export module ScEvents {
|
||||
export const OPENVIEW = Symbol();
|
||||
}
|
||||
}
|
||||
55
demo/src/UI/sc/ScView.ts
Normal file
55
demo/src/UI/sc/ScView.ts
Normal file
@@ -0,0 +1,55 @@
|
||||
module sc {
|
||||
export class ScView extends BaseView {
|
||||
private _ui: FUI.sc.UI_View_sc;
|
||||
private _sceneList: SceneData[] = [
|
||||
new SceneData("空白场景", samples.BasicScene),
|
||||
new SceneData("Tiled Tiles", samples.AnimatedTilesScene),
|
||||
];
|
||||
|
||||
constructor() {
|
||||
super("sc");
|
||||
}
|
||||
|
||||
public init() {
|
||||
this._ui = this._fuiView = FUI.sc.UI_View_sc.createInstance();
|
||||
this.addChild(this._ui.displayObject);
|
||||
super.init();
|
||||
}
|
||||
|
||||
public show() {
|
||||
this._ui.m_list_sc.callbackThisObj = this;
|
||||
this._ui.m_list_sc.itemRenderer = this.scItemRender;
|
||||
this._ui.m_list_sc.numItems = this._sceneList.length;
|
||||
}
|
||||
|
||||
public scItemRender(index: number, item: FUI.sc.UI_btn_sc){
|
||||
let sceneData = this._sceneList[index];
|
||||
item.m_name.text = sceneData.name;
|
||||
item.data = sceneData.type;
|
||||
item.addClickListener(this.scItemOnClick, this);
|
||||
}
|
||||
|
||||
private scItemOnClick(evt: egret.Event){
|
||||
let data = evt.currentTarget.data;
|
||||
es.Core.scene = new data();
|
||||
}
|
||||
|
||||
public destroy() {
|
||||
if (this._ui) {
|
||||
this._ui.dispose();
|
||||
this._ui = null;
|
||||
}
|
||||
super.destroy();
|
||||
}
|
||||
}
|
||||
|
||||
export class SceneData {
|
||||
public name: string;
|
||||
public type: any;
|
||||
|
||||
constructor(name: string, type: any){
|
||||
this.name = name;
|
||||
this.type = type;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,102 +0,0 @@
|
||||
module scene {
|
||||
export class MainScene extends es.Scene {
|
||||
constructor() {
|
||||
super();
|
||||
|
||||
// this.addEntityProcessor(new SpawnerSystem(new Matcher()));
|
||||
this.astarTest();
|
||||
this.dijkstraTest();
|
||||
this.breadthfirstTest();
|
||||
}
|
||||
|
||||
public async onStart() {
|
||||
let mapData = await es.TiledMapLoader.loadTmxMap(new es.TmxMap(), "title_json");
|
||||
let map = this.createEntity("map");
|
||||
let tiledMap = map.addComponent(new es.TiledMapRenderer(mapData)).setRenderLayer(1);
|
||||
console.log(mapData);
|
||||
|
||||
// let sprite = new es.Sprite(RES.getRes("checkbox_select_disabled_png"));
|
||||
// this.createEntityAsync("bg").then(bg => {
|
||||
// bg.addComponent(new component.PlayerController());
|
||||
// bg.addComponent(new es.Mover());
|
||||
// let spriteRenderer = bg.addComponent(new es.ScrollingSpriteRenderer(sprite)).setRenderLayer(0);
|
||||
// // spriteRenderer.scrollX = -30;
|
||||
// // bg.addComponent(new es.BoxCollider());
|
||||
|
||||
// this.camera.follow(bg, es.CameraStyle.lockOn);
|
||||
// });
|
||||
|
||||
this.camera.position = new es.Vector2(300, 265);
|
||||
es.Core.debugRenderEndabled = true;
|
||||
this.scaleX = 0.5;
|
||||
this.scaleY = 0.5;
|
||||
// // bg.addComponent(new es.SpriteRenderer()).setSprite(sprite).setColor(0xff0000);
|
||||
|
||||
// for (let i = 0; i < 20; i++) {
|
||||
// let sprite = new es.Sprite(RES.getRes("checkbox_select_disabled_png"));
|
||||
// let player2 = this.createEntity("bg");
|
||||
// player2.addComponent(new es.SpriteRenderer()).setSprite(sprite).setRenderLayer(i);
|
||||
// player2.position = new es.Vector2(30 * i, 30 * i);
|
||||
// // player2.addComponent(new es.BoxCollider());
|
||||
// }
|
||||
|
||||
|
||||
let pool = new es.ComponentPool<component.SimplePooled>(component.SimplePooled);
|
||||
let c1 = pool.obtain();
|
||||
let c2 = pool.obtain();
|
||||
pool.free(c1);
|
||||
let c1b = pool.obtain();
|
||||
|
||||
console.log(c1 != c2);
|
||||
console.log(c1 == c1b);
|
||||
|
||||
let button = new eui.Button();
|
||||
button.label = "切换场景";
|
||||
this.addChild(button);
|
||||
button.addEventListener(egret.TouchEvent.TOUCH_TAP, () => {
|
||||
es.Core.startSceneTransition(new es.FadeTransition(() => {
|
||||
return new MainScene();
|
||||
}));
|
||||
}, this);
|
||||
}
|
||||
|
||||
public breadthfirstTest() {
|
||||
let graph = new es.UnweightedGraph<string>();
|
||||
|
||||
graph.addEdgesForNode("a", ["b"]); // a->b
|
||||
graph.addEdgesForNode("b", ["a", "c", "d"]); // b->a b->c b->d
|
||||
graph.addEdgesForNode("c", ["a"]); // c->a
|
||||
graph.addEdgesForNode("d", ["e", "a"]); // d->e d->a
|
||||
graph.addEdgesForNode("e", ["b"]); // e->b
|
||||
|
||||
// 计算从c到e的路径
|
||||
let path = es.BreadthFirstPathfinder.search(graph, "c", "e");
|
||||
console.log(path);
|
||||
}
|
||||
|
||||
public dijkstraTest() {
|
||||
let graph = new es.WeightedGridGraph(20, 20);
|
||||
|
||||
graph.weightedNodes.push(new es.Vector2(3, 3));
|
||||
graph.weightedNodes.push(new es.Vector2(3, 4));
|
||||
graph.weightedNodes.push(new es.Vector2(4, 3));
|
||||
graph.weightedNodes.push(new es.Vector2(4, 4));
|
||||
|
||||
let path = graph.search(new es.Vector2(3, 4), new es.Vector2(15, 17));
|
||||
console.log(path);
|
||||
}
|
||||
|
||||
public astarTest() {
|
||||
let graph = new es.AstarGridGraph(30, 30);
|
||||
|
||||
// graph.weightedNodes.push(new Vector2(3, 3));
|
||||
// graph.weightedNodes.push(new Vector2(3, 4));
|
||||
// graph.weightedNodes.push(new Vector2(4, 3));
|
||||
// graph.weightedNodes.push(new Vector2(4, 4));
|
||||
|
||||
let startTime = egret.getTimer();
|
||||
let path = graph.search(new es.Vector2(1, 1), new es.Vector2(29, 29));
|
||||
console.log(egret.getTimer() - startTime);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,66 +0,0 @@
|
||||
module component {
|
||||
import Component = es.Component;
|
||||
import Vector2 = es.Vector2;
|
||||
import Mover = es.Mover;
|
||||
import SpriteRenderer = es.SpriteRenderer;
|
||||
import Time = es.Time;
|
||||
import Input = es.Input;
|
||||
import CollisionResult = es.CollisionResult;
|
||||
|
||||
export class PlayerController extends Component {
|
||||
private down: boolean = false;
|
||||
private touchPoint: Vector2 = Vector2.zero;
|
||||
private mover: Mover;
|
||||
private spriteRenderer: SpriteRenderer;
|
||||
|
||||
public onAddedToEntity(){
|
||||
this.entity.scene.stage.addEventListener(egret.TouchEvent.TOUCH_BEGIN, this.touchBegin, this);
|
||||
this.entity.scene.stage.addEventListener(egret.TouchEvent.TOUCH_MOVE, this.touchBegin, this);
|
||||
this.entity.scene.stage.addEventListener(egret.TouchEvent.TOUCH_END, this.touchEnd, this);
|
||||
}
|
||||
|
||||
private touchBegin(evt: egret.TouchEvent){
|
||||
this.down = true;
|
||||
this.touchPoint = new Vector2(evt.stageX, evt.stageY);
|
||||
}
|
||||
|
||||
private touchEnd(evt: egret.TouchEvent){
|
||||
this.down = false;
|
||||
this.touchPoint = new Vector2(evt.stageX, evt.stageY);
|
||||
}
|
||||
|
||||
public update(){
|
||||
if (!this.mover)
|
||||
this.mover = this.entity.getComponent<Mover>(Mover);
|
||||
|
||||
if (!this.spriteRenderer)
|
||||
this.spriteRenderer = this.entity.getComponent<SpriteRenderer>(SpriteRenderer);
|
||||
|
||||
if (!this.mover)
|
||||
return;
|
||||
|
||||
if (!SpriteRenderer)
|
||||
return;
|
||||
|
||||
if (this.down){
|
||||
let moveLeft: number = 0;
|
||||
let moveRight: number = 0;
|
||||
let speed = 100;
|
||||
let worldPos = this.entity.scene.camera.mouseToWorldPoint();
|
||||
if (worldPos.x < this.spriteRenderer.transform.position.x){
|
||||
moveLeft = -1;
|
||||
} else if(worldPos.x > this.spriteRenderer.transform.position.x){
|
||||
moveLeft = 1;
|
||||
}
|
||||
|
||||
if (worldPos.y < this.spriteRenderer.transform.position.y){
|
||||
moveRight = -1;
|
||||
} else if(worldPos.y > this.spriteRenderer.transform.position.y){
|
||||
moveRight = 1;
|
||||
}
|
||||
let collisionResult = new CollisionResult();
|
||||
this.mover.move(new Vector2(moveLeft * speed * Time.deltaTime, moveRight * speed * Time.deltaTime), collisionResult);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,9 +0,0 @@
|
||||
module component {
|
||||
import PooledComponent = es.PooledComponent;
|
||||
|
||||
export class SimplePooled extends PooledComponent {
|
||||
public reset(){
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,37 +0,0 @@
|
||||
module component {
|
||||
export class SpawnComponent extends es.Component implements es.ITriggerListener {
|
||||
public cooldown = -1;
|
||||
public minInterval = 2;
|
||||
public maxInterval = 60;
|
||||
public enemyType = EnemyType.worm;
|
||||
public numSpawned = 0;
|
||||
public numAlive = 0;
|
||||
|
||||
constructor(enemyType: EnemyType) {
|
||||
super();
|
||||
this.enemyType = enemyType;
|
||||
}
|
||||
|
||||
public initialize() {
|
||||
// console.log("initialize");
|
||||
}
|
||||
|
||||
public update() {
|
||||
// console.log("update");
|
||||
}
|
||||
|
||||
public onTriggerEnter(other: es.Collider, local: es.Collider){
|
||||
if (other == local)
|
||||
console.log("repeat collider");
|
||||
console.log("enter collider");
|
||||
}
|
||||
|
||||
public onTriggerExit(other: es.Collider, local: es.Collider){
|
||||
console.log("exit collider");
|
||||
}
|
||||
}
|
||||
|
||||
export enum EnemyType {
|
||||
worm
|
||||
}
|
||||
}
|
||||
@@ -1,36 +0,0 @@
|
||||
module system {
|
||||
export class SpawnerSystem extends es.EntityProcessingSystem {
|
||||
constructor(matcher: es.Matcher){
|
||||
super(matcher);
|
||||
}
|
||||
|
||||
public processEntity(entity: es.Entity){
|
||||
let spawner = entity.getComponent<component.SpawnComponent>(component.SpawnComponent);
|
||||
if (!spawner)
|
||||
return;
|
||||
|
||||
if (spawner.numAlive <= 0)
|
||||
spawner.enabled = true;
|
||||
|
||||
if (!spawner.enabled)
|
||||
return;
|
||||
|
||||
console.log("cooldown", spawner.cooldown);
|
||||
if (spawner.cooldown == -1){
|
||||
spawner.cooldown = Math.random() * 60;
|
||||
spawner.cooldown /= 4;
|
||||
}
|
||||
|
||||
spawner.cooldown -= es.Time.deltaTime;
|
||||
if (spawner.cooldown <= 0){
|
||||
spawner.cooldown = Math.random() * 60;
|
||||
// CreateEnemy
|
||||
spawner.numSpawned ++;
|
||||
spawner.numAlive ++;
|
||||
|
||||
if (spawner.numAlive > 0)
|
||||
spawner.enabled = false;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user