mirror of
https://gitee.com/ruanwujing/green-pack-cocos
synced 2025-10-09 16:46:17 +00:00
简单随机地穴示例
This commit is contained in:
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": {}
|
||||
}
|
Reference in New Issue
Block a user