Commit 56c43ab8 authored by 汪欢's avatar 汪欢

拼图

parent 7ebfecde
This diff is collapsed.
/**
* Created by renjianfeng on 2020-03-13.
*/
const customId = 'pictures';
(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-reset', {
"goodsProbability": [0.8,0.1,0.1],
"countDown": 30,
"acceleratedSpeed":0.1
});
engine.globalEvent.dispatchEvent('pictures-start');
}, 500);
});
engine.globalEvent.addEventListener('pictures-time-update', (e) => {
console.log(e.type, e.data);
});
engine.globalEvent.addEventListener('pictures-score-update', (e) => {
console.log(e.type, e.data);
});
engine.globalEvent.addEventListener('pictures-game-over', (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: red;
}
.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/50a7212a113175fa18c866b005d98f07c558dc77.png",
"uuid": "66f23d13-82a5-4cec-9496-301ec240d087",
"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);
}
(function (global, factory) {
typeof exports === 'object' && typeof module !== 'undefined' ? module.exports = factory(require('tslib')) :
typeof define === 'function' && define.amd ? define(['tslib'], factory) :
(global = global || self, global.pictures = factory(global.tslib));
}(this, (function (tslib) { 'use strict';
function getIndexFromRC(row, col, maxCol) {
var index = row * maxCol + col;
return index;
}
function getRandomArray(array) {
array.sort(function () {
return .5 - Math.random();
});
}
var qietu = (function (parent, url, MAX_COL, MAX_ROW) {
var W = 600;
var H = 600;
var GAP = 10;
var spr = [];
var pos = [];
var _loop_1 = function (row) {
var _loop_2 = function (col) {
var child = engine.Sprite.fromImage(url);
spr.push(child);
child.scaleX = 1 / MAX_COL;
child.scaleY = 1 / MAX_ROW;
parent.addChild(child);
child.x = col * (W / MAX_COL + GAP);
child.y = row * (H / MAX_ROW + GAP);
pos.push([child.x, child.y]);
child.addEventListener(engine.Event.COMPLETE, function () {
var uvs = new Float32Array([
col / MAX_COL,
row / MAX_ROW,
(col + 1) / MAX_COL,
row / MAX_ROW,
(col + 1) / MAX_COL,
(row + 1) / MAX_ROW,
col / MAX_COL,
(row + 1) / MAX_ROW,
]);
child.uvs = uvs;
});
};
for (var col = 0; col < MAX_COL; col++) {
_loop_2(col);
}
};
for (var row = 0; row < MAX_ROW; row++) {
_loop_1(row);
}
console.log(spr);
return [spr, pos];
});
var MAX_COL = 3;
var MAX_ROW = 3;
var W = 600;
var H = 600;
var GAP = 10;
var w = W / MAX_COL;
var h = H / MAX_ROW;
var GameView = (function (_super) {
tslib.__extends(GameView, _super);
function GameView() {
var _this = _super.call(this) || this;
_this.countTime = 10;
_this.once(engine.Event.ADDED_TO_STAGE, _this.setup, _this);
return _this;
}
GameView.prototype.createRects = function () { };
GameView.prototype.setup = function () {
var _this = this;
var url = "http://yun.duiba.com.cn/aurora/assets/fb06e66c26f17b00f677367bd7571a1aa9b1ade4.png";
var parent = new engine.Sprite();
this.addChild(parent);
var timeContainer = new engine.Rect();
this.addChild(timeContainer);
setTimeout(function () {
_this.countTime--;
}, 1000);
if (this.countTime === 0) {
console.log("时间到!");
}
var result = qietu(parent, url, MAX_COL, MAX_ROW);
this.pictures = result[0];
this.rightList = this.pictures.concat([]);
var posList = result[1];
getRandomArray(this.pictures);
var i = 0;
var len;
len = this.pictures.length;
console.log(len);
for (; i < len; i++) {
this.dragPic = this.pictures[i];
this.pictures[i].addEventListener(engine.MouseEvent.MOUSE_DOWN, this.onDown, this);
var _a = posList[i], x = _a[0], y = _a[1];
this.dragPic.x = x;
this.dragPic.y = y;
}
};
GameView.prototype.onDown = function (e) {
console.log(e);
this.dragPic = e.target;
this.localPicX = e.localX / MAX_COL;
this.localPicY = e.localY / MAX_ROW;
this.distanceX = this.dragPic.x;
this.distanceY = this.dragPic.y;
this.indexI = this.distanceX / (w + GAP);
this.indexJ = this.distanceY / (h + GAP);
this.index = (this.indexI - 1) * MAX_COL + this.indexJ;
this.centerX = e.clientX + w / 2;
this.centerY = e.clientY + h / 2;
this.stage.addEventListener(engine.MouseEvent.MOUSE_MOVE, this.onMove, this);
this.stage.addEventListener(engine.MouseEvent.MOUSE_UP, this.stageOnUp, this);
};
GameView.prototype.stageOnUp = function (e) {
this.stage.removeEventListener(engine.MouseEvent.MOUSE_MOVE, this.onMove, this);
var curJ = Math.floor(this.centerX / (w + GAP));
var curI = Math.floor(this.centerY / (h + GAP));
if (curJ < MAX_COL && curI < MAX_ROW) {
var index = getIndexFromRC(curI, curJ, MAX_COL);
console.log(index);
var dropPic = this.pictures[index];
var dropPicX = dropPic.x;
var dropPicy = dropPic.y;
dropPic.x = this.distanceX;
dropPic.y = this.distanceY;
this.dragPic.x = dropPicX;
this.dragPic.y = dropPicy;
var dropPicIndex = this.pictures.indexOf(dropPic);
var dragPicIndex = this.pictures.indexOf(this.dragPic);
this.pictures[dropPicIndex] = this.dragPic;
this.pictures[dragPicIndex] = dropPic;
var result = true;
for (var j = 0; j < this.rightList.length; j++) {
if (this.rightList[j] != this.pictures[j]) {
result = false;
break;
}
}
if (result) {
console.log('拼图成功!');
}
}
this.stage.removeEventListener(engine.MouseEvent.MOUSE_UP, this.stageOnUp, this);
};
GameView.prototype.onMove = function (e) {
this.dragPic.x = e.stageX - this.localPicX;
this.dragPic.y = e.stageY - this.localPicY;
this.centerX = this.dragPic.x + w / 2;
this.centerY = this.dragPic.y + h / 2;
console.log(this.centerX, this.centerY, "图片中心位置");
console.log(e.localX, e.localY, e.stageX, e.stageY);
console.log("====================================");
};
return GameView;
}(engine.Container));
var GameWrapper = (function (_super) {
tslib.__extends(GameWrapper, _super);
function GameWrapper() {
var _this = _super.call(this) || this;
var gameView = _this._gameView = new GameView();
_this.addChild(gameView);
console.log("hello world.");
return _this;
}
return GameWrapper;
}(engine.Container));
var props = {};
function prepareProps() {
var metaProps = getProps();
engine.injectProp(props, metaProps);
}
function injectProps(p) {
engine.injectProp(props, p);
}
function index (props) {
prepareProps();
injectProps(props);
var instance = new GameWrapper();
return instance;
}
return index;
})));
//# sourceMappingURL=main.js.map
\ No newline at end of file
This diff is collapsed.
/**
* Created by rockyl on 2020-01-21.
*/
let customModuleProps = {
};
{
"name": "口红机",
"desc": "口红机模块",
"props": {
"playerPositionY": {
"alias": "玩家Y轴位置",
"type": "number",
"default": 900
},
"rainScore": {
"alias": "接中雨滴获得分数",
"type": "number",
"default": 1
},
"stoneScore": {
"alias": "接中石块获得分数",
"type": "number",
"default": -1
},
"speed": {
"alias": "道具掉落初始速度",
"type": "number",
"default": 10
},
"maxSpeed": {
"alias": "道具掉落速度上限",
"type": "number",
"default": 3
},
"gameOverCondition": {
"alias": "游戏结束条件(1:接到炸弹死亡,2:分数负数或接到炸弹死亡)",
"type": "number",
"default": 1
}
},
"assets": [
{
"name": "玩家icon",
"url": "//yun.duiba.com.cn/aurora/assets/50a7212a113175fa18c866b005d98f07c558dc77.png",
"uuid": "66f23d13-82a5-4cec-9496-301ec240d087",
"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": "炸弹svga",
"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"
}
],
"events": {
"in": {
"pictures-reset": {
"alias": "重置",
"data": {
"goodsProbability": "[0.2,0.5,0.3]道具概率(雨滴、石头、炸弹),所有概率相加为1",
"countDown": "倒计时(s)",
"acceleratedSpeed":"道具掉落加速度(单位:每秒)"
}
},
"pictures-start": {
"alias": "开始"
},
"pictures-revive": {
"alias": "复活"
},
"pictures-pause": {
"alias": "暂停"
},
"pictures-resume": {
"alias": "恢复"
},
"pictures-clear": {
"alias": "清空,通过reset事件恢复"
}
},
"out": {
"pictures-score-update": {
"alias": "分数更新",
"data": {
"score":"分数"
}
},
"pictures-time-update": {
"alias": "倒计时更新",
"data": {
"time":"剩余时间"
}
},
"pictures-game-over": {
"alias": "游戏结束",
"data": {
"score":"分数",
"reason": "结束原因(1:时间到了,2:玩家死亡)"
}
}
}
}
}
\ No newline at end of file
/**
* Created by rockyl on 2018/8/16.
*/
import { props } from "../props";
import { playSound, createSvga, getIndexFromRC, getRandomArray } from "./utils";
import ObjectPool = engine.ObjectPool;
import { getTextureByName } from "./utils";
import { PoolName } from "./object-pool-init";
import qietu from "./qietu";
const MAX_COL = 3;
const MAX_ROW = 3;
const W = 600;
const H = 600;
const GAP = 10;
// 每张图片宽
const w = W / MAX_COL;
// 每张图片高
const h = H / MAX_ROW;
export default class GameView extends engine.Container {
constructor() {
super();
this.once(engine.Event.ADDED_TO_STAGE, this.setup, this);
}
//当前图片对象
dragPic;
localPicX;
localPicY;
distanceX;
distanceY;
centerX: number;
centerY: number;
pictures;
// curPic
// 点击图片时的索引
index;
indexI: number;
indexJ: number;
rightList: engine.Sprite[];
// 倒计时时间
countTime = 10
createRects() {}
setup() {
// debugger;
const url =
"http://yun.duiba.com.cn/aurora/assets/fb06e66c26f17b00f677367bd7571a1aa9b1ade4.png";
// 图片容器
const parent = new engine.Sprite();
this.addChild(parent);
// 创建一个显示时间的容器
const timeContainer = new engine.Rect()
this.addChild(timeContainer)
setTimeout(()=>{
this.countTime--;
},1000)
if(this.countTime === 0){
console.log("时间到!")
}
// let pictures
// 图片一维数组
// this.pictures = qietu(parent, url,MAX_COL,MAX_ROW);
const result = qietu(parent, url, MAX_COL, MAX_ROW);
this.pictures = result[0];
this.rightList = this.pictures.concat([]);
const posList = result[1];
getRandomArray(this.pictures);
let i = 0;
// let j = 0;
// 数组长度
let len;
len = this.pictures.length;
console.log(len);
for (; i < len; i++) {
this.dragPic = this.pictures[i];
this.pictures[i].addEventListener(
engine.MouseEvent.MOUSE_DOWN,
this.onDown,
this
);
const [x, y] = posList[i];
this.dragPic.x = x;
this.dragPic.y = y;
}
}
onDown(e: engine.MouseEvent) {
console.log(e);
// this.addEventListener(engine.MouseEvent.MOUSE_MOVE, this.onMove, this)
// 创建一个图片对象接收当前位置信息
this.dragPic = e.target;
// let currentpic = this.currentPic;
// 鼠标的偏移量
this.localPicX = e.localX / MAX_COL;
this.localPicY = e.localY / MAX_ROW;
// 最开始图片的位置
this.distanceX = this.dragPic.x;
this.distanceY = this.dragPic.y;
// 最开始点击的图片的索引值
this.indexI = this.distanceX / (w + GAP);
this.indexJ = this.distanceY / (h + GAP);
this.index = (this.indexI - 1) * MAX_COL + this.indexJ;
// 图片的中心位置
this.centerX = e.clientX + w / 2;
this.centerY = e.clientY + h / 2;
this.stage.addEventListener(
engine.MouseEvent.MOUSE_MOVE,
this.onMove,
this
);
this.stage.addEventListener(
engine.MouseEvent.MOUSE_UP,
this.stageOnUp,
this
);
}
stageOnUp(e) {
this.stage.removeEventListener(
engine.MouseEvent.MOUSE_MOVE,
this.onMove,
this
);
// 判断图片是否进入另一张图片的范围内
// 图片第几行第几列
let curJ = Math.floor(this.centerX / (w + GAP));
let curI = Math.floor(this.centerY / (h + GAP));
// 点击图片的位置
if (curJ < MAX_COL && curI < MAX_ROW) {
// 要交换的图片
// console.log(this.pictures[this.index]);
// 获取交互图片的索引值
let index = getIndexFromRC(curI, curJ, MAX_COL);
console.log(index);
//要交换的图片
let dropPic = this.pictures[index];
let dropPicX = dropPic.x;
let dropPicy = dropPic.y;
dropPic.x = this.distanceX;
dropPic.y = this.distanceY;
this.dragPic.x = dropPicX;
this.dragPic.y = dropPicy;
// 交换之后索引也需要交换
const dropPicIndex = this.pictures.indexOf(dropPic);
const dragPicIndex = this.pictures.indexOf(this.dragPic);
this.pictures[dropPicIndex] = this.dragPic;
this.pictures[dragPicIndex] = dropPic;
let result = true;
for (let j = 0; j < this.rightList.length; j++) {
if (this.rightList[j] != this.pictures[j]) {
result = false;
break;
}
}
if (result) {
console.log('拼图成功!')
}
}
this.stage.removeEventListener(
engine.MouseEvent.MOUSE_UP,
this.stageOnUp,
this
);
}
onMove(e: engine.MouseEvent) {
// 当前图片的位置
this.dragPic.x = e.stageX - this.localPicX;
this.dragPic.y = e.stageY - this.localPicY;
// 当前图片的中心位置
this.centerX = this.dragPic.x + w / 2;
this.centerY = this.dragPic.y + h / 2;
console.log(this.centerX, this.centerY, "图片中心位置");
// 记录移动图片的位置
console.log(e.localX, e.localY, e.stageX, e.stageY);
console.log("====================================");
}
}
/**
* 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();
//创建实例
let gameView = this._gameView = new GameView();
this.addChild(gameView);
console.log("hello world.");
}
}
/**
* Created by rockyl on 2020-02-03.
*/
import {Goods} from "./Goods";
import ObjectPool = engine.ObjectPool;
export const PoolName: string = 'goods';
ObjectPool.registerPool(PoolName, function () {
return new Goods();
}, function (item: Goods, data) {
item.reset();
});
export default (parent, url,MAX_COL,MAX_ROW) => {
const W = 600;
const H = 600;
const GAP = 10;
const spr = [];
const pos = []
for (let row = 0; row < MAX_ROW; row++) {
for (let col = 0; col < MAX_COL; col++) {
const child = engine.Sprite.fromImage(url);
spr.push(child);
child.scaleX = 1 / MAX_COL;
child.scaleY = 1 / MAX_ROW;
parent.addChild(child);
child.x = col * (W / MAX_COL + GAP);
child.y = row * (H / MAX_ROW + GAP);
pos.push([child.x,child.y]);
child.addEventListener(engine.Event.COMPLETE, () => {
const uvs = new Float32Array([
col / MAX_COL,
row / MAX_ROW,
(col + 1) / MAX_COL,
row / MAX_ROW,
(col + 1) / MAX_COL,
(row + 1) / MAX_ROW,
col / MAX_COL,
(row + 1) / MAX_ROW,
]);
child.uvs = uvs;
// spr.push(child);
});
}
}
console.log(spr);
return [spr,pos];
};
/**
* 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 getIndexFromRC(row,col,maxCol){
let index = row * maxCol + col
return index
}
export function getRandomArray(array){
array.sort(function() {
return .5 - Math.random();
});
}
\ 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