Commit ec05804f authored by zjz1994's avatar zjz1994

代码暂存

parent 75054211
This diff is collapsed.
/**
* Created by renjianfeng on 2020-03-13.
*/
const customId = 'pintu';
(async function () {
let customModule = await fetch(`../meta.json`);
customModule = await customModule.json();
console.log(customModule);
await loadAssets(customModule.assets);
launchWithCustomModule(customModule);
})();
function launchWithCustomModule(customModule) {
//engine.registerCustomCodeModule(customModule);
engine.registerCustomModule(customId, window[customId]);
const { props: propsOption, assets } = customModule;
let props = engine.computeProps(customModuleProps, propsOption);
const customModuleIns = {
id: customId,
props,
assets,
};
engine.registerCustomModules([customModuleIns]);
engine.launchWithConfig({
options: {
entrySceneView: 'entry',
},
assets: [],
views: [{
name: 'entry',
type: 'node',
properties: {
x: 0,
y: 0,
}
}],
customs: [],
}, null, function () {
setTimeout(() => {
engine.addCustomModule(customId, engine.gameStage.sceneContainer.getChildAt(0));
}, 100);
setTimeout(() => {
engine.globalEvent.dispatchEvent('pictures-start', {
picUrl: "//yun.duiba.com.cn/aurora/assets/eb952d8bfd900cb9367368ddf8048d66c44a52a0.png",
rows:3,
cols:3,
gametime:30
});
// const d = engine.gameStage.sceneContainer.getChildAt(0);
// engine.gameStage.sceneContainer.getChildAt(0).x = (d.stage.width-props.W)/2;
// engine.gameStage.sceneContainer.getChildAt(0).y = (d.stage.height-props.H)/2;
}, 1000);
// setTimeout(() => {
// engine.globalEvent.dispatchEvent('pictures-start', {
// picUrl: "http://yun.duiba.com.cn/aurora/assets/e1593b97c27077b85b92f7eaaeae1ed64a1eb79a.png",
// // picUrl: "http://yun.duiba.com.cn/aurora/assets/d23e73d37ec01931e48cbd0a4095367044c5675c.png"
// blockUrl: "888"
// });
// }, 30*1000);
});
engine.globalEvent.addEventListener('pictures-time-update', (e) => {
// console.log(e.type, e.data);
});
engine.globalEvent.addEventListener('pictures-game-fail', (e) => {
console.log(e.type, e.data);
});
engine.globalEvent.addEventListener('pictures-game-success', (e) => {
console.log(e.type, e.data);
});
}
function getAssetByUUID(uuid) {
return engine.resolveCustomAsset(customId, uuid);
}
function getProps() {
return engine.getProps(customId);
}
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>拼图</title>
<meta name="viewport"
content="width=device-width,initial-scale=1, minimum-scale=1, maximum-scale=1, user-scalable=no"/>
<meta name="apple-mobile-web-app-capable" content="yes"/>
<meta name="full-screen" content="true"/>
<meta name="screen-orientation" content="portrait"/>
<meta name="x5-fullscreen" content="true"/>
<meta name="360-fullscreen" content="true"/>
<style>
html,
body {
padding: 0;
margin: 0;
border: 0;
width: 100%;
height: 100%;
overflow: hidden;
position: absolute;
background-color: gray;
}
.game-container{
width: 100%;
height: 100%;
line-height:0;
font-size:0;
}
</style>
</head>
<body>
<div id="game-container" class="game-container"></div>
<script crossorigin="anonymous" src="//yun.duiba.com.cn/editor/zeroing/libs/engine.1de84ff79dba19e949088de63aa75af51a515e5c.js"></script>
<script crossorigin="anonymous" src="//yun.duiba.com.cn/editor/zeroing/libs/svga.fd3923ae6e664251ca7981801a65809cc5f36bc3.js"></script>
<!-- <script src="//yun.duiba.com.cn/editor/zeroing/libs/engine.ebc906f6b50b8da0a669f77027981d5f3cb560ce.js"></script> -->
<!-- <script src="http://localhost:4002/debug/engine.js"></script>
<script src="http://localhost:4003/debug/engine-svga.js"></script> -->
<!--<script src="//yun.duiba.com.cn/editor/zeroing/libs/engine.9a9dbfda4cb2dd5508ecddfe3d95dfd88063f7b5.js"></script>-->
<script src="app.js"></script>
<script src="props.js"></script>
<script src="load-assets.js"></script>
<script src="main.js"></script>
<script>
</script>
</body>
\ No newline at end of file
/**
* Created by rockyl on 2020-01-21.
*/
const assets = [
{
"name": "玩家icon",
"url": "//yun.duiba.com.cn/aurora/assets/5b3e30496b2d9fdafb0cf3835fd6704ce10e45b4.png",
"uuid": "888",
"ext": ".png"
},
{
"name": "雨滴",
"url": "//yun.duiba.com.cn/aurora/assets/8564c8c9be3aead71b05a0bab8d7d07ac3f778a1.png",
"uuid": "264a6192-d7bf-45e8-8f15-6ba2c439a532",
"ext": ".png"
},
{
"name": "炸弹",
"url": "//yun.duiba.com.cn/aurora/assets/171e92283cd13c013ee1b76d28d252ff08815d47.png",
"uuid": "eb88b42d-e151-4c1b-94b9-7c16f7bfac29",
"ext": ".png"
},
{
"name": "石块",
"url": "//yun.duiba.com.cn/aurora/assets/99b0af0c59fe79a415a3f032149cfacc27e3ac2c.png",
"uuid": "ab1bdabc-21ba-46bf-9299-6c638f766c88",
"ext": ".png"
},
{
"name": "水花",
"url": "//yun.duiba.com.cn/aurora/assets/93d37b4a0e367e80e375308a6b4414d72d7666fc.svga",
"uuid": "b521bf94-20e1-44dd-8eca-d24996cbaeae",
"ext": ".svga"
},
{
"name": "炸弹",
"url": "//yun.duiba.com.cn/aurora/assets/4dd18f0689c663bbcf710a7afc4d929084d97d36.svga",
"uuid": "322edf39-805b-4e84-9d07-5573dfeebc0e",
"ext": ".svga"
},
{
"name": "玩家",
"url": "//yun.duiba.com.cn/aurora/assets/b66300c5d4f27134b0aac3dc90a3220e8ae572eb.svga",
"uuid": "71d8dcbc-3931-471a-b585-b3ae01b25aa6",
"ext": ".svga"
}
];
function loadAssets(customModuleAssets, onProgress, onComplete){
return engine.loadAssets(assets.concat(...customModuleAssets), onProgress, onComplete);
}
This diff is collapsed.
This diff is collapsed.
/**
* Created by rockyl on 2020-01-21.
*/
let customModuleProps = {
};
{
"name": "拼图",
"desc": "拼图模块1.0",
"props": {
"MAX_COL": {
"alias": "图片分成几列",
"type": "number",
"default": 3
},
"MAX_ROW": {
"alias": "图片分成几行",
"type": "number",
"default": 4
},
"W": {
"alias": "图片的宽度",
"type": "number",
"default": 620
},
"H": {
"alias": "图片的高度",
"type": "number",
"default": 620
},
"OFFSET_X": {
"alias": "OFFSET_X",
"type": "number",
"default": 0
},
"OFFSET_Y": {
"alias": "OFFSET_Y",
"type": "number",
"default": 0
},
"GAP": {
"alias": "图片间隙",
"type": "number",
"default": 0
},
"GAME_TIME": {
"alias": "游戏时间",
"type": "number",
"default": 30
}
},
"assets": [
{
"name": "遮罩",
"url": "//yun.duiba.com.cn/aurora/assets/5b3e30496b2d9fdafb0cf3835fd6704ce10e45b4.png",
"uuid": "888",
"ext": ".png"
},
{
"name": "框3",
"url": "//yun.duiba.com.cn/aurora/assets/521e08c574bd76c7dcf708cd6f8c4906e3707f06.png",
"uuid": "kuang3",
"ext": ".png"
},
{
"name": "框4",
"url": "//yun.duiba.com.cn/aurora/assets/1c384d986504d5aabfffeb5125dc67e73ce1b539.png",
"uuid": "kuang4",
"ext": ".png"
}
],
"events": {
"in": {
"pictures-start": {
"alias": "开始",
"data": {
"rows":3,
"cols":3,
"picUrl":"图片",
"gametime":30
}
},
"pictures-pause": {
"alias": "停止"
},
"pictures-resume":{
"alias":"继续"
}
},
"out": {
"pictures-time-update": {
"alias": "倒计时更新",
"data": {
"time":"剩余时间"
}
},
"pictures-game-over": {
"alias": "游戏结束",
"data": {
"reason": "结束(1:成功,2失败)"
}
}
}
}
}
\ No newline at end of file
/**
* Created by rockyl on 2018/8/16.
*/
import { props } from "../props";
import qietu from "./qietu";
import { getTexture, loadImageByUrl,getTextureByName } from "./utils";
import ObjectPool = engine.ObjectPool;
export default class GameView extends engine.Container {
rows:number;
cols:number;
passtime:number;
gametime:number;
showtime:number;
lastframetime:number;
ingame:boolean = false;
ticker;
selectimg3:engine.Image;
selectimg4:engine.Image;
nselectimg:engine.Image;
selectImgArr:Array<string> = [];
sucposData:Object = {};
dicont:engine.Container;
linecont:engine.Container;
blockcont:engine.Container;
lineShape:engine.Shape;
constructor() {
super();
this.once(engine.Event.ADDED_TO_STAGE, this.initScene, this);
}
initScene(){
this.lineShape = new engine.Shape();
this.selectimg3 = new engine.Image(getTextureByName("框3"));
this.selectimg4 = new engine.Image(getTextureByName("框4"));
//底图
this.dicont = new engine.Container();
//线
this.linecont = new engine.Container();
//亮片
this.blockcont = new engine.Container();
this.addChild(this.dicont);
this.addChild(this.linecont);
this.addChild(this.blockcont);
this.linecont.addChild(this.lineShape);
}
async start() {
this.dicont.removeChildren();
let picUrl = props.picUrl;
this.gametime = props.gametime;
this.showtime = props.gametime;
this.rows = props.rows;
this.cols = props.cols;
this.nselectimg = null;
if(this.rows==3){
this.nselectimg = this.selectimg3;
}
if(this.rows==4){
this.nselectimg = this.selectimg4;
}
this.passtime = 0;
this.selectImgArr = new Array();
this.sucposData = {};
let result = await qietu(picUrl,this.rows,this.cols,this.dicont);
let res = result.res;
if(res){
let picarr = result.picarr;
let mwid = result.mwid;
let mhei = result.mhei;
let picnames = result.picnames;
let gap = 2;
for(let r=0;r<this.rows;r++){
for(let c=0;c<this.cols;c++){
let ipicx = mwid*c+gap;
let ipicy = mhei*r+gap;
let npicname = "pic_"+c+"_"+r;
this.sucposData[npicname] = r+"_"+c;
let selidx = Math.floor(Math.random()*picnames.length);
let ipicname:string = picnames[selidx];
if(r==c&&r!=this.rows-1){
while(ipicname==npicname){
selidx = Math.floor(Math.random()*picnames.length);
ipicname = picnames[selidx];
}
}
let ipic = picarr[ipicname];
picnames.splice(selidx,1);
this.dicont.addChild(ipic);
ipic.x = ipicx;
ipic.y = ipicy;
ipic["postr"] = r+"_"+c;
ipic.addEventListener(engine.MouseEvent.MOUSE_DOWN,
this.touchPic,this);
}
}
//框线
this.lineShape.clear();
this.lineShape.beginStroke(0x0069df,4,engine.LINE_CAP.BUTT,engine.LINE_JOIN.BEVEL,100);
this.lineShape.moveTo(0+gap,0+gap);
this.lineShape.lineTo(mhei*this.cols+gap,0+gap);
this.lineShape.lineTo(mhei*this.cols+gap,mwid*this.rows+gap);
this.lineShape.lineTo(0+gap,mwid*this.rows+gap);
this.lineShape.lineTo(0+gap,0+gap);
for(let rl=1;rl<this.rows;rl++){
this.lineShape.moveTo(0+gap,rl*mhei+gap);
this.lineShape.lineTo(mwid*this.cols+gap,rl*mhei+gap);
}
for(let cl=1;cl<this.cols;cl++){
this.lineShape.moveTo(cl*mwid+gap,0+gap);
this.lineShape.lineTo(cl*mwid+gap,this.rows*mhei+gap);
}
this.lineShape.endStroke();
this.ticker = setInterval(this.update, 30);
}else{
console.error("图片处理失败");
}
}
pause(){
this.ingame = false;
}
resume(){
this.ingame = true;
}
update() {
let ntime = Date.now();
if(!this.lastframetime){
this.lastframetime = ntime;
return;
}
if(this.ingame){
let passtime = (ntime - this.lastframetime)/1000;
this.passtime += passtime;
let showtime1 = Math.round(this.gametime - this.passtime);
// console.log(passtime,this.passtime,this.gametime,this.showtime);
if(this.showtime!=showtime1){
this.showtime = showtime1;
if(this.showtime<=0){
this.showtime = 0;
this.gameover(2);
}
// console.log("倒计时",this.showtime);
engine.globalEvent.dispatchEvent("pictures-time-update",{
time:this.showtime
});
}
}
this.lastframetime = ntime;
}
touchPic(e:engine.MouseEvent){
if(!this.ingame){
return;
}
// console.log(e.target.name);
let selectname = e.target.name;
let spx = e.target.x;
let spy = e.target.y;
let gap = 2;
if(this.selectImgArr.length==0){
if(this.nselectimg){
this.blockcont.addChild(this.nselectimg);
this.nselectimg.x = spx-gap;
this.nselectimg.y = spy-gap;
}
this.selectImgArr.push(selectname);
}else if(this.selectImgArr.length==1){
if(selectname==this.selectImgArr[0]){
return;
}else{
this.selectImgArr.push(selectname);
this.changeImgPos();
}
}
}
changeImgPos(){
let img1name = this.selectImgArr[0];
let img2name = this.selectImgArr[1];
let img1 = this.dicont.getChildByName(img1name);
let img2 = this.dicont.getChildByName(img2name);
let imgpos1x = img1.x;
let imgpos1y = img1.y;
let imgpos2x = img2.x;
let imgpos2y = img2.y;
let imgpostr1 = img1.postr;
let imgpostr2 = img2.postr;
img1.x = imgpos2x;
img1.y = imgpos2y;
img1.postr = imgpostr2;
img2.x = imgpos1x;
img2.y = imgpos1y;
img2.postr = imgpostr1;
this.selectImgArr = new Array();
if(this.nselectimg){
this.blockcont.removeChild(this.nselectimg);
}
let isuc = this.checkSuccess();
if(isuc){
console.error("成功过关");
this.gameover(1);
}
}
checkSuccess(){
let isuc:boolean = true;
for(let r=0;r<this.rows;r++){
for(let c=0;c<this.cols;c++){
let ipicname = "pic_"+c+"_"+r;
let npicstr = r+"_"+c;
let ipic = this.dicont.getChildByName(ipicname);
let ipostr = ipic.postr;
if(npicstr!=ipostr){
isuc = false;
break;
}
}
}
return isuc;
}
/**
*
* @param reason 1成功,2失败
*/
private gameover(reason:number) {
this.ingame = false;
this.ticker&&clearInterval(this.ticker);
engine.globalEvent.dispatchEvent('pictures-game-over', {
reason:reason,
time: this.passtime
});
}
}
/**
* Created by rockyl on 2020-01-09.
*/
import GameView from "./GameView";
import { injectProps } from "../props";
export class GameWrapper extends engine.Container {
// private _status;
private _gameView: GameView;
constructor() {
super();
//创建实例
this._gameView = new GameView();
this.addChild(this._gameView);
engine.globalEvent.addEventListener('pictures-start', this.start, this);
engine.globalEvent.addEventListener('pictures-pause', this.pause, this);
engine.globalEvent.addEventListener('pictures-resume', this.resume, this);
}
start(event: engine.Event) {
injectProps(event.data);
// this._status = 1;
this._gameView.start();
}
pause(event: engine.Event) {
this._gameView.pause();
}
resume(event:engine.Event){
this._gameView.resume();
}
}
import { props } from "../props";
import { loadImageByUrl } from "./utils";
const urls = [];
const picMap = {};
const posMap = {};
export default async(url:string, rows:number, cols:number,parent:engine.Container) => {
try{
let t:engine.Texture = await loadImageByUrl(url) as engine.Texture;
let mwid = t.width/rows;
let mhei = t.height/cols;
let picarr = {};
let picnames = [];
for(let r=0;r<rows;r++){
let startx = r/rows;
let endx = (r+1)/rows;
// linedraw.moveTo()
for(let c=0;c<cols;c++){
let starty = c/cols;
let endy = (c+1)/cols;
let uvs = new Float32Array([
starty,
startx,
endy,
startx,
endy,
endx,
starty,
endx
]);
let itexture = new engine.Sprite(t);
itexture.uvs = uvs;
itexture.width = mwid;
itexture.height = mhei;
let itexname = "pic_"+c+"_"+r;
itexture.name = itexname;
picarr[itexname] = itexture;
picnames.push(itexname);
}
}
return {
res:true,
picarr:picarr,
mwid:mwid,
mhei:mhei,
picnames:picnames
};
}catch(e){
console.error("图片加载失败",e);
console.log(url,rows,cols);
return {
res:false
};
}
};
/**
* Created by rockyl on 2020-01-21.
*/
export function getTexture(uuid) {
return engine.Texture.from(getAssetByUUID(uuid).uuid);
}
export function getTextureByName(name) {
return getTexture(engine.getAssetByName(name).uuid);
}
export function playSound(name) {
engine.playSound(engine.getAssetByName(name).uuid, {keep: true});
}
export function createSvga(name, anchorName?) {
let inst = new svga.Svga();
inst.source = 'asset://' + engine.getAssetByName(name).uuid;
return inst;
}
export function loadImageByUrl(url){
return new Promise((resolve) => {
if (engine.TextureCache[url]) {
resolve(engine.TextureCache[url])
} else {
var t = engine.Texture.from(url);
t.addEventListener("loaded", () => { resolve(t) })
}
})
}
\ No newline at end of file
/**
* Created by rockyl on 2019-11-20.
*/
import {GameWrapper} from "./game/GameWrapper";
import {injectProps, prepareProps} from "./props";
export default function (props) {
prepareProps();
injectProps(props);
let instance = new GameWrapper();
return instance;
}
/**
* Created by rockyl on 2020-01-21.
*/
export let props: any = {};
export function prepareProps() {
let metaProps = getProps();
engine.injectProp(props, metaProps);
}
export function injectProps(p) {
engine.injectProp(props, p);
}
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