新增相机震动、场景组件

This commit is contained in:
yhh
2020-08-11 11:07:20 +08:00
parent 14b70b307c
commit a3dbff63f0
11 changed files with 562 additions and 82 deletions

View File

@@ -0,0 +1,50 @@
module es {
export class CameraShake extends Component {
public _shakeDirection: Vector2 = Vector2.zero;
public _shakeOffset: Vector2 = Vector2.zero;
public _shakeIntensity = 0;
public _shakeDegredation = 0.95;
/**
* 如果震动已经在运行,只有震动强度>当前shakeIntensity, 将覆盖当前值
* 如果shake当前不是活动的它将被启动。
* @param shakeIntensify 震动强度
* @param shakeDegredation 较高的值会导致更快的停止震动
* @param shakeDirection 0只会导致x/y轴上的振动。任何其他的值将导致通过在抖动方向*强度是相机移动偏移
*/
public shake(shakeIntensify = 15, shakeDegredation = 0.9, shakeDirection = Vector2.zero){
this.enabled = true;
if (this._shakeIntensity < shakeIntensify) {
this._shakeDirection = shakeDirection;
this._shakeIntensity = shakeIntensify;
if (shakeDegredation < 0 || shakeDegredation >= 1){
shakeDegredation = 0.95;
}
this._shakeDegredation = shakeDegredation;
}
}
public update() {
if (Math.abs(this._shakeIntensity) > 0){
this._shakeOffset = this._shakeDirection;
if (this._shakeOffset.x != 0 || this._shakeOffset.y != 0){
this._shakeOffset.normalize();
}else{
this._shakeOffset.x = this._shakeOffset.x + Math.random() - 0.5;
this._shakeOffset.y = this._shakeOffset.y + Math.random() - 0.5;
}
// TODO: 这需要乘相机变焦
this._shakeOffset.multiply(new Vector2(this._shakeIntensity));
this._shakeIntensity *= -this._shakeDegredation;
if (Math.abs(this._shakeIntensity) <= 0.01){
this._shakeIntensity = 0;
this.enabled = false;
}
}
this.entity.scene.camera.position.add(this._shakeOffset);
}
}
}

View File

@@ -1,25 +0,0 @@
///<reference path="./RenderableComponent.ts" />
module es {
export class Mesh extends RenderableComponent {
private _mesh: egret.Mesh;
constructor() {
super();
this._mesh = new egret.Mesh();
}
public setTexture(texture: egret.Texture): Mesh {
this._mesh.texture = texture;
this._mesh.$renderNode = new egret.sys.RenderNode();
return this;
}
public reset() {
}
render(camera: es.Camera) {
}
}
}

View File

@@ -0,0 +1,89 @@
module es {
export class SceneComponent {
/**
* 这个场景组件被附加到的场景
*/
public scene: Scene;
/**
* 如果启用了SceneComponent则为true。状态的改变会导致调用onEnabled/onDisable。
*/
public get enabled(){
return this._enabled;
}
/**
* 如果启用了SceneComponent则为true。状态的改变会导致调用onEnabled/onDisable。
* @param value
*/
public set enabled(value: boolean){
this.setEnabled(value);
}
/**
* 更新此场景中SceneComponents的顺序
*/
public updateOrder: number = 0;
public _enabled: boolean = true;
/**
* 在启用此SceneComponent时调用
*/
public onEnabled(){
}
/**
* 当禁用此SceneComponent时调用
*/
public onDisabled(){
}
/**
* 当该SceneComponent从场景中移除时调用
*/
public onRemovedFromScene(){
}
/**
* 在实体更新之前每一帧调用
*/
public update(){
}
/**
* 启用/禁用这个SceneComponent
* @param isEnabled
*/
public setEnabled(isEnabled: boolean): SceneComponent{
if (this._enabled != isEnabled){
this._enabled = isEnabled;
if (this._enabled){
}else{
}
}
return this;
}
/**
* 设置SceneComponent的updateOrder并触发某种SceneComponent
* @param updateOrder
*/
public setUpdateOrder(updateOrder: number){
if (this.updateOrder != updateOrder){
this.updateOrder = updateOrder;
Core.scene._sceneComponents.sort(this.compareTo);
}
return this;
}
public compareTo(other: SceneComponent): number{
return this.updateOrder - other.updateOrder;
}
}
}

View File

@@ -27,6 +27,7 @@ module es {
*/
public readonly entityProcessors: EntityProcessorList;
public readonly _sceneComponents: SceneComponent[] = [];
public _renderers: Renderer[] = [];
public readonly _postProcessors: PostProcessor[] = [];
public _didSceneBegin;
@@ -120,6 +121,11 @@ module es {
this.entities.removeAllEntities();
this.removeChildren();
for (let i = 0; i < this._sceneComponents.length; i ++){
this._sceneComponents[i].onRemovedFromScene();
}
this._sceneComponents.length = 0;
this.camera = null;
this.content.dispose();
@@ -136,6 +142,11 @@ module es {
// 更新我们的列表,以防它们有任何变化
this.entities.updateLists();
for (let i = this._sceneComponents.length - 1; i >= 0; i --){
if (this._sceneComponents[i].enabled)
this._sceneComponents[i].update();
}
// 更新我们的实体解析器
if (this.entityProcessors)
this.entityProcessors.update();
@@ -175,6 +186,58 @@ module es {
}
}
/**
* 向组件列表添加并返回SceneComponent
* @param component
*/
public addSceneComponent<T extends SceneComponent>(component: T): T {
component.scene = this;
component.onEnabled();
this._sceneComponents.push(component);
this._sceneComponents.sort(component.compareTo);
return component;
}
/**
* 获取类型为T的第一个SceneComponent并返回它。如果没有找到组件则返回null。
* @param type
*/
public getSceneComponent<T extends SceneComponent>(type){
for (let i = 0; i < this._sceneComponents.length; i ++){
let component = this._sceneComponents[i];
if (component instanceof type)
return component as T;
}
return null;
}
/**
* 获取类型为T的第一个SceneComponent并返回它。如果没有找到SceneComponent则将创建SceneComponent。
* @param type
*/
public getOrCreateSceneComponent<T extends SceneComponent>(type){
let comp = this.getSceneComponent<T>(type);
if (comp == null)
comp = this.addSceneComponent<T>(new type());
return comp;
}
/**
* 从SceneComponents列表中删除一个SceneComponent
* @param component
*/
public removeSceneComponent(component: SceneComponent){
if (!this._sceneComponents.contains(component)){
console.warn(`SceneComponent${component}不在SceneComponents列表中!`);
return;
}
this._sceneComponents.remove(component);
component.onRemovedFromScene();
}
/**
* 为场景添加一个渲染器
* @param renderer