Documentation
¶
Overview ¶
Package render provides the graphics layer for the game.
Camera handles world-to-screen coordinate transformation with zoom and pan. Sprite wraps directional animations loaded from sprite sheets, with automatic horizontal flip to generate left/right variants. TextWriter renders text using loaded fonts. TileSize (16px) defines the base tile dimension used across the rendering pipeline.
Index ¶
- Constants
- Variables
- type Animation
- type AnimationType
- type Camera
- func (c *Camera) AddZoom(delta float64)
- func (c *Camera) Adjust(op *ebiten.DrawImageOptions, p geometry.Vector2)
- func (c *Camera) CameraDebugInfo() string
- func (c *Camera) DrawImageOptions(p geometry.Vector2) *ebiten.DrawImageOptions
- func (c *Camera) EffectiveZoom() float64
- func (c *Camera) MaxZoom() float64
- func (c *Camera) MinZoom() float64
- func (c *Camera) Move(delta geometry.Vector2)
- func (c *Camera) Position() geometry.Vector2
- func (c *Camera) ScreenHeight() int
- func (c *Camera) ScreenToWorld(screenPos geometry.Vector2) geometry.Vector2
- func (c *Camera) ScreenWidth() int
- func (c *Camera) SetPosition(pos geometry.Vector2)
- func (c *Camera) SetZeroAsCenter()
- func (c *Camera) SetZeroAsTopLeft()
- func (c *Camera) SetZoom(zoom float64)
- func (c *Camera) WorldToScreen(worldPos geometry.Vector2) geometry.Vector2
- func (c *Camera) Zoom() float64
- type CameraController
- type Drawable
- type Sprite
- func (s *Sprite) AddImage(animationType AnimationType, img *ebiten.Image)
- func (s *Sprite) AllAnimations() []AnimationType
- func (s *Sprite) CanAnimate(a AnimationType) bool
- func (s *Sprite) Draw(screen *ebiten.Image, c *Camera, p geometry.Vector2, a AnimationType)
- func (s *Sprite) DrawAnimation(screen *ebiten.Image, c *Camera, p geometry.Vector2, a AnimationType, ...)
- func (s *Sprite) HasAnimation(a AnimationType) bool
- func (s *Sprite) Image(animationType AnimationType) *ebiten.Image
- func (s *Sprite) SetScale(scale float64) *Sprite
- func (s *Sprite) SetType(spriteType SpriteType) *Sprite
- func (s *Sprite) SetZeroPosition(pos geometry.Vector2) *Sprite
- func (s *Sprite) VisibleBounds() image.Rectangle
- func (s *Sprite) VisibleTopAboveZero() float64
- type SpriteType
- type TextAlignment
- type TextWriter
- func (t *TextWriter) Clear() *TextWriter
- func (t *TextWriter) ColoredText(msg string, c color.Color) *TextWriter
- func (t *TextWriter) Draw(screen *ebiten.Image, camera *Camera, position geometry.Vector2)
- func (t *TextWriter) Print(screen *ebiten.Image, x, y int, msg string)
- func (t *TextWriter) Printf(screen *ebiten.Image, x, y int, format string, a ...interface{})
- func (t *TextWriter) RenderedHeight() float64
- func (t *TextWriter) Text(msg string) *TextWriter
- func (t *TextWriter) WithAlignment(align TextAlignment) *TextWriter
- func (t *TextWriter) WithBackground(bg color.Color) *TextWriter
- func (t *TextWriter) WithBackgroundPadding(padding int) *TextWriter
- func (t *TextWriter) WithColor(color color.Color) *TextWriter
- func (t *TextWriter) WithFont(font *text.GoTextFaceSource) *TextWriter
- func (t *TextWriter) WithMaxZoom(maxZoom float64) *TextWriter
- func (t *TextWriter) WithScaling(enabled bool) *TextWriter
- func (t *TextWriter) WithSize(size int) *TextWriter
Constants ¶
const ( // DefaultMaxZoomForText is the default maximum zoom level for text visibility // when scaling is disabled. Text will be hidden at zoom levels above this value. DefaultMaxZoomForText = 5.0 // DefaultNameplateGapPixels is the default pixel gap between the bottom // of the nameplate and the visible top of the sprite. Constant in pixels // at any camera zoom. DefaultNameplateGapPixels = 4.0 )
const (
TileSize = 16
)
Variables ¶
var AttackAnimations = map[AnimationType]AnimationType{ AnimationMoveDown: AnimationAttackDown, AnimationMoveLeft: AnimationAttackLeft, AnimationMoveRight: AnimationAttackRight, AnimationMoveUp: AnimationAttackUp, }
var IdleAnimations = map[AnimationType]AnimationType{ AnimationMoveDown: AnimationIdleDown, AnimationMoveLeft: AnimationIdleLeft, AnimationMoveRight: AnimationIdleRight, AnimationMoveUp: AnimationIdleUp, }
var ( MirroredAnimations = map[AnimationType]AnimationType{ AnimationIdleLeft: AnimationIdleRight, AnimationMoveLeft: AnimationMoveRight, AnimationAttackLeft: AnimationAttackRight, } )
MirroredAnimations maps a left-facing animation to the right-facing animation it is rendered from by a horizontal flip, so left variants need not be authored separately.
var (
TextDefault = NewTextWriter()
)
var UsePlaceholderSpriteImages bool
UsePlaceholderSpriteImages, when true, makes Sprite.Image return a placeholder for a missing animation type instead of panicking. Set by engine configuration.
Functions ¶
This section is empty.
Types ¶
type AnimationType ¶
type AnimationType int
AnimationType represents different animation states for sprites.
const ( AnimationDefault AnimationType = 0 AnimationMoveUp AnimationType = 1 AnimationMoveDown AnimationType = 2 AnimationMoveLeft AnimationType = 3 AnimationMoveRight AnimationType = 4 AnimationIdleUp AnimationType = 5 AnimationIdleDown AnimationType = 6 AnimationIdleLeft AnimationType = 7 AnimationIdleRight AnimationType = 8 AnimationAttackUp AnimationType = 9 AnimationAttackDown AnimationType = 10 AnimationAttackLeft AnimationType = 11 AnimationAttackRight AnimationType = 12 )
func AttackAnimation ¶
func AttackAnimation(direction geometry.Vector2) AnimationType
AttackAnimation returns the attack animation matching the facing direction (for actors without a movement direction, defaults to down).
func IdleAnimation ¶
func IdleAnimation(direction geometry.Vector2) AnimationType
func MoveAnimation ¶
func MoveAnimation(direction geometry.Vector2) AnimationType
MoveAnimation returns the appropriate movement animation type for a direction.
func (AnimationType) String ¶
func (i AnimationType) String() string
type Camera ¶
type Camera struct {
// contains filtered or unexported fields
}
Camera represents the game's camera.
func NewScreenCamera ¶
NewScreenCamera creates a camera for screen-space rendering. This camera uses an identity transformation (no zoom, no world offset), making it suitable for UI elements that should remain in screen coordinates.
func (*Camera) Adjust ¶
func (c *Camera) Adjust(op *ebiten.DrawImageOptions, p geometry.Vector2)
Adjust applies the camera transform in-place to op for a world position p given in tile units (scaled by TileSize). It is the tile-space counterpart to DrawImageOptions.
func (*Camera) CameraDebugInfo ¶
CameraDebugInfo returns a human-readable string of the camera's position and effective zoom, for debug overlays.
func (*Camera) DrawImageOptions ¶
func (c *Camera) DrawImageOptions(p geometry.Vector2) *ebiten.DrawImageOptions
DrawImageOptions returns draw options that map a pixel-space position p into screen space under the current camera transform. Unlike Adjust, p is in pixels (not tile units) and a fresh options value is returned.
func (*Camera) EffectiveZoom ¶
EffectiveZoom returns the screen-adjusted zoom actually applied to the world-to-screen transform: the user zoom scaled by a screen-size normalization factor so that a given user zoom frames the same number of tiles on any resolution.
func (*Camera) ScreenHeight ¶
ScreenHeight returns the camera's screen height.
func (*Camera) ScreenToWorld ¶
ScreenToWorld converts screen coordinates to world coordinates.
func (*Camera) ScreenWidth ¶
ScreenWidth returns the camera's screen width.
func (*Camera) SetPosition ¶
SetPosition sets the camera's position.
func (*Camera) SetZeroAsCenter ¶
func (c *Camera) SetZeroAsCenter()
SetZeroAsCenter sets the camera's position so that (0,0) in world space is at the center of the screen.
func (*Camera) SetZeroAsTopLeft ¶
func (c *Camera) SetZeroAsTopLeft()
SetZeroAsTopLeft sets the camera's position so that (0,0) in world space is at the top-left of the screen.
func (*Camera) WorldToScreen ¶
WorldToScreen converts world coordinates to screen coordinates.
type CameraController ¶
type CameraController struct {
// Camera is the camera this controller drives.
Camera *Camera
// MoveSpeed is the pan speed in world units per frame, before zoom scaling.
MoveSpeed float64
// ZoomSpeed is the zoom increment applied per input step.
ZoomSpeed float64
// contains filtered or unexported fields
}
CameraController drives a Camera from user input. It implements the engine's default pan/zoom control scheme: WASD keyboard panning, Q/E and mouse-wheel zoom, and middle-mouse-button drag panning. Games wanting a different scheme can drive the Camera directly instead of attaching a controller.
func NewCameraController ¶
func NewCameraController(camera *Camera) *CameraController
NewCameraController returns a controller driving the given camera with the engine's default pan and zoom speeds.
func (*CameraController) CursorWorldPosition ¶
func (cc *CameraController) CursorWorldPosition() geometry.Vector2
CursorWorldPosition returns the OS cursor position converted to world coordinates through the controller's camera.
func (*CameraController) HandleInput ¶
func (cc *CameraController) HandleInput()
HandleInput reads input for the current frame and pans/zooms the camera.
type Drawable ¶
Drawable is implemented by anything that can render itself to screen using a Camera for the world-to-screen transform.
type Sprite ¶
type Sprite struct {
Animations map[AnimationType]*Animation
ZeroPosition geometry.Vector2
Scale float64
Type SpriteType
// contains filtered or unexported fields
}
Sprite represents a game sprite with animations.
func LoadSprite ¶
func LoadSprite(img *ebiten.Image, width, height int, indexes map[AnimationType][]int, durations map[AnimationType]time.Duration) (*Sprite, error)
LoadSprite slices img into a grid of width columns by height rows, assigns the frame indexes in indexes to each AnimationType, and sets per-animation durations (defaulting to one second when unspecified). Note: width and height are column/row counts, not pixel dimensions.
func MustLoadSprite ¶
func MustLoadSprite(img *ebiten.Image, width, height int, indexes map[AnimationType][]int, durations map[AnimationType]time.Duration) *Sprite
MustLoadSprite is like LoadSprite but panics on error.
func NewSprite ¶
func NewSprite() *Sprite
NewSprite creates and returns a new Sprite with default values.
func (*Sprite) AddImage ¶
func (s *Sprite) AddImage(animationType AnimationType, img *ebiten.Image)
AddImage adds an image to the specified animation type.
func (*Sprite) AllAnimations ¶
func (s *Sprite) AllAnimations() []AnimationType
AllAnimations returns a slice of all animation types the sprite can draw.
func (*Sprite) CanAnimate ¶
func (s *Sprite) CanAnimate(a AnimationType) bool
CanAnimate checks if the sprite can perform a specific animation, either directly or by flipping an existing animation.
func (*Sprite) Draw ¶
Draw draws the sprite at the given position with the specified animation type.
func (*Sprite) DrawAnimation ¶
func (s *Sprite) DrawAnimation(screen *ebiten.Image, c *Camera, p geometry.Vector2, a AnimationType, duration time.Duration)
DrawAnimation draws the sprite at the given position with the specified animation type.
func (*Sprite) HasAnimation ¶
func (s *Sprite) HasAnimation(a AnimationType) bool
HasAnimation checks if the sprite has a specific animation defined.
func (*Sprite) Image ¶
func (s *Sprite) Image(animationType AnimationType) *ebiten.Image
Image returns the first image of the specified animation type. It logs a fatal error if the animation type does not exist.
func (*Sprite) SetType ¶
func (s *Sprite) SetType(spriteType SpriteType) *Sprite
SetType sets the type of the sprite.
func (*Sprite) SetZeroPosition ¶
SetZeroPosition sets the zero position of the sprite.
func (*Sprite) VisibleBounds ¶
VisibleBounds returns the bounding rectangle of non-transparent pixels in one frame, expressed in frame-local pixel coordinates. Cached after first call. Returns an empty rectangle if no visible content is found.
func (*Sprite) VisibleTopAboveZero ¶
VisibleTopAboveZero returns the number of pixels the visible sprite content (non-transparent pixels) extends above ZeroPosition in one frame. Frames across animations share the same size and layout, so the result is computed once from any available frame and cached.
Use this instead of the raw frame height when placing UI elements (like nameplates) above a sprite, so transparent padding at the top of the frame is not counted as part of the visible sprite.
type SpriteType ¶
type SpriteType int
SpriteType represents different types of sprites in the game.
const ( SpriteTypeUnknown SpriteType = 0 SpriteTypeActor SpriteType = 1 SpriteTypeTerrain SpriteType = 2 )
func (SpriteType) String ¶
func (i SpriteType) String() string
type TextAlignment ¶
type TextAlignment int
TextAlignment represents horizontal text alignment options.
const ( // AlignLeft aligns text to the left. AlignLeft TextAlignment = iota // AlignCenter centers text horizontally. AlignCenter // AlignRight aligns text to the right. AlignRight )
type TextWriter ¶
type TextWriter struct {
Size int
Font *text.GoTextFaceSource
Color color.Color
Background *color.Color // Optional background color
BackgroundPadding int // Padding around background (default: 2)
Scaling bool // Whether text scales with camera zoom
MaxZoom float64 // Max zoom level for text visibility (0 = use default)
Align TextAlignment // Horizontal alignment
// contains filtered or unexported fields
}
TextWriter provides a fluent API for rendering text with various styling options.
func NewTextWriter ¶
func NewTextWriter() *TextWriter
func (*TextWriter) Clear ¶
func (t *TextWriter) Clear() *TextWriter
Clear removes all built text segments, allowing the builder to be reused with different text content while keeping the same styling configuration.
func (*TextWriter) ColoredText ¶
func (t *TextWriter) ColoredText(msg string, c color.Color) *TextWriter
ColoredText adds a text segment with a specific color to the builder. This allows creating multi-colored text without string templating overhead.
func (*TextWriter) Draw ¶
Draw renders the built text segments to the screen at the specified position. The position is in world coordinates, which are converted to screen coordinates using the camera's WorldToScreen transformation.
When scaling is disabled (WithScaling(false)), text maintains fixed pixel size and is hidden at zoom levels above the configured maximum zoom threshold.
The text segments are rendered with the configured alignment and optional background.
func (*TextWriter) Printf ¶
func (t *TextWriter) Printf(screen *ebiten.Image, x, y int, format string, a ...interface{})
func (*TextWriter) RenderedHeight ¶
func (t *TextWriter) RenderedHeight() float64
RenderedHeight returns the pixel height of the text as it would be drawn without scaling. Scaling-enabled TextWriters multiply this by camera zoom at draw time; this method does not apply that multiplier.
func (*TextWriter) Text ¶
func (t *TextWriter) Text(msg string) *TextWriter
Text adds a text segment with the current color to the builder. This allows building multi-segment text where each segment can have different styling.
func (*TextWriter) WithAlignment ¶
func (t *TextWriter) WithAlignment(align TextAlignment) *TextWriter
WithAlignment sets the horizontal alignment of the text.
func (*TextWriter) WithBackground ¶
func (t *TextWriter) WithBackground(bg color.Color) *TextWriter
WithBackground sets an optional background color for the text. The background is rendered as a rectangle behind the text with padding.
func (*TextWriter) WithBackgroundPadding ¶
func (t *TextWriter) WithBackgroundPadding(padding int) *TextWriter
WithBackgroundPadding sets the padding around the background rectangle. Default padding is 2 pixels if not specified.
func (*TextWriter) WithColor ¶
func (t *TextWriter) WithColor(color color.Color) *TextWriter
func (*TextWriter) WithFont ¶
func (t *TextWriter) WithFont(font *text.GoTextFaceSource) *TextWriter
func (*TextWriter) WithMaxZoom ¶
func (t *TextWriter) WithMaxZoom(maxZoom float64) *TextWriter
WithMaxZoom sets the maximum effective zoom at which fixed-size text remains visible; above it the text is hidden. It only applies when scaling is disabled (see WithScaling). A zero value uses DefaultMaxZoomForText.
func (*TextWriter) WithScaling ¶
func (t *TextWriter) WithScaling(enabled bool) *TextWriter
WithScaling configures whether text scales with camera zoom. When disabled, text maintains a fixed pixel size regardless of zoom (and is hidden above the MaxZoom threshold; see WithMaxZoom).
func (*TextWriter) WithSize ¶
func (t *TextWriter) WithSize(size int) *TextWriter