Documentation

On this page

Download full book (text)

# **Chapter 24: Game Components — World, Object, Actor, and Player (Babylon.js)** > **Prerequisites:** Chapters 5, 6, 23 (FrontFiles, Components, lifecycle) > **Try it:** Gate `exgame` > (`apps/examples/ExGame.gate.php`, `apps/examples/fronts/exgame_main.front`), > components under `comp/gameBabylonJs/`. > Open `exgame.html`. --- ## **24.1 Why a Game Pack** `comp/babylonJs` is a **low-level** mesh toolkit: `Scene`, `Box`, `Cylinder`, `Material`, `Light`, `Model`. You wire camera, lights, and controls yourself. `comp/gameBabylonJs` is the **high-level** game pack: a few big tags that build a playable world from Front File attributes alone. Camera and lights are **World settings**, not separate components. The folder naming pattern is `comp/game{Lib}` — this chapter ships `gameBabylonJs`. The same Front API (World / Object / Actor / Player) can later exist as `gameThreeJs` or another engine folder. ## **24.2 The Four Tags** | Tag path | Role | |----------|------| | `comp/gameBabylonJs/World.php` | Canvas, engine, default camera + light + ground, tick loop | | `comp/gameBabylonJs/GameObject.php` | Prop (shape or model); optional `interactive` | | `comp/gameBabylonJs/Actor.php` | NPC / character; optional `{id}_onUpdate` | | `comp/gameBabylonJs/Player.php` | Controllable actor (WASD / arrows) | > **Note:** PHP reserves the class name `Object`, so the Front path is > `GameObject.php`. Conceptually it is still the Object component. Nest Object / Actor / Player **inside** World. World sets globals in `onprejsrender` (same idea as `Scene` + `$currentScene` in chapter 23). ## **24.3 World Defaults (No Camera / Light Components)** With `fur-runThis=""`, World: 1. Emits a full-width canvas and loads Babylon + `gameapi.js` 2. Creates a hemispheric light and a ground plane 3. Uses a **follow** camera when a Player exists (or Arc/Free when you set `cameratype`) 4. Starts the render loop and calls `{worldId}_tick` each frame Useful attributes and fusion methods: ```html <div id="world1" runat="server" path="comp/gameBabylonJs/World.php" fur-runThis="" cameratype="follow" cameraposition="0, 8, -12" ground="1" groundsize="40" fun-setLight="HemisphericLight,|0,1,0,|1" fun-setCamera="arc,|0,10,-14,|0,0,0" fun-addResource="hero,|https://example.com/hero.glb"> </div> ``` - `fun-setLight` / `fun-setCamera` / `fun-setGround` — override defaults from the Front - `fun-addResource="name,|url"` — register a CDN or local URL; children use `resource="name"` ## **24.4 Object, Actor, Player** **GameObject** — shape (`box`, `sphere`, `cylinder`, `capsule`, `plane`) or `model` / `resource` URL. Set `interactive="1"` to fire `{id}_onInteract` on click. **Actor** — same placement/model options; each frame may call `{id}_onUpdate(mesh, world)` if you fill that function. **Player** — `controls="wasd"` or `arrows`, `speed="0.12"`. Space calls `{id}_onAction`. Follow camera locks onto the player mesh. Transforms use fusion like the low-level mesh pack: ```html fur-setPosition="2,0.5,0" fur-setRotation="0,1.2,0" fur-setScaling="1,1,1" ``` ## **24.5 Resources** No separate Resource component — keep the pack small: 1. Procedural shapes always work offline 2. `model="https://…/file.glb"` on Actor / Player / GameObject 3. `fun-addResource` on World, then `resource="hero"` on a child 4. Later, drop files under `comp/gameBabylonJs/resources/` and point `model` at that URL Failed model loads fall back to a box so the demo keeps running. ## **24.6 Zero JS vs Advanced Hooks** **Zero JS:** World + Player + ground is enough to walk around. **Advanced:** inject into empty functions World / children create: ```html <script runas="jsfunctioncode" function="world1_tick"> // per-frame; argument `world` is the SphpGameWorld instance </script> <script runas="jsfunctioncode" function="crate1_onInteract"> // mesh, pick </script> <script runas="jsfunctioncode" function="hero1_onAction"> // mesh </script> <script runas="jsfunctioncode" function="npc1_onUpdate"> // mesh, world </script> ``` The JS world instance is `game_world1` (and `window.SphpGameWorlds.world1`) so it does not collide with the canvas element `id="world1"`. ## **24.7 Full Example (exgame)** ```html <div id="world1" runat="server" path="comp/gameBabylonJs/World.php" fur-runThis="" cameratype="follow" ground="1" fun-setLight="HemisphericLight,|0,1,0,|1.1"> <div id="crate1" runat="server" path="comp/gameBabylonJs/GameObject.php" shape="box" size="1.2" interactive="1" color="0.85,0.45,0.15" fur-setPosition="3,0.6,2"></div> <div id="npc1" runat="server" path="comp/gameBabylonJs/Actor.php" shape="sphere" size="1" color="0.85,0.2,0.25" fur-setPosition="-3,0.5,1"></div> <div id="hero1" runat="server" path="comp/gameBabylonJs/Player.php" shape="capsule" controls="wasd" speed="0.14" fur-setPosition="0,0,0"></div> </div> ``` Open `exgame.html`, click the crate, move with WASD, press Space for the player action. ## **24.8 Village Battle Example** `exgamebattle` builds on the same tags with `village="1"` on World: a larger map with a dirt lane, **broken walls**, and **enterable houses** (door gaps + optional rear exits). The Player and enemies use a real human GLB: `model="comp/gameBabylonJs/resources/Soldier.glb"` (Idle / Walk animations). Space → `world.shoot()` from the Player action hook. Collisions block solid walls but allow walking through doorways. Open `exgamebattle.html` — WASD to move, Space to fire, step into houses for cover, clear hostiles on the lane. ## **24.9 Next Steps** - Load your own glTF/GLB with `model` or `fun-addResource` - Override camera with `cameratype="arc"` or `fun-setCamera` - Use low-level `comp/babylonJs` when you need raw mesh/material control - Mirror the same four tags under another `comp/game{Lib}` folder for a different engine --- **Summary:** Nest GameObject, Actor, and Player inside World. Configure camera and light on World. Play without writing JS; extend with `runas="jsfunctioncode"` when you need custom behavior.