nomadro/apps/minigame-cocos/assets/scripts/bootstrap/AutoSceneSetup.ts
Eric 58a845db32 Add WeChat minigame triple-engine scaffold with Godot Chiang Mai plaza SoT.
Unity and Cocos stay as parallel schemes; apps/minigame ships the Godot 4.7 WeChat pack for DevTools.

Co-authored-by: Cursor <cursoragent@cursor.com>
2026-11-06 20:17:14 +08:00

195 lines
5.8 KiB
TypeScript
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import {
_decorator,
Component,
Node,
Camera,
Color,
DirectionalLight,
Canvas,
UITransform,
Label,
Widget,
ProgressBar,
Sprite,
SpriteFrame,
Texture2D,
ImageAsset,
view,
ResolutionPolicy,
Vec3,
MeshRenderer,
primitives,
utils,
Material,
BlockInputEvents,
} from 'cc'
import { GameRoot } from './GameRoot'
import { PlayerMotor } from '../player/PlayerMotor'
import { CameraFollow } from '../player/CameraFollow'
import { WorldBootstrap } from '../scene/WorldBootstrap'
import { HudView } from '../ui/HudView'
import { VirtualJoystick } from '../ui/VirtualJoystick'
import { CITY } from '../data/CatalogSeed'
const { ccclass } = _decorator
function solidSprite(r: number, g: number, b: number, a = 180): SpriteFrame {
const data = new Uint8Array([r, g, b, a])
const img = new ImageAsset()
img.reset({
_data: data,
width: 1,
height: 1,
format: Texture2D.PixelFormat.RGBA8888,
_compressed: false,
} as never)
const tex = new Texture2D()
tex.image = img
const sf = new SpriteFrame()
sf.texture = tex
return sf
}
function unlit(hex: string): Material {
const h = hex.replace('#', '')
const n = parseInt(h, 16)
const m = new Material()
m.initialize({ effectName: 'builtin-unlit' })
m.setProperty(
'mainColor',
new Color((n >> 16) & 255, (n >> 8) & 255, n & 255, 255),
)
return m
}
/**
* Drop on an empty scene → builds player / camera / lights / HUD / GameRoot.
* Portrait WeChat-friendly 750×1334 design size.
*/
@ccclass('AutoSceneSetup')
export class AutoSceneSetup extends Component {
/** onLoad before GameRoot.start */
onLoad() {
if (this.node.getChildByName('Player')) {
this.enabled = false
return
}
view.setDesignResolutionSize(750, 1334, ResolutionPolicy.FIXED_WIDTH)
const worldRoot = new Node('World')
this.node.addChild(worldRoot)
const sunNode = new Node('Sun')
this.node.addChild(sunNode)
sunNode.setPosition(12, 22, 8)
sunNode.lookAt(new Vec3(0, 0, 0))
const sun = sunNode.addComponent(DirectionalLight)
sun.illuminance = 42000
sun.color = new Color(255, 244, 220)
const player = new Node('Player')
this.node.addChild(player)
player.setPosition(CITY.spawn.x, CITY.spawn.y, CITY.spawn.z)
const body = new Node('Body')
player.addChild(body)
body.setScale(0.7, 1.5, 0.7)
body.setPosition(0, 0.75, 0)
const mr = body.addComponent(MeshRenderer)
mr.mesh = utils.MeshUtils.createMesh(primitives.capsule())
mr.setMaterial(unlit('#f0a060'), 0)
const motor = player.addComponent(PlayerMotor)
motor.boundRadius = CITY.boundRadius
const camNode = new Node('MainCamera')
this.node.addChild(camNode)
const cam = camNode.addComponent(Camera)
cam.fov = 50
cam.clearColor = new Color(140, 200, 242, 255)
const follow = camNode.addComponent(CameraFollow)
follow.target = player
follow.motor = motor
const world = worldRoot.addComponent(WorldBootstrap)
world.worldRoot = worldRoot
world.sun = sun
const canvasNode = new Node('Canvas')
this.node.addChild(canvasNode)
const canvas = canvasNode.addComponent(Canvas)
canvasNode.addComponent(UITransform).setContentSize(750, 1334)
const widget = canvasNode.addComponent(Widget)
widget.isAlignTop = widget.isAlignBottom = widget.isAlignLeft = widget.isAlignRight = true
widget.top = widget.bottom = widget.left = widget.right = 0
const joyNode = new Node('JoystickZone')
canvasNode.addChild(joyNode)
joyNode.addComponent(UITransform).setContentSize(750, 1334)
joyNode.addComponent(BlockInputEvents)
const joyWidget = joyNode.addComponent(Widget)
joyWidget.isAlignTop = joyWidget.isAlignBottom = joyWidget.isAlignLeft = joyWidget.isAlignRight = true
joyWidget.top = joyWidget.bottom = joyWidget.left = joyWidget.right = 0
const knob = new Node('Knob')
joyNode.addChild(knob)
knob.setPosition(-220, -420, 0)
const knobUi = knob.addComponent(UITransform)
knobUi.setContentSize(56, 56)
const knobSp = knob.addComponent(Sprite)
knobSp.spriteFrame = solidSprite(255, 200, 80, 200)
const joy = joyNode.addComponent(VirtualJoystick)
joy.stickKnob = knob
joy.maxRadius = 72
const hudNode = new Node('HUD')
canvasNode.addChild(hudNode)
const hud = hudNode.addComponent(HudView)
hud.titleLabel = this.mkLabel(hudNode, 'Title', '数字游民', 28, 0, 580)
hud.timeLabel = this.mkLabel(hudNode, 'Time', 'D1 07:10', 22, 0, 540)
hud.cashLabel = this.mkLabel(hudNode, 'Cash', '¥1200', 24, -260, 580)
hud.hintLabel = this.mkLabel(hudNode, 'Hint', '探索城市', 20, 0, -520)
hud.interactLabel = this.mkLabel(hudNode, 'Interact', '互动', 22, 220, -420)
if (hud.interactLabel) hud.interactLabel.node.active = false
const barNode = new Node('Energy')
hudNode.addChild(barNode)
barNode.setPosition(0, 500, 0)
barNode.addComponent(UITransform).setContentSize(280, 18)
const bar = barNode.addComponent(ProgressBar)
bar.totalLength = 280
bar.progress = 0.8
hud.energyBar = bar
void canvas
const root = this.node.getComponent(GameRoot) || this.node.addComponent(GameRoot)
root.player = motor
root.cameraFollow = follow
root.world = world
root.hud = hud
root.joystick = joy
root.playerNode = player
// AutoSceneSetup only scaffolds once; disable to avoid rebuild
this.enabled = false
}
private mkLabel(
parent: Node,
name: string,
text: string,
size: number,
x: number,
y: number,
): Label {
const n = new Node(name)
parent.addChild(n)
n.setPosition(x, y, 0)
n.addComponent(UITransform).setContentSize(700, 40)
const lab = n.addComponent(Label)
lab.string = text
lab.fontSize = size
lab.color = new Color(255, 255, 255, 255)
lab.overflow = Label.Overflow.SHRINK
return lab
}
}