Commit ec830ad7 authored by wildfirecode13's avatar wildfirecode13

1

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