Commit 384b73d0 authored by wildfirecode's avatar wildfirecode

1

parent 335b1357
module.exports = { module.exports = {
opn: 0,
devPort: 8080, devPort: 8080,
proxy: { proxy: {
'/ngapi/*': 'http://localhost:3000', '/ngapi/*': 'http://localhost:3000',
......
...@@ -27,7 +27,7 @@ ...@@ -27,7 +27,7 @@
<div data-entry-class="Main" <div data-entry-class="Main"
style="margin: auto;width: 100%;height: 100%;" class="egret-player" style="margin: auto;width: 100%;height: 100%;" class="egret-player"
data-orientation="auto" data-scale-mode="showAll" data-frame-rate="60" data-content-width="750" data-orientation="auto" data-scale-mode="showAll" data-frame-rate="60" data-content-width="750"
data-content-height="1624" data-multi-fingered="2" data-show-fps="false" data-show-log="false" data-content-height="1624" data-multi-fingered="2" data-show-fps="true" data-show-log="false"
data-show-fps-style="x:0,y:0,size:12,textColor:0xffffff,bgAlpha:0.9"> data-show-fps-style="x:0,y:0,size:12,textColor:0xffffff,bgAlpha:0.9">
</div> </div>
......
...@@ -5,15 +5,45 @@ import { FACTOR } from "./consts"; ...@@ -5,15 +5,45 @@ import { FACTOR } from "./consts";
* 被打的砖块 * 被打的砖块
*/ */
export default class Brick extends p2.Body { export default class Brick extends p2.Body {
_skin: egret.DisplayObject; _skin: egret.DisplayObjectContainer;
constructor() { _initNums: number;
_numText: egret.TextField;
constructor(initNums) {
super(); super();
this._initNums = initNums;
this.type = p2.Body.KINEMATIC; this.type = p2.Body.KINEMATIC;
this.init(); this.init();
} }
updateText() {
this._numText.text = this._initNums + '';
this._numText.y = this._skin.height / 2 - this._numText.textHeight / 2;
}
collideWithBall() {
if (this._initNums > 0)
this._initNums--;
if (this._initNums <= 0) {
this._skin.dispatchEvent(new egret.Event('destroy', false, false, this));
}
this.updateText();
}
onceDestroy(listener: Function, thisobj: any) {
this._skin.once('destroy', listener, thisobj);
}
init() { init() {
this._skin = addImage('rect1_png'); this._skin = new egret.Sprite();
this._numText = new egret.TextField();
this._numText.textColor = 0xffffff;
this._numText.size = 40;
const bg = addImage('rect1_png');
this._skin.addChild(bg);
this._skin.addChild(this._numText);
this._numText.width = bg.width;
this._numText.textAlign = egret.HorizontalAlign.CENTER;
this._skin.anchorOffsetX = this._skin.width / 2; this._skin.anchorOffsetX = this._skin.width / 2;
this._skin.anchorOffsetY = this._skin.height / 2; this._skin.anchorOffsetY = this._skin.height / 2;
...@@ -23,6 +53,8 @@ export default class Brick extends p2.Body { ...@@ -23,6 +53,8 @@ export default class Brick extends p2.Body {
this.addShape(shape); this.addShape(shape);
shape.collisionMask = 6;//010与001为0,010与110为1 shape.collisionMask = 6;//010与001为0,010与110为1
this.updateText();
} }
updateSkin() { updateSkin() {
......
...@@ -30,6 +30,7 @@ export default class Gun extends egret.Sprite { ...@@ -30,6 +30,7 @@ export default class Gun extends egret.Sprite {
} }
}) })
ball.applyImpulse([vec.x, vec.y], [0, 0]); ball.applyImpulse([vec.x, vec.y], [0, 0]);
this.dispatchEvent(new egret.Event('fire'));
} }
addBall() { addBall() {
...@@ -47,10 +48,11 @@ export default class Gun extends egret.Sprite { ...@@ -47,10 +48,11 @@ export default class Gun extends egret.Sprite {
this.stage.once(egret.TouchEvent.TOUCH_BEGIN, this.onTouchBegin, this); this.stage.once(egret.TouchEvent.TOUCH_BEGIN, this.onTouchBegin, this);
} }
onTouchBegin() { onTouchBegin(e: egret.TouchEvent) {
this.showLine(); this.showLine();
this.stage.addEventListener(egret.TouchEvent.TOUCH_MOVE, this.onTouchMove, this); this.stage.addEventListener(egret.TouchEvent.TOUCH_MOVE, this.onTouchMove, this);
this.stage.once(egret.TouchEvent.TOUCH_END, this.onTouchEnd, this); this.stage.once(egret.TouchEvent.TOUCH_END, this.onTouchEnd, this);
this.updateView(e.stageX, e.stageY);
} }
onTouchEnd(e: egret.TouchEvent): any { onTouchEnd(e: egret.TouchEvent): any {
...@@ -72,7 +74,11 @@ export default class Gun extends egret.Sprite { ...@@ -72,7 +74,11 @@ export default class Gun extends egret.Sprite {
} }
onTouchMove(e: egret.TouchEvent) { onTouchMove(e: egret.TouchEvent) {
const point = new egret.Point(e.stageX, e.stageY) this.updateView(e.stageX, e.stageY);
}
private updateView(stageX: number, stageY: number) {
const point = new egret.Point(stageX, stageY);
var pos = new egret.Point(point.x - this.x, point.y - this.y); var pos = new egret.Point(point.x - this.x, point.y - this.y);
var r: number = -Math.atan2(pos.x, pos.y) * 180 / Math.PI; var r: number = -Math.atan2(pos.x, pos.y) * 180 / Math.PI;
this._gun.rotation = r; this._gun.rotation = r;
......
...@@ -23,12 +23,27 @@ export class Main extends eui.UILayer { ...@@ -23,12 +23,27 @@ export class Main extends eui.UILayer {
this.createGround(); this.createGround();
this.updateBrickBody(); this.addBricks();
//添加游戏帧频事件 //添加游戏帧频事件
this.addEventListener(egret.Event.ENTER_FRAME, this.loop, this); this.addEventListener(egret.Event.ENTER_FRAME, this.loop, this);
this._gun.addEventListener('fire', this.onFire, this);
} }
ballCounter = 0;
onFire() {
this.ballCounter++;
if (this.ballCounter >= 2) {
this.ballCounter = 0;
this.currentY += 130;
this.addBricks();
this._world.bodies.forEach(body => {
if (body instanceof Brick) {
body.position[1] -= 50;
}
})
}
}
createGround() { createGround() {
const left = new Wall(true); const left = new Wall(true);
...@@ -45,21 +60,28 @@ export class Main extends eui.UILayer { ...@@ -45,21 +60,28 @@ export class Main extends eui.UILayer {
left.updateSkin(); left.updateSkin();
} }
updateBrickBody(): void { currentY: number = 700;
addBricks(): void {
const xlist = [150, 300, 450, 600]; const xlist = [150, 300, 450, 600];
for (var i = 0; i < xlist.length; i++) { for (var i = 0; i < xlist.length; i++) {
var brick = new Brick(); var brick = new Brick(Math.ceil(Math.random() * 1) + 1);
// var x = 100 this.stage.stageWidth - 100 ;
var x = xlist[i]; var x = xlist[i];
var y = Math.random() * 100 + 800; var y = Math.random() * 50 + this.currentY;
brick.position = [x / FACTOR, y / FACTOR]; brick.position = [x / FACTOR, y / FACTOR];
brick.updateSkin(); brick.updateSkin();
brick.angle = (-Math.random() * Math.PI / 4) + (Math.random() * Math.PI / 4); brick.angle = (-Math.random() * Math.PI / 4) + (Math.random() * Math.PI / 4);
this._world.skin.addChild(brick.skin); this._world.skin.addChild(brick.skin);
this._world.addBody(brick); this._world.addBody(brick);
brick.onceDestroy(this.onBrickDestroy, this)
} }
} }
onBrickDestroy(e: egret.Event) {
const brick = e.data as Brick;
this._world.removeBody(brick);
this._world.skin.removeChild(brick.skin);
}
protected createChildren(): void { protected createChildren(): void {
super.createChildren(); super.createChildren();
this.runGame().catch(e => { this.runGame().catch(e => {
......
import Ball from "./Ball"; import Ball from "./Ball";
import Gun from "./Gun"; import Gun from "./Gun";
import Brick from "./Brick";
export default class World extends p2.World { export default class World extends p2.World {
material: p2.Material;//碰撞时的弹性变化 material: p2.Material;//碰撞时的弹性变化
...@@ -12,8 +13,11 @@ export default class World extends p2.World { ...@@ -12,8 +13,11 @@ export default class World extends p2.World {
protected onHitBegin(evt): void { protected onHitBegin(evt): void {
var ball: Ball; var ball: Ball;
var body;
if (evt.bodyA instanceof Ball) ball = evt.bodyA; if (evt.bodyA instanceof Ball) ball = evt.bodyA;
else body = evt.bodyA;
if (evt.bodyB instanceof Ball) ball = evt.bodyB; if (evt.bodyB instanceof Ball) ball = evt.bodyB;
else body = evt.bodyB;
if (ball && ball.mass == 0) { if (ball && ball.mass == 0) {
ball.setImpulse = true;//可以设置给小球冲量 ball.setImpulse = true;//可以设置给小球冲量
ball.mass = 200; ball.mass = 200;
...@@ -24,6 +28,10 @@ export default class World extends p2.World { ...@@ -24,6 +28,10 @@ export default class World extends p2.World {
ball.applyImpulse([vec.x * 2, vec.y], [0, 0]); ball.applyImpulse([vec.x * 2, vec.y], [0, 0]);
ball.setImpulse = false; ball.setImpulse = false;
} }
if (ball) {
body.collideWithBall && body.collideWithBall(ball);
}
} }
/** /**
...@@ -40,7 +48,7 @@ export default class World extends p2.World { ...@@ -40,7 +48,7 @@ export default class World extends p2.World {
loop(): any { loop(): any {
this.bodies.forEach(body => { this.bodies.forEach(body => {
if (body instanceof Ball) { if (body instanceof Ball || body instanceof Brick) {
body.updateSkin(); body.updateSkin();
} }
}); });
......
...@@ -7,24 +7,14 @@ ...@@ -7,24 +7,14 @@
"dev": "node build.js dev", "dev": "node build.js dev",
"build": "node build.js build prod", "build": "node build.js build prod",
"buildTS": "node build.js build prod ts", "buildTS": "node build.js build prod ts",
"buildExml": "node build.js build prod exml", "buildExml": "node build.js build prod exml"
"backup": "node build.js backup",
"copy": "node build.js copy",
"cli": "node cli.js"
}, },
"author": "", "author": "",
"license": "ISC", "license": "ISC",
"keywords": [], "keywords": [],
"dependencies": {}, "dependencies": {},
"devDependencies": { "devDependencies": {
"ali-oss": "^4.11.4", "duiba-game-build": "git+ssh://git@gitlab2.dui88.com:wanghongyuan/dbgame-build.git"
"chalk": "^2.3.0",
"co": "^4.6.0",
"cross-spawn": "^6.0.5",
"duiba-game-build": "git+ssh://git@gitlab2.dui88.com:wanghongyuan/dbgame-build.git",
"inquirer": "^6.2.0",
"moment": "^2.22.2",
"progress": "^2.0.0"
}, },
"sideEffects": false "sideEffects": false
} }
\ No newline at end of file
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