Skip to content

code-conventions-cocos

Code Conventions — Cocos Creator / TypeScript

Section titled “Code Conventions — Cocos Creator / TypeScript”

Extends core code-conventions.md. The1Studio-specific patterns.

  • Classes: PascalCase with semantic suffix — Service, Component, Controller
  • Private fields: _camelCase (underscore prefix — standard TS convention)
  • Enums: PascalCase names, lowercase string values (BROWN = 'brown') for serialization
  • Interfaces: PascalCase, NO I prefix (modern TS style, not Java/C#)
  • Callbacks: on* or handle* prefix — onWeaponPlaced, handleTimerEnd
  • Constants: UPPER_SNAKE_CASE in config objects
  • Files: {Feature}{Suffix}.tsColorService.ts, TimerComponent.ts
  • Service: business logic, singleton — reusable utilities
  • Component: UI, extends Component — scene-attached
  • Controller: orchestration, state management
  • Singleton: private static _instance + static get instance(), set in onLoad()
  • Use signalBus.subscribe/unsubscribe() for inter-component events — NOT direct calls
  • MUST use named methods (arrow function class fields), NOT inline lambdas
  • Reason: unsubscribe() uses indexOf() — lambdas create new refs each call
  • Unsubscribe in onDestroy() — always clean up
  • @ccclass('ClassName') on every component class
  • @property(Type) for editor-exposed fields only
  • Destructure at top: const { ccclass, property } = _decorator;
  • Guard clauses for null safety — early return, not nested conditions
  • readonly for config values — prevents runtime mutation
  • Extract magic numbers to GameConfig or local const
  • Lifecycle order: onLoadonEnablestartupdateonDestroy
assets/scripts/{ProjectName}/
├── services/ # Business logic singletons
├── parameter/ # Config management
├── UI/ # UI components and controllers
├── utils/ # Static utility classes
├── constant/ # Named constants
├── Data/ # Models, enums, configs
└── Signal/ # Signal class definitions + shared signalBus export

If unsure about a convention not covered here, ask the user for their preference and update this file with the answer. Conventions grow from real decisions.