add hump camera

This commit is contained in:
yuki 2025-11-10 04:37:15 -03:00
parent 249f3749b9
commit ce7ac42206
2 changed files with 154 additions and 0 deletions

152
def/Camera.d.lua Normal file
View file

@ -0,0 +1,152 @@
---@meta
---@class Camera
---@field x number The x position of the camera in world coordinates
---@field y number The y position of the camera in world coordinates
---@field scale number The zoom level (scale) of the camera
---@field rot number The rotation of the camera in radians
---@field smoother fun(dx: number, dy: number, ...: any): number, number The smoothing function used for lock/window methods
local Camera = {}
---@class camera.smooth
local smooth = {}
--- No smoothing - instant movement.
---@return fun(dx: number, dy: number): number, number
function smooth.none() end
--- Linear interpolation toward target at constant speed.
---@param speed number Units per second
---@return fun(dx: number, dy: number, s: number|nil): number, number
function smooth.linear(speed) end
--- Damped spring-like smoothing (exponential approach).
---@param stiffness number Higher = faster response
---@return fun(dx: number, dy: number, s: number|nil): number, number
function smooth.damped(stiffness) end
--- Sets the position the camera is looking at.
---@param x number
---@param y number
---@return Camera
function Camera:lookAt(x, y) end
--- Moves the camera by the given amount.
---@param dx number
---@param dy number
---@return Camera
function Camera:move(dx, dy) end
--- Returns the current position of the camera.
---@return number x
---@return number y
function Camera:position() end
--- Rotates the camera by the given angle (radians).
---@param phi number
---@return Camera
function Camera:rotate(phi) end
--- Sets the absolute rotation of the camera (radians).
---@param phi number
---@return Camera
function Camera:rotateTo(phi) end
--- Multiplies the current zoom level.
---@param mul number
---@return Camera
function Camera:zoom(mul) end
--- Sets the absolute zoom level.
---@param zoom number
---@return Camera
function Camera:zoomTo(zoom) end
--- Begins drawing with this camera applied.
---@param x number|nil Scissor x (default: 0)
---@param y number|nil Scissor y (default: 0)
---@param w number|nil Scissor width (default: screen width)
---@param h number|nil Scissor height (default: screen height)
---@param noclip boolean|nil If true, scissor is not applied
function Camera:attach(x, y, w, h, noclip) end
--- Ends drawing with this camera and restores previous transform/scissor.
function Camera:detach() end
--- Draws a function within the camera's transform.
---@overload fun(func: fun())
---@overload fun(x: number, y: number, w: number, h: number, func: fun())
---@overload fun(x: number, y: number, w: number, h: number, noclip: boolean, func: fun())
function Camera:draw(x, y, w, h, noclip, func) end
--- Converts world coordinates to screen/camera coordinates.
---@param x number World x
---@param y number World y
---@param ox number|nil Offset x (default: 0)
---@param oy number|nil Offset y (default: 0)
---@param w number|nil Screen width (default: love.graphics.getWidth())
---@param h number|nil Screen height (default: love.graphics.getHeight())
---@return number screenX
---@return number screenY
function Camera:cameraCoords(x, y, ox, oy, w, h) end
--- Converts screen coordinates to world coordinates.
---@param x number Screen x
---@param y number Screen y
---@param ox number|nil Offset x (default: 0)
---@param oy number|nil Offset y (default: 0)
---@param w number|nil Screen width (default: love.graphics.getWidth())
---@param h number|nil Screen height (default: love.graphics.getHeight())
---@return number worldX
---@return number worldY
function Camera:worldCoords(x, y, ox, oy, w, h) end
--- Returns the current mouse position in world coordinates.
---@param ox number|nil Offset x (default: 0)
---@param oy number|nil Offset y (default: 0)
---@param w number|nil Screen width (default: love.graphics.getWidth())
---@param h number|nil Screen height (default: love.graphics.getHeight())
---@return number worldX
---@return number worldY
function Camera:mousePosition(ox, oy, w, h) end
--- Smoothly moves the camera to center on x (horizontal only).
---@param x number Target world x
---@param smoother fun(dx: number, dy: number, ...: any): number, number | nil
---@param ... any Extra args passed to smoother
---@return Camera
function Camera:lockX(x, smoother, ...) end
--- Smoothly moves the camera to center on y (vertical only).
---@param y number Target world y
---@param smoother fun(dx: number, dy: number, ...: any): number, number | nil
---@param ... any Extra args passed to smoother
---@return Camera
function Camera:lockY(y, smoother, ...) end
--- Smoothly moves the camera to center on (x,y).
---@param x number Target world x
---@param y number Target world y
---@param smoother fun(dx: number, dy: number, ...: any): number, number | nil
---@param ... any Extra args passed to smoother
---@return Camera
function Camera:lockPosition(x, y, smoother, ...) end
--- Keeps a world point inside a screen-space window using smooth movement.
---@param x number World x to track
---@param y number World y to track
---@param x_min number Left edge of window (screen coords)
---@param x_max number Right edge of window (screen coords)
---@param y_min number Top edge of window (screen coords)
---@param y_max number Bottom edge of window (screen coords)
---@param smoother fun(dx: number, dy: number, ...: any): number, number | nil
---@param ... any Extra args passed to smoother
function Camera:lockWindow(x, y, x_min, x_max, y_min, y_max, smoother, ...) end
---@class camera
---@field smooth camera.smooth
---@field new fun(x: number|nil, y: number|nil, zoom: number|nil, rot: number|nil, smoother: fun(dx: number, dy: number, ...: any): number, number | nil): Camera
local camera = {}
---@overload fun(x: number|nil, y: number|nil, zoom: number|nil, rot: number|nil, smoother: fun(dx: number, dy: number, ...: any): number, number | nil): Camera
return camera

View file

@ -11,6 +11,8 @@ Object = require 'lib/classic/classic'
Baton = require 'lib/baton/baton'
---@type Timer
Timer = require 'lib/hump/timer'
---@type Camera
Camera = require 'lib/hump/camera'
-- generic objects --
Room = require 'obj/Room'