移除所有egret 依赖。移除renderablecomponent及所有依赖,移除camera。保持ecs基础框架

This commit is contained in:
yhh
2020-11-23 16:05:06 +08:00
parent 0fd6a24f5a
commit 14a73e4010
76 changed files with 1410 additions and 28750 deletions

View File

@@ -1,71 +0,0 @@
module es {
/**
* 支持标题安全区的布局类。
*/
export class Layout {
public clientArea: Rectangle;
public safeArea: Rectangle;
constructor() {
this.clientArea = new Rectangle(0, 0, Core.graphicsDevice.viewport.width, Core.graphicsDevice.viewport.height);
this.safeArea = this.clientArea;
}
public place(size: Vector2, horizontalMargin: number, verticalMargine: number, alignment: Alignment) {
let rc = new Rectangle(0, 0, size.x, size.y);
if ((alignment & Alignment.left) != 0) {
rc.x = this.clientArea.x + (this.clientArea.width * horizontalMargin);
} else if ((alignment & Alignment.right) != 0) {
rc.x = this.clientArea.x + (this.clientArea.width * (1 - horizontalMargin)) - rc.width;
} else if ((alignment & Alignment.horizontalCenter) != 0) {
rc.x = this.clientArea.x + (this.clientArea.width - rc.width) / 2 + (horizontalMargin * this.clientArea.width);
} else {
}
if ((alignment & Alignment.top) != 0) {
rc.y = this.clientArea.y + (this.clientArea.height * verticalMargine);
} else if ((alignment & Alignment.bottom) != 0) {
rc.y = this.clientArea.y + (this.clientArea.height * (1 - verticalMargine)) - rc.height;
} else if ((alignment & Alignment.verticalCenter) != 0) {
rc.y = this.clientArea.y + (this.clientArea.height - rc.height) / 2 + (verticalMargine * this.clientArea.height);
} else {
}
// 确保布局区域在安全区域内。
if (rc.left < this.safeArea.left)
rc.x = this.safeArea.left;
if (rc.right > this.safeArea.right)
rc.x = this.safeArea.right - rc.width;
if (rc.top < this.safeArea.top)
rc.y = this.safeArea.top;
if (rc.bottom > this.safeArea.bottom)
rc.y = this.safeArea.bottom - rc.height;
return rc;
}
}
export enum Alignment {
none = 0,
left = 1,
right = 2,
horizontalCenter = 4,
top = 8,
bottom = 16,
verticalCenter = 32,
topLeft = top | left,
topRight = top | right,
topCenter = top | horizontalCenter,
bottomLeft = bottom | left,
bottomRight = bottom | right,
bottomCenter = bottom | horizontalCenter,
centerLeft = verticalCenter | left,
centerRight = verticalCenter | right,
center = verticalCenter | horizontalCenter
}
}

View File

@@ -1,408 +0,0 @@
module es {
/**
* 通过使用这个类您可以直观地找到瓶颈和基本的CPU使用情况。
*/
export class TimeRuler {
/** 最大条数 8 */
public static readonly maxBars = 8;
/** */
public static readonly maxSamples = 256;
/** 每条的最大嵌套调用 */
public static readonly maxNestCall = 32;
/** 条的高度(以像素为单位) */
public static readonly barHeight = 8;
/** 最大显示帧 */
public static readonly maxSampleFrames = 4;
/** 持续时间(帧数)为采取抓拍日志。 */
public static readonly logSnapDuration = 120;
public static readonly barPadding = 2;
public static readonly autoAdjustDelay = 30;
private static _instance;
/** 获取/设置目标样本帧。 */
public targetSampleFrames: number;
/** 获取/设置计时器标尺宽度。 */
public width: number;
public enabled: true;
/** 获取/设置日志显示或否 */
public showLog = false;
/** 每帧的日志 */
public logs: FrameLog[];
/** 当前显示帧计数 */
private sampleFrames: number;
/** TimerRuler画的位置。 */
private _position: Vector2;
/** 上一帧日志 */
private prevLog: FrameLog;
/** 当前帧日志 */
private curLog: FrameLog;
/** 当前帧数量 */
private frameCount: number;
/** */
private markers: MarkerInfo[] = [];
/** 秒表用来测量时间。 */
private stopwacth: stopwatch.Stopwatch = new stopwatch.Stopwatch();
/** 从标记名映射到标记id的字典。 */
private _markerNameToIdMap: Map<string, number> = new Map<string, number>();
/**
* 你想在游戏开始时调用StartFrame更新方法。
* 当游戏在固定时间步进模式下运行缓慢时,更新会多次调用。
* 在这种情况下我们应该忽略StartFrame调用。
* 为此我们只需一直跟踪StartFrame调用的次数直到Draw被调用。
*/
private _updateCount: number;
private _frameAdjust: number;
/** 画透明背景 */
private _rectShape1: egret.Shape = new egret.Shape();
private _rectShape2: egret.Shape = new egret.Shape();
private _rectShape3: egret.Shape = new egret.Shape();
private _rectShape4: egret.Shape = new egret.Shape();
private _rectShape5: egret.Shape = new egret.Shape();
private _rectShape6: egret.Shape = new egret.Shape();
constructor() {
this.logs = new Array<FrameLog>(2);
for (let i = 0; i < this.logs.length; ++i)
this.logs[i] = new FrameLog();
this.sampleFrames = this.targetSampleFrames = 1;
this.width = Math.floor(Core.graphicsDevice.viewport.width * 0.8);
es.Core.emitter.addObserver(CoreEvents.GraphicsDeviceReset, this.onGraphicsDeviceReset, this);
this.onGraphicsDeviceReset();
es.Core.Instance.stage.addChild(this._rectShape1);
es.Core.Instance.stage.addChild(this._rectShape2);
es.Core.Instance.stage.addChild(this._rectShape3);
es.Core.Instance.stage.addChild(this._rectShape4);
es.Core.Instance.stage.addChild(this._rectShape5);
es.Core.Instance.stage.addChild(this._rectShape6);
}
public static get Instance(): TimeRuler {
if (!this._instance)
this._instance = new TimeRuler();
return this._instance;
}
/**
* 开始新的帧
*/
public startFrame() {
// 当这个方法被多次调用时,我们跳过复位帧。
if (isNaN(this._updateCount))
this._updateCount = 0;
let count = this._updateCount ++;
if (this.enabled && (1 < count && count < TimeRuler.maxSampleFrames))
return;
// 更新当前帧记录
this.prevLog = this.logs[this.frameCount++ & 0x1];
this.curLog = this.logs[this.frameCount & 0x1];
let endFrameTime = this.stopwacth.getTime();
// 更新标记并创建日志。
for (let barIdx = 0; barIdx < this.prevLog.bars.length; ++barIdx) {
let prevBar = this.prevLog.bars[barIdx];
let nextBar = this.curLog.bars[barIdx];
// 重新打开前一帧中没有被调用的EndMark的标记
for (let nest = 0; nest < prevBar.nestCount; ++nest) {
let markerIdx = prevBar.markerNests[nest];
prevBar.markers[markerIdx].endTime = endFrameTime;
nextBar.markerNests[nest] = nest;
nextBar.markers[nest].markerId = prevBar.markers[markerIdx].markerId;
nextBar.markers[nest].beginTime = 0;
nextBar.markers[nest].endTime = -1;
nextBar.markers[nest].color = prevBar.markers[markerIdx].color;
}
// 更新日志标记
for (let markerIdx = 0; markerIdx < prevBar.markCount; ++markerIdx) {
let duration = prevBar.markers[markerIdx].endTime - prevBar.markers[markerIdx].beginTime;
let markerId = prevBar.markers[markerIdx].markerId;
let m = this.markers[markerId];
m.logs[barIdx].color = prevBar.markers[markerIdx].color;
if (!m.logs[barIdx].initialized) {
m.logs[barIdx].min = duration;
m.logs[barIdx].max = duration;
m.logs[barIdx].avg = duration;
m.logs[barIdx].initialized = true;
} else {
m.logs[barIdx].min = Math.min(m.logs[barIdx].min, duration);
m.logs[barIdx].max = Math.min(m.logs[barIdx].max, duration);
m.logs[barIdx].avg += duration;
m.logs[barIdx].avg *= 0.5;
if (m.logs[barIdx].samples++ >= TimeRuler.logSnapDuration) {
m.logs[barIdx].snapMin = m.logs[barIdx].min;
m.logs[barIdx].snapMax = m.logs[barIdx].max;
m.logs[barIdx].snapAvg = m.logs[barIdx].avg;
m.logs[barIdx].samples = 0;
}
}
}
nextBar.markCount = prevBar.nestCount;
nextBar.nestCount = prevBar.nestCount;
}
this.stopwacth.reset();
this.stopwacth.start();
}
/**
* 开始测量时间。
* @param markerName
* @param color
* @param barIndex
*/
public beginMark(markerName: string, color: number, barIndex: number = 0) {
if (barIndex < 0 || barIndex >= TimeRuler.maxBars)
throw new Error("barIndex argument out of range");
let bar = this.curLog.bars[barIndex];
if (bar.markCount >= TimeRuler.maxSamples) {
throw new Error("超出采样次数可以设置更大的数字为timeruler.maxsaple或者降低采样次数");
}
if (bar.nestCount >= TimeRuler.maxNestCall) {
throw new Error("超出采样次数可以设置更大的数字为timeruler.maxsaple或者降低采样次数");
}
// 获取注册的标记
let markerId = this._markerNameToIdMap.get(markerName);
if (isNaN(markerId)) {
// 如果此标记未注册,则注册此标记。
markerId = this.markers.length;
this._markerNameToIdMap.set(markerName, markerId);
this.markers.push(new MarkerInfo(markerName));
}
// 开始测量
bar.markerNests[bar.nestCount++] = bar.markCount;
// 填充标记参数
bar.markers[bar.markCount].markerId = markerId;
bar.markers[bar.markCount].color = color;
bar.markers[bar.markCount].beginTime = this.stopwacth.getTime();
bar.markers[bar.markCount].endTime = -1;
bar.markCount ++;
}
/**
*
* @param markerName
* @param barIndex
*/
public endMark(markerName: string, barIndex: number = 0) {
if (barIndex < 0 || barIndex >= TimeRuler.maxBars)
throw new Error("barIndex参数超出范围");
let bar = this.curLog.bars[barIndex];
if (bar.nestCount <= 0) {
throw new Error("先调用beginMark方法再调用endMark方法");
}
let markerId = this._markerNameToIdMap.get(markerName);
if (isNaN(markerId)) {
throw new Error(`标记 ${markerName} 未注册。请确认您指定的名称与 beginMark 方法使用的名称相同`);
}
let markerIdx = bar.markerNests[--bar.nestCount];
if (bar.markers[markerIdx].markerId != markerId) {
throw new Error("beginMark/endMark方法的调用顺序不正确beginMark(A)beginMark(B)endMark(B)endMark(A)但你不能像beginMark(A)beginMark(B)endMark(A)endMark(B)这样调用。");
}
bar.markers[markerIdx].endTime = this.stopwacth.getTime();
}
/**
* 获取给定bar索引和标记名称的平均时间。
* @param barIndex
* @param markerName
*/
public getAverageTime(barIndex: number, markerName: string) {
if (barIndex < 0 || barIndex >= TimeRuler.maxBars) {
throw new Error("barIndex参数超出范围");
}
let result = 0;
let markerId = this._markerNameToIdMap.get(markerName);
if (markerId) {
result = this.markers[markerId].logs[barIndex].avg;
}
return result;
}
/**
*
*/
public resetLog() {
this.markers.forEach(markerInfo => {
for (let i = 0; i < markerInfo.logs.length; ++i) {
markerInfo.logs[i].initialized = false;
markerInfo.logs[i].snapMin = 0;
markerInfo.logs[i].snapMax = 0;
markerInfo.logs[i].snapAvg = 0;
markerInfo.logs[i].min = 0;
markerInfo.logs[i].max = 0;
markerInfo.logs[i].avg = 0;
markerInfo.logs[i].samples = 0;
}
});
}
public render(position: Vector2 = this._position, width: number = this.width) {
if (!this.showLog)
return;
let height = 0;
let maxTime = 0;
this.prevLog.bars.forEach(bar => {
if (bar.markCount > 0) {
height += TimeRuler.barHeight + TimeRuler.barPadding * 2;
maxTime = Math.max(maxTime, bar.markers[bar.markCount - 1].endTime);
}
});
const frameSpan = 1 / 60 * 1000;
let sampleSpan = this.sampleFrames * frameSpan;
if (maxTime > sampleSpan) {
this._frameAdjust = Math.max(0, this._frameAdjust) + 1;
} else {
this._frameAdjust = Math.min(0, this._frameAdjust) - 1;
}
if (Math.max(this._frameAdjust) > TimeRuler.autoAdjustDelay) {
this.sampleFrames = Math.min(TimeRuler.maxSampleFrames, this.sampleFrames);
this.sampleFrames = Math.max(this.targetSampleFrames, Math.floor(maxTime / frameSpan) + 1);
this._frameAdjust = 0;
}
let msToPs = width / sampleSpan;
let startY = position.y - (height - TimeRuler.barHeight);
let y = startY;
// 画透明背景
let rc = new Rectangle(position.x, y, width, height);
this._rectShape1.graphics.clear();
this._rectShape1.graphics.beginFill(0x000000, 128 / 255);
this._rectShape1.graphics.drawRect(rc.x, rc.y, rc.width, rc.height);
this._rectShape1.graphics.endFill();
// 为每条线画标记
rc.height = TimeRuler.barHeight;
this._rectShape2.graphics.clear();
for (let bar of this.prevLog.bars){
rc.y = y + TimeRuler.barPadding;
if (bar.markCount > 0){
for (let j = 0; j < bar.markCount; ++j){
let bt = bar.markers[j].beginTime;
let et = bar.markers[j].endTime;
let sx = Math.floor(position.x + bt * msToPs);
let ex = Math.floor(position.x + et * msToPs);
rc.x = sx;
rc.width = Math.max(ex - sx, 1);
this._rectShape2.graphics.beginFill(bar.markers[j].color);
this._rectShape2.graphics.drawRect(rc.x, rc.y, rc.width, rc.height);
this._rectShape2.graphics.endFill();
}
}
y += TimeRuler.barHeight + TimeRuler.barPadding;
}
// 画网格线
// 每个网格代表ms
rc = new Rectangle(position.x, startY, 1, height);
this._rectShape3.graphics.clear();
for (let t = 1; t < sampleSpan; t += 1){
rc.x = Math.floor(position.x + t * msToPs);
this._rectShape3.graphics.beginFill(0x808080);
this._rectShape3.graphics.drawRect(rc.x, rc.y, rc.width, rc.height);
this._rectShape3.graphics.endFill();
}
// 画frame grid
this._rectShape4.graphics.clear();
for (let i = 0; i <= this.sampleFrames; ++i){
rc.x = Math.floor(position.x + frameSpan * i * msToPs);
this._rectShape4.graphics.beginFill(0xFFFFFF);
this._rectShape4.graphics.drawRect(rc.x, rc.y, rc.width, rc.height);
this._rectShape4.graphics.endFill();
}
// TODO: 绘制字体
}
private onGraphicsDeviceReset() {
let layout = new Layout();
this._position = layout.place(new Vector2(this.width, TimeRuler.barHeight), 0, 0.01, Alignment.bottomCenter).location;
}
}
/**
* 日志信息
*/
export class FrameLog {
public bars: MarkerCollection[];
constructor() {
this.bars = new Array<MarkerCollection>(TimeRuler.maxBars);
this.bars.fill(new MarkerCollection(), 0, TimeRuler.maxBars);
}
}
/**
* 标记的集合
*/
export class MarkerCollection {
public markers: Marker[] = new Array<Marker>(TimeRuler.maxSamples);
public markCount: number = 0;
public markerNests: number[] = new Array<number>(TimeRuler.maxNestCall);
public nestCount: number = 0;
constructor() {
this.markers.fill(new Marker(), 0, TimeRuler.maxSamples);
this.markerNests.fill(0, 0, TimeRuler.maxNestCall);
}
}
export class Marker {
public markerId: number = 0;
public beginTime: number = 0;
public endTime: number = 0;
public color: number = 0x000000;
}
export class MarkerInfo {
public name: string;
public logs: MarkerLog[] = new Array<MarkerLog>(TimeRuler.maxBars);
constructor(name) {
this.name = name;
this.logs.fill(new MarkerLog(), 0, TimeRuler.maxBars);
}
}
export class MarkerLog {
public snapMin: number = 0;
public snapMax: number = 0;
public snapAvg: number = 0;
public min: number = 0;
public max: number = 0;
public avg: number = 0;
public samples: number = 0;
public color: number = 0x000000;
public initialized: boolean = false;
}
}

View File

@@ -1,44 +0,0 @@
module es {
export class ContentManager {
protected loadedAssets: Map<string, any> = new Map<string, any>();
/** 异步加载资源 */
public loadRes(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;
if (RES.destroyRes(assetsToRemove))
assetsToRemove.dispose();
});
this.loadedAssets.clear();
}
}
}

View File

@@ -1,38 +0,0 @@
module es {
/**
* startCoroutine返回的接口提供了在执行中停止协同程序的能力
*/
export interface ICoroutine {
/**
* 停止协同程序
*/
stop();
/**
* 设置协程应该使用时间增量还是使用非缩放时间增量来计时
* @param useUnscaledDeltaTime
*/
setUseUnscaledDeltaTime(useUnscaledDeltaTime): ICoroutine;
}
export class Coroutine {
public static waitForSeconds(seconds: number){
return WaitForSeconds.waiter.wait(seconds);
}
}
/**
* 协作程序需要暂停一段时间时的助手类。
* 返回协同程序。
* waitForSeconds返回number。
*/
export class WaitForSeconds {
public static waiter: WaitForSeconds = new WaitForSeconds();
public waitTime: number;
public wait(seconds: number): WaitForSeconds {
WaitForSeconds.waiter.waitTime = seconds;
return WaitForSeconds.waiter;
}
}
}

View File

@@ -1,153 +0,0 @@
module es {
/**
* CoroutineManager使用的内部类用于隐藏协同程序所需的数据
*/
export class CoroutineImpl implements ICoroutine, IPoolable {
public enumerator: Iterator<any>;
/**
* 每当产生延迟时它就被添加到跟踪延迟的waitTimer中
*/
public waitTimer: number;
public isDone: boolean;
public waitForCoroutine: CoroutineImpl;
public useUnscaledDeltaTime: boolean = false;
public stop(){
this.isDone = true;
}
public setUseUnscaledDeltaTime(useUnscaledDeltaTime): es.ICoroutine {
this.useUnscaledDeltaTime = useUnscaledDeltaTime;
return this;
}
public prepareForuse(){
this.isDone = false;
}
public reset() {
this.isDone = true;
this.waitTimer = 0;
this.waitForCoroutine = null;
this.enumerator = null;
this.useUnscaledDeltaTime = false;
}
}
/**
* 基本CoroutineManager。协同程序可以做以下事情:
* - return null(在下一帧继续执行)
* - return Coroutine.waitForSeconds(3)。等待3秒(延时3秒后再次执行)
* - return startCoroutine(another())(在再次执行之前等待另一个协程)
*/
export class CoroutineManager extends GlobalManager {
/**
* 标记来跟踪何时处于更新循环中。
* 如果一个新的协程在更新循环期间启动我们必须将其插入到shouldRunNextFrame列表中以避免在迭代时修改列表。
*/
public _isInUpdate: boolean;
public _unblockedCoroutines: CoroutineImpl[] = [];
public _shouldRunNextFrame: CoroutineImpl[] = [];
/**
* 将i枚举器添加到CoroutineManager。协程在每一帧调用更新之前被执行。
* @param enumerator
*/
public startCoroutine(enumerator: Iterator<any>) {
// 查找或创建CoroutineImpl
let coroutine = Pool.obtain<CoroutineImpl>(CoroutineImpl);
coroutine.prepareForuse();
// 设置协程并添加它
coroutine.enumerator = enumerator;
let shouldContinueCoroutine = this.tickCoroutine(coroutine);
// 防止空协程
if (!shouldContinueCoroutine)
return null;
if (this._isInUpdate)
this._shouldRunNextFrame.push(coroutine);
else
this._unblockedCoroutines.push(coroutine);
return coroutine;
}
public update() {
this._isInUpdate = true;
for (let i = 0; i < this._unblockedCoroutines.length; i ++){
let coroutine = this._unblockedCoroutines[i];
// 检查已停止的协程
if (coroutine.isDone){
Pool.free<CoroutineImpl>(coroutine);
continue;
}
// 我们是否在等待其他协程完成
if (coroutine.waitForCoroutine != null){
if (coroutine.waitForCoroutine.isDone){
coroutine.waitForCoroutine = null;
}else{
this._shouldRunNextFrame.push(coroutine);
continue;
}
}
// 如果我们有计时器,就用它
if (coroutine.waitTimer > 0){
// 还有时间。递减并再次运行下一帧确保递减与适当的deltaTime。
coroutine.waitTimer -= coroutine.useUnscaledDeltaTime ? Time.unscaledDeltaTime : Time.deltaTime;
this._shouldRunNextFrame.push(coroutine);
continue;
}
if (this.tickCoroutine(coroutine))
this._shouldRunNextFrame.push(coroutine);
}
this._unblockedCoroutines.length = 0;
this._unblockedCoroutines.concat(this._shouldRunNextFrame);
this._shouldRunNextFrame.length = 0;
this._isInUpdate = false;
}
/**
* 如果协同程序在下一帧继续运行则返回true。此方法将把完成的协程放回池中!
* @param coroutine
*/
public tickCoroutine(coroutine: CoroutineImpl){
let current = coroutine.enumerator.next();
// 这个协同程序已经完成了
if (!current.value || current.done){
Pool.free<CoroutineImpl>(coroutine);
return false;
}
if (!current.value){
return true;
}
if (current.value instanceof WaitForSeconds){
coroutine.waitTimer = (current.value as WaitForSeconds).waitTime;
return true;
}
if (current.value instanceof Number){
console.warn("协同程序检查返回一个Number类型请不要在生产环境使用");
coroutine.waitTimer = Number(current);
return true;
}
if (current.value instanceof CoroutineImpl){
coroutine.waitForCoroutine = current.value as CoroutineImpl;
return true;
}else {
return true;
}
}
}
}

View File

@@ -1,17 +0,0 @@
module es {
/** 各种辅助方法来辅助绘图 */
export class DrawUtils {
public static getColorMatrix(color: number): egret.ColorMatrixFilter {
let colorMatrix = [
1, 0, 0, 0, 0,
0, 1, 0, 0, 0,
0, 0, 1, 0, 0,
0, 0, 0, 1, 0
];
colorMatrix[0] = Math.floor(color / 256 / 256) / 255;
colorMatrix[6] = Math.floor(color / 256 % 256) / 255;
colorMatrix[12] = color % 256 / 255;
return new egret.ColorMatrixFilter(colorMatrix);
}
}
}

View File

@@ -1,193 +0,0 @@
module es {
export class TouchState {
public x = 0;
public y = 0;
public touchPoint: number = -1;
public touchDown: boolean = false;
public get position() {
return new Vector2(this.x, this.y);
}
public reset() {
this.x = 0;
this.y = 0;
this.touchDown = false;
this.touchPoint = -1;
}
}
export class Input {
private static _init: boolean = false;
private static _previousTouchState: TouchState = new TouchState();
private static _resolutionOffset: Vector2 = new Vector2();
private static _touchIndex: number = 0;
private static _gameTouchs: TouchState[] = [];
/**
* 触摸列表 存放最大个数量触摸点信息
* 可通过判断touchPoint是否为-1 来确定是否为有触摸
* 通过判断touchDown 判断触摸点是否有按下
*/
public static get gameTouchs() {
return this._gameTouchs;
}
private static _resolutionScale: Vector2 = Vector2.one;
/** 获取缩放值 默认为1 */
public static get resolutionScale() {
return this._resolutionScale;
}
private static _totalTouchCount: number = 0;
/** 当前触摸点数量 */
public static get totalTouchCount() {
return this._totalTouchCount;
}
/** 返回第一个触摸点的坐标 */
public static get touchPosition() {
if (!this._gameTouchs[0])
return Vector2.zero;
return this._gameTouchs[0].position;
}
public static _virtualInputs: VirtualInput[] = [];
/** 获取最大触摸数 */
public static get maxSupportedTouch() {
return Core._instance.stage.maxTouches;
}
/**
* 设置最大触摸数
*/
public static set maxSupportedTouch(value: number) {
Core._instance.stage.maxTouches = value;
this.initTouchCache();
}
/** 获取第一个触摸点距离上次距离的增量 */
public static get touchPositionDelta() {
let delta = Vector2.subtract(this.touchPosition, this._previousTouchState.position);
if (delta.length() > 0) {
this.setpreviousTouchState(this._gameTouchs[0]);
}
return delta;
}
public static initialize() {
if (this._init)
return;
this._init = true;
Core._instance.stage.addEventListener(egret.TouchEvent.TOUCH_BEGIN, this.touchBegin, this);
Core._instance.stage.addEventListener(egret.TouchEvent.TOUCH_MOVE, this.touchMove, this);
Core._instance.stage.addEventListener(egret.TouchEvent.TOUCH_END, this.touchEnd, this);
Core._instance.stage.addEventListener(egret.TouchEvent.TOUCH_CANCEL, this.touchEnd, this);
Core._instance.stage.addEventListener(egret.TouchEvent.TOUCH_RELEASE_OUTSIDE, this.touchEnd, this);
this.initTouchCache();
}
public static update(){
KeyboardUtils.update();
for (let i = 0; i < this._virtualInputs.length; i ++)
this._virtualInputs[i].update();
}
public static scaledPosition(position: Vector2) {
let scaledPos = new Vector2(position.x - this._resolutionOffset.x, position.y - this._resolutionOffset.y);
return Vector2.multiply(scaledPos, this.resolutionScale);
}
/**
* 只有在当前帧按下并且在上一帧没有按下时才算press
* @param key
*/
public static isKeyPressed(key: Keys): boolean{
return KeyboardUtils.currentKeys.contains(key) && !KeyboardUtils.previousKeys.contains(key);
}
public static isKeyPressedBoth(keyA: Keys, keyB: Keys){
return this.isKeyPressed(keyA) || this.isKeyPressed(keyB);
}
public static isKeyDown(key: Keys): boolean {
return KeyboardUtils.currentKeys.contains(key);
}
public static isKeyDownBoth(keyA: Keys, keyB: Keys){
return this.isKeyDown(keyA) || this.isKeyDown(keyB);
}
public static isKeyReleased(key: Keys){
return !KeyboardUtils.currentKeys.contains(key) && KeyboardUtils.previousKeys.contains(key);
}
public static isKeyReleasedBoth(keyA: Keys, keyB: Keys){
return this.isKeyReleased(keyA) || this.isKeyReleased(keyB);
}
private static initTouchCache() {
this._totalTouchCount = 0;
this._touchIndex = 0;
this._gameTouchs.length = 0;
for (let i = 0; i < this.maxSupportedTouch; i++) {
this._gameTouchs.push(new TouchState());
}
}
private static touchBegin(evt: egret.TouchEvent) {
if (this._touchIndex < this.maxSupportedTouch) {
this._gameTouchs[this._touchIndex].touchPoint = evt.touchPointID;
this._gameTouchs[this._touchIndex].touchDown = evt.touchDown;
this._gameTouchs[this._touchIndex].x = evt.stageX;
this._gameTouchs[this._touchIndex].y = evt.stageY;
if (this._touchIndex == 0) {
this.setpreviousTouchState(this._gameTouchs[0]);
}
this._touchIndex++;
this._totalTouchCount++;
}
}
private static touchMove(evt: egret.TouchEvent) {
if (evt.touchPointID == this._gameTouchs[0].touchPoint) {
this.setpreviousTouchState(this._gameTouchs[0]);
}
let touchIndex = this._gameTouchs.findIndex(touch => touch.touchPoint == evt.touchPointID);
if (touchIndex != -1) {
let touchData = this._gameTouchs[touchIndex];
touchData.x = evt.stageX;
touchData.y = evt.stageY;
}
}
private static touchEnd(evt: egret.TouchEvent) {
let touchIndex = this._gameTouchs.findIndex(touch => touch.touchPoint == evt.touchPointID);
if (touchIndex != -1) {
let touchData = this._gameTouchs[touchIndex];
touchData.reset();
if (touchIndex == 0)
this._previousTouchState.reset();
this._totalTouchCount--;
if (this.totalTouchCount == 0) {
this._touchIndex = 0;
}
}
}
private static setpreviousTouchState(touchState: TouchState) {
this._previousTouchState = new TouchState();
this._previousTouchState.x = touchState.position.x;
this._previousTouchState.y = touchState.position.y;
this._previousTouchState.touchPoint = touchState.touchPoint;
this._previousTouchState.touchDown = touchState.touchDown;
}
}
}

View File

@@ -1,47 +0,0 @@
import Keys = es.Keys;
class KeyboardUtils {
/**
* 当前帧按键状态
*/
public static currentKeys: Keys[] = [];
/**
* 上一帧按键状态
*/
public static previousKeys: Keys[] = [];
private static keyStatusKeys: Keys[] = [];
public static init(): void {
document.addEventListener("keydown", KeyboardUtils.onKeyDownHandler);
document.addEventListener("keyup", KeyboardUtils.onKeyUpHandler);
}
public static update(){
KeyboardUtils.previousKeys.length = 0;
for (let key of KeyboardUtils.currentKeys){
KeyboardUtils.previousKeys.push(key);
KeyboardUtils.currentKeys.remove(key);
}
KeyboardUtils.currentKeys.length = 0;
for (let key of KeyboardUtils.keyStatusKeys){
KeyboardUtils.currentKeys.push(key);
}
}
public static destroy(): void {
KeyboardUtils.currentKeys.length = 0;
document.removeEventListener("keyup", KeyboardUtils.onKeyUpHandler);
document.removeEventListener("keypress", KeyboardUtils.onKeyDownHandler);
}
private static onKeyDownHandler(event: KeyboardEvent): void {
if (!KeyboardUtils.keyStatusKeys.contains(event.keyCode))
KeyboardUtils.keyStatusKeys.push(event.keyCode);
}
private static onKeyUpHandler(event: KeyboardEvent): void {
if (KeyboardUtils.keyStatusKeys.contains(event.keyCode))
KeyboardUtils.keyStatusKeys.remove(event.keyCode);
}
}

View File

@@ -1,116 +0,0 @@
module es {
export enum Keys {
none,
back = 8,
tab = 9,
enter = 13,
capsLock = 20,
escape = 27,
space = 32,
pageUp = 33,
pageDown = 34,
end = 35,
home = 36,
left = 37,
up = 38,
right = 39,
down = 40,
select = 41,
print = 42,
execute = 43,
printScreen = 44,
insert = 45,
delete = 46,
help = 47,
d0 = 48,
d1 = 49,
d2 = 50,
d3 = 51,
d4 = 52,
d5 = 53,
d6 = 54,
d7 = 55,
d8 = 56,
d9 = 57,
a = 65,
b = 66,
c = 67,
d = 68,
e = 69,
f = 70,
g = 71,
h = 72,
i = 73,
j = 74,
k = 75,
l = 76,
m = 77,
n = 78,
o = 79,
p = 80,
q = 81,
r = 82,
s = 83,
t = 84,
u = 85,
v = 86,
w = 87,
x = 88,
y = 89,
z = 90,
leftWindows = 91,
rightWindows = 92,
apps = 93,
sleep = 95,
numPad0 = 96,
numPad1 = 97,
numPad2 = 98,
numPad3 = 99,
numPad4 = 100,
numPad5 = 101,
numPad6 = 102,
numPad7 = 103,
numPad8 = 104,
numPad9 = 105,
multiply = 106,
add = 107,
seperator = 108,
subtract = 109,
decimal = 110,
divide = 111,
f1 = 112,
f2 = 113,
f3 = 114,
f4 = 115,
f5 = 116,
f6 = 117,
f7 = 118,
f8 = 119,
f9 = 120,
f10 = 121,
f11 = 122,
f12 = 123,
f13 = 124,
f14 = 125,
f15 = 126,
f16 = 127,
f17 = 128,
f18 = 129,
f19 = 130,
f20 = 131,
f21 = 132,
f22 = 133,
f23 = 134,
f24 = 135,
numLock = 144,
scroll = 145,
leftShift = 160,
rightShift = 161,
leftControl = 162,
rightControl = 163,
leftAlt = 164,
rightAlt = 165,
browserBack = 166,
browserForward = 167
}
}

View File

@@ -1,79 +0,0 @@
///<reference path="VirtualInput.ts"/>
///<reference path="VirtualIntegerAxis.ts"/>
module es {
/**
* 用-1和1之间的浮点数表示的虚拟输入
*/
export class VirtualAxis extends VirtualInput {
public nodes: VirtualAxisNode[] = [];
public get value() {
for (let i = 0; i < this.nodes.length; i++) {
let val = this.nodes[i].value;
if (val != 0)
return val;
}
return 0;
}
constructor(...nodes: VirtualAxisNode[]) {
super();
this.nodes.concat(nodes);
}
public update() {
for (let i = 0; i < this.nodes.length; i++)
this.nodes[i].update();
}
}
export class KeyboardKeys extends VirtualAxisNode {
public overlapBehavior: OverlapBehavior;
public positive: Keys;
public negative: Keys;
public _value: number = 0;
public _turned: boolean;
constructor(overlapBehavior: OverlapBehavior, negative: Keys, positive: Keys) {
super();
this.overlapBehavior = overlapBehavior;
this.negative = negative;
this.positive = positive;
}
public update() {
if (Input.isKeyDown(this.positive)) {
if (Input.isKeyDown(this.negative)) {
switch (this.overlapBehavior) {
default:
case es.OverlapBehavior.cancelOut:
this._value = 0;
break;
case es.OverlapBehavior.takeNewer:
if (!this._turned) {
this._value *= -1;
this._turned = true;
}
break;
case es.OverlapBehavior.takeOlder:
break;
}
} else {
this._turned = false;
this._value = 1;
}
} else if (Input.isKeyDown(this.negative)) {
this._turned = false;
this._value = -1;
} else {
this._turned = false;
this._value = 0;
}
}
public get value(){
return this._value;
}
}
}

View File

@@ -1,134 +0,0 @@
///<reference path="./VirtualInput.ts" />
module es {
/**
* 用布尔值表示的虚拟输入。除了简单地检查当前按钮状态,
* 您还可以询问它是否刚刚被按下或释放了这个框架。
* 您还可以将按下的按钮存储在缓冲区中一段有限的时间或者直到调用consumeBuffer()将其使用为止。
*/
export class VirtualButton extends VirtualInput{
public nodes: Node[] = [];
public bufferTime: number = 0;
public firstRepeatTime: number = 0;
public mutiRepeatTime: number = 0;
public isRepeating: boolean;
public _bufferCounter: number = 0;
public _repeatCounter: number = 0;
public _willRepeat: boolean;
constructor(bufferTime: number = 0, ...nodes: Node[]){
super();
this.nodes = nodes;
this.bufferTime = bufferTime;
}
public setRepeat(firstRepeatTime: number, mutiRepeatTime: number = firstRepeatTime){
this.firstRepeatTime = firstRepeatTime;
this.mutiRepeatTime = mutiRepeatTime;
this._willRepeat = this.firstRepeatTime > 0;
if (!this._willRepeat)
this.isRepeating = false;
}
public update() {
this._bufferCounter -= Time.unscaledDeltaTime;
this.isRepeating = false;
let check = false;
for (let i = 0; i < this.nodes.length; i ++){
this.nodes[i].update();
if (this.nodes[i].isPressed){
this._bufferCounter = this.bufferTime;
check = true;
}else if(this.nodes[i].isDown){
check = true;
}
}
if (!check){
this._repeatCounter = 0;
this._bufferCounter = 0;
}else if(this._willRepeat){
if (this._repeatCounter == 0){
this._repeatCounter = this.firstRepeatTime;
}else{
this._repeatCounter -= Time.unscaledDeltaTime;
if (this._repeatCounter <= 0){
this.isRepeating = true;
this._repeatCounter = this.mutiRepeatTime;
}
}
}
}
public get isDown(){
for (let node of this.nodes){
if (node.isDown)
return true;
}
return false;
}
public get isPressed(){
if (this._bufferCounter > 0 || this.isRepeating)
return true;
for (let node of this.nodes){
if (node.isPressed)
return true;
}
return false;
}
public get isReleased(){
for (let node of this.nodes){
if (node.isReleased)
return true;
}
return false;
}
public consumeBuffer(){
this._bufferCounter = 0;
}
/**
* 添加一个键盘键到这个虚拟按钮
* @param key
*/
public addKeyboardKey(key: Keys): VirtualButton{
this.nodes.push(new KeyboardKey(key));
return this;
}
}
export abstract class Node extends VirtualInputNode {
public abstract isDown: boolean;
public abstract isPressed: boolean;
public abstract isReleased: boolean;
}
export class KeyboardKey extends Node {
public key: Keys;
constructor(key: Keys){
super();
this.key = key;
}
public get isDown(){
return Input.isKeyDown(this.key);
}
public get isPressed(){
return Input.isKeyPressed(this.key);
}
public get isReleased(){
return Input.isKeyReleased(this.key);
}
}
}

View File

@@ -1,43 +0,0 @@
module es {
export enum OverlapBehavior {
/**
* 重复的输入将导致相互抵消,并且不会记录任何输入。
* 例如:按左箭头键,按住时按右箭头键。这将导致相互抵消。
*/
cancelOut,
/**
* 将使用找到的第一个输入
*/
takeOlder,
/**
* 将使用找到的最后一个输入
*/
takeNewer,
}
/**
* 虚拟按钮其状态由其VirtualInputNodes的状态决定
*/
export abstract class VirtualInput {
protected constructor() {
Input._virtualInputs.push(this);
}
/**
* 从输入系统取消虚拟输入的注册。在轮询VirtualInput之后调用这个函数
*/
public deregister(){
Input._virtualInputs.remove(this);
}
public abstract update();
}
/**
* 将它们添加到您的VirtualInput中以定义它如何确定当前输入状态。
* 例如如果你想检查一个键盘键是否被按下创建一个VirtualButton并添加一个VirtualButton.keyboardkey
*/
export abstract class VirtualInputNode {
public update() {}
}
}

View File

@@ -1,44 +0,0 @@
module es {
/**
* 用int(-1、0或1)表示的虚拟输入。它对应的输入可以从上到下到下,
* 如使用两个键盘键作为正/负检查。
*/
export class VirtualIntegerAxis extends VirtualInput {
public nodes: VirtualAxisNode[] = [];
public get value(){
for (let i = 0; i < this.nodes.length; i ++){
let val = this.nodes[i].value;
if (val != 0)
return Math.sign(val);
}
return 0;
}
constructor(...nodes: VirtualAxisNode[]){
super();
this.nodes.concat(nodes);
}
public update() {
for (let i = 0; i < this.nodes.length; i ++)
this.nodes[i].update();
}
/**
* 添加键盘键来模拟这个虚拟输入的左/右或上/下
* @param overlapBehavior
* @param negative
* @param positive
*/
public addKeyboardKeys(overlapBehavior: OverlapBehavior, negative: Keys, positive: Keys){
this.nodes.push(new KeyboardKeys(overlapBehavior, negative, positive));
return this;
}
}
export abstract class VirtualAxisNode extends VirtualInputNode {
public abstract value: number;
}
}

View File

@@ -52,7 +52,7 @@ module es {
public static free<T>(obj: T) {
this._objectQueue.unshift(obj);
if (egret.is(obj, "IPoolable")){
if (isIPoolable(obj)){
obj["reset"]();
}
}
@@ -64,4 +64,6 @@ module es {
*/
reset();
}
export var isIPoolable = (props: any): props is IPoolable => typeof (props as IPoolable)['js'] !== 'undefined';
}

View File

@@ -1,133 +0,0 @@
module es {
import Bitmap = egret.Bitmap;
export class AssetPacker {
protected itemsToRaster: TextureToPack[] = [];
public onProcessCompleted: Function;
public useCache: boolean = false;
public cacheName: string = "";
protected _sprites: Map<string, egret.Texture> = new Map<string, egret.Texture>();
protected allow4096Textures = false;
public addTextureToPack(texture: egret.Texture, customID: string){
this.itemsToRaster.push(new TextureToPack(texture, customID));
}
public async process(allow4096Textures: boolean = false){
this.allow4096Textures = allow4096Textures;
if (this.useCache){
if (this.cacheName == "") {
console.error("未指定缓存名称");
return;
}
let cacheExist = await RES.getResByUrl(this.cacheName);
if (!cacheExist)
this.createPack();
else
this.loadPack();
}else{
this.createPack();
}
}
protected async loadPack() {
let loaderTexture = await RES.getResByUrl(this.cacheName);
if (this.onProcessCompleted) this.onProcessCompleted();
return loaderTexture;
}
protected createPack(){
let textures: egret.Bitmap[] = [];
let images = [];
for (let itemToRaster of this.itemsToRaster){
textures.push(new Bitmap(itemToRaster.texture));
images.push(itemToRaster.id);
}
let textureSize = this.allow4096Textures ? 4096 : 2048;
let rectangles = [];
for (let i = 0; i < textures.length; i ++){
if (textures[i].width > textureSize || textures[i].height > textureSize){
throw new Error("一个纹理的大小比图集的大小大");
}else{
rectangles.push(new Rectangle(0, 0, textures[i].width, textures[i].height));
}
}
const padding = 1;
let numSpriteSheet = 0;
while (rectangles.length > 0){
let texture = new egret.RenderTexture();
let packer: RectanglePacker = new RectanglePacker(textureSize, textureSize, padding);
for (let i = 0; i < rectangles.length; i ++)
packer.insertRectangle(Math.floor(rectangles[i].width), Math.floor(rectangles[i].height), i);
packer.packRectangles();
if (packer.rectangleCount > 0){
let rect = new IntegerRectangle();
let textureAssets: TextureAsset[] = [];
let garbageRect: Rectangle[] = [];
let garabeTextures: egret.Texture[] = [];
let garbageImages: string[] = [];
for (let j = 0; j < packer.rectangleCount; j ++){
rect = packer.getRectangle(j, rect);
let index = packer.getRectangleId(j);
texture.drawToTexture(textures[index], new Rectangle(rect.x, rect.y, rect.width, rect.height));
let textureAsset: TextureAsset = new TextureAsset();
textureAsset.x = rect.x;
textureAsset.y = rect.y;
textureAsset.width = rect.width;
textureAsset.height = rect.height;
textureAsset.name = images[index];
textureAssets.push(textureAsset);
garbageRect.push(rectangles[index]);
garabeTextures.push(textures[index].texture);
garbageImages.push(images[index]);
}
for (let garbage of garbageRect)
rectangles.remove(garbage);
for (let garbage of garabeTextures)
textures.removeAll(a => {return a.texture.hashCode == garbage.hashCode});
for (let garbage of garbageImages)
images.remove(garbage);
if (this.cacheName != ""){
texture.saveToFile("image/png", this.cacheName);
++ numSpriteSheet;
}
for (let textureAsset of textureAssets)
this._sprites.set(textureAsset.name, texture);
}
}
if (this.onProcessCompleted) this.onProcessCompleted();
}
public dispose(){
this._sprites.forEach((asset, name) => {
asset.dispose();
RES.destroyRes(name);
});
this._sprites.clear();
}
public getTexture(id: string){
return this._sprites.get(id);
}
}
}

View File

@@ -1,9 +0,0 @@
module es {
/**
* 类中用于存储矩形值的矩形封装器
* ID参数需要连接矩形和原来插入的矩形
*/
export class IntegerRectangle extends Rectangle{
public id: number;
}
}

View File

@@ -1,278 +0,0 @@
module es {
/**
* 类用于在容器矩形内包装矩形,并具有接近最优解。
*/
export class RectanglePacker {
private _width: number = 0;
private _height: number = 0;
private _padding: number = 8;
private _packedWidth = 0;
private _packedHeight = 0;
private _insertList: SortableSize[] = [];
private _insertedRectangles: IntegerRectangle[] = [];
private _freeAreas: IntegerRectangle[] = [];
private _newFreeAreas: IntegerRectangle[] = [];
private _outsideRectangle: IntegerRectangle;
private _sortableSizeStack: SortableSize[] = [];
private _rectangleStack: IntegerRectangle[] = [];
public get rectangleCount() {
return this._insertedRectangles.length;
}
public get packedWidth() {
return this._packedWidth;
}
public get packedHeight() {
return this._packedHeight;
}
public get padding() {
return this._padding;
}
constructor(width: number, height: number, padding: number = 0) {
this._outsideRectangle = new IntegerRectangle(width + 1, height + 1, 0, 0);
this.reset(width, height, padding);
}
public reset(width: number, height: number, padding: number = 0) {
while (this._insertedRectangles.length > 0)
this.freeRectangle(this._insertedRectangles.pop());
while (this._freeAreas.length > 0)
this.freeRectangle(this._freeAreas.pop());
this._width = width;
this._height = height;
this._packedWidth = 0;
this._packedHeight = 0;
this._freeAreas.push(this.allocateRectangle(0, 0, this._width, this._height));
while (this._insertedRectangles.length > 0)
this.freeSize(this._insertList.pop());
this._padding = padding;
}
public insertRectangle(width: number, height: number, id: number) {
let sortableSize = this.allocateSize(width, height, id);
this._insertList.push(sortableSize);
}
public packRectangles(sort: boolean = true) {
if (sort)
this._insertList.sort((emp1, emp2) => {
return emp1.width - emp2.width;
});
while (this._insertList.length > 0) {
let sortableSize = this._insertList.pop();
let width = sortableSize.width;
let height = sortableSize.height;
let index = this.getFreeAreaIndex(width, height);
if (index >= 0) {
let freeArea = this._freeAreas[index];
let target = this.allocateRectangle(freeArea.x, freeArea.y, width, height);
target.id = sortableSize.id;
this.generateNewFreeAreas(target, this._freeAreas, this._newFreeAreas);
while (this._newFreeAreas.length > 0)
this._freeAreas.push(this._newFreeAreas.pop());
this._insertedRectangles.push(target);
if (target.right > this._packedWidth)
this._packedWidth = target.right;
if (target.bottom > this._packedHeight)
this._packedHeight = target.bottom;
}
this.freeSize(sortableSize);
}
return this.rectangleCount;
}
public getRectangle(index: number, rectangle: IntegerRectangle){
let inserted = this._insertedRectangles[index];
rectangle.x = inserted.x;
rectangle.y = inserted.y;
rectangle.width = inserted.width;
rectangle.height = inserted.height;
return rectangle;
}
public getRectangleId(index: number){
let inserted = this._insertedRectangles[index];
return inserted.id;
}
private generateNewFreeAreas(target: IntegerRectangle, areas: IntegerRectangle[], results: IntegerRectangle[]) {
let x = target.x;
let y = target.y;
let right = target.right + 1 + this._padding;
let bottom = target.bottom + 1 + this._padding;
let targetWithPadding: IntegerRectangle = null;
if (this._padding == 0)
targetWithPadding = target;
for (let i = areas.length - 1; i >= 0; i --){
let area = areas[i];
if (!(x >= area.right || right <= area.x || y >= area.bottom || bottom <= area.y)){
if (targetWithPadding == null)
targetWithPadding = this.allocateRectangle(target.x, target.y, target.width + this._padding, target.height + this._padding);
this.generateDividedAreas(targetWithPadding, area, results);
let topOfStack = areas.pop();
if (i < areas.length){
areas[i] = topOfStack;
}
}
}
if (targetWithPadding != null && targetWithPadding != target)
this.freeRectangle(targetWithPadding);
this.filterSelfSubAreas(results);
}
private filterSelfSubAreas(areas: IntegerRectangle[]){
for (let i = areas.length - 1; i >= 0; i --){
let filtered = areas[i];
for (let j = areas.length - 1; j >= 0; j --){
if (i != j){
let area = areas[j];
if (filtered.x >= area.x && filtered.y >= area.y && filtered.right <= area.right && filtered.bottom <= area.bottom){
this.freeRectangle(filtered);
let topOfStack = areas.pop();
if (i < areas.length){
areas[i] = topOfStack;
}
break;
}
}
}
}
}
private generateDividedAreas(divider: IntegerRectangle, area: IntegerRectangle, results: IntegerRectangle[]){
let count = 0;
let rightDelta = area.right - divider.right;
if (rightDelta > 0){
results.push(this.allocateRectangle(divider.right, area.y, rightDelta, area.height));
count ++;
}
let leftDelta = divider.x - area.x;
if (leftDelta > 0){
results.push(this.allocateRectangle(area.x, area.y, leftDelta, area.height));
count ++;
}
let bottomDelta = area.bottom - divider.bottom;
if (bottomDelta > 0){
results.push(this.allocateRectangle(area.x, divider.bottom, area.width, bottomDelta));
count ++;
}
let topDelta = divider.y - area.y;
if (topDelta > 0){
results.push(this.allocateRectangle(area.x, area.y, area.width, topDelta));
count ++;
}
if (count == 0 && (divider.width < area.width || divider.height < area.height)){
results.push(area);
}else{
this.freeRectangle(area);
}
}
private getFreeAreaIndex(width: number, height: number) {
let best = this._outsideRectangle;
let index = -1;
let paddedWidth = width + this._padding;
let paddedHeight = height + this._padding;
let count = this._freeAreas.length;
for (let i = count - 1; i >= 0; i--) {
let free = this._freeAreas[i];
if (free.x < this._packedWidth || free.y < this.packedHeight) {
if (free.x < best.x && paddedWidth <= free.width && paddedHeight <= free.height) {
index = i;
if ((paddedWidth == free.width && free.width <= free.height && free.right < this._width) ||
(paddedHeight == free.height && free.height <= free.width)) {
break;
}
best = free;
}
} else {
if (free.x < best.x && width <= free.width && height <= free.height) {
index = i;
if ((width == free.width && free.width <= free.height && free.right < this._width) ||
(height == free.height && free.height <= free.width)) {
break;
}
best = free;
}
}
}
return index;
}
private allocateSize(width: number, height: number, id: number): SortableSize {
if (this._sortableSizeStack.length > 0) {
let size: SortableSize = this._sortableSizeStack.pop();
size.width = width;
size.height = height;
size.id = id;
return size;
}
return new SortableSize(width, height, id);
}
private freeSize(size: SortableSize) {
this._sortableSizeStack.push(size);
}
private allocateRectangle(x: number, y: number, width: number, height: number) {
if (this._rectangleStack.length > 0) {
let rectangle = this._rectangleStack.pop();
rectangle.x = x;
rectangle.y = y;
rectangle.width = width;
rectangle.height = height;
rectangle.right = x + width;
rectangle.bottom = y + height;
return rectangle;
}
return new IntegerRectangle(x, y, width, height);
}
private freeRectangle(rectangle: IntegerRectangle) {
this._rectangleStack.push(rectangle);
}
}
}

View File

@@ -1,16 +0,0 @@
module es {
/**
* 用于根据维度对插入的矩形进行排序
*/
export class SortableSize {
public width: number;
public height: number;
public id: number;
constructor(width: number, height: number, id: number){
this.width = width;
this.height = height;
this.id = id;
}
}
}

View File

@@ -1,16 +0,0 @@
module es {
export class TextureAssets {
public assets: TextureAsset[];
constructor(assets: TextureAsset[]){
this.assets = assets;
}
}
export class TextureAsset {
public x: number;
public y: number;
public width: number;
public height: number;
public name: string;
}
}

View File

@@ -1,11 +0,0 @@
module es {
export class TextureToPack {
public texture: egret.Texture;
public id: string;
constructor(texture: egret.Texture, id: string){
this.texture = texture;
this.id = id;
}
}
}