mirror of
https://gitee.com/ruanwujing/green-pack-cocos
synced 2024-12-24 10:48:38 +00:00
简单随机地穴示例
This commit is contained in:
parent
c9a7906dfd
commit
87d7bbf3b8
@ -484,6 +484,29 @@
|
||||
"_isUnderline": false,
|
||||
"_underlineHeight": 2,
|
||||
"_cacheMode": 0,
|
||||
"_enableOutline": false,
|
||||
"_outlineColor": {
|
||||
"__type__": "cc.Color",
|
||||
"r": 0,
|
||||
"g": 0,
|
||||
"b": 0,
|
||||
"a": 255
|
||||
},
|
||||
"_outlineWidth": 2,
|
||||
"_enableShadow": false,
|
||||
"_shadowColor": {
|
||||
"__type__": "cc.Color",
|
||||
"r": 0,
|
||||
"g": 0,
|
||||
"b": 0,
|
||||
"a": 255
|
||||
},
|
||||
"_shadowOffset": {
|
||||
"__type__": "cc.Vec2",
|
||||
"x": 2,
|
||||
"y": 2
|
||||
},
|
||||
"_shadowBlur": 2,
|
||||
"_id": "49Mt6u3ylAhIjwUjPL/7hO"
|
||||
},
|
||||
{
|
||||
@ -728,6 +751,29 @@
|
||||
"_isUnderline": false,
|
||||
"_underlineHeight": 2,
|
||||
"_cacheMode": 0,
|
||||
"_enableOutline": false,
|
||||
"_outlineColor": {
|
||||
"__type__": "cc.Color",
|
||||
"r": 0,
|
||||
"g": 0,
|
||||
"b": 0,
|
||||
"a": 255
|
||||
},
|
||||
"_outlineWidth": 2,
|
||||
"_enableShadow": false,
|
||||
"_shadowColor": {
|
||||
"__type__": "cc.Color",
|
||||
"r": 0,
|
||||
"g": 0,
|
||||
"b": 0,
|
||||
"a": 255
|
||||
},
|
||||
"_shadowOffset": {
|
||||
"__type__": "cc.Vec2",
|
||||
"x": 2,
|
||||
"y": 2
|
||||
},
|
||||
"_shadowBlur": 2,
|
||||
"_id": "28WdAu1Z5MH72mVIZBx9Zf"
|
||||
},
|
||||
{
|
||||
@ -972,6 +1018,29 @@
|
||||
"_isUnderline": false,
|
||||
"_underlineHeight": 2,
|
||||
"_cacheMode": 0,
|
||||
"_enableOutline": false,
|
||||
"_outlineColor": {
|
||||
"__type__": "cc.Color",
|
||||
"r": 0,
|
||||
"g": 0,
|
||||
"b": 0,
|
||||
"a": 255
|
||||
},
|
||||
"_outlineWidth": 2,
|
||||
"_enableShadow": false,
|
||||
"_shadowColor": {
|
||||
"__type__": "cc.Color",
|
||||
"r": 0,
|
||||
"g": 0,
|
||||
"b": 0,
|
||||
"a": 255
|
||||
},
|
||||
"_shadowOffset": {
|
||||
"__type__": "cc.Vec2",
|
||||
"x": 2,
|
||||
"y": 2
|
||||
},
|
||||
"_shadowBlur": 2,
|
||||
"_id": "3788BizyZKEKxXNzRYnqkE"
|
||||
},
|
||||
{
|
||||
@ -1219,6 +1288,29 @@
|
||||
"_isUnderline": false,
|
||||
"_underlineHeight": 2,
|
||||
"_cacheMode": 0,
|
||||
"_enableOutline": false,
|
||||
"_outlineColor": {
|
||||
"__type__": "cc.Color",
|
||||
"r": 0,
|
||||
"g": 0,
|
||||
"b": 0,
|
||||
"a": 255
|
||||
},
|
||||
"_outlineWidth": 2,
|
||||
"_enableShadow": false,
|
||||
"_shadowColor": {
|
||||
"__type__": "cc.Color",
|
||||
"r": 0,
|
||||
"g": 0,
|
||||
"b": 0,
|
||||
"a": 255
|
||||
},
|
||||
"_shadowOffset": {
|
||||
"__type__": "cc.Vec2",
|
||||
"x": 2,
|
||||
"y": 2
|
||||
},
|
||||
"_shadowBlur": 2,
|
||||
"_id": "adiMFX1khPFoPqgS1WlUy4"
|
||||
},
|
||||
{
|
||||
@ -1475,6 +1567,29 @@
|
||||
"_isUnderline": false,
|
||||
"_underlineHeight": 2,
|
||||
"_cacheMode": 0,
|
||||
"_enableOutline": false,
|
||||
"_outlineColor": {
|
||||
"__type__": "cc.Color",
|
||||
"r": 0,
|
||||
"g": 0,
|
||||
"b": 0,
|
||||
"a": 255
|
||||
},
|
||||
"_outlineWidth": 2,
|
||||
"_enableShadow": false,
|
||||
"_shadowColor": {
|
||||
"__type__": "cc.Color",
|
||||
"r": 0,
|
||||
"g": 0,
|
||||
"b": 0,
|
||||
"a": 255
|
||||
},
|
||||
"_shadowOffset": {
|
||||
"__type__": "cc.Vec2",
|
||||
"x": 2,
|
||||
"y": 2
|
||||
},
|
||||
"_shadowBlur": 2,
|
||||
"_id": "46OSFJAOlLa5icud6LYf4t"
|
||||
},
|
||||
{
|
||||
@ -2057,6 +2172,7 @@
|
||||
"z": 0
|
||||
},
|
||||
"_distance": 0,
|
||||
"_planeBias": 1,
|
||||
"_shadowColor": {
|
||||
"__type__": "cc.Color",
|
||||
"r": 76,
|
||||
|
@ -1,5 +1,5 @@
|
||||
{
|
||||
"ver": "1.1.49",
|
||||
"ver": "1.1.50",
|
||||
"importer": "scene",
|
||||
"imported": true,
|
||||
"uuid": "3fd7ab19-8a50-470c-8dc7-0cd664f0fb5b",
|
||||
|
1415
assets/resources/scenes/randomCave.scene
Normal file
1415
assets/resources/scenes/randomCave.scene
Normal file
File diff suppressed because it is too large
Load Diff
11
assets/resources/scenes/randomCave.scene.meta
Normal file
11
assets/resources/scenes/randomCave.scene.meta
Normal file
@ -0,0 +1,11 @@
|
||||
{
|
||||
"ver": "1.1.50",
|
||||
"importer": "scene",
|
||||
"imported": true,
|
||||
"uuid": "310fd3ed-a1d3-4adb-8766-8b82bb6508c1",
|
||||
"files": [
|
||||
".json"
|
||||
],
|
||||
"subMetas": {},
|
||||
"userData": {}
|
||||
}
|
@ -1,5 +1,5 @@
|
||||
{
|
||||
"ver": "1.1.49",
|
||||
"ver": "1.1.50",
|
||||
"importer": "scene",
|
||||
"imported": true,
|
||||
"uuid": "64bd0402-59ce-4670-ae7d-24b450f09bb9",
|
||||
|
@ -1,5 +1,5 @@
|
||||
{
|
||||
"ver": "1.1.49",
|
||||
"ver": "1.1.50",
|
||||
"importer": "scene",
|
||||
"imported": true,
|
||||
"uuid": "2d052f05-f3ae-4ade-a629-7dc0680a169b",
|
||||
|
@ -1,5 +1,5 @@
|
||||
{
|
||||
"ver": "1.1.49",
|
||||
"ver": "1.1.50",
|
||||
"importer": "scene",
|
||||
"imported": true,
|
||||
"uuid": "d3d4c22a-8da8-4e57-94c4-1cfa29c09130",
|
||||
|
@ -26,10 +26,10 @@ export enum EasingString {
|
||||
}
|
||||
@ccclass('GPDrag')
|
||||
export class GPDrag extends Component {
|
||||
@property({type:CCBoolean, tooltip:"触碰点偏移量敏感"})
|
||||
@property({tooltip:"触碰点偏移量敏感"})
|
||||
public touchOffsetSensitive = true
|
||||
|
||||
@property({type:CCBoolean, tooltip:"失败时回到起始位置"})
|
||||
@property({tooltip:"失败时回到起始位置"})
|
||||
public backHomeWhenFailed = true
|
||||
|
||||
@property({type:Enum(DragBackHomeType), tooltip:"回家的方式", visible:function(this){
|
||||
@ -56,7 +56,7 @@ export class GPDrag extends Component {
|
||||
}})
|
||||
public topLayerNode:Node
|
||||
|
||||
@property({type:CCBoolean, tooltip:"松开后返回原来的层级", visible:function(){
|
||||
@property({tooltip:"松开后返回原来的层级", visible:function(){
|
||||
return this.floatType != FloatType.None
|
||||
}})
|
||||
public backToOriZ = true;
|
||||
|
9
assets/scripts/randomCave.meta
Normal file
9
assets/scripts/randomCave.meta
Normal file
@ -0,0 +1,9 @@
|
||||
{
|
||||
"ver": "1.2.0",
|
||||
"importer": "directory",
|
||||
"imported": true,
|
||||
"uuid": "a7782b29-017e-412b-9975-3e3c4adc9caa",
|
||||
"files": [],
|
||||
"subMetas": {},
|
||||
"userData": {}
|
||||
}
|
48
assets/scripts/randomCave/RandomCaveGenerator.ts
Normal file
48
assets/scripts/randomCave/RandomCaveGenerator.ts
Normal file
@ -0,0 +1,48 @@
|
||||
import { _decorator, Component, Node } from 'cc';
|
||||
const { ccclass, property } = _decorator;
|
||||
|
||||
export class RandomCaveGenerator {
|
||||
public columns = 30
|
||||
public rows = 35
|
||||
public data:boolean[] = []
|
||||
private cache:boolean[] = []
|
||||
|
||||
public initRate = 0.5;
|
||||
public init() {
|
||||
for (let i = 0; i < this.columns * this.rows; i++) {
|
||||
this.data[i] = Math.random() < this.initRate;
|
||||
}
|
||||
}
|
||||
public isWall(c: number, r: number) {
|
||||
let idx = c + r * this.columns
|
||||
if (idx < 0 || idx >= this.columns * this.rows)
|
||||
return true
|
||||
return this.data[idx]
|
||||
}
|
||||
public count(c: number, r: number, n: number) {
|
||||
let count = 0;
|
||||
for (let i = -n; i <= n; i++) {
|
||||
for (let j = -n; j <= n; j++) {
|
||||
if (this.isWall(c + i, r + j)) {
|
||||
count++
|
||||
}
|
||||
}
|
||||
}
|
||||
return count
|
||||
}
|
||||
public setCache(c:number, r:number, b:boolean) {
|
||||
this.cache[c + r * this.columns] = b;
|
||||
}
|
||||
public step() {
|
||||
for (let c = 0; c < this.columns; c++) {
|
||||
for (let r = 0; r < this.rows; r++) {
|
||||
let count = this.count(c, r, 1)
|
||||
let count2 = this.count(c, r, 2)
|
||||
this.setCache(c, r, count >= 5 || count2 <= 2)
|
||||
}
|
||||
}
|
||||
this.data = Array.from(this.cache)
|
||||
}
|
||||
}
|
||||
|
||||
|
9
assets/scripts/randomCave/RandomCaveGenerator.ts.meta
Normal file
9
assets/scripts/randomCave/RandomCaveGenerator.ts.meta
Normal file
@ -0,0 +1,9 @@
|
||||
{
|
||||
"ver": "4.0.23",
|
||||
"importer": "typescript",
|
||||
"imported": true,
|
||||
"uuid": "e4eccc10-5bc2-4921-9ab7-69896f59e3bf",
|
||||
"files": [],
|
||||
"subMetas": {},
|
||||
"userData": {}
|
||||
}
|
49
assets/scripts/randomCave/randomCaveExample.ts
Normal file
49
assets/scripts/randomCave/randomCaveExample.ts
Normal file
@ -0,0 +1,49 @@
|
||||
import { _decorator, Color, Component, instantiate, Node, Sprite, v3 } from 'cc';
|
||||
import { RandomCaveGenerator } from './RandomCaveGenerator';
|
||||
const { ccclass, property } = _decorator;
|
||||
|
||||
@ccclass('randomCaveExample')
|
||||
export class randomCaveExample extends Component {
|
||||
@property({type:Node})
|
||||
public cell:Node
|
||||
private generator = new RandomCaveGenerator()
|
||||
private nodes:Node[] = []
|
||||
start() {
|
||||
this.generator.init();
|
||||
let startX = -this.generator.columns / 2 * 10
|
||||
let startY = -this.generator.rows / 2 * 10
|
||||
for (let i = 0; i < this.generator.columns; i++) {
|
||||
for (let j = 0; j < this.generator.rows; j++) {
|
||||
let node = instantiate(this.cell);
|
||||
this.node.addChild(node)
|
||||
node.setPosition(v3(i * 10 + startX, j * 10 + startY, 0))
|
||||
if (this.generator.isWall(i, j))
|
||||
node.getComponent(Sprite).color = Color.GRAY
|
||||
this.nodes[i + j * this.generator.rows] = node;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public randomInit() {
|
||||
this.generator.init();
|
||||
this.apply()
|
||||
}
|
||||
|
||||
public step() {
|
||||
this.generator.step();
|
||||
this.apply()
|
||||
}
|
||||
private apply() {
|
||||
let startX = -this.generator.columns / 2 * 10
|
||||
let startY = -this.generator.rows / 2 * 10
|
||||
for (let i = 0; i < this.generator.columns; i++) {
|
||||
for (let j = 0; j < this.generator.rows; j++) {
|
||||
let node = this.nodes[i + j * this.generator.rows]
|
||||
node.setPosition(v3(i * 10 + startX, j * 10 + startY, 0))
|
||||
node.getComponent(Sprite).color = this.generator.isWall(i, j) ? Color.GRAY: Color.WHITE
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
9
assets/scripts/randomCave/randomCaveExample.ts.meta
Normal file
9
assets/scripts/randomCave/randomCaveExample.ts.meta
Normal file
@ -0,0 +1,9 @@
|
||||
{
|
||||
"ver": "4.0.23",
|
||||
"importer": "typescript",
|
||||
"imported": true,
|
||||
"uuid": "4a0c2960-68a9-4277-a862-a94646e58a34",
|
||||
"files": [],
|
||||
"subMetas": {},
|
||||
"userData": {}
|
||||
}
|
Loading…
Reference in New Issue
Block a user