Commit 977e31bb authored by rockyl's avatar rockyl

去除原有的Component的逻辑代码

修改过程属性的链接方式
parent 672906ea
This source diff could not be displayed because it is too large. You can view the blob instead.
This source diff could not be displayed because it is too large. You can view the blob instead.
This source diff could not be displayed because it is too large. You can view the blob instead.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
/**
* Created by rockyl on 2019-11-07.
*/
declare const engine: any;
export default class ZoomButton {
static id = 'zoom-button';
host;
zoomTo = 1.1;
mounted() {
this.host.anchorX = this.host.width / 2;
this.host.anchorY = this.host.height / 2;
this.host.addEventListener(engine.MouseEvent.MOUSE_DOWN, this._onMouseDown, this);
this.host.addEventListener(engine.MouseEvent.MOUSE_UP, this._onMouseUp, this);
}
_onMouseDown(e) {
this.host.scaleX = this.host.scaleY = this.zoomTo;
}
_onMouseUp(e) {
this.host.scaleX = this.host.scaleY = 1;
}
}
......@@ -35,7 +35,7 @@
<body>
<script type="text/javascript" src="./build/render.min.js"></script>
<script type="text/javascript" src="./dist/index.js"></script>
<div id="cusEngine" style="line-height:0;font-size:0"></div>
<script>
......
......@@ -5,26 +5,32 @@
"main": "index.js",
"types": "index.d.ts",
"dependencies": {
"glob": "^7.1.6",
"rollup-plugin-typescript": "^1.0.1"
},
"devDependencies": {
"dts-bundle": "^0.7.3",
"protobufjs": "^6.8.0",
"rollup-plugin-commonjs": "^10.1.0",
"rollup-plugin-node-resolve": "^5.2.0",
"rollup-plugin-progress": "^1.1.1",
"rollup-plugin-typescript": "^1.0.1",
"rollup-plugin-typescript2": "^0.25.2",
"rollup-plugin-uglify": "^6.0.3",
"ts-loader": "^4.0.0",
"typescript": "^2.7.2",
"uglifyjs-webpack-plugin": "^2.1.2",
"webpack": "^4.1.0",
"webpack-cli": "^3.3.2",
"webpack-dev-server": "^3.1.0"
},
"devDependencies": {
"protobufjs": "^6.8.0",
"webpack-cli": "^3.3.2"
},
"scripts": {
"build": "webpack",
"rollup": "rollup -c",
"ts": "dts-bundle --name engine --main types/src/index.d.ts --out ../../dist/index.d.ts",
"test": "echo \"Error: no test specified\" && exit 1",
"dev": "webpack -w",
"watch": "webpack --watch"
"watch": "webpack --watch",
"mergeDts": "node scripts/mergeDts.js"
},
"author": "",
"license": "ISC"
......
......@@ -23,7 +23,9 @@ export default {
resolve({
//only: [/^\.{0,2}\//],
}),
typescript(),
typescript({
//useTsconfigDeclarationDir: true,
}),
commonjs(),
//uglify({}),
],
......
/**
* Created by rockyl on 2019-11-16.
*/
const fs = require('fs');
const glob = require("glob");
const regLine = /(export|declare)((?!from).)*/g;
glob('types/src/**/*.d.ts', function (err, files) {
const exports = [];
for (let file of files) {
const fileContent = fs.readFileSync(file, 'utf-8');
const result = fileContent.match(regLine);
for (let line of result) {
if(line.match(/export (default)? \w+;/)){
continue;
}
if(line.endsWith(';')){
if(!line.startsWith('_') && !line.startsWith('export default function')){
exports.push(line);
}
}else {
if(line.endsWith('{')){
let start = fileContent.indexOf(line);
const block = fileContent.substring(start, fileContent.indexOf('\n}', start) + 2);
if(!block.startsWith('_')){
exports.push(block);
}
}
}
}
}
let allExports = exports.join('\n\n')
.replace(/export default _default;/g, '')
.replace(/export declare/g, 'export ')
.replace(/export default/g, 'export ')
.replace(/declare /g, 'export ')
;
const content = `
declare const args: any;
declare const props: any;
declare const target: engine.Container;
declare const global: any;
declare function next(type: string, payload?: any);
declare module engine{
${allExports}
}
`;
fs.writeFileSync('dist/types.d.ts', content);
});
/**
* Created by rockyl on 2018/11/5.
*/
import {HashObject} from "../HashObject";
import {DisplayObject} from "../display/DisplayObject";
/**
* 组件基类
*/
export class Component extends HashObject {
/**
* 所依附的显示对象
*/
entity: DisplayObject;
/**
* 是否有效
*/
protected _enabled: boolean
constructor() {
super();
this._instanceType = "Component";
this.onCreate();
}
/**
* 是否有效状态
*/
get enabled(): boolean {
return this._enabled;
}
set enabled(value: boolean) {
if (this._enabled !== value) {
this._enabled = value;
if (this._enabled) {
this.onEnable();
} else {
this.onDisable();
}
}
}
/**
* 装配实体
* @param entity
*/
_setup(entity: DisplayObject) {
this.entity = entity;
}
/**
* 卸载实体
*/
_unSetup() {
this.entity = null;
}
/**
* 当组件被创建时
*/
onCreate() {
}
/**
* 当组件生效时
*/
onEnable() {
}
/**
* 当组件失效时
*/
onDisable() {
}
/**
* 更新
*/
onUpdate() {
}
/**
* 当组件被销毁时
*/
onDestroy() {
}
destroy() {
this.onDestroy();
}
}
......@@ -3,7 +3,6 @@ import Transform from '../math/Transform';
import { Rectangle } from '../math/Rectangle';
import { Point } from "../math/Point";
import { Event } from "../events/Event";
import { Component } from '../component/Component';
import Graphics from '../graphics/Graphics';
import { DEG_TO_RAD, RAD_TO_DEG } from '../const';
......@@ -605,89 +604,10 @@ export class DisplayObject extends EventDispatcher {
this._height = value;
}
/**
* 组件数组
*/
private _components: Component[] = [];
/**
* 更新方法,帧循环的监听事件放在这
*/
public update(deltaTime: number) {
//更新组件方法
for (var i = 0; i < this._components.length; i++) {
if (this._components[i].enabled) {
this._components[i].onUpdate()
}
}
//监听的
if (this.hasEventListener(Event.ENTER_FRAME)) {
this.dispatchEvent(Event.ENTER_FRAME, deltaTime);
}
}
//组件相关方法,添加移除等
/**
* 增加组件
* @param component
*/
addComponent(component: Component) {
this.onAddComponent(component);
this._components.push(component);
}
/**
* 在指定索引增加组件,重复添加可作为顺序交换
* @param component
* @param index
*/
addComponentAt(component: Component, index: number) {
const currentIndex = this._components.indexOf(component);
if (currentIndex == index) {
return;
}
if (currentIndex >= 0) {
this._components.splice(currentIndex, 1);
}
this._components.splice(index, 0, component);
this.onAddComponent(component);
}
/**
* 移除组件
* @param component
*/
removeComponent(component: Component) {
this.onRemoveComponent(component);
const index = this._components.indexOf(component);
if (index >= 0) {
this._components.splice(index, 1);
}
}
/**
* 移除所有组件
*/
removeAllComponents() {
while (this._components.length > 0) {
this.removeComponent(this._components[0]);
}
}
/**
* 当添加组件时
* @param component
*/
onAddComponent(component: Component) {
component._setup(this);
component.enabled = true;
}
/**
* 当移除组件时
* @param component
*/
onRemoveComponent(component: Component) {
component._unSetup();
component.enabled = false;
}
}
/**
......
......@@ -52,7 +52,7 @@ export class Loader extends EventDispatcher {
}, url)
}
loadRaw(callback: Function, url: string, type: 'text'|'json') {
httpRequest(callback: Function, url: string, method: string = 'get',type: 'text'|'json' = 'text'){
//每次都要new
let _req;
if (window["XMLHttpRequest"]) {
......@@ -78,6 +78,10 @@ export class Loader extends EventDispatcher {
}
}
loadRaw(callback: Function, url: string, type: 'text'|'json') {
this.httpRequest(callback, url, 'get', type);
}
loadJson(callback: Function, url: string) {
this.loadRaw(callback, url, 'json');
}
......
This diff is collapsed.
This diff is collapsed.
/**
* Created by rockyl on 2019-11-14.
*/
export * from './Tween'
export * from './Ease'
import * as constant from './2d/const';
//具体导出哪些,看情况
export { Component } from "./2d/component/Component";
export * from "./2d/display";
export * from "./2d/events";
......@@ -10,7 +8,7 @@ export { default as Graphics } from "./2d/graphics/Graphics";
export { Shape } from "./2d/graphics/Shape";
export { Loader } from "./2d/loader/Loader";
export { Loader, globalLoader } from "./2d/loader/Loader";
export { Matrix } from "./2d/math/Matrix";
export { Point } from "./2d/math/Point";
......@@ -25,6 +23,7 @@ export * from "./2d/texture";
export * from "./2d/ui";
export * from './2d/tween'
export { default as toDisplayDataURL } from "./2d/utils/toDisplayDataURL";
......
......@@ -3,7 +3,7 @@
*/
import {Event, MouseEvent} from "../../2d/events";
import {Event, EventDispatcher, MouseEvent} from "../../2d/events";
import {HashObject} from "../../2d/HashObject";
import {executeBehavior} from "../behavior-runtime";
......@@ -16,6 +16,10 @@ const eventsMapping = {
[MouseEvent.MOUSE_UP]: 'touchend',
};
export const globalEvent = new EventDispatcher();
export const DATA_CENTER_EVENT: string = 'DATA_CENTER_EVENT';
/**
* 应用事件委托
* @param ctor
......@@ -27,7 +31,7 @@ export function applyEvents(ctor: Function) {
this.addEventListener(k, eventsProxy.onEvent, eventsProxy);
}
eventsProxy.invoke('init', this);
globalEvent.addEventListener(DATA_CENTER_EVENT, eventsProxy.onDateCenterEvent, eventsProxy);
};
}
......@@ -38,7 +42,11 @@ class EventsProxy extends HashObject {
super();
}
invoke(name, target) {
start() {
this.invoke('init', this);
}
invoke(name, target, payload?) {
if (this.eventsConfig) {
const eventConfig = this.eventsConfig[name];
if (eventConfig) {
......@@ -52,10 +60,14 @@ class EventsProxy extends HashObject {
onEvent(e) {
let eventName = eventsMapping[e.type];
if (eventName) {
this.invoke(eventName, e.target);
this.invoke(eventName, e.target, e.data);
}
}
onDateCenterEvent(e) {
this.invoke('datacenter', this, e.data);
}
destroy(): void {
}
}
......@@ -9,6 +9,8 @@ import {instantiate} from "./view-interpreter";
import {dataCenter, DataCenter} from "./data-center";
import {setProcessMetaLibs} from "../behavior-runtime";
import {registerScripts} from "..";
import {Tween} from "../../2d/tween";
import {Rect} from "./nodes";
/**
* 游戏舞台
......@@ -16,6 +18,7 @@ import {registerScripts} from "..";
export class GameStage extends Container {
private _sceneContainer: StackContainer; //场景容器
private _popupContainer: StackContainer; //弹层容器
private _blackLayer: Rect;
private _stage;
private _dataCenter: DataCenter;
......@@ -33,11 +36,22 @@ export class GameStage extends Container {
this['percentHeight'] = 100;
this.mouseEnabled = false;
this.addChild(this._sceneContainer = new StackContainer());
let blackLayer = this._blackLayer = new Rect();
this.addChild(this._sceneContainer = new StackContainer(false));
this.addChild(blackLayer);
this.addChild(this._popupContainer = new StackContainer());
blackLayer['percentWidth'] = 100;
blackLayer['percentHeight'] = 100;
blackLayer.visible = false;
blackLayer.fillColor = 0;
blackLayer.alpha = 0.7;
this._sceneContainer.name = 'scene-container';
this._popupContainer.name = 'popup-container';
this._popupContainer.addEventListener('change', this.onPopupContainerChange, this);
}
/**
......@@ -83,6 +97,9 @@ export class GameStage extends Container {
*/
start() {
const {options, dataMapping, processes, builtinProcesses, scripts} = this._config;
Stage.addUpdateObj(Tween);
registerScripts(scripts);
this.dataCenter.registerDataMapping(dataMapping);
setProcessMetaLibs(processes, builtinProcesses);
......@@ -103,4 +120,29 @@ export class GameStage extends Container {
getViewConfigByName(name) {
return this._config.views.find(view => view.name === name);
}
/**
* 设置半透明层是否可见
* @param visible
*/
setBlackLayerVisible(visible) {
this._blackLayer.visible = visible;
}
onPopupContainerChange(e) {
const {action, view, options} = e.data;
switch (action) {
case 'push':
case 'replace':
case 'popAll':
if(options.center){
view.horizonCenter = 0;
view.verticalCenter = 0;
}
break;
}
this.setBlackLayerVisible(this._popupContainer.children.length > 0);
}
}
......@@ -8,9 +8,13 @@ import {Container, DisplayObject} from "../../2d/display";
* 栈式视图容器
*/
export class StackContainer extends Container {
constructor(){
private _mutex: boolean;
constructor(mutex = true) {
super();
this._mutex = mutex;
this['percentWidth'] = 100;
this['percentHeight'] = 100;
this.mouseEnabled = false;
......@@ -19,18 +23,29 @@ export class StackContainer extends Container {
/**
* 推入视图
* @param view
* @param options
* @param dispatch
*/
push(view: DisplayObject) {
push(view: DisplayObject, options?, dispatch = true) {
if(this._mutex){
this.removeChildren();
}
this.addChild(view);
if (dispatch) {
this.dispatchEvent('change', {action: 'push', view, options});
}
}
/**
* 替换顶层视图
* @param view
* @param options
*/
replace(view: DisplayObject) {
replace(view: DisplayObject, options?) {
if (this.pop()) {
this.push(view);
this.dispatchEvent('change', {action: 'replace', view, options});
this.push(view, options, false);
}
}
......@@ -43,15 +58,20 @@ export class StackContainer extends Container {
return false;
}
this.removeChildAt(len - 1);
this.dispatchEvent('change', {action: 'pop'});
return true;
}
/**
* 撤出全部视图
* @param view
* @param options
*/
popAll(view: DisplayObject) {
popAll(view?: DisplayObject, options?) {
this.removeChildren();
this.push(view);
this.dispatchEvent('change', {action: 'popAll', view, options});
if(view){
this.push(view, options, false);
}
}
}
......@@ -5,6 +5,7 @@
*/
import {EventDispatcher} from "../../2d/events";
import {arrayFind} from "../utils";
import {DATA_CENTER_EVENT, globalEvent} from "../decorators/events";
/**
* 数据中心
......@@ -22,7 +23,7 @@ export class DataCenter extends EventDispatcher {
* @param name
* @param origin
*/
registerGroup(name, origin) {
registerGroup(name, origin?) {
this.store[name] = origin || {};
}
......@@ -52,7 +53,7 @@ export class DataCenter extends EventDispatcher {
try {
return func(this.store);
} catch (e) {
console.warn(e);
//console.warn(e);
if (throwException) {
throw e;
}
......@@ -89,7 +90,7 @@ export class DataCenter extends EventDispatcher {
let group = this.getGroup(name);
if (!group) {
return;
this.registerGroup(name);
}
if (data) {
if (path) {
......@@ -115,6 +116,12 @@ export class DataCenter extends EventDispatcher {
}
}
}
globalEvent.dispatchEvent(DATA_CENTER_EVENT, {
name,
path,
data,
})
}
/**
......
......@@ -41,6 +41,7 @@ function instantiateView(config) {
if (events) {
node.eventsProxy.eventsConfig = events;
}
node.eventsProxy.start();
if (children && children.length > 0) {
for (let childConfig of children) {
......
......@@ -4,3 +4,8 @@
export * from './launcher'
export * from './decorators/scripts'
import {instantiate} from './game-warpper/view-interpreter'
export {
instantiate
}
......@@ -8,6 +8,8 @@ import {GameStage} from "./game-warpper";
import {setGlobalContext} from "./behavior-runtime";
import {globalLoader} from "../2d/loader/Loader";
export let gameStage;
export function launch(url, onAssetsProgress, onAssetsComplete) {
return new Promise((resolve, reject) => {
globalLoader.loadJson((s, payload) => {
......@@ -38,7 +40,7 @@ export function launchWithConfig(config, onAssetsProgress, onAssetsComplete) {
);
Stage.flushAll();
let gameStage = new GameStage(stage);
gameStage = new GameStage(stage);
setGlobalContext({
gameStage
});
......
{
"compilerOptions": {
"module": "commonjs",
"module": "ES2015",
"target": "es5",
"noImplicitAny": false,
"sourceMap": true,
"removeComments": true,
"noEmitOnError": true,
// "declarationDir": "types",
//"declaration": true,
"declarationDir": "types",
"declaration": true,
"experimentalDecorators": true,
"outDir": "dist",
"outDir": "dist-m",
"lib": [
"es5",
"dom",
......
......@@ -5,11 +5,11 @@ var webpack = require('webpack')
module.exports = {
entry: {
"render.min": "./src/index.ts",
"index": "./src/index.ts",
},
output: {
path: __dirname,
filename: "build/[name].js",
filename: "dist/[name].js",
libraryTarget: 'umd',
library: 'render',
},
......
This diff is collapsed.
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment