Commit ec830ad7 authored by wildfirecode13's avatar wildfirecode13

1

parent cc8ebb4c
import { Sprite, TextureCache } from 'spark-wrapper-fyge'; import { Sprite, TextureCache } from 'spark-wrapper-fyge';
export default class Ground extends Sprite { export default class StaticBody extends Sprite {
constructor(props) { constructor(props) {
super(); super();
this.data = props.data; this.data = props.data;
......
import { WidgetBase, Event, Shape } from 'spark-wrapper-fyge'; import { WidgetBase, Event, Shape } from 'spark-wrapper-fyge';
import metaConfig from '../meta.json'; import metaConfig from '../meta.json';
import Body from './Body'; import Body from './Body';
import Ground from './Ground'; import StaticBody from './StaticBody';
const createGroundDisplay = (ground) => { const createStaticBodyDisplay = (staticBody) => {
const shape = new Shape(); const shape = new Shape();
shape.beginFill(0, 1); shape.beginFill(0, 1);
shape.drawRect(0, 0, ground.width, ground.height); shape.drawRect(0, 0, staticBody.width, staticBody.height);
shape.endFill(); shape.endFill();
return shape; return shape;
} }
...@@ -20,32 +20,31 @@ class GameStage extends WidgetBase { ...@@ -20,32 +20,31 @@ class GameStage extends WidgetBase {
this this
); );
const groundData = { x: 0, y: this.stage.viewRect.height / 2, width: 100, height: 10 }; const staticBodyData = { x: 0, y: this.stage.viewRect.height / 2, width: 100, height: 10 };
const ground = new Ground({ data: groundData, display: createGroundDisplay(groundData) }) const staticBody = new StaticBody({ data: staticBodyData, display: createStaticBodyDisplay(staticBodyData) })
this.addGround(ground); this.addStaticBoy(staticBody);
const groundData1 = { x: 100, y: this.stage.viewRect.height / 3 * 2, width: 100, height: 10 }; const staticBodyData1 = { x: 100, y: this.stage.viewRect.height / 3 * 2, width: 100, height: 10 };
const ground1 = new Ground({ data: groundData1, display: createGroundDisplay(groundData1) }) const staticBody1 = new StaticBody({ data: staticBodyData1, display: createStaticBodyDisplay(staticBodyData1) })
this.addGround(ground1); this.addStaticBoy(staticBody1);
const groundData2 = { x: 200, y: this.stage.viewRect.height / 3 * 2 * 1.2, width: 200, height: 10 }; const staticBodyData2 = { x: 200, y: this.stage.viewRect.height / 3 * 2 * 1.2, width: 200, height: 10 };
const ground2 = new Ground({ data: groundData2, display: createGroundDisplay(groundData2) }) const staticBody2 = new StaticBody({ data: staticBodyData2, display: createStaticBodyDisplay(staticBodyData2) })
this.addGround(ground2); this.addStaticBoy(staticBody2);
this.addbodys(); this.addbodys();
} }
grounds; addStaticBoy(staticBody) {
addGround(ground) { this.staticBodys = this.staticBodys || [];
this.grounds = this.grounds || []; this.staticBodys.push(staticBody);
this.grounds.push(ground); this.addChild(staticBody)
this.addChild(ground)
} }
removeGround(ground) { removeStaticBody(staticBody) {
const index = this.grounds.indexOf(ground); const index = this.staticBodys.indexOf(staticBody);
if (index != -1) { if (index != -1) {
this.removeChild(ground); this.removeChild(staticBody);
this.grounds.splice(index, 1); this.staticBodys.splice(index, 1);
} }
} }
addbodys() { addbodys() {
...@@ -65,19 +64,19 @@ class GameStage extends WidgetBase { ...@@ -65,19 +64,19 @@ class GameStage extends WidgetBase {
onEnterFrame() { onEnterFrame() {
this.bodys.forEach(i => { this.bodys.forEach(i => {
i.onEnterFrame(); i.onEnterFrame();
this.checkCollideGround(i); this.checkCollideStaticBody(i);
}); });
} }
checkCollideGround(body) { checkCollideStaticBody(body) {
const y0 = body.y + body.bodyData.height; const y0 = body.y + body.bodyData.height;
//默认从上往下走,碰到第一个就停止 //默认从上往下走,碰到第一个就停止
for (let i = 0; i < this.grounds.length; i++) { for (let i = 0; i < this.staticBodys.length; i++) {
const ground = this.grounds[i]; const staticBody = this.staticBodys[i];
const dis0 = body.bodyData.width / 2 + ground.data.width / 2; const dis0 = body.bodyData.width / 2 + staticBody.data.width / 2;
const dis1 = Math.abs(body.x + body.bodyData.width / 2 - ground.data.x - ground.data.width / 2); const dis1 = Math.abs(body.x + body.bodyData.width / 2 - staticBody.data.x - staticBody.data.width / 2);
if (y0 > ground.y && dis1 < dis0) { if (y0 > staticBody.y && dis1 < dis0) {
body.y = ground.y - body.bodyData.height; body.y = staticBody.y - body.bodyData.height;
body.velocityY = -body.velocityY * body.restitution; body.velocityY = -body.velocityY * body.restitution;
break; break;
} }
......
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