Commit 9b7b3989 authored by XieChuanJin's avatar XieChuanJin

更新mgr、pool机制

parent 78b9c9db
This source diff could not be displayed because it is too large. You can view the blob instead.
import GameMgr from "../Mgr/GameMgr";
import { DataMgr } from "../Mgr/DataMgr";
/* type AltaData = { /* type AltaData = {
key: string, key: string,
frameRate: number, frameRate: number,
...@@ -15,7 +18,36 @@ interface Frame { ...@@ -15,7 +18,36 @@ interface Frame {
texture: engine.Texture texture: engine.Texture
} }
export class FrameAnimationMgr extends GameMgr {
constructor() { super("FrameAnimationMgr") }
public playingAnimations: FrameAnimation[] = [];
protected onUpdate() {
if (this.pause) return;
const filters: FrameAnimation[] = [];
let anim: FrameAnimation = null;
for (let i = 0; i < this.playingAnimations.length; i++) {
anim = this.playingAnimations[i];
anim.onUpdate();
if (anim.curPos >= anim.frameCount) {
anim.onCompleted && anim.onCompleted();
} else {
filters.push(anim);
}
}
this.playingAnimations = filters;
}
}
export default class FrameAnimation extends engine.Container { export default class FrameAnimation extends engine.Container {
private frames: Frame[] = [];
public frameCount: number = 0;
public curPos: number = 0;
private frameTimer: number = 0;
private frameRate: number = 60;
private sprite: engine.Sprite = new engine.Sprite;
private static caches: { private static caches: {
[key: string]: { [key: string]: {
frameCount: number, frameCount: number,
...@@ -23,6 +55,7 @@ export default class FrameAnimation extends engine.Container { ...@@ -23,6 +55,7 @@ export default class FrameAnimation extends engine.Container {
frames: Frame[] frames: Frame[]
} }
} = {}; } = {};
public static loadRes(texture: engine.Texture, egretData: any) { public static loadRes(texture: engine.Texture, egretData: any) {
let name: string = null; let name: string = null;
const mc = egretData.mc; const mc = egretData.mc;
...@@ -55,21 +88,6 @@ export default class FrameAnimation extends engine.Container { ...@@ -55,21 +88,6 @@ export default class FrameAnimation extends engine.Container {
} }
} }
private static playingAnimations: FrameAnimation[] = [];
public static onUpdate() {
const list = FrameAnimation.playingAnimations;
for (let i = 0; i < list.length; i++) {
list[i].onUpdate();
}
}
private frames: Frame[] = [];
private frameCount: number = 0;
public curPos: number = 0;
private frameTimer: number = 0;
private frameRate: number = 60;
private sprite: engine.Sprite = new engine.Sprite;
constructor(key: string) { constructor(key: string) {
super(); super();
if (!FrameAnimation.caches[key]) { if (!FrameAnimation.caches[key]) {
...@@ -77,16 +95,6 @@ export default class FrameAnimation extends engine.Container { ...@@ -77,16 +95,6 @@ export default class FrameAnimation extends engine.Container {
return; return;
} }
/* for (let i = 0; i < FrameAnimation.caches[key].frameCount; i++) {
let frame = engine.Texture.from(key + "_" + i);
if (frame) {
this.frames.push(frame);
} else {
console.error(`can not find frame ${i} of ${key}`);
return;
}
} */
this.frames = FrameAnimation.caches[key].frames; this.frames = FrameAnimation.caches[key].frames;
this.frameCount = Object.keys(this.frames).length; this.frameCount = Object.keys(this.frames).length;
this.frameRate = FrameAnimation.caches[key].frameRate; this.frameRate = FrameAnimation.caches[key].frameRate;
...@@ -96,8 +104,7 @@ export default class FrameAnimation extends engine.Container { ...@@ -96,8 +104,7 @@ export default class FrameAnimation extends engine.Container {
onUpdate() { onUpdate() {
if (this.frameTimer >= 60 / this.frameRate) { if (this.frameTimer >= 60 / this.frameRate) {
if (this.curPos >= this.frameCount) { if (this.curPos >= this.frameCount) {
this.onCompleted && this.onCompleted(); return;
FrameAnimation.playingAnimations.splice(FrameAnimation.playingAnimations.indexOf(this), 1);
} else { } else {
const frameData = this.frames[this.curPos]; const frameData = this.frames[this.curPos];
this.sprite.texture = frameData.texture; this.sprite.texture = frameData.texture;
...@@ -112,7 +119,7 @@ export default class FrameAnimation extends engine.Container { ...@@ -112,7 +119,7 @@ export default class FrameAnimation extends engine.Container {
} }
play() { play() {
FrameAnimation.playingAnimations.push(this); DataMgr.game._animationMgr.playingAnimations.push(this);
} }
onCompleted: () => void; onCompleted: () => void;
......
import { MUtils } from "../Global/MUtils"; import { MUtils } from "../Global/MUtils";
import MTimer from "../Global/MTimer"; import MTimer from "../Global/MTimer";
import GameMgr from "../Mgr/GameMgr";
class MTweenObject { class MTweenObject {
public static readonly frameRate = 60; public static readonly frameRate = 60;
...@@ -145,57 +146,39 @@ class CallbackTween extends MTweenBase { ...@@ -145,57 +146,39 @@ class CallbackTween extends MTweenBase {
this.callback = null; this.callback = null;
} }
} }
export class MTween extends GameMgr {
export namespace MTween { constructor() { super("TweenMgr") }
export let pause: boolean = false;
private tweenObjectList: MTweenObject[] = [];
let tweenObjectList: MTweenObject[] = []; private removeQueue: any[] = [];
// let tweenAnimationList: TweenAnimation[] = []; private isTraversing = false;
public removeTweens(target: object) {
let removeQueue: any[] = []; if (this.isTraversing) {
let isTraversing = false; this.removeQueue.push(target);
export function init() {
// engine.gameStage.addEventListener(engine.Event.ENTER_FRAME, update, this);
MTimer.onFrame("MTween", update, this)
}
/* export function destroy() {
engine.gameStage.removeEventListener(engine.Event.ENTER_FRAME, update, this);
} */
export function removeTweens(target: object) {
if (isTraversing) {
removeQueue.push(target);
} else { } else {
tweenObjectList = tweenObjectList.filter(e => e.target !== target); this.tweenObjectList = this.tweenObjectList.filter(e => e.target !== target);
} }
} }
export function get(target: object) { public get(target: object) {
let tween = new MTweenObject(target); let tween = new MTweenObject(target);
tweenObjectList.push(tween); this.tweenObjectList.push(tween);
return tween; return tween;
} }
/* export function createAnimation(target: object) { public onUpdate() {
return new TweenAnimation(target); if (this.pause) return;
} */
function update() {
if (pause) return;
isTraversing = true; this.isTraversing = true;
for (let i = 0; i <= tweenObjectList.length - 1; i++) { for (let i = 0; i <= this.tweenObjectList.length - 1; i++) {
tweenObjectList[i].update(); this.tweenObjectList[i].update();
} }
isTraversing = false; this.isTraversing = false;
tweenObjectList = tweenObjectList.filter((tween) => { this.tweenObjectList = this.tweenObjectList.filter((tween) => {
if (tween.currentTween == null) return false; if (tween.currentTween == null) return false;
for (let removeTarget of removeQueue) { for (let removeTarget of this.removeQueue) {
if (removeTarget === tween.target) { if (removeTarget === tween.target) {
return false; return false;
} }
...@@ -204,53 +187,7 @@ export namespace MTween { ...@@ -204,53 +187,7 @@ export namespace MTween {
return true; return true;
}); });
removeQueue = []; this.removeQueue = [];
/* const filter: TweenAnimation[] = [];
let temp: TweenAnimation = null;
for (let i = 0; i < tweenAnimationList.length; i++) {
temp = tweenAnimationList[i];
temp.update();
if (temp.currentTween != null) {
filter.push(temp);
}
}
tweenAnimationList = filter; */
} }
/* export class TweenAnimation extends MTweenObject {
protected addTween(tween: MTweenBase) {
this.tweenList.push(tween);
}
private pos: number = 0;
public update() {
if (!this.isPlaying) return;
this.currentTween.apply(MTimer.dtFactor);
if (this.currentTween.frameCount <= 0) {
this.pos++;
if (this.pos < this.tweenList.length) {
this.currentTween = this.tweenList[this.pos];
} else {
this.isPlaying = false;
this.currentTween = null;
}
}
}
public play() {
if (this.isPlaying) return;
this.isPlaying = true;
this.pos = 0;
this.currentTween = this.tweenList[0];
tweenAnimationList.push(this);
}
// public onCompleted: () => void;
private isPlaying: boolean = false;
// private inited: boolean = false;
} */
} }
import { MUtils } from "../Global/MUtils";
import { MTween } from "./MTween";
export abstract class PoolGroup<T extends PoolElement>{ export abstract class PoolGroup<T extends PoolElement>{
constructor(protected layer: engine.Container) { }
protected data: { [key: string]: Pool<T> } = {} protected data: { [key: string]: Pool<T> } = {}
public recycle(key: string, element: T) { public recycle(key: string, element: T) {
this.data[key].recycle(element); this.data[key].recycle(element);
...@@ -11,6 +9,8 @@ export abstract class PoolGroup<T extends PoolElement>{ ...@@ -11,6 +9,8 @@ export abstract class PoolGroup<T extends PoolElement>{
} }
export abstract class Pool<T extends PoolElement>{ export abstract class Pool<T extends PoolElement>{
constructor(protected layer: engine.Container) { }
protected data: T[] = []; protected data: T[] = [];
public recycle(element: T) { public recycle(element: T) {
......
...@@ -8,13 +8,7 @@ import Drop from "./Drop"; ...@@ -8,13 +8,7 @@ import Drop from "./Drop";
import Bullet from "./Bullet"; import Bullet from "./Bullet";
import { MUtils } from "../Global/MUtils"; import { MUtils } from "../Global/MUtils";
import SoundMgr from "../Mgr/SoundMgr"; import SoundMgr from "../Mgr/SoundMgr";
import Game from "./Game";
import BoomEffect from "./BoomEffect";
import ballPool from "../Pools/BallPool";
import dropPool from "../Pools/DropPool";
import { EffectMgr } from "../Mgr/EffectMgr";
import { DataMgr } from "../Mgr/DataMgr"; import { DataMgr } from "../Mgr/DataMgr";
import SpriteFontLabel, { SpriteFont } from "../Component/SpriteFont";
import GuideMgr from "../Mgr/GuideMgr"; import GuideMgr from "../Mgr/GuideMgr";
import MTimer from "../Global/MTimer"; import MTimer from "../Global/MTimer";
...@@ -61,12 +55,12 @@ export default class Ball extends MoveObjcet implements PoolElement { ...@@ -61,12 +55,12 @@ export default class Ball extends MoveObjcet implements PoolElement {
this._score = v; this._score = v;
} }
private static ballTextures: engine.Texture[] = []; // private static ballTextures: engine.Texture[] = [];
public static loadTextures() { /* public static loadTextures() {
for (let i in imageNames) { for (let i in imageNames) {
Ball.ballTextures[i] = RES.getRes(imageNames[i]); Ball.ballTextures[i] = RES.getRes(imageNames[i]);
} }
} } */
private onScoreIsZero() { private onScoreIsZero() {
if (GuideMgr.instance.guideFlag == true) { if (GuideMgr.instance.guideFlag == true) {
...@@ -79,9 +73,9 @@ export default class Ball extends MoveObjcet implements PoolElement { ...@@ -79,9 +73,9 @@ export default class Ball extends MoveObjcet implements PoolElement {
} }
} }
EffectMgr.playBoomEffect(this.position, this.scaleRatio); DataMgr.game._playBoomEffect(this.position, this.scaleRatio);
SoundMgr.instance.playEffect("boom"); SoundMgr.instance.playEffect("boom");
ballPool.recycle(this.poolKey, this); DataMgr.game._pool.ball.recycle(this.poolKey, this);
} }
constructor(sizeIndex: number) { constructor(sizeIndex: number) {
...@@ -122,7 +116,7 @@ export default class Ball extends MoveObjcet implements PoolElement { ...@@ -122,7 +116,7 @@ export default class Ball extends MoveObjcet implements PoolElement {
init(colorIndex: number, direction: 1 | -1, score: number): Ball { init(colorIndex: number, direction: 1 | -1, score: number): Ball {
this.colorIndex = colorIndex; this.colorIndex = colorIndex;
this.initColorIndex = colorIndex; this.initColorIndex = colorIndex;
this.bitmap.texture = Ball.ballTextures[colorIndex]; this.bitmap.texture = RES.getRes(imageNames[colorIndex]);
this.initScore = score; this.initScore = score;
this.score = score; this.score = score;
...@@ -131,13 +125,6 @@ export default class Ball extends MoveObjcet implements PoolElement { ...@@ -131,13 +125,6 @@ export default class Ball extends MoveObjcet implements PoolElement {
} }
startBornStage(dir: 1 | -1) { startBornStage(dir: 1 | -1) {
/* this.x = 200;
this.y = 200;
EffectMgr.playBoomEffect(this.position, this.scaleRatio);
return; */
this.isBornStage = true; this.isBornStage = true;
if (dir == 1) { if (dir == 1) {
this.x = 0 - this.width / 2 - 10; this.x = 0 - this.width / 2 - 10;
...@@ -188,7 +175,7 @@ export default class Ball extends MoveObjcet implements PoolElement { ...@@ -188,7 +175,7 @@ export default class Ball extends MoveObjcet implements PoolElement {
this.physics.velocity.y = -(MConst.BallVelocityY * (1 + ((1 / this.scaleRatio) - 1) * MUtils.random(0, MConst.BallVelocityYRandomFactor))); this.physics.velocity.y = -(MConst.BallVelocityY * (1 + ((1 / this.scaleRatio) - 1) * MUtils.random(0, MConst.BallVelocityYRandomFactor)));
//播放灰尘动画 //播放灰尘动画
const clip = EffectMgr.createAnimation("duang"); const clip = DataMgr.game._createAnimation("duang");
clip.x = this.x - 140 * this.scaleRatio; clip.x = this.x - 140 * this.scaleRatio;
clip.y = this.y - 50 * this.scaleRatio; clip.y = this.y - 50 * this.scaleRatio;
clip.scaleX = clip.scaleY = this.scaleRatio; clip.scaleX = clip.scaleY = this.scaleRatio;
...@@ -236,8 +223,7 @@ export default class Ball extends MoveObjcet implements PoolElement { ...@@ -236,8 +223,7 @@ export default class Ball extends MoveObjcet implements PoolElement {
let colorIndex = Math.max(this.initColorIndex - 1, 0); let colorIndex = Math.max(this.initColorIndex - 1, 0);
let callback = (direction: 1 | -1) => { let callback = (direction: 1 | -1) => {
let ball = ballPool.spwan(sizeIndex).init(colorIndex, direction, score); let ball = DataMgr.game._pool.ball.spwan(sizeIndex).init(colorIndex, direction, score);
DataMgr.game.addGameObject(ball, "Ball");
ball.x = this.x; ball.x = this.x;
ball.y = this.y; ball.y = this.y;
ball.startGravityStage(direction); ball.startGravityStage(direction);
...@@ -302,9 +288,7 @@ export default class Ball extends MoveObjcet implements PoolElement { ...@@ -302,9 +288,7 @@ export default class Ball extends MoveObjcet implements PoolElement {
let drops: Drop[] = []; let drops: Drop[] = [];
for (let id of dropIds) { for (let id of dropIds) {
let drop = dropPool.spwan(id.toString()); drops.push(DataMgr.game._pool.drop.spwan(id.toString()));
DataMgr.game.addGameObject(drop, "Drop");
drops.push(drop);
} }
let dir = this.x > MConst.DesignResolution.width / 2 ? -1 : 1; let dir = this.x > MConst.DesignResolution.width / 2 ? -1 : 1;
......
import { MConst } from "../Global/MConst";
import { MUtils } from "../Global/MUtils";
import { MTween } from "../Component/MTween";
import { TextureMgr } from "../Mgr/TextureMgr";
import { PoolElement, Pool, PoolGroup } from "../Component/Pool";
const BoomEffectDuration = 400;
const DefaultRingWidth = 672;
export default class BoomEffect {
constructor(position: engine.Point, parent: engine.Container, colorIndex: number, size: number) {
let range = MConst.DefaultBallWidth * 1.5 * size;
for (let i = 0; i <= 7; i++) {
const star = spritePool.spwan("star", colorIndex);
parent.addChild(star);
const randomMax = 0.5 + (size - 0.3125) / 2;
const scale = MUtils.random(randomMax - 0.64 * size, randomMax);
star.width = MConst.DefaultStarSize.width * scale;
star.height = MConst.DefaultStarSize.height * scale;
star.x = position.x;
star.y = position.y;
MTween.get(star)
.to({
x: position.x + MUtils.random(-1, 1) * range - star.width / 2,
y: position.y + MUtils.random(-1, 1) * range - star.height / 2,
alpha: 0
}, BoomEffectDuration, false)
.call(() => {
spritePool.recycle(star.poolKey, star);
});
}
//环
for (let i = 0; i <= 2; i++) {
let ring = spritePool.spwan("ring", colorIndex);
parent.addChild(ring);
ring.scaleX = ring.scaleY = 0.186 * (i + 1) * size;
ring.x = position.x - DefaultRingWidth / 2;
ring.y = position.y - DefaultRingWidth / 2;
ring.anchorX = (DefaultRingWidth / 2);
ring.anchorY = (DefaultRingWidth / 2);
let tween = MTween.get(ring)
.to({
scaleX: ring.scaleX * 1.4,
scaleY: ring.scaleY * 1.4,
alpha: 0
}, BoomEffectDuration + (i - 1) * BoomEffectDuration * 0.33, false)
if (i == 2) {
tween.call(() => {
spritePool.recycle(ring.poolKey, ring);
});
} else {
tween.call(() => {
spritePool.recycle(ring.poolKey, ring);
});
}
}
}
onElementInit() {
}
onElementRecycle() {
}
}
export class BoomEffectSprite extends engine.Sprite implements PoolElement {
constructor(type: "star" | "ring", colorIndex: number) {
super(TextureMgr.get(type, colorIndex));
}
poolKey: string;
onElementInit() {
this.alpha = 1;
this.visible = true;
}
onElementRecycle() {
this.visible = false;
}
}
/* class Ring extends BoomEffectSprite {
constructor(type: "star" | "ring", colorIndex: number) {
super(type, colorIndex);
this.anchorX = (DefaultRingWidth / 2);
this.anchorY = (DefaultRingWidth / 2);
}
} */
class SpritePool extends Pool<BoomEffectSprite> {
public spwan(type: "star" | "ring", colorIndex: number): BoomEffectSprite {
if (this.data.length > 0) {
const element = this.data.shift();
element.onElementInit();
return element;
} else {
return new BoomEffectSprite(type, colorIndex);
}
}
}
class SpritePoolGroup extends PoolGroup<BoomEffectSprite> {
public spwan(type: "star" | "ring", colorIndex: number) {
const key = type + colorIndex;
if (!this.data[key]) {
this.data[key] = new SpritePool();
}
const element = this.data[key].spwan(type, colorIndex);
element.poolKey = key;
return element;
}
}
const spritePool = new SpritePoolGroup();
/* class RingPool extends Pool<Ring> {
public spwan(color: number): Ring {
if (this.data.length > 0) {
const element = this.data.shift();
element.onElementInit();
return element;
} else {
return new Ring(color);
}
}
}
class RingPoolGroup extends PoolGroup<Ring> {
public spwan(color: number) {
const key = color.toString();
if (!this.data[key]) {
this.data[key] = new RingPool();
}
const element = this.data[key].spwan(color);
element.poolKey = key;
return element;
}
}
const ringPool = new RingPoolGroup(); */
/* class BoomEffectPool extends Pool<BoomEffect> {
public spwan(color: number, size: number): BoomEffect {
if (this.data.length > 0) {
const element = this.data.shift();
element.onElementInit();
return element;
} else {
return new BoomEffect(color, size);
}
}
}
class BoomEffectPoolGroup extends PoolGroup<BoomEffect> {
public spwan(color: number, size: number) {
const key = color.toString() + size.toString();
if (!this.data[key]) {
this.data[key] = new BoomEffectPool()
}
const element = this.data[key].spwan(color, size);
element.poolKey = key;
return element;
}
}
let boomEffectPool = new BoomEffectPoolGroup();
export default boomEffectPool; */
\ No newline at end of file
...@@ -5,8 +5,8 @@ import GameObject from "./GameObject"; ...@@ -5,8 +5,8 @@ import GameObject from "./GameObject";
import Collider, { CircleCollider, ColliderGroup, PointCollider } from "../Phycics/Collider"; import Collider, { CircleCollider, ColliderGroup, PointCollider } from "../Phycics/Collider";
import Game from "./Game"; import Game from "./Game";
import { DataMgr } from "../Mgr/DataMgr"; import { DataMgr } from "../Mgr/DataMgr";
import bulletPool from "../Pools/BulletPool";
import MTimer from "../Global/MTimer"; import MTimer from "../Global/MTimer";
import GameMgr from "../Mgr/GameMgr";
const bulletSpeedValue = 20; const bulletSpeedValue = 20;
/**顾名思义 */ /**顾名思义 */
...@@ -41,7 +41,7 @@ export default class Bullet extends MoveObjcet implements PoolElement { ...@@ -41,7 +41,7 @@ export default class Bullet extends MoveObjcet implements PoolElement {
onCollisionEnter(other: Collider) { onCollisionEnter(other: Collider) {
if (other.group == ColliderGroup.Ball || other.group == ColliderGroup.Top) { if (other.group == ColliderGroup.Ball || other.group == ColliderGroup.Top) {
bulletPool.recycle(this); DataMgr.game._pool.bullet.recycle(this);
} }
} }
} }
...@@ -51,17 +51,16 @@ export class BulletGroup { ...@@ -51,17 +51,16 @@ export class BulletGroup {
public restFrameCountThatBulletsMoveInX: number = frameCountThatBulletsMoveInX; public restFrameCountThatBulletsMoveInX: number = frameCountThatBulletsMoveInX;
constructor(position: engine.Point, rowBulletNum: number) { constructor(position: engine.Point, rowBulletNum: number) {
for (let i = 0; i < rowBulletNum; i++) { for (let i = 0; i < rowBulletNum; i++) {
const bullet = bulletPool.spwan().init(Math.floor(DataMgr.game.powerScore / 100)); const bullet = DataMgr.game._pool.bullet.spwan().init(Math.floor(DataMgr.game.powerScore / 100));
DataMgr.game.addGameObject(bullet, "Bullet");
bullet.x = position.x - bullet.width / 2; bullet.x = position.x - bullet.width / 2;
bullet.y = position.y + bullet.height / 2; bullet.y = position.y + bullet.height / 2;
this.moveDatas.push({ this.moveDatas.push({
bullet: bullet, bullet: bullet,
frameOffset: ((i - (rowBulletNum - 1) / 2) * (bullet.width + 2)) / frameCountThatBulletsMoveInX frameOffset: ((i - (rowBulletNum - 1) / 2) * (bullet.width + 3)) / frameCountThatBulletsMoveInX
}); });
} }
HorizontalMoveMgr.instance.movingList.push(this); DataMgr.game._horizontalMoveMgr.movingList.push(this);
} }
public onUpdate() { public onUpdate() {
...@@ -74,20 +73,11 @@ export class BulletGroup { ...@@ -74,20 +73,11 @@ export class BulletGroup {
} }
} }
class HorizontalMoveMgr { export class HorizontalMoveMgr extends GameMgr {
private static _instance: HorizontalMoveMgr = null; constructor() { super("HorizontalMoveMgr") }
public static get instance(): HorizontalMoveMgr {
if (!HorizontalMoveMgr._instance) {
const instance = new HorizontalMoveMgr();
HorizontalMoveMgr._instance = instance;
MTimer.onFrame("HorizontalMoveMgr", instance.onUpdate, instance); //单例注册回调
}
return HorizontalMoveMgr._instance;
}
public movingList: BulletGroup[] = []; public movingList: BulletGroup[] = [];
public onUpdate() { protected onUpdate() {
if (this.pause) return;
for (let i = 0; i < this.movingList.length; i++) { for (let i = 0; i < this.movingList.length; i++) {
this.movingList[i].onUpdate(); this.movingList[i].onUpdate();
} }
......
...@@ -2,12 +2,10 @@ import GameObject from "./GameObject"; ...@@ -2,12 +2,10 @@ import GameObject from "./GameObject";
import Collider, { RectCollider, ColliderGroup } from "../Phycics/Collider"; import Collider, { RectCollider, ColliderGroup } from "../Phycics/Collider";
import { MConst } from "../Global/MConst"; import { MConst } from "../Global/MConst";
import Bullet, { BulletGroup } from "./Bullet"; import Bullet, { BulletGroup } from "./Bullet";
import { MTween } from "../Component/MTween";
import SoundMgr from "../Mgr/SoundMgr"; import SoundMgr from "../Mgr/SoundMgr";
import MTimer from "../Global/MTimer"; import MTimer from "../Global/MTimer";
import Game from "./Game"; import Game from "./Game";
import { DataMgr } from "../Mgr/DataMgr"; import { DataMgr } from "../Mgr/DataMgr";
import bulletPool from "../Pools/BulletPool";
...@@ -47,8 +45,6 @@ export default class Car extends GameObject { ...@@ -47,8 +45,6 @@ export default class Car extends GameObject {
let collider = this.addComponent<RectCollider>(RectCollider); let collider = this.addComponent<RectCollider>(RectCollider);
collider.setData(41, 13, 65, 129); collider.setData(41, 13, 65, 129);
collider.group = ColliderGroup.Car; collider.group = ColliderGroup.Car;
this.addEventListener(engine.Event.REMOVED_FROM_STAGE, this.onDestroy, this);
} }
/* private createPart(source: string, x: number, y: number) { /* private createPart(source: string, x: number, y: number) {
...@@ -96,7 +92,7 @@ export default class Car extends GameObject { ...@@ -96,7 +92,7 @@ export default class Car extends GameObject {
} }
private onceBlink() { private onceBlink() {
MTween.get(this) DataMgr.game._tweenMgr.get(this)
.to({ alpha: 0 }, 1, true) .to({ alpha: 0 }, 1, true)
.wait(2) .wait(2)
.to({ alpha: 1 }, 1, true) .to({ alpha: 1 }, 1, true)
...@@ -106,12 +102,12 @@ export default class Car extends GameObject { ...@@ -106,12 +102,12 @@ export default class Car extends GameObject {
revive() { revive() {
this.invincibleDuration = MConst.ReviveInvincibleDuration * 1000; this.invincibleDuration = MConst.ReviveInvincibleDuration * 1000;
MTween.removeTweens(this); DataMgr.game._tweenMgr.removeTweens(this);
this.onceBlink(); this.onceBlink();
MTimer.setFrameTimer(MConst.ReviveInvincibleDuration * 60, () => { MTimer.setFrameTimer(MConst.ReviveInvincibleDuration * 60, () => {
//无敌结束 //无敌结束
this.alpha = 1; this.alpha = 1;
MTween.removeTweens(this); DataMgr.game._tweenMgr.removeTweens(this);
}); });
} }
...@@ -162,8 +158,4 @@ export default class Car extends GameObject { ...@@ -162,8 +158,4 @@ export default class Car extends GameObject {
this.fireParticleTimer++; this.fireParticleTimer++;
} }
onDestroy() {
MTween.removeTweens(this);
}
} }
\ No newline at end of file
import MoveObjcet from "./MoveObject"; import MoveObjcet from "./MoveObject";
import { MConfigs } from "../Global/MConfigs"; import { MConfigs } from "../Global/MConfigs";
import Collider, { ColliderGroup, PointCollider } from "../Phycics/Collider"; import Collider, { ColliderGroup, PointCollider } from "../Phycics/Collider";
import { MTween } from "../Component/MTween";
import SoundMgr from "../Mgr/SoundMgr"; import SoundMgr from "../Mgr/SoundMgr";
import Game from "./Game";
import { PoolElement } from "../Component/Pool"; import { PoolElement } from "../Component/Pool";
import dropPool from "../Pools/DropPool";
import { DataMgr } from "../Mgr/DataMgr"; import { DataMgr } from "../Mgr/DataMgr";
import DropBlinkMgr from "../Mgr/DropBlinkMgr";
const dropImgNames = [ const dropImgNames = [
"f7221f86-f376-40ce-b0e8-f7ea573ec780", "f7221f86-f376-40ce-b0e8-f7ea573ec780",
...@@ -65,7 +61,7 @@ export default class Drop extends MoveObjcet implements PoolElement { ...@@ -65,7 +61,7 @@ export default class Drop extends MoveObjcet implements PoolElement {
private eated() { private eated() {
SoundMgr.instance.playEffect("collect"); SoundMgr.instance.playEffect("collect");
this.physics.rotateVelocity = 0; this.physics.rotateVelocity = 0;
DropBlinkMgr.instance.remove(this); DataMgr.game._dropBlinkMgr.remove(this);
this.visible = true; this.visible = true;
this.rotation = 0; this.rotation = 0;
this.physics.enabled = false; this.physics.enabled = false;
...@@ -77,12 +73,14 @@ export default class Drop extends MoveObjcet implements PoolElement { ...@@ -77,12 +73,14 @@ export default class Drop extends MoveObjcet implements PoolElement {
DataMgr.game._PowerScore += this.scoreValue; DataMgr.game._PowerScore += this.scoreValue;
} }
MTween.removeTweens(this); const tweenMgr = DataMgr.game._tweenMgr;
MTween.get(this)
tweenMgr.removeTweens(this);
tweenMgr.get(this)
.to({ y: this.y - 200, alpha: 0 }, 1000, false) .to({ y: this.y - 200, alpha: 0 }, 1000, false)
.call(() => { .call(() => {
dropPool.recycle(this.id.toString(), this); DataMgr.game._pool.drop.recycle(this.id.toString(), this);
MTween.removeTweens(this); tweenMgr.removeTweens(this);
}); });
} }
...@@ -112,7 +110,7 @@ export default class Drop extends MoveObjcet implements PoolElement { ...@@ -112,7 +110,7 @@ export default class Drop extends MoveObjcet implements PoolElement {
this.physics.velocity.y = 0; this.physics.velocity.y = 0;
this.physics.acceleration.y = 0; this.physics.acceleration.y = 0;
this.physics.velocity.x *= 0.5; this.physics.velocity.x *= 0.5;
DropBlinkMgr.instance.add(this); DataMgr.game._dropBlinkMgr.add(this);
this.physics.rotateVelocity = this.physics.velocity.x * (180 / (Math.PI * 13)); this.physics.rotateVelocity = this.physics.velocity.x * (180 / (Math.PI * 13));
} }
......
...@@ -6,19 +6,26 @@ import Physics from "../Phycics/Physics"; ...@@ -6,19 +6,26 @@ import Physics from "../Phycics/Physics";
import GameObject from "./GameObject"; import GameObject from "./GameObject";
import { RectCollider, ColliderGroup } from "../Phycics/Collider"; import { RectCollider, ColliderGroup } from "../Phycics/Collider";
import { MConfigs } from "../Global/MConfigs"; import { MConfigs } from "../Global/MConfigs";
import { MTween } from "../Component/MTween"; import { MTween as TweenMgr } from "../Component/MTween";
import Drop from "./Drop"; import Drop from "./Drop";
import { MUtils } from "../Global/MUtils"; import { MUtils } from "../Global/MUtils";
import { arrayRemove, getBallScore } from "../Global/GUtils"; import { arrayRemove, getBallScore } from "../Global/GUtils";
import MTimer from "../Global/MTimer"; import MTimer from "../Global/MTimer";
import PhycicsSystem from "../Phycics/PhycicsSystem"; import PhycicsSystem from "../Phycics/PhycicsSystem";
import Bullet from "./Bullet"; import Bullet, { HorizontalMoveMgr } from "./Bullet";
import ballPool from "../Pools/BallPool"; import { BallPoolGroup } from "../Pools/BallPool";
import { EffectMgr } from "../Mgr/EffectMgr"; import AnimationPoolGroup from "../Pools/AnimationPool";
import FrameAnimation from "../Component/FrameAnimation"; import FrameAnimation, { FrameAnimationMgr } from "../Component/FrameAnimation";
import { DuangAltaData, NumFontData, BoomAnimationData } from "../Global/JsonData"; import { DuangAltaData, NumFontData, BoomAnimationData } from "../Global/JsonData";
import SpriteFontLabel, { SpriteFont } from "../Component/SpriteFont"; import SpriteFontLabel, { SpriteFont } from "../Component/SpriteFont";
import GuideMgr from "../Mgr/GuideMgr"; import GuideMgr from "../Mgr/GuideMgr";
import { Pool, PoolGroup, PoolElement } from "../Component/Pool";
import { BulletPool } from "../Pools/BulletPool";
import SpBoomEffectPoolGroup, { SpBoomEffectMgr } from "./SpBoomEffect";
import DropPoolGroup from "../Pools/DropPool";
import DropBlinkMgr from "../Mgr/DropBlinkMgr";
import GameMgr from "../Mgr/GameMgr";
import { DataMgr } from "../Mgr/DataMgr";
type LayerName = "Bullet" | "Ball" | "Drop" | "Effect"; type LayerName = "Bullet" | "Ball" | "Drop" | "Effect";
...@@ -62,8 +69,10 @@ export default class Game { ...@@ -62,8 +69,10 @@ export default class Game {
this._pause = v; this._pause = v;
this.timing = !this._pause; //取反 this.timing = !this._pause; //取反
PhycicsSystem.instance.pause = this._pause;
MTween.pause = this._pause; for (let mgr of this.mgrs) {
mgr.pause = this._pause;
}
} }
public get pause() { public get pause() {
return this._pause; return this._pause;
...@@ -72,7 +81,7 @@ export default class Game { ...@@ -72,7 +81,7 @@ export default class Game {
/**游戏结束回调函数 */ /**游戏结束回调函数 */
public onGameOver: () => void; public onGameOver: () => void;
/**分数改变时的回调函数 delta是分数的变化量 */ /**分数改变时的回调函数 delta是分数的变化量 */
public onScoreChange: (delta: number) => void; public onScoreChange: (delta: number) => void;
...@@ -83,26 +92,49 @@ export default class Game { ...@@ -83,26 +92,49 @@ export default class Game {
this.pause = false; this.pause = false;
} }
/**结束游戏 */ /**销毁游戏 */
public destroy() { public destroy() {
this.node.destroy(); this.node.destroy();
PhycicsSystem.instance.enabled = false; for (let mgr of this.mgrs) {
mgr.onDestroy();
}
MTimer.removeOnFrame("Game");
MTimer.removeOnFrame("Car");
} }
private mgrs: GameMgr[] = [];
public _tweenMgr: TweenMgr = new TweenMgr();
public _phycicsSystem: PhycicsSystem = new PhycicsSystem();
public _boomEffectMgr: SpBoomEffectMgr = new SpBoomEffectMgr();
public _animationMgr: FrameAnimationMgr = new FrameAnimationMgr();
public _horizontalMoveMgr: HorizontalMoveMgr = new HorizontalMoveMgr();
private loadRes() { private loadRes() {
FrameAnimation.loadRes(RES.getRes("217164f4-a185-429c-8706-818137a4e438"), DuangAltaData); FrameAnimation.loadRes(RES.getRes("217164f4-a185-429c-8706-818137a4e438"), DuangAltaData);
SpriteFont.load(RES.getRes("b4d821e8-8274-4b60-95d0-47da803ac5cf"), NumFontData); SpriteFont.load(RES.getRes("b4d821e8-8274-4b60-95d0-47da803ac5cf"), NumFontData);
FrameAnimation.loadRes(RES.getRes("53c65221-3fbc-41d9-8cef-8a846876fe06"), BoomAnimationData); FrameAnimation.loadRes(RES.getRes("53c65221-3fbc-41d9-8cef-8a846876fe06"), BoomAnimationData);
} }
private initMgrs() {
this.mgrs = [
this._dropBlinkMgr,
this._tweenMgr,
this._phycicsSystem,
this._boomEffectMgr,
this._animationMgr,
this._horizontalMoveMgr
];
for (let mgr of this.mgrs) {
mgr.onInit();
}
}
constructor(parent: engine.Container) { constructor(parent: engine.Container) {
Ball.loadTextures(); DataMgr.game = this;
PhycicsSystem.instance.enabled = true; this.initMgrs();
this.pause = false; this.pause = false;
this.loadRes(); this.loadRes();
//创建墙和地面
this.createWall();
parent.addChild(this.node); parent.addChild(this.node);
this.node.width = MConst.DesignResolution.width; this.node.width = MConst.DesignResolution.width;
...@@ -112,7 +144,7 @@ export default class Game { ...@@ -112,7 +144,7 @@ export default class Game {
bg.y = -10; bg.y = -10;
this.node.addChild(bg); this.node.addChild(bg);
//子弹层 //子弹层
this.node.addChild(this.layers["Bullet"]); this.node.addChild(this.layers.bullet);
//炮车 //炮车
let car = new Car(); let car = new Car();
car.x = this.node.width / 2 - car.width / 2; car.x = this.node.width / 2 - car.width / 2;
...@@ -124,30 +156,20 @@ export default class Game { ...@@ -124,30 +156,20 @@ export default class Game {
}; };
this._car = car; this._car = car;
let rect = new GameObject();
rect.width = 100;
rect.height = 100;
this.node.addChild(rect);
//球的层 //球的层
this.node.addChild(this.layers["Ball"]); this.node.addChild(this.layers.ball);
//掉落物层 //掉落物层
this.node.addChild(this.layers["Drop"]); this.node.addChild(this.layers.drop);
//特效层
this.node.addChild(this.layers.effect);
//动画层 //动画层
this.node.addChild(this.layers["Effect"]); this.node.addChild(this.layers.animation);
EffectMgr.init(this.layers["Effect"]);
//禁用层级交互事件 //禁用层级交互事件
this.layers["Bullet"].mouseChildren = false; for (let k in this.layers) {
this.layers["Bullet"].mouseEnabled = false; this.layers[k].mouseChildren = false;
this.layers["Ball"].mouseChildren = false; this.layers[k].mouseEnabled = false;
this.layers["Ball"].mouseEnabled = false; }
this.layers["Drop"].mouseChildren = false;
this.layers["Drop"].mouseEnabled = false;
this.layers["Effect"].mouseChildren = false;
this.layers["Effect"].mouseEnabled = false;
//玩家控制器 //玩家控制器
let playerController = new PlayerController(); let playerController = new PlayerController();
...@@ -161,6 +183,11 @@ export default class Game { ...@@ -161,6 +183,11 @@ export default class Game {
//添加监听器 //添加监听器
MTimer.onFrame("Game", this.onUpdate, this); MTimer.onFrame("Game", this.onUpdate, this);
this.createPools();
//创建墙和地面
this.createWall();
//开始倒计时 //开始倒计时
this.timing = true; this.timing = true;
...@@ -168,11 +195,6 @@ export default class Game { ...@@ -168,11 +195,6 @@ export default class Game {
// this._PowerScore = 2000; // this._PowerScore = 2000;
} }
public addGameObject(gameObject: GameObject, layer: LayerName) {
this.layers[layer].addChild(gameObject);
}
/****** private method ******/ /****** private method ******/
...@@ -229,8 +251,7 @@ export default class Game { ...@@ -229,8 +251,7 @@ export default class Game {
size = 3; size = 3;
} }
let color = MUtils.randomInt(size, MConst.MaxColorIndex); let color = MUtils.randomInt(size, MConst.MaxColorIndex);
let ball = ballPool.spwan(size); let ball = this._pool.ball.spwan(size);
this.addGameObject(ball, "Ball");
let dir: 1 | -1 = Math.random() > 0.5 ? -1 : 1; let dir: 1 | -1 = Math.random() > 0.5 ? -1 : 1;
const score = getBallScore(this._BulletScore, this._PowerScore, color); const score = getBallScore(this._BulletScore, this._PowerScore, color);
ball.init(color, dir, score); ball.init(color, dir, score);
...@@ -294,7 +315,7 @@ export default class Game { ...@@ -294,7 +315,7 @@ export default class Game {
* 不建议调用 * 不建议调用
*/ */
public _shake() { public _shake() {
MTween.removeTweens(this.node); this._tweenMgr.removeTweens(this.node);
let count = 0; let count = 0;
let callback = () => { let callback = () => {
...@@ -302,7 +323,7 @@ export default class Game { ...@@ -302,7 +323,7 @@ export default class Game {
count++; count++;
this.node.x = 10; this.node.x = 10;
MTween.get(this.node) this._tweenMgr.get(this.node)
.wait(1, true) .wait(1, true)
.to({ x: 0, y: -10 }, 1, true) .to({ x: 0, y: -10 }, 1, true)
.to({ x: -10, y: 0 }, 1, true) .to({ x: -10, y: 0 }, 1, true)
...@@ -314,17 +335,49 @@ export default class Game { ...@@ -314,17 +335,49 @@ export default class Game {
callback(); callback();
} }
public _pool: {
ball: BallPoolGroup,
bullet: BulletPool,
boomEffect: SpBoomEffectPoolGroup
animation: AnimationPoolGroup,
drop: DropPoolGroup
} = null;
private createPools() {
this._pool = {
ball: new BallPoolGroup(this.layers.ball),
bullet: new BulletPool(this.layers.bullet),
animation: new AnimationPoolGroup(this.layers.animation),
boomEffect: new SpBoomEffectPoolGroup(this.layers.effect),
drop: new DropPoolGroup(this.layers.drop)
};
}
public _playBoomEffect(position: engine.Point, size: number) {
const effect = this._pool.boomEffect.spwan(MUtils.randomInt(0, 3), size);
effect.position = position;
}
public _createAnimation(key: string) {
const clip = this._pool.animation.spwan(key);
clip.onCompleted = () => {
this._pool.animation.recycle(key, clip);
}
return clip;
}
/****** private field ******/ /****** private field ******/
private layers: { public _dropBlinkMgr = new DropBlinkMgr();
[key in LayerName]: engine.Container
} = { private layers = {
"Bullet": new engine.Container(), bullet: new engine.Container(),
"Ball": new engine.Container(), ball: new engine.Container(),
"Drop": new engine.Container(), drop: new engine.Container(),
"Effect": new engine.Container() effect: new engine.Container(),
}; animation: new engine.Container()
};
/**不建议使用 */ /**不建议使用 */
public get _score(): number { public get _score(): number {
return this.__score; return this.__score;
......
...@@ -2,6 +2,8 @@ import { TextureMgr } from "../Mgr/TextureMgr"; ...@@ -2,6 +2,8 @@ import { TextureMgr } from "../Mgr/TextureMgr";
import { MUtils } from "../Global/MUtils"; import { MUtils } from "../Global/MUtils";
import MTimer from "../Global/MTimer"; import MTimer from "../Global/MTimer";
import { Pool, PoolGroup, PoolElement } from "../Component/Pool"; import { Pool, PoolGroup, PoolElement } from "../Component/Pool";
import { DataMgr } from "../Mgr/DataMgr";
import GameMgr from "../Mgr/GameMgr";
const InitSpeedValue: number = 0.08; const InitSpeedValue: number = 0.08;
...@@ -18,16 +20,7 @@ class Line { ...@@ -18,16 +20,7 @@ class Line {
const sprite = new engine.Sprite(TextureMgr.get(("fireworks_line_" + type) as any, color)); const sprite = new engine.Sprite(TextureMgr.get(("fireworks_line_" + type) as any, color));
parent.addChild(sprite); parent.addChild(sprite);
sprite.anchorX = sprite.width / 2; sprite.anchorX = sprite.width / 2;
// sprite.y = -sprite.height;
/* let mask = new engine.Graphics();
mask.beginFill(0xffffff);
mask.drawRect(0, 0, sprite.width, 200);
mask.endFill();
parent.addChild(mask);
sprite.mask = mask; */
sprite.rotation = rotation; sprite.rotation = rotation;
// mask.rotation = rotation;
// mask.anchorX = sprite.anchorX;
this.sprite = sprite; this.sprite = sprite;
//计算方向 //计算方向
...@@ -54,31 +47,27 @@ class Line { ...@@ -54,31 +47,27 @@ class Line {
} }
this.frameTimer++; this.frameTimer++;
/* if (this.speedValue < InitSpeedValue * 0.4) {
} */
/* this.speedValue += InitAcceleration * dt;
if (this.speedValue < 0) {
this.speedValue = 0;
} */
} }
} }
export namespace SpBoomEffectMgr { export class SpBoomEffectMgr extends GameMgr {
export let list: SpBoomEffect[] = []; constructor() { super("SpBoomEffectMgr") }
export function onUpdate(dt: number) { public list: SpBoomEffect[] = [];
protected onUpdate(dt: number) {
if (this.pause) return;
let filter: SpBoomEffect[] = []; let filter: SpBoomEffect[] = [];
for (let i of list) { for (let i of this.list) {
if (i.onUpdate(dt)) { if (i.onUpdate(dt)) {
filter.push(i); filter.push(i);
} }
} }
list = filter; this.list = filter;
} }
} }
class SpBoomEffect extends engine.Container implements PoolElement { class SpBoomEffect extends engine.Container implements PoolElement {
private lines: Line[] = []; private lines: Line[] = [];
public onUpdate(dt: number) { public onUpdate(dt: number) {
...@@ -87,7 +76,7 @@ class SpBoomEffect extends engine.Container implements PoolElement { ...@@ -87,7 +76,7 @@ class SpBoomEffect extends engine.Container implements PoolElement {
} }
if (this.lines[0].sprite.alpha <= 0) { if (this.lines[0].sprite.alpha <= 0) {
spBoomEffectPool.recycle(this.poolKey, this); DataMgr.game._pool.boomEffect.recycle(this.poolKey, this);
return false; return false;
} }
return true; return true;
...@@ -100,7 +89,7 @@ class SpBoomEffect extends engine.Container implements PoolElement { ...@@ -100,7 +89,7 @@ class SpBoomEffect extends engine.Container implements PoolElement {
let line = new Line(this, i % 2, color, i * 22.5); let line = new Line(this, i % 2, color, i * 22.5);
this.lines.push(line); this.lines.push(line);
} }
SpBoomEffectMgr.list.push(this); DataMgr.game._boomEffectMgr.list.push(this);
} }
onElementInit() { onElementInit() {
...@@ -109,7 +98,7 @@ class SpBoomEffect extends engine.Container implements PoolElement { ...@@ -109,7 +98,7 @@ class SpBoomEffect extends engine.Container implements PoolElement {
for (let i of this.lines) { for (let i of this.lines) {
i.init(); i.init();
} }
SpBoomEffectMgr.list.push(this); DataMgr.game._boomEffectMgr.list.push(this);
} }
onElementRecycle() { onElementRecycle() {
...@@ -127,16 +116,18 @@ class SpBoomEffectPool extends Pool<SpBoomEffect> { ...@@ -127,16 +116,18 @@ class SpBoomEffectPool extends Pool<SpBoomEffect> {
element.onElementInit(); element.onElementInit();
return element; return element;
} else { } else {
return new SpBoomEffect(color, size); const e = new SpBoomEffect(color, size);
this.layer.addChild(e);
return e;
} }
} }
} }
class SpBoomEffectPoolGroup extends PoolGroup<SpBoomEffect> { export default class SpBoomEffectPoolGroup extends PoolGroup<SpBoomEffect> {
public spwan(color: number, size: number) { public spwan(color: number, size: number) {
const key = color.toString() + size; const key = color.toString() + size;
if (!this.data[key]) { if (!this.data[key]) {
this.data[key] = new SpBoomEffectPool() this.data[key] = new SpBoomEffectPool(this.layer)
} }
const element = this.data[key].spwan(color, size); const element = this.data[key].spwan(color, size);
element.poolKey = key; element.poolKey = key;
...@@ -144,6 +135,3 @@ class SpBoomEffectPoolGroup extends PoolGroup<SpBoomEffect> { ...@@ -144,6 +135,3 @@ class SpBoomEffectPoolGroup extends PoolGroup<SpBoomEffect> {
} }
} }
let spBoomEffectPool: SpBoomEffectPoolGroup = new SpBoomEffectPoolGroup();
export default spBoomEffectPool;
...@@ -49,6 +49,13 @@ export default class MTimer { ...@@ -49,6 +49,13 @@ export default class MTimer {
_onFrame.add(callback, thisObj); _onFrame.add(callback, thisObj);
} }
public static removeOnFrame(key: string) {
if (onFrameCaches[key]) {
_onFrame.remove(onFrameCaches[key].callback, onFrameCaches[key].thisObj);
delete onFrameCaches[key];
}
}
/** /**
* 设置帧计时器 * 设置帧计时器
*/ */
......
import Drop from "../Game/Drop"; import Drop from "../Game/Drop";
import MTimer from "../Global/MTimer"; import MTimer from "../Global/MTimer";
import GameMgr from "./GameMgr";
export default class DropBlinkMgr { export default class DropBlinkMgr extends GameMgr {
private static _instance: DropBlinkMgr = null; constructor() {
public static get instance(): DropBlinkMgr { super("DropBlinkMgr");
if (!this._instance) {
this._instance = new DropBlinkMgr();
}
return this._instance;
} }
private list: { private list: {
drop: Drop, drop: Drop,
count: number count: number
...@@ -25,18 +20,11 @@ export default class DropBlinkMgr { ...@@ -25,18 +20,11 @@ export default class DropBlinkMgr {
this.list = this.list.filter(e => e.drop !== drop); this.list = this.list.filter(e => e.drop !== drop);
} }
public static init() {
MTimer.onFrame("DropBlinkMgr", DropBlinkMgr.instance.onUpdate, DropBlinkMgr.instance);
// engine.gameStage.addEventListener(engine.Event.ENTER_FRAME, DropBlinkMgr.instance.onUpdate, DropBlinkMgr.instance);
}
/* public static destroy() {
engine.gameStage.addEventListener(engine.Event.ENTER_FRAME, DropBlinkMgr.instance.onUpdate, DropBlinkMgr.instance);
} */
private frameTimer: number = 0; private frameTimer: number = 0;
public onUpdate() { public onUpdate() {
if (this.pause) return;
if (this.frameTimer % BlinkDuration == 0) { if (this.frameTimer % BlinkDuration == 0) {
let temp: { let temp: {
drop: Drop, drop: Drop,
......
import { MUtils } from "../Global/MUtils";
import BoomEffect from "../Game/BoomEffect";
import FrameAnimation from "../Component/FrameAnimation";
import { Pool, PoolGroup, PoolElement } from "../Component/Pool";
import SpBoomEffect from "../Game/SpBoomEffect";
import spBoomEffectPool from "../Game/SpBoomEffect";
export namespace EffectMgr {
let effectLayer: engine.Container = null;
let layers: engine.Container[] = [];
export function init(effectLayer: engine.Container) {
this.effectLayer = effectLayer;
layers[0] = new engine.Container();
layers[1] = new engine.Container();
effectLayer.addChild(layers[0]);
effectLayer.addChild(layers[1]);
}
export function playBoomEffect(position: engine.Point, size: number) {
const colorIndex = MUtils.randomInt(0, 5);
// new BoomEffect(position, layers[1], colorIndex, size);
let effect = spBoomEffectPool.spwan(MUtils.randomInt(0, 3), size);
layers[1].addChild(effect);
effect.position = position;
}
export function createAnimation(key: string): Animation {
let clip = animationPool.spwan(key);
layers[0].addChild(clip);
clip.onCompleted = () => {
animationPool.recycle(key, clip);
}
return clip;
}
}
class Animation extends FrameAnimation implements PoolElement {
onElementInit() {
this.visible = true;
this.curPos = 0;
}
onElementRecycle() {
this.visible = false;
}
}
class AnimationPool extends Pool<Animation> {
public spwan(key: string): Animation {
if (this.data.length > 0) {
const element = this.data.shift();
element.onElementInit();
return element;
} else {
return new Animation(key);
}
}
}
class AnimationPoolGroup extends PoolGroup<Animation> {
public spwan(key: string) {
if (!this.data[key]) {
this.data[key] = new AnimationPool() as Pool<Animation>;
}
const element = this.data[key].spwan(key);
return element;
}
}
const animationPool = new AnimationPoolGroup();
import MTimer from "../Global/MTimer";
export default abstract class GameMgr {
constructor(private name: string) { }
public onInit(): void {
MTimer.onFrame(this.name, this.onUpdate, this);
}
public onDestroy() {
MTimer.removeOnFrame(this.name);
}
protected onUpdate(dt: number) {
}
public pause: boolean = false
}
\ No newline at end of file
...@@ -34,15 +34,15 @@ export namespace TextureMgr { ...@@ -34,15 +34,15 @@ export namespace TextureMgr {
] ]
} }
const caches: { /* const caches: {
[uuid: string]: engine.Texture [uuid: string]: engine.Texture
} = {}; } = {}; */
export function get(type: ImgType, index: number) { export function get(type: ImgType, index: number) {
const uuid = uuids[type][index]; const uuid = uuids[type][index];
if (!caches[uuid]) { /* if (!caches[uuid]) {
caches[uuid] = RES.getRes(uuid); caches[uuid] = ;
} } */
return caches[uuid]; return RES.getRes(uuid);
} }
} }
\ No newline at end of file
...@@ -2,6 +2,7 @@ import GameComponent from "../Game/GameComponent"; ...@@ -2,6 +2,7 @@ import GameComponent from "../Game/GameComponent";
import GameObject from "../Game/GameObject"; import GameObject from "../Game/GameObject";
import PhycicsSystem from "./PhycicsSystem"; import PhycicsSystem from "./PhycicsSystem";
import Physics from "./Physics"; import Physics from "./Physics";
import { DataMgr } from "../Mgr/DataMgr";
export default abstract class Collider extends GameComponent { export default abstract class Collider extends GameComponent {
public physics: Physics = null; public physics: Physics = null;
...@@ -9,7 +10,7 @@ export default abstract class Collider extends GameComponent { ...@@ -9,7 +10,7 @@ export default abstract class Collider extends GameComponent {
set group(group: ColliderGroup) { set group(group: ColliderGroup) {
if (this._group == null) { if (this._group == null) {
this._group = group; this._group = group;
PhycicsSystem.instance.addCollider(this); DataMgr.game._phycicsSystem.addCollider(this);
} else { } else {
this._group = group; this._group = group;
} }
...@@ -27,7 +28,7 @@ export default abstract class Collider extends GameComponent { ...@@ -27,7 +28,7 @@ export default abstract class Collider extends GameComponent {
onEnabled() { onEnabled() {
//自动设置物理组件 //自动设置物理组件
if (this.group) if (this.group)
PhycicsSystem.instance.addCollider(this); DataMgr.game._phycicsSystem.addCollider(this);
let physics = this.owner.getComponent<Physics>(Physics); let physics = this.owner.getComponent<Physics>(Physics);
if (physics) { if (physics) {
this.physics = physics; this.physics = physics;
...@@ -40,7 +41,7 @@ export default abstract class Collider extends GameComponent { ...@@ -40,7 +41,7 @@ export default abstract class Collider extends GameComponent {
abstract getWorldPosition(out: number[]): void; abstract getWorldPosition(out: number[]): void;
onDisabled() { onDisabled() {
PhycicsSystem.instance.removeCollider(this); DataMgr.game._phycicsSystem.removeCollider(this);
} }
/**获取碰撞器的中心的节点坐标 */ /**获取碰撞器的中心的节点坐标 */
......
...@@ -3,31 +3,12 @@ import Collider, { CircleCollider, RectCollider, ColliderType, ColliderGroup, Po ...@@ -3,31 +3,12 @@ import Collider, { CircleCollider, RectCollider, ColliderType, ColliderGroup, Po
import { arrayRemove } from "../Global/GUtils"; import { arrayRemove } from "../Global/GUtils";
import MTimer from "../Global/MTimer"; import MTimer from "../Global/MTimer";
import DebugMgr from "../Mgr/DebugMgr"; import DebugMgr from "../Mgr/DebugMgr";
import GameMgr from "../Mgr/GameMgr";
let instanceId1: number = null; let instanceId1: number = null;
let instanceId2: number = null; let instanceId2: number = null;
export default class PhycicsSystem { export default class PhycicsSystem extends GameMgr {
private static _instance: PhycicsSystem = null; /* private _enabled: boolean = false;
public static get instance(): PhycicsSystem {
if (!PhycicsSystem._instance) {
PhycicsSystem._instance = new PhycicsSystem();
}
return PhycicsSystem._instance;
}
public static init() {
const ins = PhycicsSystem.instance;
// engine.gameStage.addEventListener(engine.Event.ENTER_FRAME, ins.onUpdate, ins);
MTimer.onFrame("PhycicsSystem", ins.onUpdate, ins);
}
/* public static destroy() {
const ins = PhycicsSystem.instance;
engine.gameStage.removeEventListener(engine.Event.ENTER_FRAME, ins.onUpdate, ins)
} */
private _enabled: boolean = false;
public set enabled(v: boolean) { public set enabled(v: boolean) {
this._enabled = v; this._enabled = v;
...@@ -38,25 +19,30 @@ export default class PhycicsSystem { ...@@ -38,25 +19,30 @@ export default class PhycicsSystem {
} }
public get enabled() { public get enabled() {
return this._enabled; return this._enabled;
} } */
public pause: boolean = false;
constructor() { super("PhycicsSystem"); }
private phycicsList: Physics[] = []; private phycicsList: Physics[] = [];
private clear() { onInit() {
super.onInit();
for (let i = 0; i <= GroupMaxIndex; i++) {
this.colliderList[i] = [];
}
}
public onDestroy() {
super.onDestroy();
for (let i = 0; i <= GroupMaxIndex; i++) { for (let i = 0; i <= GroupMaxIndex; i++) {
this.colliderList[i] = []; this.colliderList[i] = [];
} }
this.phycicsList = []; this.phycicsList = [];
} }
private onUpdate() { protected onUpdate() {
if (this.pause) return; if (this.pause) return;
for (let i of this.phycicsList) {
for (let i of PhycicsSystem.instance.phycicsList) {
if (this.enabled == false) return;
i.onFixedUpdate(MTimer.dtFactor); i.onFixedUpdate(MTimer.dtFactor);
} }
...@@ -118,7 +104,6 @@ export default class PhycicsSystem { ...@@ -118,7 +104,6 @@ export default class PhycicsSystem {
} }
} */ } */
//查找所有碰撞 //查找所有碰撞
let i = 0, let i = 0,
j = 0, j = 0,
...@@ -130,12 +115,11 @@ export default class PhycicsSystem { ...@@ -130,12 +115,11 @@ export default class PhycicsSystem {
for (i = 0; i <= length - 1; i++) { for (i = 0; i <= length - 1; i++) {
for (j = i + 1; j <= length - 1; j++) { for (j = i + 1; j <= length - 1; j++) {
if (CollisionMap[i] & 1 << j) { if (CollisionMap[i] & 1 << j) {
if (this.enabled == false) return;
group1 = this.colliderList[i]; group1 = this.colliderList[i];
group2 = this.colliderList[j]; group2 = this.colliderList[j];
for (m = 0; m <= group1.length - 1; m++) { for (m = 0; m <= group1.length - 1; m++) {
for (n = 0; n <= group2.length - 1; n++) { for (n = 0; n <= group2.length - 1; n++) {
// if (this.enabled == false) return;
this.detectTraverse(group1[m], group2[n]); this.detectTraverse(group1[m], group2[n]);
} }
} }
......
...@@ -2,6 +2,7 @@ import GameComponent from "../Game/GameComponent"; ...@@ -2,6 +2,7 @@ import GameComponent from "../Game/GameComponent";
import GameObject from "../Game/GameObject"; import GameObject from "../Game/GameObject";
import PhycicsSystem from "./PhycicsSystem"; import PhycicsSystem from "./PhycicsSystem";
import Collider from "./Collider"; import Collider from "./Collider";
import { DataMgr } from "../Mgr/DataMgr";
export default class Physics extends GameComponent { export default class Physics extends GameComponent {
/**旋转速度 /**旋转速度
...@@ -61,10 +62,10 @@ export default class Physics extends GameComponent { ...@@ -61,10 +62,10 @@ export default class Physics extends GameComponent {
public onMoved: (owner: GameObject) => void; public onMoved: (owner: GameObject) => void;
protected onDisabled() { protected onDisabled() {
PhycicsSystem.instance.remove(this); DataMgr.game._phycicsSystem.remove(this);
} }
protected onEnabled() { protected onEnabled() {
PhycicsSystem.instance.add(this); DataMgr.game._phycicsSystem.add(this);
} }
public onColliderResize(collider: Collider) { public onColliderResize(collider: Collider) {
......
import FrameAnimation from "../Component/FrameAnimation";
import { Pool, PoolGroup, PoolElement } from "../Component/Pool";
export class PoolFrameAnimation extends FrameAnimation implements PoolElement {
onElementInit() {
this.visible = true;
this.curPos = 0;
}
onElementRecycle() {
this.visible = false;
}
}
class AnimationPool extends Pool<PoolFrameAnimation> {
public spwan(key: string): PoolFrameAnimation {
if (this.data.length > 0) {
const element = this.data.shift();
element.onElementInit();
return element;
} else {
let anim = new PoolFrameAnimation(key);
this.layer.addChild(anim);
return anim;
}
}
}
export default class AnimationPoolGroup extends PoolGroup<PoolFrameAnimation> {
public spwan(key: string) {
if (!this.data[key]) {
this.data[key] = new AnimationPool(this.layer);
}
const element = this.data[key].spwan(key);
return element;
}
}
\ No newline at end of file
...@@ -10,6 +10,7 @@ class BallPool extends Pool<Ball>{ ...@@ -10,6 +10,7 @@ class BallPool extends Pool<Ball>{
element.onElementInit(); element.onElementInit();
} else { } else {
element = new Ball(sizeIndex); element = new Ball(sizeIndex);
this.layer.addChild(element);
} }
DataMgr.game._ballList.push(element); DataMgr.game._ballList.push(element);
return element; return element;
...@@ -21,16 +22,16 @@ class BallPool extends Pool<Ball>{ ...@@ -21,16 +22,16 @@ class BallPool extends Pool<Ball>{
} }
}; };
class BallPoolGroup extends PoolGroup<Ball>{ export class BallPoolGroup extends PoolGroup<Ball>{
public spwan(sizeIndex: number) { public spwan(sizeIndex: number) {
const key = sizeIndex.toString(); const key = sizeIndex.toString();
if (!this.data[key]) { if (!this.data[key]) {
this.data[key] = new BallPool() const ball =
this.data[key] = new BallPool(this.layer)
} }
let element = this.data[key].spwan(sizeIndex); let element = this.data[key].spwan(sizeIndex);
element.poolKey = key; element.poolKey = key;
return element; return element;
} }
} }
let ballPool: BallPoolGroup = new BallPoolGroup(); \ No newline at end of file
export default ballPool;
\ No newline at end of file
import { Pool, PoolElement, PoolGroup } from "../Component/Pool";
import { BoomEffectSprite } from "../Game/BoomEffect";
/* import BoomEffect from "../BoomEffect";
import { Pool, PoolGroup } from "../Pool";
class BoomEffectPool extends Pool<BoomEffect> {
public spwan(color: number, size: number): BoomEffect {
if (this.data.length > 0) {
const element = this.data.shift();
element.onElementInit();
return element;
} else {
return new BoomEffect(color, size);
}
}
}
class BoomEffectPoolGroup extends PoolGroup<BoomEffect> {
public spwan(color: number, size: number) {
const key = color.toString() + size.toString();
if (!this.data[key]) {
this.data[key] = new BoomEffectPool()
}
const element = this.data[key].spwan(color, size);
element.poolKey = key;
return element;
}
}
let boomEffectPool = new BoomEffectPoolGroup();
export default boomEffectPool;
*/
import Bullet from "../Game/Bullet"; import Bullet from "../Game/Bullet";
import { Pool } from "../Component/Pool"; import { Pool } from "../Component/Pool";
class BulletPool extends Pool<Bullet> { export class BulletPool extends Pool<Bullet> {
public spwan() { public spwan() {
if (this.data.length > 0) { if (this.data.length > 0) {
const element = this.data.shift(); const element = this.data.shift();
element.onElementInit(); element.onElementInit();
return element; return element;
} else { } else {
return new Bullet(); const b = new Bullet();
this.layer.addChild(b);
return b;
} }
} }
} }
\ No newline at end of file
let bulletPool: BulletPool = new BulletPool();
export default bulletPool;
\ No newline at end of file
...@@ -8,19 +8,18 @@ class DropPool extends Pool<Drop> { ...@@ -8,19 +8,18 @@ class DropPool extends Pool<Drop> {
element.onElementInit(); element.onElementInit();
return element; return element;
} else { } else {
return new Drop(id); const d = new Drop(id);
this.layer.addChild(d);
return d;
} }
} }
} }
class DropPoolGroup extends PoolGroup<Drop>{ export default class DropPoolGroup extends PoolGroup<Drop>{
public spwan(id: string) { public spwan(id: string) {
if (!this.data[id]) { if (!this.data[id]) {
this.data[id] = new DropPool() this.data[id] = new DropPool(this.layer);
} }
return this.data[id].spwan(id); return this.data[id].spwan(id);
} }
} }
\ No newline at end of file
let dropPool: DropPoolGroup = new DropPoolGroup();
export default dropPool;
\ No newline at end of file
import Game from "./Game/Game"; import Game from "./Game/Game";
import PhycicsSystem from "./Phycics/PhycicsSystem";
import MTimer from "./Global/MTimer"; import MTimer from "./Global/MTimer";
import { MTween } from "./Component/MTween";
import DropBlinkMgr from "./Mgr/DropBlinkMgr";
import Ball from "./Game/Ball";
import MEvent from "./Component/MEvent";
import { DataMgr } from "./Mgr/DataMgr"; import { DataMgr } from "./Mgr/DataMgr";
import DebugMgr from "./Mgr/DebugMgr"; import DebugMgr from "./Mgr/DebugMgr";
import FrameAnimation from "./Component/FrameAnimation"; import FrameAnimation from "./Component/FrameAnimation";
import Drop from "./Game/Drop";
import { SpBoomEffectMgr } from "./Game/SpBoomEffect";
import GuideMask from "./Game/GuideMask";
import GuideMgr from "./Mgr/GuideMgr"; import GuideMgr from "./Mgr/GuideMgr";
import { MConst } from "./Global/MConst"; import { MConst } from "./Global/MConst";
import { NetUtils } from "./Global/NetUtils"; import { NetUtils } from "./Global/NetUtils";
import Net from "./Global/Net"; import Net from "./Global/Net";
import Loading from "./Global/Loading"; import Loading from "./Global/Loading";
import { initNECaptcha } from "./Component/InitNECaptcha"; import { initNECaptcha } from "./Component/InitNECaptcha";
import SoundMgr from "./Mgr/SoundMgr";
/** /**
* Created by rockyl on 2019-11-22. * Created by rockyl on 2019-11-22.
*/ */
export class ShootPlanet extends engine.Container { export class ShootPlanet extends engine.Container {
public game: Game = null; private game: Game = null;
private isSubmited = false; private isSubmited = false;
private constantSubmitSeq = 1; private constantSubmitSeq = 1;
private needSubmitCount = 0; //实际分数减去ConstantSubmitScoreNum的次数 private needSubmitCount = 0; //实际分数减去ConstantSubmitScoreNum的次数
private isSubmiting = false; private isSubmiting = false;
public startId: number = null; public startId: number = 0;
private _localScore: number = 0; private _localScore: number = 0;
public get localScore(): number { public get localScore(): number {
...@@ -48,42 +39,36 @@ export class ShootPlanet extends engine.Container { ...@@ -48,42 +39,36 @@ export class ShootPlanet extends engine.Container {
} }
} }
constructor() { public onSleep() {
super(); this.game.destroy();
this.game = null;
}
this.customProperty(); public onActive() {
this.game = new Game(this); this.game = new Game(this);
//监听游戏分数变化 //监听游戏分数变化
this.game.onScoreChange = (delta: number) => { this.game.onScoreChange = (delta: number) => {
this.localScore += delta; this.localScore += delta;
} };
this.width = 750;
this.height = 1624;
this.addEventListener(engine.Event.REMOVED_FROM_STAGE, this.onDestroy, this);
this.init();
if (GuideMgr.instance.guideFlag == true) { if (GuideMgr.instance.guideFlag == true) {
const car = this.game._car; const car = this.game._car;
GuideMgr.instance.runGuide(0, car.x + car.width / 2, car.y + car.height / 2 /* - 130 */); GuideMgr.instance.runGuide(0, car.x + car.width / 2, car.y + car.height / 2 /* - 130 */);
} }
} }
constructor() {
super();
this.customProperty();
this.width = 750;
this.height = 1624;
this.init();
}
private init() { private init() {
engine.env.soundEnabled = true; engine.env.soundEnabled = true;
PhycicsSystem.init();
MTimer.init(); MTimer.init();
MTween.init();
DropBlinkMgr.init();
DataMgr.game = this.game;
DebugMgr.instance.init(this); DebugMgr.instance.init(this);
MTimer.onFrame("FrameAnimation", FrameAnimation.onUpdate, FrameAnimation);
MTimer.onFrame("SpBoomEffectMgr", SpBoomEffectMgr.onUpdate, SpBoomEffectMgr);
}
private onDestroy() {
MTimer.destroy();
} }
private customProperty() { private customProperty() {
......
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