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

初始化bird

parent 986c1c4e
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
import Movable from "./Movable";
export default class Bullet extends Movable {
constructor() {
super();
this.texture = FYGE.Texture.fromUrl('//yun.duiba.com.cn/aurora/assets/0bcb2c26a85addb1714b4c63f2a873aafe210749.png')
}
}
\ No newline at end of file
import Bird from "./Bird";
import MovableManager from "./MovableManager";
import Vector2 from "./Vector2";
export function addGame(stage: FYGE.Stage) {
const movableManager = new MovableManager(stage);//创建管理器
const bird = stage.addChild(new Bird());//有加速度
bird.position.set(300, 200);
bird.velocity = new Vector2(0, 0);
bird.acceleration=new Vector2(0, 1);
movableManager.add(bird);
stage.addEventListener(FYGE.MouseEvent.CLICK,onclick);
function onclick() {
bird.velocity.y = -20;
}
}
\ No newline at end of file
import Movable from "../lib/Movable";
import MovableManager from "../lib/MovableManager";
import Vector2 from "../lib/Vector2";
class BackgroundItem extends Movable {
constructor() {
super();
this.texture = FYGE.Texture.fromUrl(
"//yun.duiba.com.cn/aurora/assets/bd7b3b10169265123e52d02acf8739db5ff59b3d.png"
);
this.velocity = new Vector2(1, 0);
}
getCanRemove() {
return false;
}
}
const width = 375;
export default class Background {
constructor(stage: FYGE.Stage, movableManager: MovableManager) {
var bg = new BackgroundItem();
var bg2 = new BackgroundItem();
movableManager.add(bg);
movableManager.add(bg2);
stage.addChild(bg);
stage.addChild(bg2);
bg.y = 0;
bg2.x = width;
stage.addEventListener(FYGE.Event.ENTER_FRAME, () => {
console.log("bg.x", bg.x);
console.log("bg2.x", bg2.x);
if (bg.x > width) {
bg.x = bg2.x - width;
}
if (bg2.x > width) {
bg2.x = bg.x - width;
}
});
}
}
import Movable from "../lib/Movable";
export default class Bullet extends Movable {
constructor() {
super();
this.texture = FYGE.Texture.fromUrl(
"//yun.duiba.com.cn/aurora/assets/0bcb2c26a85addb1714b4c63f2a873aafe210749.png"
);
}
}
import Bird from "./Bird";
import MovableManager from "../lib/MovableManager";
import Vector2 from "../lib/Vector2";
import Background from "./Background";
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);
bird.velocity = new Vector2(0, 0);
bird.acceleration = new Vector2(0, 1);
movableManager.add(bird);
stage.addEventListener(FYGE.MouseEvent.CLICK, onclick);
function onclick() {
bird.velocity.y = -20;
}
}
import Vector2 from "./Vector2"; import Vector2 from "./Vector2";
export default class Movable extends FYGE.Sprite { export default class Movable extends FYGE.Sprite {
private _velocity: Vector2; private _velocity: Vector2;
set velocity(val: Vector2) { this._velocity = val } set velocity(val: Vector2) { this._velocity = val }
get velocity() { return this._velocity } get velocity() { return this._velocity }
private _acceleration: Vector2; private _acceleration: Vector2;
set acceleration(val: Vector2) { this._acceleration = val } set acceleration(val: Vector2) { this._acceleration = val }
step() { step() {
this._acceleration = this._acceleration || new Vector2(0, 0); this._acceleration = this._acceleration || new Vector2(0, 0);
this._velocity.x += this._acceleration.x; this._velocity.x += this._acceleration.x;
this._velocity.y += this._acceleration.y; this._velocity.y += this._acceleration.y;
this.y += this._velocity.y; this.y += this._velocity.y;
this.x += this._velocity.x; this.x += this._velocity.x;
} }
} }
\ No newline at end of file
import Movable from "./Movable"; import Movable from "./Movable";
export default class MovableManager { export default class MovableManager {
private _stage: FYGE.Stage; private _stage: FYGE.Stage;
private _movableList: Movable[] = []; private _movableList: Movable[] = [];
constructor(stage: FYGE.Stage) { constructor(stage: FYGE.Stage) {
this._stage = stage; this._stage = stage;
this._stage.addEventListener(FYGE.Event.ENTER_FRAME, this.onEnterFrame); this._stage.addEventListener(FYGE.Event.ENTER_FRAME, this.onEnterFrame);
} }
onEnterFrame = () => { onEnterFrame = () => {
this.step(); this.step();
this.checkRemove(); this.checkRemove();
console.log('移动对象的数量:',this._movableList.length) console.log('移动对象的数量:',this._movableList.length)
} }
private calcCanRemove(item: FYGE.DisplayObject) { private calcCanRemove(item: FYGE.DisplayObject) {
if (item.y > 1624) return true; if (item.y > 1624) return true;
if (item.y < -item.height) return true; if (item.y < -item.height) return true;
if (item.x > 750) return true; if (item.x > 750) return true;
if (item.x < -item.width) return true; if (item.x < -item.width) return true;
return false; return false;
} }
private checkRemove() { private checkRemove() {
for (let index = 0; index < this._movableList.length; index++) { for (let index = 0; index < this._movableList.length; index++) {
const item = this._movableList[index]; const item = this._movableList[index];
if (this.calcCanRemove(item)) { if (this.calcCanRemove(item)) {
this._movableList.splice(index, 1); this._movableList.splice(index, 1);
index--; index--;
} }
} }
} }
private step() { private step() {
this._movableList.forEach(item => item.step()); this._movableList.forEach(item => item.step());
} }
add(item: Movable) { add(item: Movable) {
this._movableList.push(item); this._movableList.push(item);
} }
remove(item: Movable) { remove(item: Movable) {
const index = this._movableList.indexOf(item); const index = this._movableList.indexOf(item);
this._movableList.splice(index, 1); this._movableList.splice(index, 1);
} }
} }
\ No newline at end of file
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 "./addGame"; import { addGame } from "./bird/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
{ {
"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