新增ninja adventure例子

This commit is contained in:
YHH
2020-08-23 22:09:22 +08:00
parent 6c1cfec928
commit 7345a17d24
26 changed files with 400 additions and 27 deletions

View File

@@ -1,4 +1,8 @@
module es {
import RenderTexture = egret.RenderTexture;
import Bitmap = egret.Bitmap;
import SpriteSheet = egret.SpriteSheet;
export class Sprite {
public texture2D: egret.Texture;
public readonly sourceRect: Rectangle;
@@ -22,5 +26,37 @@ module es {
this.uvs.width = sourceRect.width * inverseTexW;
this.uvs.height = sourceRect.height * inverseTexH;
}
/**
* 提供一个精灵的列/行等间隔的图集的精灵列表
* @param texture
* @param cellWidth
* @param cellHeight
* @param cellOffset 处理时要包含的第一个单元格。基于0的索引
* @param maxCellsToInclude 包含的最大单元
*/
public static spritesFromAtlas(texture: egret.Texture, cellWidth: number, cellHeight: number,
cellOffset: number = 0, maxCellsToInclude: number = Number.MAX_VALUE){
let sprites: Sprite[] = [];
let cols = texture.textureWidth / cellWidth;
let rows = texture.textureHeight / cellHeight;
let i = 0;
let spriteSheet = new SpriteSheet(texture);
for (let y = 0; y < rows; y ++){
for (let x = 0; x < cols; x ++) {
if (i++ < cellOffset) continue;
let texture = spriteSheet.getTexture(`${y}_${x}`);
if (!texture)
texture = spriteSheet.createTexture(`${y}_${x}`, x * cellWidth, y * cellHeight, cellWidth, cellHeight);
sprites.push(new Sprite(texture));
if (sprites.length == maxCellsToInclude) return sprites;
}
}
return sprites;
}
}
}

View File

@@ -82,7 +82,7 @@ module es {
this.animationState = State.completed;
this._elapsedTime = 0;
this.currentFrame = 0;
this.sprite = animation.sprites[this.currentFrame];
(this.displayObject as egret.Bitmap).texture = animation.sprites[this.currentFrame].texture2D;
return;
}
@@ -97,7 +97,7 @@ module es {
this.currentFrame = i % n;
}
this.sprite = animation.sprites[this.currentFrame];
(this.displayObject as egret.Bitmap).texture = animation.sprites[this.currentFrame].texture2D;
}
/**
@@ -123,8 +123,7 @@ module es {
this.currentAnimationName = name;
this.currentFrame = 0;
this.animationState = State.running;
this.sprite = this.currentAnimation.sprites[0];
(this.displayObject as egret.Bitmap).texture = this.currentAnimation.sprites[0].texture2D;
this._elapsedTime = 0;
this._loopMode = loopMode ? loopMode : LoopMode.loop;
}

View File

@@ -203,7 +203,7 @@ module es {
}
if (this._nextScene) {
this.removeChild(this._scene);
if (this._scene.parent) this._scene.parent.removeChild(this._scene);
this._scene.end();
this._scene = this._nextScene;

View File

@@ -157,7 +157,8 @@ module es {
public handleRemove(component: Component) {
// 处理渲染层列表
if (component instanceof RenderableComponent) {
this._entity.scene.removeChild(component.displayObject);
if (component.displayObject.parent)
component.displayObject.parent.removeChild(component.displayObject);
this._entity.scene.renderableComponents.remove(component);
}

View File

@@ -109,7 +109,7 @@ module es {
let component = this._components[i] as RenderableComponent;
let egretDisplayObject = scene.$children.find(a => {return a.hashCode == component.displayObject.hashCode});
let displayIndex = scene.getChildIndex(egretDisplayObject);
if (displayIndex != i) scene.swapChildrenAt(displayIndex, i);
if (displayIndex != -1 && displayIndex != i) scene.swapChildrenAt(displayIndex, i);
}
}
}

View File

@@ -0,0 +1,32 @@
module es {
/**
* 它存储值直到累计的总数大于1。一旦超过1该值将在调用update时添加到amount中
* 一般用法如下:
*
* let deltaMove = this.velocity * es.Time.deltaTime;
* deltaMove.x = this._x.update(deltaMove.x);
* deltaMove.y = this._y.update(deltaMove.y);
*/
export class SubpixelFloat {
public remainder: number = 0;
/**
* 以amount递增余数将值截断存储新的余数并将amount设置为当前值
* @param amount
*/
public update(amount: number){
this.remainder += amount;
let motion = Math.trunc(this.remainder);
this.remainder -= motion;
amount = motion;
return amount;
}
/**
* 将余数重置为0
*/
public reset(){
this.remainder = 0;
}
}
}

View File

@@ -0,0 +1,23 @@
module es {
export class SubpixelVector2 {
public _x: SubpixelFloat = new SubpixelFloat();
public _y: SubpixelFloat = new SubpixelFloat();
/**
* 以数量递增s/y余数将值截断为整数存储新的余数并将amount设置为当前值
* @param amount
*/
public update(amount: Vector2) {
amount.x = this._x.update(amount.x);
amount.y = this._y.update(amount.y);
}
/**
* 将余数重置为0
*/
public reset(){
this._x.reset();
this._y.reset();
}
}
}

View File

@@ -34,7 +34,8 @@ module es {
public dispose() {
this.loadedAssets.forEach(value => {
let assetsToRemove = value;
assetsToRemove.dispose();
if (RES.destroyRes(assetsToRemove))
assetsToRemove.dispose();
});
this.loadedAssets.clear();