


function OnInit()
    player = Engine.GetObjectByTag("PlayerRoot")
    if not player then
        Engine.LogError("PlayerRoot not found!")
        return
    end

    if Engine.GetGlobal("player_health") == nil then
        Engine.SetGlobal("player_health", 100)
    end
end

-- Called every frame
function OnUpdate(dt)
    if not player then return end

    local speed = 200
    local move = { x = 0, y = 0 }

    if Engine.KeyDown(Keycode.W) then move.y = move.y - 1 end
    if Engine.KeyDown(Keycode.S) then move.y = move.y + 1 end
    if Engine.KeyDown(Keycode.A) then move.x = move.x - 1 end
    if Engine.KeyDown(Keycode.D) then move.x = move.x + 1 end

    local pos = player:GetPosition()
    pos.x = pos.x + move.x * speed * dt
    pos.y = pos.y + move.y * speed * dt
    player:SetPosition(pos)

    Engine.SetGlobal("player_pos.x", pos.x)
    Engine.SetGlobal("player_pos.y", pos.y)



    -- Simulate damage each second
    local hp = Engine.GetGlobal("player_health") or 100
    hp = hp - dt * 5
    Engine.SetGlobal("player_health", hp)

    -- Debug print
end
