> For the complete documentation index, see [llms.txt](https://docs.perfectcs2.com/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.perfectcs2.com/exemplos/caixas.md).

# Caixas nos inimigos

Uma caixa, o nome e a barra de vida em cada inimigo. Mostra:

* `entities.players("enemies")` e os campos de cada jogador;
* `vec3:to_screen()` para passar do mapa para a tela;
* `color:lerp` para a barra ir do vermelho ao verde.

Salve como `enemy_boxes.lua` na pasta de scripts e ligue na aba **Scripts**.

```lua
-- @name Enemy Boxes
-- @author perfect
-- @version 1.0
-- @description A box, name and health bar on each enemy.

ui.group("Boxes", "Drawn from the game's player list")
local enabled = ui.toggle("Enabled", "Draw the boxes", true, color(255, 140, 40))
local names   = ui.toggle("Names", "Name above the box", true)
local health  = ui.toggle("Health", "Bar on the left", true)
local visible = ui.toggle("Visible only", "Skip enemies behind walls", false)

local function draw_enemy(enemy, clr)
    local feet_x, feet_y = enemy.origin:to_screen()
    local top_x, top_y = (enemy.head + vec3(0, 0, 8)):to_screen()
    if not feet_x or not top_x then return end

    local h = feet_y - top_y
    local w = h * 0.5
    local x = feet_x - w / 2
    render.rect(x, top_y, w, h, clr, 0, 1.5)

    if names:get() then
        render.text(feet_x, top_y - 16, enemy.name, color(255, 255, 255), { size = 13, align = "center", shadow = true })
    end
    if health:get() then
        local hp = utils.clamp(enemy.health / 100, 0, 1)
        render.rect_filled(x - 5, top_y, 3, h, color(0, 0, 0, 160))
        render.rect_filled(x - 5, top_y + h * (1 - hp), 3, h * hp, color(255, 80, 80):lerp(color(80, 220, 120), hp))
    end
end

callbacks.add("draw", function()
    local on, clr = enabled:get()
    if not on or not engine.is_in_game() then return end
    for _, enemy in ipairs(entities.players("enemies")) do
        if not enemy.dormant and (enemy.visible or not visible:get()) then
            draw_enemy(enemy, clr)
        end
    end
end)
```

A altura da caixa vem da distância na tela entre os pés e um ponto um pouco acima da cabeça, por isso ela diminui com a distância.


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation by asking a question.

Perform an HTTP GET request on the following URL with the `ask` and `goal` query parameters:

```
GET https://docs.perfectcs2.com/exemplos/caixas.md?ask=<question>&goal=<user_goal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is what the user is ultimately trying to achieve, the reason they need the answer. Sharing it helps GitBook give you a better, more relevant answer. A goal is most helpful when it describes the outcome the user wants rather than restating the question. For example, with `ask=how do I create an API token`, a goal like `automate deployments from our CI pipeline` lets GitBook tailor the answer to that use case.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
