Commit 1282263e authored by wildfirecode13's avatar wildfirecode13

init

parent 872e687d
......@@ -7,11 +7,15 @@ export default class DragDropManager extends FYGE.EventDispatcher {
public add(item: DragDropable) {
item.addEventListener('DROP', this.onDisplayItemDrop, this)
item.addEventListener(FYGE.MouseEvent.MOUSE_DOWN, this.onMouseDown, this)
item.once(FYGE.Event.REMOVED_FROM_STAGE, this.remove, this)
item.once(FYGE.Event.REMOVED_FROM_STAGE, this.onRemoveFromStage, this)
}
remove = (event: FYGE.Event) => {
onRemoveFromStage = (event: FYGE.Event) => {
const item = event.target;
this.remove(item)
}
remove(item) {
item.removeEventListener('DROP', this.onDisplayItemDrop, this)
item.removeEventListener(FYGE.MouseEvent.MOUSE_DOWN, this.onMouseDown, this)
}
......
import Enemy from "../planewar/Enemy";
import Hero from "../planewar/Hero";
import Bullet from "../weapon/Bullet";
import Movable from "./Movable";
export default class MovableManager {
export default class MovableManager extends FYGE.EventDispatcher {
private _stage: FYGE.Stage;
private _movableList: Movable[] = [];
hero: Hero;
constructor(stage: FYGE.Stage) {
super();
this._stage = stage;
this._stage.addEventListener(FYGE.Event.ENTER_FRAME, this.onEnterFrame);
}
......@@ -11,7 +16,67 @@ export default class MovableManager {
onEnterFrame = () => {
this.step();
this.checkRemove();
// console.log('移动对象的数量:',this._movableList.length)
this.checkHitEnemy();
this.checkHitByBullet();
this.checkHitByEnemy();
// console.log('移动对象的数量:', this._movableList.length)
}
checkHitByEnemy() {
const enemyList = this._movableList.filter(i => i instanceof Enemy);
for (let i = 0; i < enemyList.length; i++) {
const oneEnemy = enemyList[i];
if (this.checkHit(oneEnemy, this.hero)) {
console.log('被敌机碰撞了,游戏结束了');
this.gameOver()
return;
}
}
}
private gameOver() {
this._stage.removeEventListener(FYGE.Event.ENTER_FRAME, this.onEnterFrame);
this.dispatchEvent('gameover');
}
checkHitByBullet() {
const enemyBulletList = this._movableList.filter(i => i instanceof Bullet && i.host instanceof Enemy);
for (let j = 0; j < enemyBulletList.length; j++) {
const bullet = enemyBulletList[j];
if (this.checkHit(this.hero, bullet)) {
console.log('被子弹碰撞了,游戏结束了');
this.gameOver()
return;
}
}
}
private checkHitEnemy() {
const heroBulletList = this._movableList.filter(i => i instanceof Bullet && i.host instanceof Hero);
const enemyList = this._movableList.filter(i => i instanceof Enemy);
for (let i = 0; i < enemyList.length; i++) {
const oneEnemy = enemyList[i];
for (let j = 0; j < heroBulletList.length; j++) {
const onHeroBullet = heroBulletList[j];
if (this.checkHit(oneEnemy, onHeroBullet)) {
this.remove(oneEnemy);
this.remove(onHeroBullet);
return;
}
}
}
}
private checkHit(A: FYGE.DisplayObject, B: FYGE.DisplayObject) {
const pointA = [A.x + A.width / 2, A.y + A.height / 2];
const pointB = [B.x + B.width / 2, B.y + B.height / 2];
const x0 = Math.abs(pointA[0] - pointB[0]);
const y0 = Math.abs(pointA[1] - pointB[1]);
const x1 = A.width / 2 + B.width / 2;
const y1 = A.height / 2 + B.height / 2;
return x0 < x1 && y0 < y1;
}
private calcCanRemove(item: FYGE.DisplayObject) {
......@@ -28,7 +93,7 @@ export default class MovableManager {
if (this.calcCanRemove(item)) {
this._movableList.splice(index, 1);
item.parent && item.parent.removeChild(item);
index--;
return;
}
}
}
......@@ -44,6 +109,7 @@ export default class MovableManager {
remove(item: Movable) {
const index = this._movableList.indexOf(item);
this._movableList.splice(index, 1);
item.parent && item.parent.removeChild(item);
}
}
\ No newline at end of file
......@@ -13,17 +13,23 @@ export default class Enemy extends Movable implements IWeaponHost {
movableManager.add(this);
this.texture = FYGE.Texture.fromUrl('//yun.duiba.com.cn/spark/assets/enemy2_fly1.0f4acd3728e90763d98a7328a5a32dbc78ec204e.png')
this.texture = FYGE.Texture.fromUrl('//yun.duiba.com.cn/aurora/assets/26e1539bb9c5961c693f05186b086c8e04f2f6a2.png')
const weapon = new Weapon(this);
// const bg = new FYGE.Graphics;
// bg.beginFill(0x00ff00, 0.2)
// bg.drawRect(0, 0, 69, 88)
// bg.endFill()
// this.addChild(bg)
this.addEventListener(FYGE.Event.REMOVED_FROM_STAGE, () => {
console.log('敌人消失了')
// console.log('敌人消失了')
weapon.destroy();
})
}
getShootInterval(){return 100}
getShootInterval() { return 100 }
getShootVelocity() { return new Vector2(0, 10) }
......
......@@ -15,9 +15,15 @@ export default class Hero extends DragDropable implements IWeaponHost {
this.texture = FYGE.Texture.fromUrl('//yun.duiba.com.cn/spark/assets/hero_fly1.f292cb1c04589c6ee395fe29538d5385540755f7.png');
const weapon = new Weapon(this);
// const bg = new FYGE.Graphics;
// bg.beginFill(0xff0000,0.2)
// bg.drawRect(0,0,99,124)
// bg.endFill()
// this.addChild(bg)
}
getShootInterval(){return 10}
getShootVelocity(){return new Vector2(0, -8)}
getShootInterval(){return 50}
getShootVelocity(){return new Vector2(0, -20)}
getShootPoint() {
return new FYGE.Point(this.x + this.width / 2, this.y)
......
import DragDropable from "../dragdrop/DragDropable";
import DragDropManager from "../dragdrop/DragDropManager";
import MovableManager from "../lib/MovableManager";
import Vector2 from "../lib/Vector2";
import Enemy from "./Enemy";
import EnemyFactory from "./EnemyFactory";
import Hero from "./Hero";
......@@ -11,10 +9,18 @@ export function addGame(stage: FYGE.Stage) {
const movableManager = new MovableManager(stage);//创建管理器
const dragDropManager = new DragDropManager();
const hero = stage.addChild(new Hero(movableManager)) as DragDropable;
const hero = stage.addChild(new Hero(movableManager));
hero.position.set(300, 1000);
const enemyFactory = new EnemyFactory(stage,movableManager);
const enemyFactory = new EnemyFactory(stage, movableManager);
dragDropManager.add(hero);
movableManager.hero = hero;
function onGamOver() {
dragDropManager.remove(hero);
}
movableManager.addEventListener('gameover', onGamOver);
}
\ No newline at end of file
import Movable from "../lib/Movable";
export default class Bullet extends Movable {
host;
constructor() {
super();
this.texture = FYGE.Texture.fromUrl('//yun.duiba.com.cn/spark/assets/bullet1.63bb85f32cabe5f986366c2d428c5e2aa2435230.png')
......
......@@ -30,7 +30,8 @@ export default class Weapon {
createBullet = () => {
// console.log('武器在发射子弹')
const bullet: Movable = this._host.parent.addChild(new Bullet());
const bullet: Bullet = this._host.parent.addChild(new Bullet());
bullet.host = this._host;
bullet.velocity = this._host.getShootVelocity();
const shootPoint = this._host.getShootPoint();
bullet.position.set(shootPoint.x, shootPoint.y);
......
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