Commit 6c0ccd04 authored by 谌继荃's avatar 谌继荃

项目初步完成

parent bf202af7
import Movable from "../lib/Movable";
import MovableManager from "../lib/MovableManager";
import Vector2 from "../lib/Vector2";
import ScoreClass from "../tools/Score";
let scoreClass;
class BackgroundItem extends Movable {
constructor() {
constructor(stage) {
super();
scoreClass = new ScoreClass(stage);
this.texture = FYGE.Texture.fromUrl(
"//yun.duiba.com.cn/aurora/assets/bd7b3b10169265123e52d02acf8739db5ff59b3d.png"
);
this.velocity = new Vector2(1, 0);
this.velocity = new Vector2(2.5, 0);
}
getCanRemove() {
return false;
}
}
const width = 375;
const width = 749; // 750 有一根白线
export default class Background {
constructor(stage: FYGE.Stage, movableManager: MovableManager) {
var bg = new BackgroundItem();
var bg2 = new BackgroundItem();
var bg = new BackgroundItem(stage);
var bg2 = new BackgroundItem(stage);
movableManager.add(bg);
movableManager.add(bg2);
......@@ -28,9 +33,22 @@ export default class Background {
bg.y = 0;
bg2.x = width;
let count = 0;
let scoreCount = 30; // 30px 加1分
let score = 0;
stage.addEventListener(FYGE.Event.ENTER_FRAME, () => {
console.log("bg.x", bg.x);
console.log("bg2.x", bg2.x);
// console.log("bg.x", bg.x);
// console.log("bg2.x", bg2.x);
if (count < scoreCount) {
count++;
} else if (count == scoreCount) {
count = 0;
score++;
// console.log("scoreClass", scoreClass.change );
scoreClass.change(score);
console.log("score", score);
}
if (bg.x > width) {
bg.x = bg2.x - width;
}
......
import Movable from "../lib/Movable";
export default class Bullet extends Movable {
export default class Bird extends Movable {
constructor() {
super();
this.texture = FYGE.Texture.fromUrl(
......
import Movable from "../lib/Movable";
import MovableManager from "../lib/MovableManager";
import Vector2 from "../lib/Vector2";
class BirdItem extends Movable {
constructor(stage) {
super();
this.texture = FYGE.Texture.fromUrl(
"//yun.duiba.com.cn/aurora/assets/0bcb2c26a85addb1714b4c63f2a873aafe210749.png"
);
this.position.set(300, 0);
this.velocity = new Vector2(0, 0);
this.acceleration = new Vector2(0, 1);
}
getCanRemove() {
return false;
}
}
export default class Bird {
constructor(stage: FYGE.Stage, movableManager: MovableManager) {
var bird = new BirdItem(stage);
movableManager.add(bird);
stage.addChild(bird);
stage.addEventListener(FYGE.Event.ENTER_FRAME, () => {});
}
}
import Movable from "../lib/Movable";
import MovableManager from "../lib/MovableManager";
export default class Enemy extends Movable {
movableManager: MovableManager;
constructor(movableManager: MovableManager) {
super();
this.movableManager = movableManager;
movableManager.add(this);
this.texture = FYGE.Texture.fromUrl(
"//yun.duiba.com.cn/aurora/assets/9fe8d613157bddffdb2bf5ca9a748e8fbb7e9471.png"
);
this.addEventListener(FYGE.Event.REMOVED_FROM_STAGE, () => {});
}
}
import MovableManager from "../lib/MovableManager";
import Vector2 from "../lib/Vector2";
import Enemy from "./Enemy";
export default class EnemyFactory {
private _timer;
private _stage: FYGE.Stage;
movableManager: MovableManager;
constructor(stage: FYGE.Stage, movableManager: MovableManager) {
this.movableManager = movableManager;
this._stage = stage;
this._timer = setInterval(this.onTimer, 1000);
}
onTimer = () => {
const enemy1 = this._stage.addChild(new Enemy(this.movableManager));
const enemy2 = this._stage.addChild(new Enemy(this.movableManager));
enemy1.position.set(750, 0);
enemy2.position.set(750, 1060);
enemy1.velocity = new Vector2(-10, 0);
enemy2.velocity = new Vector2(-10, 0);
};
destroy() {
clearInterval(this._timer);
}
}
import Bird from "./Bird";
import MovableManager from "../lib/MovableManager";
import Vector2 from "../lib/Vector2";
import Background from "./Background";
import Bird from "./Bird";
import Score from "../tools/Score";
import EnemyFactory from "./EnemyFactory";
export function addGame(stage: FYGE.Stage) {
const movableManager = new MovableManager(stage); //创建管理器
const background = new Background(stage, movableManager);
const bird = stage.addChild(new Bird()); //有加速度
bird.position.set(300, 200);
// const bird = new Bird(stage, movableManager); //有加速度
bird.position.set(300, 500);
bird.velocity = new Vector2(0, 0);
bird.acceleration = new Vector2(0, 1);
const enemyFactory = new EnemyFactory(stage, movableManager);
movableManager.add(bird);
// 当前分数
const score = new Score(stage);
function onGamOver() {
console.log("onGamOver");
// alert(`gameOver 您获得的分数为${movableManager.Score}`);
enemyFactory.destroy();
movableManager.destroy();
window.dispatchEvent(new Event("pause"));
}
stage.addEventListener(FYGE.MouseEvent.CLICK, onclick);
function onclick() {
bird.velocity.y = -20;
window.addEventListener("gameover", onGamOver);
function onclick(e) {
bird.velocity.y = -15;
}
}
import Vector2 from "./Vector2";
export default class Movable extends FYGE.Sprite {
getCanRemove() {
return true;
}
private _velocity: Vector2;
set velocity(val: Vector2) {
this._velocity = val;
}
get velocity() {
return this._velocity;
}
private _acceleration: Vector2;
set acceleration(val: Vector2) {
this._acceleration = val;
}
private _velocity: Vector2;
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;
}
}
\ No newline at end of file
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;
}
}
import Enemy from "../bird/Enemy";
import Bird from "../bird/Bird";
import Movable from "./Movable";
import ScoreClass from "../tools/Score";
export default class MovableManager {
private _stage: FYGE.Stage;
private _movableList: Movable[] = [];
constructor(stage: FYGE.Stage) {
this._stage = stage;
this._stage.addEventListener(FYGE.Event.ENTER_FRAME, this.onEnterFrame);
}
let scoreClass;
let birdClass;
export default class MovableManager extends FYGE.EventDispatcher {
private _stage: FYGE.Stage;
public Score: 0;
bird: Bird;
private _movableList: Movable[] = [];
onEnterFrame = () => {
this.step();
this.checkRemove();
console.log('移动对象的数量:',this._movableList.length)
}
constructor(stage: FYGE.Stage) {
super();
this._stage = stage;
this.Score = 0;
private calcCanRemove(item: FYGE.DisplayObject) {
if (item.y > 1624) return true;
if (item.y < -item.height) return true;
if (item.x > 750) return true;
if (item.x < -item.width) return true;
return false;
}
scoreClass = new ScoreClass(stage);
this._stage.addEventListener(FYGE.Event.ENTER_FRAME, this.onEnterFrame);
}
private checkRemove() {
for (let index = 0; index < this._movableList.length; index++) {
const item = this._movableList[index];
if (this.calcCanRemove(item)) {
this._movableList.splice(index, 1);
index--;
}
}
}
onEnterFrame = () => {
this.step();
this.checkBirdHeight();
this.checkEnemy();
this.checkHitByBird();
};
private step() {
this._movableList.forEach(item => item.step());
}
private gameOver() {
this._stage.removeEventListener(FYGE.Event.ENTER_FRAME, this.onEnterFrame);
// this.dispatchEvent("gameover");
window.dispatchEvent(new Event("gameover"));
}
add(item: Movable) {
this._movableList.push(item);
}
remove(item: Movable) {
const index = this._movableList.indexOf(item);
this._movableList.splice(index, 1);
// 小鸟碰到顶部和底部 死亡
checkBirdHeight() {
const BirdList = this._movableList.filter((i) => i instanceof Bird);
const bird = BirdList[0];
console.log("移动对象bird:", bird.y);
if (bird.y > this._stage.stageHeight - bird.height || bird.y <= 0) {
console.log("死亡");
this.gameOver();
}
}
// 检查管道 回收对象
checkEnemy() {
const EnemyList = this._movableList.filter((i) => i instanceof Enemy);
console.log("移动对象EnemyList:", EnemyList);
EnemyList.forEach((item) => {
if (item.x <= 0) {
this.remove(item);
}
});
console.log("this._movableList", this._movableList);
}
// 小鸟和管道的碰撞
checkHitByBird() {
const enemyList = this._movableList.filter((i) => i instanceof Enemy);
const BirdList = this._movableList.filter((i) => i instanceof Bird);
const bird = BirdList[0];
enemyList.forEach((enemy) => {
if (this.checkHit(bird, enemy)) {
console.log("被碰撞了,游戏结束了");
this.gameOver();
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 step() {
this._movableList.forEach((item) => item.step());
}
add(item: Movable) {
this._movableList.push(item);
}
}
\ No newline at end of file
remove(item: Movable) {
const index = this._movableList.indexOf(item);
this._movableList.splice(index, 1);
item.parent && item.parent.removeChild(item);
}
}
......@@ -12,7 +12,8 @@ var stage = new FYGE.Stage(
false,
false
);
let pause = false;
let Game = new addGame(stage);
var mouseEvent = stage.onMouseEvent.bind(stage);
canvas.addEventListener("touchstart", mouseEvent, false);
canvas.addEventListener("touchmove", mouseEvent, false);
......@@ -21,11 +22,20 @@ canvas.addEventListener("touchend", mouseEvent, false);
stage.addEventListener(FYGE.Event.INIT_STAGE, onInitStage, this);
function onInitStage() {
new addGame(stage);
Game;
}
(function loop() {
FYGE.Tween.flush();
stage.flush();
if (!pause) {
FYGE.Tween.flush();
stage.flush();
}
requestAnimationFrame(loop);
})();
function pauseGame() {
pause = !pause;
console.log("收到暂停", pause);
}
window.addEventListener("pause", pauseGame);
let PauseText;
export default class Pause {
constructor(stage: FYGE.Stage) {
PauseText = new FYGE.TextField();
PauseText.text = "暂停";
PauseText.fillColor = "#000000";
PauseText.size = 50;
PauseText.x = 50;
PauseText.y = 30;
stage.addChild(PauseText);
PauseText.addEventListener(FYGE.MouseEvent.CLICK, this.pause, this);
}
pause() {
console.log("暂停");
// 目前点击一次不能点击了
window.dispatchEvent(new Event("pause"));
}
reset() {}
destroy(stage) {
PauseText.removeEventListener(FYGE.MouseEvent.CLICK, this.pause, this);
stage.removeChild(PauseText);
}
}
let scoreText;
let score = 0;
export default class Score {
constructor(stage: FYGE.Stage) {
scoreText = new FYGE.TextField();
scoreText.text = `当前分数:0`;
scoreText.x = 250;
scoreText.y = 30;
scoreText.size = 45;
stage.addChild(scoreText);
stage.addEventListener("score", this.change);
window.addEventListener("gameover", this.onGamOver);
}
change(changeScore) {
score = changeScore;
scoreText.text = `当前分数:${score}`;
console.log("change---scoreText.text", scoreText.text);
}
onGamOver() {
alert(`gameOver 您获得的分数为${score}`);
}
reset() {}
destroy() {}
}
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