Commit c19ec7c9 authored by 谌继荃's avatar 谌继荃

初始化项目

parent 6a30f9f6
......@@ -5,10 +5,21 @@ export default class Movable extends FYGE.Sprite {
return true;
}
private _velocity: Vector2;
set velocity(val: Vector2) { this._velocity = val }
set velocity(val: Vector2) {
this._velocity = val;
}
get velocity() {
return this._velocity;
}
private _acceleration: Vector2;
set acceleration(val: Vector2) {
this._acceleration = val;
}
step() {
this._acceleration = this._acceleration || new Vector2(0, 0);
this._velocity.x += this._acceleration.x;
this._velocity.y += this._acceleration.y;
this.y += this._velocity.y;
this.x += this._velocity.x;
}
......
......@@ -5,11 +5,13 @@ import Movable from "./Movable";
export default class MovableManager extends FYGE.EventDispatcher {
private _stage: FYGE.Stage;
public Score: 0;
private _movableList: Movable[] = [];
hero: Hero;
constructor(stage: FYGE.Stage) {
super();
this._stage = stage;
this.Score = 0;
this._stage.addEventListener(FYGE.Event.ENTER_FRAME, this.onEnterFrame);
}
......@@ -20,16 +22,17 @@ export default class MovableManager extends FYGE.EventDispatcher {
this.checkHitByBullet();
this.checkHitByEnemy();
// console.log('移动对象的数量:', this._movableList.length)
}
};
checkHitByEnemy() {
const enemyList = this._movableList.filter(i => i instanceof Enemy);
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()
console.log("被敌机碰撞了,游戏结束了");
// this.Score = 0;
this.gameOver();
return;
}
}
......@@ -37,31 +40,38 @@ export default class MovableManager extends FYGE.EventDispatcher {
private gameOver() {
this._stage.removeEventListener(FYGE.Event.ENTER_FRAME, this.onEnterFrame);
this.dispatchEvent('gameover');
this.dispatchEvent("gameover");
}
checkHitByBullet() {
const enemyBulletList = this._movableList.filter(i => i instanceof Bullet && i.host instanceof Enemy);
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()
console.log("被子弹碰撞了,游戏结束了");
// this.Score = 0;
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);
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.Score += 10;
this.remove(oneEnemy);
this.remove(onHeroBullet);
console.log("this.Score", this.Score);
return;
}
}
......@@ -99,7 +109,7 @@ export default class MovableManager extends FYGE.EventDispatcher {
}
private step() {
this._movableList.forEach(item => item.step());
this._movableList.forEach((item) => item.step());
}
add(item: Movable) {
......@@ -111,5 +121,4 @@ export default class MovableManager extends FYGE.EventDispatcher {
this._movableList.splice(index, 1);
item.parent && item.parent.removeChild(item);
}
}
......@@ -7,7 +7,7 @@ class BackgroundItem extends Movable {
this.texture = FYGE.Texture.fromUrl(
"//yun.duiba.com.cn/aurora/assets/bd7b3b10169265123e52d02acf8739db5ff59b3d.png"
);
this.velocity = new Vector2(0, 10);
this.velocity = new Vector2(0, 5);
}
getCanRemove() {
return false;
......@@ -29,8 +29,8 @@ export default class Background {
bg2.y = height;
stage.addEventListener(FYGE.Event.ENTER_FRAME, () => {
console.log("bg.y", bg.y);
console.log("bg2.y", bg2.y);
// console.log("bg.y", bg.y);
// console.log("bg2.y", bg2.y);
if (bg.y > height) {
bg.y = bg2.y - height;
}
......
......@@ -4,7 +4,6 @@ import Vector2 from "../lib/Vector2";
import { IWeaponHost } from "../weapon/IWeaponHost";
import Weapon from "../weapon/Weapon";
export default class Enemy extends Movable implements IWeaponHost {
movableManager: MovableManager;
constructor(movableManager: MovableManager) {
......@@ -13,7 +12,9 @@ export default class Enemy extends Movable implements IWeaponHost {
movableManager.add(this);
this.texture = FYGE.Texture.fromUrl('//yun.duiba.com.cn/aurora/assets/26e1539bb9c5961c693f05186b086c8e04f2f6a2.png')
this.texture = FYGE.Texture.fromUrl(
"//yun.duiba.com.cn/aurora/assets/26e1539bb9c5961c693f05186b086c8e04f2f6a2.png"
);
const weapon = new Weapon(this);
......@@ -26,14 +27,22 @@ export default class Enemy extends Movable implements IWeaponHost {
this.addEventListener(FYGE.Event.REMOVED_FROM_STAGE, () => {
// console.log('敌人消失了')
weapon.destroy();
})
});
}
getShootInterval() { return 100 }
getShootInterval() {
return 50;
}
getShootVelocity() { return new Vector2(0, 10) }
getShootVelocity() {
return new Vector2(0, 10);
}
getShootAcceleration() {
return new Vector2(0, 0);
}
getShootPoint() {
return new FYGE.Point(this.x + this.width / 2, this.y + this.height)
return new FYGE.Point(this.x + this.width / 2, this.y + this.height);
}
}
......@@ -5,19 +5,23 @@ import Enemy from "./Enemy";
export default class EnemyFactory {
private _timer;
private _stage: FYGE.Stage;
movableManager: MovableManager
movableManager: MovableManager;
constructor(stage: FYGE.Stage, movableManager: MovableManager) {
this.movableManager = movableManager;
this._stage = stage;
this._timer = setInterval(this.onTimer, 1000)
this._timer = setInterval(this.onTimer, 1000);
}
onTimer = () => {
const enemy = this._stage.addChild(new Enemy(this.movableManager));
enemy.position.set(Math.random() * 300 + 200, 0);
enemy.velocity = new Vector2((Math.random() - 0.5) * 2, Math.random() * 5 + 2);;
}
enemy.velocity = new Vector2(
(Math.random() - 0.5) * 2,
Math.random() * 5 + 2
);
enemy.acceleration = new Vector2(0, 0.1);
};
destroy() {
clearInterval(this._timer);
......
......@@ -4,15 +4,15 @@ import Vector2 from "../lib/Vector2";
import { IWeaponHost } from "../weapon/IWeaponHost";
import Weapon from "../weapon/Weapon";
export default class Hero extends DragDropable implements IWeaponHost {
movableManager: MovableManager;
constructor(movableManager) {
super();
this.movableManager = movableManager;
this.texture = FYGE.Texture.fromUrl('//yun.duiba.com.cn/spark/assets/hero_fly1.f292cb1c04589c6ee395fe29538d5385540755f7.png');
this.texture = FYGE.Texture.fromUrl(
"//yun.duiba.com.cn/spark/assets/hero_fly1.f292cb1c04589c6ee395fe29538d5385540755f7.png"
);
const weapon = new Weapon(this);
......@@ -22,14 +22,24 @@ export default class Hero extends DragDropable implements IWeaponHost {
// bg.endFill()
// this.addChild(bg)
}
getShootInterval(){return 50}
getShootVelocity(){return new Vector2(0, -20)}
getShootInterval() {
return 50;
}
getShootVelocity() {
return new Vector2(0, -20);
}
getShootPoint() {
return new FYGE.Point(this.x + this.width / 2, this.y)
return new FYGE.Point(this.x + this.width / 2, this.y);
}
getDropPoint() { return {} }
getShootAcceleration() {
return new Vector2(0, 0);
}
getDropPoint() {
return {};
}
onDragEnd() { }
onDragEnd() {}
}
......@@ -11,7 +11,11 @@ export function addGame(stage: FYGE.Stage) {
const background = new Background(stage, movableManager);
const hero = stage.addChild(new Hero(movableManager));
// const hero2 = stage.addChild(new Hero(movableManager));
// const hero3 = stage.addChild(new Hero(movableManager));
hero.position.set(300, 1000);
// hero2.position.set(250, 1000);
// hero3.position.set(350, 1000);
const enemyFactory = new EnemyFactory(stage, movableManager);
......@@ -21,6 +25,8 @@ export function addGame(stage: FYGE.Stage) {
function onGamOver() {
alert("gameOver");
console.log("Score", movableManager.Score);
dragDropManager.remove(hero);
enemyFactory.destroy();
}
......
......@@ -5,5 +5,6 @@ export interface IWeaponHost extends FYGE.Sprite {
movableManager: MovableManager;
getShootPoint(): FYGE.Point;
getShootVelocity(): Vector2;
getShootAcceleration(): Vector2;
getShootInterval();
}
......@@ -3,7 +3,7 @@ import Bullet from "./Bullet";
import { IWeaponHost } from "./IWeaponHost";
export default class Weapon {
onCreateBullet: Function
onCreateBullet: Function;
_timer;
_count;
_host: IWeaponHost;
......@@ -23,7 +23,7 @@ export default class Weapon {
this.createBullet();
this.reset();
}
}
};
canCreateBullet() {
return this._count >= this._host.getShootInterval();
}
......@@ -33,11 +33,11 @@ export default class Weapon {
const bullet: Bullet = this._host.parent.addChild(new Bullet());
bullet.host = this._host;
bullet.velocity = this._host.getShootVelocity();
bullet.acceleration = this._host.getShootAcceleration();
const shootPoint = this._host.getShootPoint();
bullet.position.set(shootPoint.x, shootPoint.y);
this._host.movableManager.add(bullet);
}
};
destroy() {
clearInterval(this._timer);
......
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