Commit 9072b9dc authored by wildfirecode's avatar wildfirecode

1

parent 52ef2046
No preview for this file type
...@@ -3,8 +3,14 @@ import { FACTOR } from "./consts"; ...@@ -3,8 +3,14 @@ import { FACTOR } from "./consts";
export default class Ball extends p2.Body { export default class Ball extends p2.Body {
_skin: egret.DisplayObject; _skin: egret.DisplayObject;
setImpulse: boolean;
constructor() {
super({ mass: 10 });
this.type = p2.Body.DYNAMIC;
this.init();
}
init() { private init() {
this._skin = addImage('ball_png'); this._skin = addImage('ball_png');
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;
...@@ -13,7 +19,7 @@ export default class Ball extends p2.Body { ...@@ -13,7 +19,7 @@ export default class Ball extends p2.Body {
const shape = new p2.Circle({ radius: radius }); const shape = new p2.Circle({ radius: radius });
this.addShape(shape); this.addShape(shape);
this.mass = 1; shape.collisionGroup = 2;//010与001为0,010与110为1
} }
updateSkin() { updateSkin() {
......
import { addImage } from "./utils";
import { FACTOR } from "./consts";
/**
* 被打的砖块
*/
export default class Brick extends p2.Body {
_skin: egret.DisplayObject;
constructor() {
super();
this.type = p2.Body.KINEMATIC;
this.init();
}
init() {
this._skin = addImage('rect1_png');
this._skin.anchorOffsetX = this._skin.width / 2;
this._skin.anchorOffsetY = this._skin.height / 2;
const width = this._skin.width / FACTOR;
const height = this._skin.height / FACTOR;
const shape = new p2.Box({ width: width, height: height });
this.addShape(shape);
shape.collisionMask = 6;//010与001为0,010与110为1
}
updateSkin() {
this._skin.x = this.position[0] * FACTOR;
this._skin.y = this.position[1] * FACTOR;
this._skin.rotation = this.angle * 180 / Math.PI;
}
get skin() { return this._skin }
}
\ No newline at end of file
import { addImage } from "./utils"; import { addImage } from "./utils";
import Ball from "./Ball"; import Ball from "./Ball";
import World from "./World"; import World from "./World";
import { FACTOR } from "./consts";
import Brick from "./Brick";
export default class Gun extends egret.Sprite { export default class Gun extends egret.Sprite {
_root: egret.DisplayObjectContainer; _root: egret.DisplayObjectContainer;
_world: World; _world: World;
_line: egret.Bitmap; _line: egret.Bitmap;
_gun: egret.Bitmap; _gun: egret.Bitmap;
vec: egret.Point
constructor(root: egret.DisplayObjectContainer, world: World) { constructor(root: egret.DisplayObjectContainer, world: World) {
super(); super();
this._root = root; this._root = root;
...@@ -14,18 +17,28 @@ export default class Gun extends egret.Sprite { ...@@ -14,18 +17,28 @@ export default class Gun extends egret.Sprite {
this.initUI(); this.initUI();
} }
fire() { fire(vec: egret.Point) {
egret.Tween.get(this._gun) egret.Tween.get(this._gun)
.to({ scaleY: 0.6 }, 100) .to({ scaleY: 0.6 }, 100)
.to({ scaleY: 1 }, 100); .to({ scaleY: 1 }, 100);
this.addBall(); const ball = this.addBall();
ball.mass = 0;
this._world.bodies.forEach(body => {
if (body instanceof Brick) {
this._world.setMaterial(ball, body);
}
})
ball.applyImpulse([vec.x, vec.y], [0, 0]);
} }
addBall() { addBall() {
const ball = new Ball(); const ball = new Ball();
ball.init(); ball.position = [this.x / FACTOR, this.y / FACTOR];
ball.skin.x = this.x;
ball.skin.y = this.y;
this._world.addBody(ball); this._world.addBody(ball);
this._root.addChild(ball.skin); this._world.skin.addChild(ball.skin);
return ball;
} }
enable() { enable() {
...@@ -39,10 +52,19 @@ export default class Gun extends egret.Sprite { ...@@ -39,10 +52,19 @@ export default class Gun extends egret.Sprite {
this.stage.once(egret.TouchEvent.TOUCH_END, this.onTouchEnd, this); this.stage.once(egret.TouchEvent.TOUCH_END, this.onTouchEnd, this);
} }
onTouchEnd(): any { onTouchEnd(e: egret.TouchEvent): any {
this.hideLine(); this.hideLine();
this.stage.removeEventListener(egret.TouchEvent.TOUCH_MOVE, this.onTouchMove, this); this.stage.removeEventListener(egret.TouchEvent.TOUCH_MOVE, this.onTouchMove, this);
// this.fire();
const point = new egret.Point(e.stageX, e.stageY)
var pos = new egret.Point(point.x - this.x, point.y - this.y);
var angle: number = Math.atan2(pos.x, pos.y)
var dis: number = 10000;
var x: number = Math.ceil(Math.sin(angle) * dis);
var y: number = Math.ceil(Math.cos(angle) * dis);
const vec = new egret.Point(x, y);
this.vec = vec;
this.fire(vec);
} }
onTouchMove(e: egret.TouchEvent) { onTouchMove(e: egret.TouchEvent) {
......
import { getResPath } from "./utils"; import { getResPath } from "./utils";
import Gun from "./Gun"; import Gun from "./Gun";
import World from "./World"; import World from "./World";
import Brick from "./Brick";
import { FACTOR } from "./consts";
export class Main extends eui.UILayer { export class Main extends eui.UILayer {
private _gun: Gun; private _gun: Gun;
private _world: World; private _world: World;
protected createGameScene(): void { protected createGameScene(): void {
this._world = new World(this); this._world = new World();
this._gun = new Gun(this, this._world); this._gun = new Gun(this, this._world);
this._world.gun = this._gun;
this._world.skin = new egret.Sprite();
this.addChild(this._world.skin);
this.addChild(this._gun); this.addChild(this._gun);
this._gun.x = this.stage.stageWidth >> 1; this._gun.x = this.stage.stageWidth >> 1;
this._gun.y = 100; this._gun.y = 100;
this._gun.enable(); this._gun.enable();
setInterval(() => {
this._gun.fire(); this.updateBrickBody();
}, 500);
//添加游戏帧频事件 //添加游戏帧频事件
this.addEventListener(egret.Event.ENTER_FRAME, this.loop, this); this.addEventListener(egret.Event.ENTER_FRAME, this.loop, this);
} }
updateBrickBody(): void {
for (var i = 0; i < 1; i++) {
var brick = new Brick();
var x = this.stage.stageWidth >> 1;
var y = 1000;
brick.position = [x / FACTOR, y / FACTOR];
brick.updateSkin();
brick.angle = (-Math.random() * Math.PI / 4) + (Math.random() * Math.PI / 4);
this._world.skin.addChild(brick.skin);
this._world.addBody(brick);
}
}
protected createChildren(): void { protected createChildren(): void {
super.createChildren(); super.createChildren();
this.runGame().catch(e => { this.runGame().catch(e => {
...@@ -29,7 +48,7 @@ export class Main extends eui.UILayer { ...@@ -29,7 +48,7 @@ export class Main extends eui.UILayer {
} }
loop() { loop() {
this._world.step(1); this._world.step(1 / 60);
this._world.loop(); this._world.loop();
} }
......
import Ball from "./Ball"; import Ball from "./Ball";
import Gun from "./Gun";
export default class World extends p2.World { export default class World extends p2.World {
_root: egret.DisplayObjectContainer; material: p2.Material;//碰撞时的弹性变化
constructor(root: egret.DisplayObjectContainer, options?: any) { _skin: egret.DisplayObjectContainer;
gun: Gun;
constructor(options?: any) {
super(options); super(options);
this._root = root;
this.init(); this.init();
} }
protected onHitBegin(evt): void {
var ball: Ball;
if (evt.bodyA instanceof Ball) ball = evt.bodyA;
if (evt.bodyB instanceof Ball) ball = evt.bodyB;
if (ball && ball.mass == 0) {
ball.setImpulse = true;//可以设置给小球冲量
ball.mass = 200;
}
if (ball && ball.setImpulse) {
const vec = this.gun.vec;
ball.applyImpulse([vec.x * 2, vec.y], [0, 0]);
ball.setImpulse = false;
}
}
/**
* 设置刚体碰撞的弹性
* */
setMaterial(body1: p2.Body, body2: p2.Body): void {
body1.shapes[0].material = this.material;
body2.shapes[0].material = this.material;
var roleAndStoneMaterial = new p2.ContactMaterial(
this.material, this.material,
{ restitution: 0.7, friction: 0 });//弹性,摩擦力
this.addContactMaterial(roleAndStoneMaterial);
}
loop(): any { loop(): any {
this.bodies.forEach(body => { this.bodies.forEach(body => {
if (body instanceof Ball) { if (body instanceof Ball) {
...@@ -17,6 +47,11 @@ export default class World extends p2.World { ...@@ -17,6 +47,11 @@ export default class World extends p2.World {
} }
init() { init() {
this.gravity = [0, 10]; this.gravity = [0, 200];
this.material = new p2.Material(0);
this.on("beginContact", this.onHitBegin.bind(this));
} }
set skin(val: egret.DisplayObjectContainer) { this._skin = val }
get skin() { return this._skin }
} }
\ No newline at end of file
export const FACTOR = 30; export const FACTOR = 1;
\ No newline at end of file // export const FACTOR = 30;
\ 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