<!-- skill: Defold 2D | version: 1.0.0 | description: Defold collections messaging atlases and HTML5 mobile bundles | category: Games -->
## Instructions
- Use Defold 1.9+ with collections as levels; keep bootstrap collection small, load others via collection proxies.
- Model everything as game object + components (sprite, collision, script); avoid deep hierarchies over 3 levels.
- Communicate only via msg.post() with hashed ids; never reach directly into another object's lua state.
- Store shared state in Lua modules + go.property(); use save/load via sys.save/sys.load for persistence.
- Build atlases for sprites and tilesources for tilemaps; keep max texture 2048px for mobile/HTML5.
- Design input with input bindings (keyboard+touch+gamepad); test touch triggers as well as mouse.
- Handle camera via render script or Orthographic camera asset; fix 960x640 base with stretch projection.
- Bundle with Bob + game.project variants; set separate HTML5, Android, iOS icons/splash.
- Optimize draw calls by batching same-atlas sprites; keep physics 2D box shapes, avoid concave polygons.
- Debug with web profiler (port 8002) and console; check lua memory with resource profiler before release.
- Version dependencies via .lib URLs pinned to release tags; document in game.project readme section.
- Publish HTML5 with custom shell + loading screen; test on itch.io iframe 1280x720 before launch.

## Examples
- Example 1: "player takes damage" -> msg.post("/player#script","take_damage",{amount=10}) handled in on_message.
- Example 2: "load level 2" -> msg.post("#proxy","load") then enable on proxy_loaded; unload prior collection.
- Example 3: "animate sprite" -> flipbook play via sprite.play_flipbook("#sprite","run") on movement state change.

## Workflows
- New project workflow: Setup input bindings -> create main.collection -> add player.go -> add tilesource level -> add HUD gui.
- Messaging workflow: Define message id hash -> post from sender -> handle on_message -> ack back if needed -> log unknown.
- Atlas workflow: Import PNGs -> create .atlas with margin 2px -> assign to sprite -> set slice9 for UI -> repack.
- Bundle workflow: Set version in game.project -> Bundle HTML5 -> test wasm locally -> Bundle Android APK/AAB -> sign.

## Templates
- on_message handler:
```lua
function on_message(self, message_id, message, sender)
  if message_id == hash("take_damage") then
    self.hp = self.hp - message.amount
    if self.hp <= 0 then msg.post("#", hash("die")) end
  elseif message_id == hash("die") then
    go.delete()
  end
end
```
- game.project checklist:
```
[project] title, version
[display] width=960 height=640 high_dpi=1
[html5] heap=256 custom_heap=1
[android] immersive=1
```
- Collection proxy loader snippet + save snippet with sys.save("slot1",{hp=self.hp,level=2}).
- Release checklist: [ ] icons [ ] bundle id [ ] profiler clean [ ] HTML5 tested.

## Tools
- Engine: Defold Editor 2, Bob bundler, web profiler, Debugger (mobdebug).
- Assets: Aseprite export, Tiled -> Defold tilesource converter, TexturePacker fallback.
- Extensions: Orthographic camera, DefOS, GameAnalytics, IAP + Push via Defold portal.
- Hosting: itch.io, Poki, CrazyGames HTML5 targets; Fastlane for mobile signing.

## Advanced
- Use collection factories for pooling bullets; pre-warm 50 instances to avoid hitches.
- Implement custom render script for daylight/night blend + GUI stencil clipping.
- Use LiveUpdate + CDN for >50MB HTML5 games to stay under portal limits.
- Write native extensions in C for heavy pathfinding; bridge via dmScript calls.
- Add deterministic fixed-timestep accumulator in update() for netcode-ready logic.

## Documentation
- Defold manuals: https://defold.com/manuals/introduction/
- Message passing: https://defold.com/manuals/message-passing/
- HTML5 manual: https://defold.com/manuals/html5/
- Asset portal: https://www.defold.com/assets/
