新增相机震动、场景组件

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

@@ -348,6 +348,7 @@ declare module es {
readonly entities: EntityList;
readonly renderableComponents: RenderableComponentList;
readonly entityProcessors: EntityProcessorList;
readonly _sceneComponents: SceneComponent[];
_renderers: Renderer[];
readonly _postProcessors: PostProcessor[];
_didSceneBegin: any;
@@ -363,6 +364,10 @@ declare module es {
update(): void;
render(): void;
postRender(): void;
addSceneComponent<T extends SceneComponent>(component: T): T;
getSceneComponent<T extends SceneComponent>(type: any): T;
getOrCreateSceneComponent<T extends SceneComponent>(type: any): T;
removeSceneComponent(component: SceneComponent): void;
addRenderer<T extends Renderer>(renderer: T): T;
getRenderer<T extends Renderer>(type: any): T;
removeRenderer(renderer: Renderer): void;
@@ -521,6 +526,16 @@ declare module es {
protected updateMatrixes(): void;
}
}
declare module es {
class CameraShake extends Component {
_shakeDirection: Vector2;
_shakeOffset: Vector2;
_shakeIntensity: number;
_shakeDegredation: number;
shake(shakeIntensify?: number, shakeDegredation?: number, shakeDirection?: Vector2): void;
update(): void;
}
}
declare module es {
class ComponentPool<T extends PooledComponent> {
private _cache;
@@ -568,12 +583,18 @@ declare module es {
}
}
declare module es {
class Mesh extends RenderableComponent {
private _mesh;
constructor();
setTexture(texture: egret.Texture): Mesh;
reset(): void;
render(camera: es.Camera): void;
class SceneComponent {
scene: Scene;
enabled: boolean;
updateOrder: number;
_enabled: boolean;
onEnabled(): void;
onDisabled(): void;
onRemovedFromScene(): void;
update(): void;
setEnabled(isEnabled: boolean): SceneComponent;
setUpdateOrder(updateOrder: number): this;
compareTo(other: SceneComponent): number;
}
}
declare module es {

View File

@@ -1615,6 +1615,7 @@ var es;
function Scene() {
var _this = _super.call(this) || this;
_this.enablePostProcessing = true;
_this._sceneComponents = [];
_this._renderers = [];
_this._postProcessors = [];
_this.entities = new es.EntityList(_this);
@@ -1676,6 +1677,10 @@ var es;
}
this.entities.removeAllEntities();
this.removeChildren();
for (var i = 0; i < this._sceneComponents.length; i++) {
this._sceneComponents[i].onRemovedFromScene();
}
this._sceneComponents.length = 0;
this.camera = null;
this.content.dispose();
if (this.entityProcessors)
@@ -1686,6 +1691,10 @@ var es;
};
Scene.prototype.update = function () {
this.entities.updateLists();
for (var i = this._sceneComponents.length - 1; i >= 0; i--) {
if (this._sceneComponents[i].enabled)
this._sceneComponents[i].update();
}
if (this.entityProcessors)
this.entityProcessors.update();
this.entities.update();
@@ -1711,6 +1720,35 @@ var es;
}
}
};
Scene.prototype.addSceneComponent = function (component) {
component.scene = this;
component.onEnabled();
this._sceneComponents.push(component);
this._sceneComponents.sort(component.compareTo);
return component;
};
Scene.prototype.getSceneComponent = function (type) {
for (var i = 0; i < this._sceneComponents.length; i++) {
var component = this._sceneComponents[i];
if (component instanceof type)
return component;
}
return null;
};
Scene.prototype.getOrCreateSceneComponent = function (type) {
var comp = this.getSceneComponent(type);
if (comp == null)
comp = this.addSceneComponent(new type());
return comp;
};
Scene.prototype.removeSceneComponent = function (component) {
if (!this._sceneComponents.contains(component)) {
console.warn("SceneComponent" + component + "\u4E0D\u5728SceneComponents\u5217\u8868\u4E2D!");
return;
}
this._sceneComponents.remove(component);
component.onRemovedFromScene();
};
Scene.prototype.addRenderer = function (renderer) {
this._renderers.push(renderer);
this._renderers.sort();
@@ -2494,6 +2532,55 @@ var es;
es.Camera = Camera;
})(es || (es = {}));
var es;
(function (es) {
var CameraShake = (function (_super) {
__extends(CameraShake, _super);
function CameraShake() {
var _this = _super !== null && _super.apply(this, arguments) || this;
_this._shakeDirection = es.Vector2.zero;
_this._shakeOffset = es.Vector2.zero;
_this._shakeIntensity = 0;
_this._shakeDegredation = 0.95;
return _this;
}
CameraShake.prototype.shake = function (shakeIntensify, shakeDegredation, shakeDirection) {
if (shakeIntensify === void 0) { shakeIntensify = 15; }
if (shakeDegredation === void 0) { shakeDegredation = 0.9; }
if (shakeDirection === void 0) { shakeDirection = es.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;
}
};
CameraShake.prototype.update = function () {
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;
}
this._shakeOffset.multiply(new es.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);
};
return CameraShake;
}(es.Component));
es.CameraShake = CameraShake;
})(es || (es = {}));
var es;
(function (es) {
var ComponentPool = (function () {
function ComponentPool(typeClass) {
@@ -2662,25 +2749,52 @@ var es;
})(es || (es = {}));
var es;
(function (es) {
var Mesh = (function (_super) {
__extends(Mesh, _super);
function Mesh() {
var _this = _super.call(this) || this;
_this._mesh = new egret.Mesh();
return _this;
var SceneComponent = (function () {
function SceneComponent() {
this.updateOrder = 0;
this._enabled = true;
}
Mesh.prototype.setTexture = function (texture) {
this._mesh.texture = texture;
this._mesh.$renderNode = new egret.sys.RenderNode();
Object.defineProperty(SceneComponent.prototype, "enabled", {
get: function () {
return this._enabled;
},
set: function (value) {
this.setEnabled(value);
},
enumerable: true,
configurable: true
});
SceneComponent.prototype.onEnabled = function () {
};
SceneComponent.prototype.onDisabled = function () {
};
SceneComponent.prototype.onRemovedFromScene = function () {
};
SceneComponent.prototype.update = function () {
};
SceneComponent.prototype.setEnabled = function (isEnabled) {
if (this._enabled != isEnabled) {
this._enabled = isEnabled;
if (this._enabled) {
}
else {
}
}
return this;
};
Mesh.prototype.reset = function () {
SceneComponent.prototype.setUpdateOrder = function (updateOrder) {
if (this.updateOrder != updateOrder) {
this.updateOrder = updateOrder;
es.Core.scene._sceneComponents.sort(this.compareTo);
}
return this;
};
Mesh.prototype.render = function (camera) {
SceneComponent.prototype.compareTo = function (other) {
return this.updateOrder - other.updateOrder;
};
return Mesh;
}(es.RenderableComponent));
es.Mesh = Mesh;
return SceneComponent;
}());
es.SceneComponent = SceneComponent;
})(es || (es = {}));
var es;
(function (es) {

File diff suppressed because one or more lines are too long

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