Commit 5eca0d6b authored by wildfirecode's avatar wildfirecode

1

parent 6ebb4c81
...@@ -24,7 +24,7 @@ ...@@ -24,7 +24,7 @@
</head> </head>
<body> <body>
<div data-entry-class="TestAsset" <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="false" data-show-log="false"
......
No preview for this file type
import { addImage } from "./utils";
export default class Ball extends p2.Body {
_skin: egret.DisplayObject;
constructor(options?: any) {
super(options);
this.init();
}
private init() {
this._skin = addImage('ball_png');
this._skin.anchorOffsetX = this._skin.width / 2;
this._skin.anchorOffsetY = this._skin.height / 2;
const radius = this._skin.width / 2;
const shape = new p2.Circle({ radius: radius });
this.addShape(shape);
this.mass = 1;
}
updateSkin() {
this._skin.x = this.position[0];
this._skin.y = this.position[1];
this._skin.rotation = this.angle * 180 / Math.PI;
}
get skin() { return this._skin }
}
\ No newline at end of file
import { addImage } from "./utils";
import Ball from "./Ball";
import World from "./World";
export default class Gun extends egret.Sprite {
_root: egret.DisplayObjectContainer;
_world: World;
_line: egret.Bitmap;
_gun: egret.Bitmap;
constructor(root: egret.DisplayObjectContainer, world: World) {
super();
this._root = root;
this._world = world;
this.initUI();
}
fire() {
egret.Tween.get(this._gun)
.to({ scaleY: 0.6 }, 100)
.to({ scaleY: 1 }, 100);
this.addBall();
}
addBall() {
const ball = new Ball();
this._world.addBody(ball);
this._root.addChild(ball.skin);
}
enable() {
this.hideLine();
this.stage.once(egret.TouchEvent.TOUCH_BEGIN, this.onTouchBegin, this);
}
onTouchBegin() {
this.showLine();
this.stage.addEventListener(egret.TouchEvent.TOUCH_MOVE, this.onTouchMove, this);
this.stage.once(egret.TouchEvent.TOUCH_END, this.onTouchEnd, this);
}
onTouchEnd(): any {
this.hideLine();
this.stage.removeEventListener(egret.TouchEvent.TOUCH_MOVE, this.onTouchMove, this);
// this.fire();
}
onTouchMove(e: egret.TouchEvent) {
const point = new egret.Point(e.stageX, e.stageY)
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;
this._gun.rotation = r;
this._line.rotation = r;
}
private initUI() {
const line = addImage('line_png', this);
line.anchorOffsetX = line.width / 2;
const gun = addImage('gun_png', this);
gun.anchorOffsetX = gun.width / 2;
const numBg = addImage('numBg_png', this);
numBg.anchorOffsetX = numBg.width >> 1;
numBg.anchorOffsetY = numBg.height >> 1;
this._line = line;
this._gun = gun;
}
showLine() {
this.addChildAt(this._line, 0);
}
hideLine() {
this.removeChild(this._line)
}
}
\ No newline at end of file
import { SimpleP2APP } from "./section1/SimpleP2APP"; import { getResPath } from "./utils";
import { SimpleP2APPWithDebugDraw } from "./section1/SimpleP2APPWithDebugDraw"; import Gun from "./Gun";
import TestAsset from "./section2/TestAsset"; import World from "./World";
SimpleP2APP; export class Main extends eui.UILayer {
SimpleP2APPWithDebugDraw; private _gun: Gun;
TestAsset private _world: World;
\ No newline at end of file protected createGameScene(): void {
this._world = new World(this);
this._gun = new Gun(this, this._world);
this.addChild(this._gun);
this._gun.x = this.stage.stageWidth >> 1;
this._gun.y = 100;
this._gun.enable();
setInterval(() => {
this._gun.fire();
}, 500);
//添加游戏帧频事件
this.addEventListener(egret.Event.ENTER_FRAME, this.loop, this);
}
protected createChildren(): void {
super.createChildren();
this.runGame().catch(e => {
console.log(e);
})
}
loop() {
this._world.step(1 / 60);
this._world.loop();
}
private async runGame() {
await this.loadResource()
this.createGameScene();
}
private async loadResource() {
try {
await RES.loadConfig("default.res.json", getResPath() + "resource/");
await RES.loadGroup("preload", 0);
}
catch (e) {
console.error(e);
}
}
}
window['Main'] = Main;
\ No newline at end of file
import { getResPath } from "./utils";
export class StarterBase extends eui.UILayer {
public factor: number = 30;
protected createGameScene(): void {
//添加游戏帧频事件
this.addEventListener(egret.Event.ENTER_FRAME, this.loop, this);
}
protected createChildren(): void {
super.createChildren();
this.runGame().catch(e => {
console.log(e);
})
}
loop() {
}
private async runGame() {
await this.loadResource()
this.createGameScene();
}
private async loadResource() {
try {
await RES.loadConfig("default.res.json", getResPath() + "resource/");
await RES.loadGroup("preload", 0);
}
catch (e) {
console.error(e);
}
}
}
\ No newline at end of file
import Ball from "./Ball";
export default class World extends p2.World {
_root: egret.DisplayObjectContainer;
constructor(root: egret.DisplayObjectContainer, options?: any) {
super(options);
this._root = root;
this.init();
}
loop(): any {
this.bodies.forEach(body => {
if (body instanceof Ball) {
body.updateSkin();
}
});
}
init() {
this.gravity = [0, 10];
}
}
\ No newline at end of file
export const FACTOR = 30;
\ No newline at end of file
This diff is collapsed.
import { StarterBase } from "../StarterBase";
export class SimpleP2APP extends StarterBase {
private world: p2.World;
private body: p2.Body;
public createP2App(): void {
//创建p2物理世界world
this.world = new p2.World();
var world = this.world;
world.gravity = [10, 10];
//创建矩形形状shape
var shape: p2.Box = new p2.Box({ width: 100 / this.factor, height: 50 / this.factor });
//创建刚体body
var body: p2.Body = new p2.Body({ mass: 1 });
body.position = [275 / this.factor, 100 / this.factor];
body.addShape(shape);
world.addBody(body);
this.body = body;
}
loop(): void {
this.world.step(1 / 60);
console.log(this.body.position);
}
protected createGameScene(): void {
super.createGameScene();
this.createP2App();
}
}
window['SimpleP2APP'] = SimpleP2APP;
\ No newline at end of file
import { StarterBase } from "../StarterBase";
import p2DebugDraw from "../p2DebugDraw";
export class SimpleP2APPWithDebugDraw extends StarterBase {
private world: p2.World;
private debugDraw: p2DebugDraw;
public createP2App(): void {
//创建p2物理世界world
this.world = new p2.World();
var world = this.world;
world.gravity = [0, 10];
//创建矩形形状shape
var shape: p2.Box = new p2.Box({ width: 100 / this.factor, height: 50 / this.factor });
//创建刚体body
var body: p2.Body = new p2.Body({ mass: 1 });
body.position = [275 / this.factor, 100 / this.factor];
body.addShape(shape);
world.addBody(body);
//添加p2调试试图
var sprite: egret.Sprite = new egret.Sprite();
this.addChild(sprite);
this.debugDraw = new p2DebugDraw(world, sprite);
//添加游戏帧频事件
this.addEventListener(egret.Event.ENTER_FRAME, this.loop, this);
}
loop(): void {
this.world.step(1 / 60);
this.debugDraw.drawDebug();
}
protected createGameScene(): void {
super.createGameScene();
this.createP2App();
}
}
window['SimpleP2APPWithDebugDraw'] = SimpleP2APPWithDebugDraw;
\ No newline at end of file
import { StarterBase } from "../StarterBase";
import p2DebugDraw from "../p2DebugDraw";
export default class TestAsset extends StarterBase {
createGameScene() {
super.createGameScene();
this.createWorld();
this.createGround();
this.createBodies();
this.createDebug();
this.stage.addEventListener(egret.TouchEvent.TOUCH_BEGIN, this.addOneBox, this);
}
private addOneBox(e: egret.TouchEvent): void {
var positionX: number = Math.floor(e.stageX);
var positionY: number = Math.floor(e.stageY);
if (Math.random() < 0.5) {
this.createBox(positionX, positionY);
} else {
this.createDesk(positionX, positionY);
}
}
private createBodies(): void {
this.createBox(100, 200);
this.createDesk(300, 100);
}
private createBox(x: number, y: number): void {
x = x / this.factor;
y = y / this.factor;
var boxShape: p2.Box = new p2.Box({ width: 60 / this.factor, height: 60 / this.factor });
var boxBody: p2.Body = new p2.Body({ mass: 1, position: [x, y] });
boxBody.addShape(boxShape);
this.world.addBody(boxBody);
this.bindAsset(boxBody, "box_png");
}
private createDesk(x: number, y: number): void {
var _this = this;
x = x / this.factor;
y = y / this.factor;
var deskVertices: number[][] = new Array(
[0, 0],
[0, 16],
[16, 16],
[16, 46],
[32, 46],
[32, 16],
[178, 16],
[178, 46],
[194, 46],
[194, 16],
[210, 16],
[210, 0]
);
deskVertices.forEach(function (vertice: number[]) {
vertice[0] = vertice[0] / _this.factor;
vertice[1] = vertice[1] / _this.factor;
});
var deskBody: p2.Body = new p2.Body({ mass: 1, position: [x, y] });
deskBody.fromPolygon(deskVertices);
this.world.addBody(deskBody);
this.bindAsset(deskBody, "desk_png");
}
private bindAsset(body: p2.Body, assetName: string): void {
var offset: number[] = [];
body.updateAABB();
var bodyWidth: number = body.aabb.upperBound[0] - body.aabb.lowerBound[0];
var bodyHeight: number = body.aabb.upperBound[1] - body.aabb.lowerBound[1];
var asset: egret.Bitmap = new egret.Bitmap();
asset.texture = RES.getRes(assetName);
asset.scaleX = bodyWidth / asset.width * this.factor;
asset.scaleY = bodyHeight / asset.height * this.factor;
this.addChild(asset);
p2.vec2.subtract(offset, body.position, body.aabb.lowerBound);
asset.anchorOffsetX = offset[0] / asset.scaleX * this.factor;
asset.anchorOffsetY = offset[1] / asset.scaleY * this.factor;
body['userData'] = asset;
}
private world: p2.World;
private createWorld(): void {
var wrd: p2.World = new p2.World();
wrd.sleepMode = p2.World.BODY_SLEEPING;
wrd.gravity = [0, 10];
this.world = wrd;
}
//生成地板Plane
private planeBody: p2.Body;
private createGround(): void {
var stageHeight: number = this.stage.stageHeight;
var groundShape: p2.Plane = new p2.Plane();
var groundBody: p2.Body = new p2.Body();
groundBody.position[1] = stageHeight / this.factor;
groundBody.angle = Math.PI;
groundBody.addShape(groundShape);
this.world.addBody(groundBody);
}
loop(): void {
this.world.step(1 / 60);
// this.debugDraw.drawDebug();
var _this = this;
this.world.step(1 / 60);
this.world.bodies.forEach(function (b: p2.Body) {
if (b['userData'] != null) {
b['userData'].x = b.position[0] * _this.factor;
b['userData'].y = b.position[1] * _this.factor;
b['userData'].rotation = b.angle * 180 / Math.PI;
}
});
}
private debugDraw: p2DebugDraw;
private createDebug(): void {
var sprite: egret.Sprite = new egret.Sprite();
this.addChild(sprite);
this.debugDraw = new p2DebugDraw(this.world, sprite);
}
}
window['TestAsset'] = TestAsset;
\ No newline at end of file
export const addImage = (key: string, parent?: egret.DisplayObjectContainer) => {
const texture = RES.getRes(key);
const bitmap = new egret.Bitmap(texture);
parent && parent.addChild(bitmap);
return bitmap;
}
export const getResPath = () => window['resPath'] || ''; export const getResPath = () => window['resPath'] || '';
\ 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