# Efail 3D: making a game

Efail 3D is a deterministic low-poly 3D game engine written in Bend. A game is
one `game.bend` file of pure functions. The engine owns the loop, renderer,
input, screenshots, simulation, replays, and basic physics.

## The workflow

```sh
efail3d check
efail3d sim --ticks 300 --every 60
efail3d shot --ticks 120                 # build/shot.png
efail3d run                              # Esc quits and records a replay
efail3d replay --every 30
efail3d bench --input keys.txt           # render every scripted frame
```

Input scripts and replay files use the same format documented in the included
2D engine guide: a tick followed by held keys, plus an optional seed and end.

## The game contract

```python
import Base
import ./efail/efail.bend as E
import ./efail/efail3d.bend as E3

def title() -> String
def width() -> U32
def height() -> U32

type State is Data:
  ...

def init(seed: U32) -> State
def step(+input: E.Input, state: State) -> State
def scene(state: State) -> E3.Scene
def show(state: State) -> String
```

`step` runs at a fixed 60 ticks per second. `scene` describes the current
picture. `show` returns the one-line state used by headless simulation.

## Coordinates

- X points right, Y points up, and Z points forward.
- A camera with zero rotation looks along positive Z.
- Positions, sizes, velocities, and angles are `F32`; angles use radians.
- Screen coordinates begin at the top left, so projection flips Y.
- Raw triangle vertices use clockwise winding when viewed from the visible
  side. Built-in meshes already have the correct winding.

## Vectors and transforms

```python
E3.vec3(x, y, z)
E3.Vec3.zero()
E3.Vec3.add(a, b)
E3.Vec3.sub(a, b)
E3.Vec3.scale(v, factor)
E3.Vec3.dot(a, b)
E3.Vec3.cross(a, b)
E3.Vec3.length(v)
E3.Vec3.normalize(v)
E3.Vec3.rotate_x(v, angle)  # also rotate_y and rotate_z

E3.transform(position, rotation, scale)
E3.Transform.at(position)
E3.Transform.identity()
```

Rotation order is X, then Y, then Z.

## Cameras

```python
E3.camera(position, yaw, pitch, fov, near)
E3.Camera.look_at(position, target, fov, near)
E3.Camera.move(camera, right, up, forward)
E3.Camera.turn(camera, yaw_by, pitch_by)
E3.Camera.with_fov(camera, fov)
E3.Camera.forward(camera)
E3.Camera.right(camera)
E3.Camera.select(first, a, b)
```

The ready-made deterministic controls are:

```python
E3.Camera.controls(input, camera, move_speed, turn_speed)
```

WASD moves, Space/Shift rises and falls, and the arrow keys turn. Speeds are
units and radians per fixed tick.

## Scenes, objects, and lighting

```python
E3.Mesh.cube(size, color)
E3.Mesh.quad(size, color)
E3.Mesh.plane(size, color)
E3.mesh(triangles)

E3.object(mesh, transform)
E3.directional(direction, ambient)
E3.scene(camera, objects, light, background)
```

Example:

```python
def scene(state: State) -> E3.Scene:
  State{camera, angle} = state
  E3.scene(camera, [
    E3.object(E3.Mesh.cube(2.0, E.orange()),
      E3.transform(E3.vec3(0.0, 0.0, 5.0),
        E3.vec3(0.0, angle, 0.0), E3.vec3(1.0, 1.0, 1.0))),
    E3.object(E3.Mesh.plane(12.0, E.gray()),
      E3.Transform.at(E3.vec3(0.0, (0.0 - 1.0 : F32), 5.0)))
  ], E3.directional(E3.vec3(0.3, 0.8, 1.0), 0.25), E.navy())
```

Lighting is flat directional light plus an ambient minimum. Colors use the 2D
engine's `E.rgb` and named colors.

## Basic physics

Physics is deterministic and fixed-tick. Bodies have a center position,
velocity, collider, and grounded flag.

```python
E3.box_collider(half_size)
E3.sphere_collider(radius)
E3.body(position, velocity, collider)
E3.static_body(position, collider)

E3.Body.step(moving_body, static_bodies, gravity_per_tick)
E3.Body.overlaps(a, b)
E3.Body.position(body)
E3.Body.velocity(body)
E3.Body.grounded(body)
E3.Body.with_velocity(body, velocity)

E3.physics(moving_bodies, static_bodies, gravity)
E3.Physics.step(world)
E3.raycast(origin, direction, bodies)
```

`Body.step` applies gravity, then resolves X, Y, and Z movement against static
box or sphere bodies. A downward collision sets `grounded` and clears vertical
velocity. Raycasts return `E3.RayMiss{}` or `E3.RayHit{distance, point}`.

This is intentionally basic gameplay physics: no joints, rotation dynamics,
soft bodies, or triangle-mesh colliders.

## Input

Use the core engine API:

```python
E.held(input, key)
E.pressed(input, key)
E.axis(input, negative_key, positive_key)
E.tick(input)
E.mouse_x(input)
E.mouse_y(input)
```

Keys include `E.left()`, `right()`, `up()`, `down()`, `space()`, `enter()`,
`shift()`, and `E.key('w')`. Input is recorded and replayed exactly.

## What the renderer handles

The engine transforms, lights, clips, projects, culls, bins, and depth-tests
triangles. Geometry crossing the near plane is clipped. Backfaces and
off-screen regions are removed. Triangle list order does not affect depth.

The MVP renders flat colors. It does not yet include textures, model importing,
shadows, skeletal animation, advanced materials, or a visual editor.

## Bend rules that matter

- Every def must appear before it is used.
- There is no `if`; match a `Bool` parameter in a helper.
- Match and destructure parameters or pattern fields, not computed values.
- Mark reused values with `+`.
- Operators need spaces and a type annotation: `(a + b : F32)`.
- There are no negative literals: `(0.0 - 1.0 : F32)`.
- State lists use `List<&2, T>` so the state remains `Data`.
- Keep all game logic pure. Randomness comes from a seed carried in State.

Run `efail3d check` after every edit. Use `sim` to inspect state and `shot` to
inspect pixels before trying the game interactively.
