Commit 52de0a2b authored by Friends233's avatar Friends233

随机生成游戏舞台

parent 03f5a902
This diff is collapsed.
...@@ -14,7 +14,7 @@ export const CUSTOM_EVENT = { ...@@ -14,7 +14,7 @@ export const CUSTOM_EVENT = {
export const Config = { export const Config = {
/** 爪子能伸出去的长度 */ /** 爪子能伸出去的长度 */
maxLong: 544, maxLong: 644,
/** 摇晃速度 */ /** 摇晃速度 */
rotationSpeed: 1, rotationSpeed: 1,
...@@ -24,4 +24,24 @@ export const Config = { ...@@ -24,4 +24,24 @@ export const Config = {
/** 高级道具移动速度 */ /** 高级道具移动速度 */
bestPropSpeed: 500, bestPropSpeed: 500,
/** 每轮生成加分道具数量 */
bestPropsNum: 3,
/** 普通道具每种最多生成数量 */
propsNum: 2,
/** 道具分布图 4*3 */
propsMap: [
[
1, 0, 0, 1,
1, 1, 1, 0,
1, 1, 1, 1
],
[
1, 0, 0, 1,
1, 1, 1, 1,
1, 0, 1, 1
]
]
} }
\ No newline at end of file
...@@ -6,7 +6,7 @@ ...@@ -6,7 +6,7 @@
// - https://docs.cocos.com/creator/2.4/manual/en/scripting/life-cycle-callbacks.html // - https://docs.cocos.com/creator/2.4/manual/en/scripting/life-cycle-callbacks.html
import { CUSTOM_EVENT, Config, LevelInfo } from "./Config/GameConfig"; import { CUSTOM_EVENT, Config, LevelInfo } from "./Config/GameConfig";
import { loadGameResources, numToChinese, set16ToRgb } from "./utils"; import { getProbability, getRandomArrayElements, loadGameResources, numToChinese, randomNum, set16ToRgb } from "./utils";
const { ccclass, property } = cc._decorator; const { ccclass, property } = cc._decorator;
// 爪子状态 // 爪子状态
...@@ -25,6 +25,14 @@ export default class GameScene extends cc.Component { ...@@ -25,6 +25,14 @@ export default class GameScene extends cc.Component {
@property(cc.Prefab) @property(cc.Prefab)
procItem: cc.Prefab = null procItem: cc.Prefab = null
/** 舞台道具 */
@property([cc.Prefab])
propItem: cc.Prefab[] = []
/** 加分道具 */
@property(cc.Prefab)
bsetPropItem: cc.Prefab = null
/** 当前关卡 0、1、2... */ /** 当前关卡 0、1、2... */
actLevel = 0, actLevel = 0,
...@@ -57,8 +65,11 @@ export default class GameScene extends cc.Component { ...@@ -57,8 +65,11 @@ export default class GameScene extends cc.Component {
/** 抓取的目标 */ /** 抓取的目标 */
clipTarget: cc.Node = null clipTarget: cc.Node = null
/** 场面上剩余加分道具数量 */
surplusStar = Config.bestPropsNum
protected onLoad(): void { protected onLoad(): void {
loadGameResources() // loadGameResources()
/** 开启碰撞检测 */ /** 开启碰撞检测 */
const cm = cc.director.getCollisionManager() const cm = cc.director.getCollisionManager()
cm.enabled = true cm.enabled = true
...@@ -70,12 +81,14 @@ export default class GameScene extends cc.Component { ...@@ -70,12 +81,14 @@ export default class GameScene extends cc.Component {
this.resetConfig() this.resetConfig()
this.refreshLevelInfo() this.refreshLevelInfo()
this.refreshStageProps()
this.addNodeEvent() this.addNodeEvent()
} }
/** 重置游戏设置 */ /** 重置游戏设置 */
resetConfig() { resetConfig() {
this.clipSpeed = Config.normalPropSpeed this.clipSpeed = Config.normalPropSpeed
this.surplusStar = Config.bestPropsNum
this.clip.getChildByName('line').height = Config.maxLong this.clip.getChildByName('line').height = Config.maxLong
const rotationAni = cc.find('clipMask/clipWrp', this.node) const rotationAni = cc.find('clipMask/clipWrp', this.node)
.getComponent(cc.Animation) .getComponent(cc.Animation)
...@@ -115,7 +128,7 @@ export default class GameScene extends cc.Component { ...@@ -115,7 +128,7 @@ export default class GameScene extends cc.Component {
this.clipState = state this.clipState = state
switch (state) { switch (state) {
case CLIP_STATE.DEFAULT: case CLIP_STATE.DEFAULT:
// 复旋转 // 复旋转
ani.resume() ani.resume()
// 显示默认动效 // 显示默认动效
def.active = true def.active = true
...@@ -127,6 +140,10 @@ export default class GameScene extends cc.Component { ...@@ -127,6 +140,10 @@ export default class GameScene extends cc.Component {
this.clipTarget = null this.clipTarget = null
}) })
} }
// 场面上没有剩余加分道具,刷新道具舞台
if (this.surplusStar == 0) {
this.refreshStageProps()
}
break; break;
case CLIP_STATE.PLAY: case CLIP_STATE.PLAY:
case CLIP_STATE.STOP: case CLIP_STATE.STOP:
...@@ -199,6 +216,7 @@ export default class GameScene extends cc.Component { ...@@ -199,6 +216,7 @@ export default class GameScene extends cc.Component {
setStarProc() { setStarProc() {
const proc = cc.find('procBg/proc', this.node) const proc = cc.find('procBg/proc', this.node)
const key = this.starNum const key = this.starNum
this.surplusStar--
if (key >= this.levelObjectives) return if (key >= this.levelObjectives) return
const node = cc.instantiate(this.procItem) const node = cc.instantiate(this.procItem)
...@@ -235,7 +253,65 @@ export default class GameScene extends cc.Component { ...@@ -235,7 +253,65 @@ export default class GameScene extends cc.Component {
procBg.x = -(procBg.width / 2) procBg.x = -(procBg.width / 2)
} }
/** 刷新舞台道具 */
refreshStageProps() {
this.surplusStar = Config.bestPropsNum
const gameStage = cc.find('gameStage', this.node)
const stageProps: cc.Node[] = gameStage.children
const mapIdx = Math.floor(randomNum(0, Config.propsMap.length))
const stageMap: number[] = [...Config.propsMap[mapIdx]]
// 生成加分道具
for (let i = 0; i < Config.bestPropsNum; i++) {
// 随机取
let idx = Math.floor(randomNum(0, stageMap.length))
// 找到第一个非空的位置
while (stageMap[idx % stageMap.length] !== 1) idx++
// 放入加分道具
stageMap[idx % stageMap.length] = 2
}
// 随机道具池
let propsRandom = []
for (let i = 0; i < Config.propsNum; i++) {
propsRandom.push(...this.propItem)
}
// 剩余格子数量
const emptyNum = stageMap.filter(_ => _ == 1).length
propsRandom = getRandomArrayElements(propsRandom, emptyNum)
for (let i = 0; i < stageProps.length; i++) {
const node = stageProps[i]
const k = stageMap[i]
// 随机旋转角度
const angle = randomNum(0, 90).toFixed(2)
let propNode: cc.Node = null
node.removeAllChildren()
if (k == 1) {
const pre = cc.instantiate(propsRandom.pop())
propNode = cc.instantiate(pre)
} else if (k == 2) { // 加分道具
propNode = cc.instantiate(this.bsetPropItem)
} else {
continue;
}
if (propNode) {
propNode.angle = angle > 45 ? angle - 90 : angle
propNode.setParent(node)
}
}
/** 随机翻转地图 */
const layout = gameStage.getComponent(cc.Layout)
layout.horizontalDirection = getProbability(50) ?
cc.Layout.HorizontalDirection.LEFT_TO_RIGHT : cc.Layout.HorizontalDirection.RIGHT_TO_LEFT
}
update(dt: number): void { update(dt: number): void {
if (this.isGameOver) return
// 锚点跟爪子之间的大致距离 // 锚点跟爪子之间的大致距离
const offset = 41 const offset = 41
const maxLong = Config.maxLong - offset const maxLong = Config.maxLong - offset
......
...@@ -37,9 +37,51 @@ export const numToChinese = (num) => { ...@@ -37,9 +37,51 @@ export const numToChinese = (num) => {
export const loadGameResources = async () => { export const loadGameResources = async () => {
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
cc.resources.preloadDir('images', cc.SpriteFrame, (err, res) => { cc.resources.preloadDir('images', cc.SpriteFrame, (err, res) => {
console.log('111',res) console.log('111', res)
resolve(res,err) resolve(res, err)
}) })
}) })
} }
/**
* 获取区间随机数 [min,max)
* @export
* @param {*} min
* @param {*} max
* @return {*}
*/
export function randomNum(min, max):number {
return Math.floor(Math.random() * (max - min)) + min
}
/**
* 从数组里随机取元素
* @param arr
* @param count
*/
export function getRandomArrayElements(arr, count) {
if (arr.length <= count) return arr;
let shuffled = arr.slice(0), i = arr.length, min = i - count, temp, index;
while (i-- > min) {
index = (i + 1) * Math.random() >> 0;
temp = shuffled[index];
shuffled[index] = shuffled[i];
shuffled[i] = temp;
}
return shuffled.slice(min);
}
/**
* 获取概率
* @param pro 1-100
*/
export function getProbability(pro) {
const randomAry = [
...Array(100-pro).fill(false),
...Array(pro).fill(true)
]
const num = Math.floor(randomNum(0,99))
return randomAry[num]
}
...@@ -16,11 +16,11 @@ ...@@ -16,11 +16,11 @@
}, },
{ {
"frame": 1, "frame": 1,
"value": 40 "value": 50
}, },
{ {
"frame": 3, "frame": 3,
"value": -40 "value": -50
}, },
{ {
"frame": 3.966666666666667, "frame": 3.966666666666667,
......
...@@ -54,8 +54,8 @@ ...@@ -54,8 +54,8 @@
"__type__": "TypedArray", "__type__": "TypedArray",
"ctor": "Float64Array", "ctor": "Float64Array",
"array": [ "array": [
-254.344, 0,
-121.5, 0,
0, 0,
0, 0,
0, 0,
...@@ -63,7 +63,7 @@ ...@@ -63,7 +63,7 @@
1, 1,
1, 1,
1, 1,
1 1.2125
] ]
}, },
"_eulerAngles": { "_eulerAngles": {
......
[
{
"__type__": "cc.Prefab",
"_name": "",
"_objFlags": 0,
"_native": "",
"data": {
"__id__": 1
},
"optimizationPolicy": 0,
"asyncLoadAssets": false,
"readonly": false
},
{
"__type__": "cc.Node",
"_name": "propWrp",
"_objFlags": 0,
"_parent": null,
"_children": [],
"_active": true,
"_components": [],
"_prefab": {
"__id__": 2
},
"_opacity": 255,
"_color": {
"__type__": "cc.Color",
"r": 255,
"g": 255,
"b": 255,
"a": 255
},
"_contentSize": {
"__type__": "cc.Size",
"width": 194,
"height": 173
},
"_anchorPoint": {
"__type__": "cc.Vec2",
"x": 0.5,
"y": 0.5
},
"_trs": {
"__type__": "TypedArray",
"ctor": "Float64Array",
"array": [
0,
0,
0,
0,
0,
0,
1,
1,
1,
1
]
},
"_eulerAngles": {
"__type__": "cc.Vec3",
"x": 0,
"y": 0,
"z": 0
},
"_skewX": 0,
"_skewY": 0,
"_is3DNode": false,
"_groupIndex": 0,
"groupIndex": 0,
"_id": ""
},
{
"__type__": "cc.PrefabInfo",
"root": {
"__id__": 1
},
"asset": {
"__id__": 0
},
"fileId": "",
"sync": false
}
]
\ No newline at end of file
{
"ver": "1.3.2",
"uuid": "5ac08756-721e-492f-893d-0bfa92750f89",
"importer": "prefab",
"optimizationPolicy": "AUTO",
"asyncLoadAssets": false,
"readonly": false,
"subMetas": {}
}
\ No newline at end of file
...@@ -54,8 +54,8 @@ ...@@ -54,8 +54,8 @@
"__type__": "TypedArray", "__type__": "TypedArray",
"ctor": "Float64Array", "ctor": "Float64Array",
"array": [ "array": [
219.967, 0,
119.302, 0,
0, 0,
0, 0,
0, 0,
......
...@@ -57,8 +57,8 @@ ...@@ -57,8 +57,8 @@
"__type__": "TypedArray", "__type__": "TypedArray",
"ctor": "Float64Array", "ctor": "Float64Array",
"array": [ "array": [
-243.227, 0,
138.001, 0,
0, 0,
0, 0,
0, 0,
......
...@@ -54,8 +54,8 @@ ...@@ -54,8 +54,8 @@
"__type__": "TypedArray", "__type__": "TypedArray",
"ctor": "Float64Array", "ctor": "Float64Array",
"array": [ "array": [
-10.36, 0,
134.681, 0,
0, 0,
0, 0,
0, 0,
......
...@@ -54,8 +54,8 @@ ...@@ -54,8 +54,8 @@
"__type__": "TypedArray", "__type__": "TypedArray",
"ctor": "Float64Array", "ctor": "Float64Array",
"array": [ "array": [
248.696, 0,
-138.446, 0,
0, 0,
0, 0,
0, 0,
......
...@@ -54,8 +54,8 @@ ...@@ -54,8 +54,8 @@
"__type__": "TypedArray", "__type__": "TypedArray",
"ctor": "Float64Array", "ctor": "Float64Array",
"array": [ "array": [
-19.134, 0,
-92.25, 0,
0, 0,
0, 0,
0, 0,
......
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