> For the complete documentation index, see [llms.txt](https://docs.allout.game/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.allout.game/all-out-docs/docs-zh/jiao-ben-bian-xie/player-model.md). Agents can also search and read this documentation through its [MCP server](https://docs.allout.game/~gitbook/mcp).

# 添加玩家逻辑

存储每位玩家的状态、检查执行上下文、配置朝向和信任等级，并管理 Player\_Base 上计数型状态原因。

你的 `Player :: class : Player_Base` 是管理每位玩家状态的核心对象，包括生命值、装备配置、进度、HUD 和摄像机行为。始终将玩家专属状态存储在 `Player` 上，而不是存储在全局变量中，这样多个已连接的玩家之间就不会发生冲突。

```go
Player :: class : Player_Base {
    health: int = 100;
    inventory_open: bool;
}
```

## 共享游戏逻辑与本地检查

游戏逻辑会在预测客户端和权威服务器上运行——不要使用 `Game.is_server()`.

* **`is_local_or_server()`:** 将所有玩家 UI（包括 UIDocs、商店和摄像机更新）放在此处的 `Player.ao_late_update`.
* **`is_local()`:** 仅用于客户端专属的外观效果覆写（切勿在其中修改同步的游戏字段， `is_local()`因为下一次服务器同步会覆盖这些修改）。

```go
Player :: class : Player_Base {
    ao_late_update :: method(dt: float) {
        if is_local_or_server() {
            // 玩家 UI、商店和摄像机控制
        }

        if is_local() {
            // 仅客户端的外观视觉覆盖
        }
    }
}
```

有关 3D 角色设置（`set_use_3d_physics`, `set_character(.MODEL_3D)`）、手部物品栏和 `player.defaults`），请参阅 [3D 摄像机、控制与手持物品](https://docs.allout.game/all-out-docs/docs-zh/3d-kai-fa/cameras-and-player-controls.md).

## 运行时方向（`device_orientation`)

`player.device_orientation` 允许每位玩家在运行时切换 `.LANDSCAPE` （默认）和 `.PORTRAIT` （在 `ao_start` 中，或通过常规的预测游戏逻辑 / `is_local_or_server()`):

```go
Player :: class : Player_Base {
    ao_start :: method() {
        device_orientation = .PORTRAIT;
    }
}
```

引擎会自动同步方向、更新安全屏幕区域，并调整内置摇杆和技能按钮。

## 身份、个人资料与平台辅助函数

* `get_username() -> string` / `get_user_id() -> string`
* `avatar_color`, `device_kind` (`.PHONE`, `.TABLET`, `.PC`), `is_mobile() -> bool`
* `is_game_owner()`, `is_game_editor()`, `is_owner_or_editor()`
* `is_vip()`, `is_moderator()`, `is_youtuber()`
* `get_all_friends() -> []Player_Base.Friend`
* `get_friend_referral_count() -> int` （加入时记录的好友码推荐次数）
* `is_chat_open()`, `get_chat_rect()`, `get_system_buttons_rect()`, `is_ptt_button_visible()`, `get_ptt_rect()`
* `get_name_override()`, `set_name_override(...)`, `nameplate_mode` (`.SORTED`, `.DEPTH_TESTED`, `.OVERLAY`)

## 账户信任等级（`get_trust_level`)

`player.get_trust_level()` 返回一个 `Account_Trust_Level`: `.BASIC` (0), `.ESTABLISHED` （1），或 `.TRUSTED` （2）。可用它来限制容易被滥用的高风险功能，例如玩家间交易或全局聊天：

```go
can_use_trading :: proc(player: Player_Base) -> bool {
    return player.get_trust_level() >= .ESTABLISHED;
}
```

服务器会在玩家生成前判定其等级，并在该会话期间保持不变（本地编辑器预览默认使用 `.BASIC`）。在预测和权威游戏逻辑中都执行相同的检查——账户信任度是投入程度的信号，而非身份凭证；获得 `.TRUSTED`.

## 临时状态原因

冻结、隐身、隐藏名称、幽灵化、禁用移动输入、禁用摇杆、阻止表情动作，以及禁用 3D 鼠标视角，都使用 **计数式** 命名原因——每次 `add_*_reason(name)` 都必须配对调用一次对应的 `remove_*_reason(name)`。切勿每帧都调用 `add_*_reason` 方法。

```go
if !player.has_freeze_reason("cutscene") {
    player.add_freeze_reason("cutscene");
}

// 稍后：
if player.has_freeze_reason("cutscene") {
    player.remove_freeze_reason("cutscene");
}
```

可用的类别（`add_*_reason`, `remove_*_reason`, `has_*_reason`, `has_any_*_reason`):

* `freeze`
* `invisibility`
* `name_invisibility` / `name_offset`
* `ghost` （对玩家和其他幽灵玩家半透明；对非幽灵玩家隐藏）
* `disable_movement_input` / `joystick_disable`
* `emote_block`
* `look_disable` （还会释放 3D `.FIRST_PERSON` 和 `.SHOULDER` 摄像机捕获的鼠标视角）

若要为单个刻编写角色移动脚本（包括 `{0, 0}` 即使禁用了移动输入也保持静止），请每个步骤调用一次 `player.override_movement_input_for_next_step(input)` 。

## 持久化与完整参考

* **货币：** [经济](https://docs.allout.game/all-out-docs/docs-zh/shu-ju-yu-chi-jiu-hua/economy.md)
* **键/值与 JSON：** [保存系统](https://docs.allout.game/all-out-docs/docs-zh/shu-ju-yu-chi-jiu-hua/save.md)
* **物品与手部：** [物品栏](https://docs.allout.game/all-out-docs/docs-zh/he-xin-yin-qing-gai-nian/inventory.md)

检查 `scripts/.ao_core/ao/core.csl` 以获取项目中的完整 `Player_Base` API。
