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

初始化项目

parent 6a30f9f6
This diff is collapsed.
{ {
"scripts": { "scripts": {
"start": "webpack serve --open", "start": "webpack serve --open",
"build": "webpack" "build": "webpack"
}, },
"devDependencies": { "devDependencies": {
"ts-loader": "^9.2.6", "ts-loader": "^9.2.6",
"typescript": "^4.4.4", "typescript": "^4.4.4",
"webpack": "^5.61.0", "webpack": "^5.61.0",
"webpack-cli": "^4.9.1", "webpack-cli": "^4.9.1",
"webpack-dev-server": "^4.4.0" "webpack-dev-server": "^4.4.0"
} }
} }
This diff is collapsed.
<!DOCTYPE html> <!DOCTYPE html>
<html lang="en"> <html lang="en">
<head> <head>
<meta charset="UTF-8"> <meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0"> <meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title> <title>Document</title>
<script src="http://yun.duiba.com.cn/db_games/libs0924/fyge2018.minSpine.js" crossorigin="anonymous"></script> <script src="http://yun.duiba.com.cn/db_games/libs0924/fyge2018.minSpine.js" crossorigin="anonymous"></script>
<style> <style>
html, html,
body { body {
padding: 0; padding: 0;
margin: 0; margin: 0;
border: 0; border: 0;
width: 100%; width: 100%;
height: 100%; height: 100%;
overflow: hidden; overflow: hidden;
position: absolute; position: absolute;
background-color: #faf4f4; background-color: #faf4f4;
/* background: linear-gradient(#93dbb7,#ff0,#b5d89a); */ /* background: linear-gradient(#93dbb7,#ff0,#b5d89a); */
/* background: linear-gradient(#93dbb7,#b5d89a); */ /* background: linear-gradient(#93dbb7,#b5d89a); */
/* 背景图片,解决加载太慢,白屏问题,加了这个下面的__loading__可以删掉了 */ /* 背景图片,解决加载太慢,白屏问题,加了这个下面的__loading__可以删掉了 */
/* background-size: 100%; /* background-size: 100%;
background-position: center; background-position: center;
background-image: url("https://yun.duiba.com.cn/db_games/activity/game/1550472986/resource/assets/playscene/playscenebg.jpg"); */ background-image: url("https://yun.duiba.com.cn/db_games/activity/game/1550472986/resource/assets/playscene/playscenebg.jpg"); */
} }
</style> </style>
</head> </head>
<body> <body>
<div id="cusEngine" style="line-height:0;font-size:0"> <div id="cusEngine" style="line-height:0;font-size:0">
<canvas id="canvas" style="width: 100%;height: 100%"></canvas> <canvas id="canvas" style="width: 100%;height: 100%"></canvas>
</div> </div>
<script src="bundle.js"></script> <script src="bundle.js"></script>
</body> </body>
</html> </html>
\ No newline at end of file
export default class DragDropEvent extends FYGE.Event { export default class DragDropEvent extends FYGE.Event {
static DRAG_START = 'DRAG_START'; static DRAG_START = 'DRAG_START';
static DRAG_END = 'DRAG_END'; static DRAG_END = 'DRAG_END';
} }
\ No newline at end of file
import DragDropable from "./DragDropable"; import DragDropable from "./DragDropable";
import DragDropEvent from "./DragDropEvent"; import DragDropEvent from "./DragDropEvent";
//管理一个拖放实例 //管理一个拖放实例
export default class DragDropManager extends FYGE.EventDispatcher { export default class DragDropManager extends FYGE.EventDispatcher {
//初始化拖放对象事件 //初始化拖放对象事件
public add(item: DragDropable) { public add(item: DragDropable) {
item.addEventListener('DROP', this.onDisplayItemDrop, this) item.addEventListener('DROP', this.onDisplayItemDrop, this)
item.addEventListener(FYGE.MouseEvent.MOUSE_DOWN, this.onMouseDown, this) item.addEventListener(FYGE.MouseEvent.MOUSE_DOWN, this.onMouseDown, this)
item.once(FYGE.Event.REMOVED_FROM_STAGE, this.onRemoveFromStage, this) item.once(FYGE.Event.REMOVED_FROM_STAGE, this.onRemoveFromStage, this)
} }
onRemoveFromStage = (event: FYGE.Event) => { onRemoveFromStage = (event: FYGE.Event) => {
const item = event.target; const item = event.target;
this.remove(item) this.remove(item)
} }
remove(item) { remove(item) {
item.removeEventListener('DROP', this.onDisplayItemDrop, this) item.removeEventListener('DROP', this.onDisplayItemDrop, this)
item.removeEventListener(FYGE.MouseEvent.MOUSE_DOWN, this.onMouseDown, this) item.removeEventListener(FYGE.MouseEvent.MOUSE_DOWN, this.onMouseDown, this)
} }
private onMouseDown(event: FYGE.MouseEvent) { private onMouseDown(event: FYGE.MouseEvent) {
const item: DragDropable = event.target; const item: DragDropable = event.target;
if (!item.checkCanDragDrop()) return; if (!item.checkCanDragDrop()) return;
item.startDrag(event); item.startDrag(event);
this.dispatchEvent(DragDropEvent.DRAG_START, item); this.dispatchEvent(DragDropEvent.DRAG_START, item);
} }
private onDisplayItemDrop(e: FYGE.Event) { private onDisplayItemDrop(e: FYGE.Event) {
const drag: DragDropable = e.target; const drag: DragDropable = e.target;
drag.mouseEnable = false; drag.mouseEnable = false;
const dragParent = drag.parent; const dragParent = drag.parent;
const drop: DragDropable = dragParent.hitTestPoint(new FYGE.Point(e.data.x / 2, e.data.y / 2), true); const drop: DragDropable = dragParent.hitTestPoint(new FYGE.Point(e.data.x / 2, e.data.y / 2), true);
drag.mouseEnable = true; drag.mouseEnable = true;
if (drop) { if (drop) {
drop.onDrop(drag); drop.onDrop(drag);
} }
drag.onDragEnd(drop); drag.onDragEnd(drop);
this.dispatchEvent(DragDropEvent.DRAG_END, { drop, drag }); this.dispatchEvent(DragDropEvent.DRAG_END, { drop, drag });
} }
} }
\ No newline at end of file
export default class DragDropable extends FYGE.Sprite { export default class DragDropable extends FYGE.Sprite {
//图片起始位置 //图片起始位置
private _originPos: FYGE.Point; private _originPos: FYGE.Point;
public get originPos() { return this._originPos } public get originPos() { return this._originPos }
//鼠标按下起始点 //鼠标按下起始点
private _startPoint: FYGE.Point; private _startPoint: FYGE.Point;
startDrag = (event: FYGE.MouseEvent) => { startDrag = (event: FYGE.MouseEvent) => {
this.stage.addEventListener(FYGE.MouseEvent.MOUSE_MOVE, this.onMouseMove, this); this.stage.addEventListener(FYGE.MouseEvent.MOUSE_MOVE, this.onMouseMove, this);
this.stage.once(FYGE.MouseEvent.MOUSE_UP, this.onMouseUp, this); this.stage.once(FYGE.MouseEvent.MOUSE_UP, this.onMouseUp, this);
this._originPos = new FYGE.Point(this.x, this.y); this._originPos = new FYGE.Point(this.x, this.y);
this._startPoint = new FYGE.Point(event.stageX, event.stageY); this._startPoint = new FYGE.Point(event.stageX, event.stageY);
} }
private onMouseUp(event: FYGE.MouseEvent) { private onMouseUp(event: FYGE.MouseEvent) {
this.stage.removeEventListener(FYGE.MouseEvent.MOUSE_MOVE, this.onMouseMove, this); this.stage.removeEventListener(FYGE.MouseEvent.MOUSE_MOVE, this.onMouseMove, this);
this.dispatchEvent('DROP', this.getDropPoint(event)); this.dispatchEvent('DROP', this.getDropPoint(event));
} }
private onMouseMove(event: FYGE.MouseEvent) { private onMouseMove(event: FYGE.MouseEvent) {
//鼠标当前位置 //鼠标当前位置
const currentPoint = { x: event.stageX, y: event.stageY }; const currentPoint = { x: event.stageX, y: event.stageY };
//鼠标按下点到鼠标当前点的偏移量 //鼠标按下点到鼠标当前点的偏移量
let mouseOffsetX = currentPoint.x - this._startPoint.x; let mouseOffsetX = currentPoint.x - this._startPoint.x;
let mouseOffsetY = currentPoint.y - this._startPoint.y; let mouseOffsetY = currentPoint.y - this._startPoint.y;
this.x = this._originPos.x + mouseOffsetX; this.x = this._originPos.x + mouseOffsetX;
this.y = this._originPos.y + mouseOffsetY; this.y = this._originPos.y + mouseOffsetY;
} }
checkCanDragDrop() { checkCanDragDrop() {
return true; return true;
} }
public onDragEnd(drop: DragDropable) { public onDragEnd(drop: DragDropable) {
throw new Error("Method not implemented."); throw new Error("Method not implemented.");
} }
protected getDropPoint(event: FYGE.MouseEvent) { protected getDropPoint(event: FYGE.MouseEvent) {
throw new Error('必须重写此方法') throw new Error('必须重写此方法')
} }
public onDrop = (drag: DragDropable): void => { public onDrop = (drag: DragDropable): void => {
throw new Error('必须重写此方法') throw new Error('必须重写此方法')
} }
} }
\ No newline at end of file
import Vector2 from "./Vector2"; import Vector2 from "./Vector2";
export default class Movable extends FYGE.Sprite { export default class Movable extends FYGE.Sprite {
getCanRemove() { getCanRemove() {
return true; return true;
} }
private _velocity: Vector2; private _velocity: Vector2;
set velocity(val: Vector2) {
set velocity(val: Vector2) { this._velocity = val } this._velocity = val;
}
step() { get velocity() {
this.y += this._velocity.y; return this._velocity;
this.x += this._velocity.x; }
} private _acceleration: Vector2;
} set acceleration(val: Vector2) {
\ No newline at end of file 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;
}
}
import Enemy from "../planewar/Enemy"; import Enemy from "../planewar/Enemy";
import Hero from "../planewar/Hero"; import Hero from "../planewar/Hero";
import Bullet from "../weapon/Bullet"; import Bullet from "../weapon/Bullet";
import Movable from "./Movable"; import Movable from "./Movable";
export default class MovableManager extends FYGE.EventDispatcher { export default class MovableManager extends FYGE.EventDispatcher {
private _stage: FYGE.Stage; private _stage: FYGE.Stage;
private _movableList: Movable[] = []; public Score: 0;
hero: Hero; private _movableList: Movable[] = [];
constructor(stage: FYGE.Stage) { hero: Hero;
super(); constructor(stage: FYGE.Stage) {
this._stage = stage; super();
this._stage.addEventListener(FYGE.Event.ENTER_FRAME, this.onEnterFrame); this._stage = stage;
} this.Score = 0;
this._stage.addEventListener(FYGE.Event.ENTER_FRAME, this.onEnterFrame);
onEnterFrame = () => { }
this.step();
this.checkRemove(); onEnterFrame = () => {
this.checkHitEnemy(); this.step();
this.checkHitByBullet(); this.checkRemove();
this.checkHitByEnemy(); this.checkHitEnemy();
// console.log('移动对象的数量:', this._movableList.length) 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++) { checkHitByEnemy() {
const oneEnemy = enemyList[i]; const enemyList = this._movableList.filter((i) => i instanceof Enemy);
for (let i = 0; i < enemyList.length; i++) {
if (this.checkHit(oneEnemy, this.hero)) { const oneEnemy = enemyList[i];
console.log('被敌机碰撞了,游戏结束了');
this.gameOver() if (this.checkHit(oneEnemy, this.hero)) {
return; console.log("被敌机碰撞了,游戏结束了");
} // this.Score = 0;
} this.gameOver();
} return;
}
private gameOver() { }
this._stage.removeEventListener(FYGE.Event.ENTER_FRAME, this.onEnterFrame); }
this.dispatchEvent('gameover');
} private gameOver() {
this._stage.removeEventListener(FYGE.Event.ENTER_FRAME, this.onEnterFrame);
checkHitByBullet() { this.dispatchEvent("gameover");
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]; checkHitByBullet() {
if (this.checkHit(this.hero, bullet)) { const enemyBulletList = this._movableList.filter(
console.log('被子弹碰撞了,游戏结束了'); (i) => i instanceof Bullet && i.host instanceof Enemy
this.gameOver() );
return; for (let j = 0; j < enemyBulletList.length; j++) {
} const bullet = enemyBulletList[j];
} if (this.checkHit(this.hero, bullet)) {
} console.log("被子弹碰撞了,游戏结束了");
// this.Score = 0;
private checkHitEnemy() { this.gameOver();
const heroBulletList = this._movableList.filter(i => i instanceof Bullet && i.host instanceof Hero); return;
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]; private checkHitEnemy() {
if (this.checkHit(oneEnemy, onHeroBullet)) { const heroBulletList = this._movableList.filter(
this.remove(oneEnemy); (i) => i instanceof Bullet && i.host instanceof Hero
this.remove(onHeroBullet); );
return; 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)) {
private checkHit(A: FYGE.DisplayObject, B: FYGE.DisplayObject) { this.Score += 10;
const pointA = [A.x + A.width / 2, A.y + A.height / 2]; this.remove(oneEnemy);
const pointB = [B.x + B.width / 2, B.y + B.height / 2]; this.remove(onHeroBullet);
const x0 = Math.abs(pointA[0] - pointB[0]); console.log("this.Score", this.Score);
const y0 = Math.abs(pointA[1] - pointB[1]); return;
const x1 = A.width / 2 + B.width / 2; }
const y1 = A.height / 2 + B.height / 2; }
return x0 < x1 && y0 < y1; }
} }
private calcCanRemove(item: Movable) { private checkHit(A: FYGE.DisplayObject, B: FYGE.DisplayObject) {
if (!item.getCanRemove()) return; const pointA = [A.x + A.width / 2, A.y + A.height / 2];
if (item.y > 1624) return true; const pointB = [B.x + B.width / 2, B.y + B.height / 2];
if (item.y < -item.height) return true; const x0 = Math.abs(pointA[0] - pointB[0]);
if (item.x > 750) return true; const y0 = Math.abs(pointA[1] - pointB[1]);
if (item.x < -item.width) return true; const x1 = A.width / 2 + B.width / 2;
return false; const y1 = A.height / 2 + B.height / 2;
} return x0 < x1 && y0 < y1;
}
private checkRemove() {
for (let index = 0; index < this._movableList.length; index++) { private calcCanRemove(item: Movable) {
const item = this._movableList[index]; if (!item.getCanRemove()) return;
if (this.calcCanRemove(item)) { if (item.y > 1624) return true;
this._movableList.splice(index, 1); if (item.y < -item.height) return true;
item.parent && item.parent.removeChild(item); if (item.x > 750) return true;
return; if (item.x < -item.width) return true;
} return false;
} }
}
private checkRemove() {
private step() { for (let index = 0; index < this._movableList.length; index++) {
this._movableList.forEach(item => item.step()); const item = this._movableList[index];
} if (this.calcCanRemove(item)) {
this._movableList.splice(index, 1);
add(item: Movable) { item.parent && item.parent.removeChild(item);
this._movableList.push(item); return;
} }
}
remove(item: Movable) { }
const index = this._movableList.indexOf(item);
this._movableList.splice(index, 1); private step() {
item.parent && item.parent.removeChild(item); this._movableList.forEach((item) => item.step());
} }
} add(item: Movable) {
\ No newline at end of file this._movableList.push(item);
}
remove(item: Movable) {
const index = this._movableList.indexOf(item);
this._movableList.splice(index, 1);
item.parent && item.parent.removeChild(item);
}
}
export default class Vector2 { export default class Vector2 {
x:number; x:number;
y:number; y:number;
constructor(x:number,y:number){ constructor(x:number,y:number){
this.x=x; this.x=x;
this.y=y; this.y=y;
} }
} }
\ No newline at end of file
import { addGame } from "./planewar/addGame"; import { addGame } from "./planewar/addGame";
var canvas: any = document.getElementById("canvas") var canvas: any = document.getElementById("canvas")
canvas.width = document.body.clientWidth * 1 canvas.width = document.body.clientWidth * 1
canvas.height = document.body.clientHeight * 1 canvas.height = document.body.clientHeight * 1
var stage = new FYGE.Stage( var stage = new FYGE.Stage(
canvas, canvas,
750, 750,
1624, 1624,
canvas.width, canvas.width,
canvas.height, canvas.height,
FYGE.RENDERER_TYPE.CANVAS, FYGE.RENDERER_TYPE.CANVAS,
false, false,
false false
) )
var mouseEvent = stage.onMouseEvent.bind(stage); var mouseEvent = stage.onMouseEvent.bind(stage);
canvas.addEventListener("touchstart", mouseEvent, false); canvas.addEventListener("touchstart", mouseEvent, false);
canvas.addEventListener('touchmove', mouseEvent, false); canvas.addEventListener('touchmove', mouseEvent, false);
canvas.addEventListener('touchend', mouseEvent, false); canvas.addEventListener('touchend', mouseEvent, false);
stage.addEventListener(FYGE.Event.INIT_STAGE, onInitStage, this); stage.addEventListener(FYGE.Event.INIT_STAGE, onInitStage, this);
function onInitStage() { function onInitStage() {
new addGame(stage) new addGame(stage)
} }
(function loop() { (function loop() {
FYGE.Tween.flush() FYGE.Tween.flush()
stage.flush(); stage.flush();
requestAnimationFrame(loop); requestAnimationFrame(loop);
})(); })();
\ No newline at end of file
import Movable from "../lib/Movable"; import Movable from "../lib/Movable";
import MovableManager from "../lib/MovableManager"; import MovableManager from "../lib/MovableManager";
import Vector2 from "../lib/Vector2"; import Vector2 from "../lib/Vector2";
class BackgroundItem extends Movable { class BackgroundItem extends Movable {
constructor() { constructor() {
super(); super();
this.texture = FYGE.Texture.fromUrl( this.texture = FYGE.Texture.fromUrl(
"//yun.duiba.com.cn/aurora/assets/bd7b3b10169265123e52d02acf8739db5ff59b3d.png" "//yun.duiba.com.cn/aurora/assets/bd7b3b10169265123e52d02acf8739db5ff59b3d.png"
); );
this.velocity = new Vector2(0, 10); this.velocity = new Vector2(0, 5);
} }
getCanRemove() { getCanRemove() {
return false; return false;
} }
} }
const height = 1624; const height = 1624;
export default class Background { export default class Background {
constructor(stage: FYGE.Stage, movableManager: MovableManager) { constructor(stage: FYGE.Stage, movableManager: MovableManager) {
var bg = new BackgroundItem(); var bg = new BackgroundItem();
var bg2 = new BackgroundItem(); var bg2 = new BackgroundItem();
movableManager.add(bg); movableManager.add(bg);
movableManager.add(bg2); movableManager.add(bg2);
stage.addChild(bg); stage.addChild(bg);
stage.addChild(bg2); stage.addChild(bg2);
bg.x = 0; bg.x = 0;
bg2.y = height; bg2.y = height;
stage.addEventListener(FYGE.Event.ENTER_FRAME, () => { stage.addEventListener(FYGE.Event.ENTER_FRAME, () => {
console.log("bg.y", bg.y); // console.log("bg.y", bg.y);
console.log("bg2.y", bg2.y); // console.log("bg2.y", bg2.y);
if (bg.y > height) { if (bg.y > height) {
bg.y = bg2.y - height; bg.y = bg2.y - height;
} }
if (bg2.y > height) { if (bg2.y > height) {
bg2.y = bg.y - height; bg2.y = bg.y - height;
} }
}); });
} }
} }
import Movable from "../lib/Movable"; import Movable from "../lib/Movable";
import MovableManager from "../lib/MovableManager"; import MovableManager from "../lib/MovableManager";
import Vector2 from "../lib/Vector2"; import Vector2 from "../lib/Vector2";
import { IWeaponHost } from "../weapon/IWeaponHost"; import { IWeaponHost } from "../weapon/IWeaponHost";
import Weapon from "../weapon/Weapon"; import Weapon from "../weapon/Weapon";
export default class Enemy extends Movable implements IWeaponHost {
export default class Enemy extends Movable implements IWeaponHost { movableManager: MovableManager;
movableManager: MovableManager; constructor(movableManager: MovableManager) {
constructor(movableManager: MovableManager) { super();
super(); this.movableManager = movableManager;
this.movableManager = movableManager;
movableManager.add(this);
movableManager.add(this);
this.texture = FYGE.Texture.fromUrl(
this.texture = FYGE.Texture.fromUrl('//yun.duiba.com.cn/aurora/assets/26e1539bb9c5961c693f05186b086c8e04f2f6a2.png') "//yun.duiba.com.cn/aurora/assets/26e1539bb9c5961c693f05186b086c8e04f2f6a2.png"
);
const weapon = new Weapon(this);
const weapon = new Weapon(this);
// const bg = new FYGE.Graphics;
// bg.beginFill(0x00ff00, 0.2) // const bg = new FYGE.Graphics;
// bg.drawRect(0, 0, 69, 88) // bg.beginFill(0x00ff00, 0.2)
// bg.endFill() // bg.drawRect(0, 0, 69, 88)
// this.addChild(bg) // bg.endFill()
// this.addChild(bg)
this.addEventListener(FYGE.Event.REMOVED_FROM_STAGE, () => {
// console.log('敌人消失了') this.addEventListener(FYGE.Event.REMOVED_FROM_STAGE, () => {
weapon.destroy(); // console.log('敌人消失了')
}) weapon.destroy();
} });
}
getShootInterval() { return 100 }
getShootInterval() {
getShootVelocity() { return new Vector2(0, 10) } return 50;
}
getShootPoint() {
return new FYGE.Point(this.x + this.width / 2, this.y + this.height) getShootVelocity() {
} return new Vector2(0, 10);
} }
\ No newline at end of file
getShootAcceleration() {
return new Vector2(0, 0);
}
getShootPoint() {
return new FYGE.Point(this.x + this.width / 2, this.y + this.height);
}
}
import MovableManager from "../lib/MovableManager"; import MovableManager from "../lib/MovableManager";
import Vector2 from "../lib/Vector2"; import Vector2 from "../lib/Vector2";
import Enemy from "./Enemy"; import Enemy from "./Enemy";
export default class EnemyFactory { export default class EnemyFactory {
private _timer; private _timer;
private _stage: FYGE.Stage; private _stage: FYGE.Stage;
movableManager: MovableManager movableManager: MovableManager;
constructor(stage: FYGE.Stage, movableManager: MovableManager) { constructor(stage: FYGE.Stage, movableManager: MovableManager) {
this.movableManager = movableManager; this.movableManager = movableManager;
this._stage = stage; this._stage = stage;
this._timer = setInterval(this.onTimer, 1000) this._timer = setInterval(this.onTimer, 1000);
} }
onTimer = () => { onTimer = () => {
const enemy = this._stage.addChild(new Enemy(this.movableManager)); const enemy = this._stage.addChild(new Enemy(this.movableManager));
enemy.position.set(Math.random() * 300 + 200, 0); 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
destroy() { );
clearInterval(this._timer); enemy.acceleration = new Vector2(0, 0.1);
} };
}
\ No newline at end of file destroy() {
clearInterval(this._timer);
}
}
import DragDropable from "../dragdrop/DragDropable"; import DragDropable from "../dragdrop/DragDropable";
import MovableManager from "../lib/MovableManager"; import MovableManager from "../lib/MovableManager";
import Vector2 from "../lib/Vector2"; import Vector2 from "../lib/Vector2";
import { IWeaponHost } from "../weapon/IWeaponHost"; import { IWeaponHost } from "../weapon/IWeaponHost";
import Weapon from "../weapon/Weapon"; import Weapon from "../weapon/Weapon";
export default class Hero extends DragDropable implements IWeaponHost {
export default class Hero extends DragDropable implements IWeaponHost { movableManager: MovableManager;
movableManager: MovableManager; constructor(movableManager) {
super();
constructor(movableManager) { this.movableManager = movableManager;
super(); this.texture = FYGE.Texture.fromUrl(
this.movableManager = movableManager; "//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); const weapon = new Weapon(this);
// const bg = new FYGE.Graphics; // const bg = new FYGE.Graphics;
// bg.beginFill(0xff0000,0.2) // bg.beginFill(0xff0000,0.2)
// bg.drawRect(0,0,99,124) // bg.drawRect(0,0,99,124)
// bg.endFill() // bg.endFill()
// this.addChild(bg) // this.addChild(bg)
} }
getShootInterval(){return 50} getShootInterval() {
getShootVelocity(){return new Vector2(0, -20)} return 50;
}
getShootPoint() { getShootVelocity() {
return new FYGE.Point(this.x + this.width / 2, this.y) return new Vector2(0, -20);
} }
getDropPoint() { return {} } getShootPoint() {
return new FYGE.Point(this.x + this.width / 2, this.y);
onDragEnd() { } }
}
\ No newline at end of file getShootAcceleration() {
return new Vector2(0, 0);
}
getDropPoint() {
return {};
}
onDragEnd() {}
}
import DragDropManager from "../dragdrop/DragDropManager"; import DragDropManager from "../dragdrop/DragDropManager";
import MovableManager from "../lib/MovableManager"; import MovableManager from "../lib/MovableManager";
import Background from "./Background"; import Background from "./Background";
import EnemyFactory from "./EnemyFactory"; import EnemyFactory from "./EnemyFactory";
import Hero from "./Hero"; import Hero from "./Hero";
export function addGame(stage: FYGE.Stage) { export function addGame(stage: FYGE.Stage) {
const movableManager = new MovableManager(stage); //创建管理器 const movableManager = new MovableManager(stage); //创建管理器
const dragDropManager = new DragDropManager(); const dragDropManager = new DragDropManager();
const background = new Background(stage, movableManager); const background = new Background(stage, movableManager);
const hero = stage.addChild(new Hero(movableManager)); const hero = stage.addChild(new Hero(movableManager));
hero.position.set(300, 1000); // const hero2 = stage.addChild(new Hero(movableManager));
// const hero3 = stage.addChild(new Hero(movableManager));
const enemyFactory = new EnemyFactory(stage, movableManager); hero.position.set(300, 1000);
// hero2.position.set(250, 1000);
dragDropManager.add(hero); // hero3.position.set(350, 1000);
movableManager.hero = hero; const enemyFactory = new EnemyFactory(stage, movableManager);
function onGamOver() { dragDropManager.add(hero);
alert("gameOver");
dragDropManager.remove(hero); movableManager.hero = hero;
enemyFactory.destroy();
} function onGamOver() {
movableManager.addEventListener("gameover", onGamOver); alert("gameOver");
} console.log("Score", movableManager.Score);
dragDropManager.remove(hero);
enemyFactory.destroy();
}
movableManager.addEventListener("gameover", onGamOver);
}
import Movable from "../lib/Movable"; import Movable from "../lib/Movable";
export default class Bullet extends Movable { export default class Bullet extends Movable {
host; host;
constructor() { constructor() {
super(); super();
this.texture = FYGE.Texture.fromUrl('//yun.duiba.com.cn/spark/assets/bullet1.63bb85f32cabe5f986366c2d428c5e2aa2435230.png') this.texture = FYGE.Texture.fromUrl('//yun.duiba.com.cn/spark/assets/bullet1.63bb85f32cabe5f986366c2d428c5e2aa2435230.png')
} }
} }
\ No newline at end of file
import MovableManager from "../lib/MovableManager"; import MovableManager from "../lib/MovableManager";
import Vector2 from "../lib/Vector2"; import Vector2 from "../lib/Vector2";
export interface IWeaponHost extends FYGE.Sprite { export interface IWeaponHost extends FYGE.Sprite {
movableManager: MovableManager; movableManager: MovableManager;
getShootPoint(): FYGE.Point; getShootPoint(): FYGE.Point;
getShootVelocity(): Vector2; getShootVelocity(): Vector2;
getShootInterval(); getShootAcceleration(): Vector2;
} getShootInterval();
\ No newline at end of file }
import Movable from "../lib/Movable"; import Movable from "../lib/Movable";
import Bullet from "./Bullet"; import Bullet from "./Bullet";
import { IWeaponHost } from "./IWeaponHost"; import { IWeaponHost } from "./IWeaponHost";
export default class Weapon { export default class Weapon {
onCreateBullet: Function onCreateBullet: Function;
_timer; _timer;
_count; _count;
_host: IWeaponHost; _host: IWeaponHost;
constructor(host) { constructor(host) {
this._host = host; this._host = host;
this._timer = setInterval(this.onTimer, 10); this._timer = setInterval(this.onTimer, 10);
this.reset(); this.reset();
} }
reset() { reset() {
this._count = 0; this._count = 0;
} }
onTimer = () => { onTimer = () => {
this._count++; this._count++;
if (this.canCreateBullet()) { if (this.canCreateBullet()) {
this.createBullet(); this.createBullet();
this.reset(); this.reset();
} }
} };
canCreateBullet() { canCreateBullet() {
return this._count >= this._host.getShootInterval(); return this._count >= this._host.getShootInterval();
} }
createBullet = () => { createBullet = () => {
// console.log('武器在发射子弹') // console.log('武器在发射子弹')
const bullet: Bullet = this._host.parent.addChild(new Bullet()); const bullet: Bullet = this._host.parent.addChild(new Bullet());
bullet.host = this._host; bullet.host = this._host;
bullet.velocity = this._host.getShootVelocity(); bullet.velocity = this._host.getShootVelocity();
const shootPoint = this._host.getShootPoint(); bullet.acceleration = this._host.getShootAcceleration();
bullet.position.set(shootPoint.x, shootPoint.y); const shootPoint = this._host.getShootPoint();
this._host.movableManager.add(bullet); bullet.position.set(shootPoint.x, shootPoint.y);
} this._host.movableManager.add(bullet);
};
destroy() { destroy() {
clearInterval(this._timer); clearInterval(this._timer);
} }
} }
\ No newline at end of file
{ {
"compilerOptions": { "compilerOptions": {
"outDir": "./dist/", "outDir": "./dist/",
"noImplicitAny": false, "noImplicitAny": false,
"module": "es6", "module": "es6",
"target": "es5", "target": "es5",
"jsx": "react", "jsx": "react",
"allowJs": true "allowJs": true
} }
} }
\ No newline at end of file
const path = require('path'); const path = require('path');
module.exports = { module.exports = {
mode: 'development', mode: 'development',
devtool: 'eval', devtool: 'eval',
entry: './src/main.ts', entry: './src/main.ts',
module: { module: {
rules: [ rules: [
{ {
test: /\.tsx?$/, test: /\.tsx?$/,
use: 'ts-loader', use: 'ts-loader',
exclude: /node_modules/, exclude: /node_modules/,
}, },
], ],
}, },
resolve: { resolve: {
extensions: ['.tsx', '.ts', '.js'], extensions: ['.tsx', '.ts', '.js'],
}, },
output: { output: {
filename: 'bundle.js', filename: 'bundle.js',
}, },
}; };
This diff is collapsed.
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