> ## Documentation Index
> Fetch the complete documentation index at: https://docs.summerengine.com/llms.txt
> Use this file to discover all available pages before exploring further.

# CraftyGame

> Required base class that connects your game to the Crafty platform lifecycle.

## What Is `CraftyGame`?

Every game published on Crafty must extend `CraftyGame`.

`CraftyGame` is the contract between your game and the platform:

* game lifecycle,
* player join/leave callbacks,
* timer/match control helpers,
* server-authoritative movement helper for 3D templates.

```gdscript theme={null}
extends CraftyGame
```

## Required Lifecycle Hooks

Override these in your game script:

```gdscript theme={null}
func _game_init() -> void:
    pass

func _game_start() -> void:
    pass

func _game_end() -> void:
    pass

func _player_joined(player) -> void:
    pass

func _player_left(player) -> void:
    pass
```

Use untyped `player` for maximum compatibility across:

* `CraftyCharacter3D` players (3D action template),
* custom `CraftyPlayer` subclasses (2D/RTS/card/turn-based).

## Common Platform Methods On `CraftyGame`

```gdscript theme={null}
end_game(from_timer: bool = false) -> void
set_time_limit(seconds: float) -> void
get_time_remaining() -> float
get_players() -> Array
get_player_count() -> int
get_max_players() -> int
get_random_spawn_point() -> Vector3
apply_default_movement(player, delta, move_speed := 7.0, gravity := 20.0) -> void
```

## Hook Responsibilities

* `_game_init()`: one-time setup (spawn points, static data, signal wiring).
* `_game_start()`: begin active match flow.
* `_game_end()`: finalize and cleanup.
* `_player_joined(player)`: attach defaults for a newly connected player.
* `_player_left(player)`: cleanup per-player transient state.

## Server-Authoritative Pattern

```gdscript theme={null}
func _process(delta: float) -> void:
    super._process(delta)
    if not Crafty.is_server():
        return
    # authoritative gameplay logic here
```

## 3D Template Pattern (Optional)

If your player scene uses `CraftyCharacter3D`, you can use the built-in movement helper:

```gdscript theme={null}
const MOVE_SPEED := 8.0
const GRAVITY := 20.0

func _process(delta: float) -> void:
    super._process(delta)
    if not Crafty.is_server():
        return
    for p in get_players():
        apply_default_movement(p, delta, MOVE_SPEED, GRAVITY)
```

For non-3D games, ignore `apply_default_movement` and implement your own state model with `set_synced`.

## Minimal Example

```gdscript theme={null}
extends CraftyGame

func _game_start() -> void:
    Crafty.set_time_limit(180.0)
    Crafty.send_announcement("Match started")

func _player_joined(player) -> void:
    player.set_synced("score", 0)
```
