API リファレンスはソースコード内の Go ドキュメントコメントから生成されており、英語で記述されています。
package platformer
import "github.com/nsgo-dev/game/pkg/platformer"概要
Package platformer provides a platformer character controller on top of physics.CharacterBody: coyote time, jump buffering, variable jump height, wall slides and wall jumps, each with its own tunable timing.
A Controller wraps a CharacterBody and is stepped once per fixed game.Loop step with that step's Input. It reads the contacts the body's previous Move reported (Grounded, OnLeftWall, OnRightWall), sets the body's velocity and moves it with CharacterBody.Move (or CharacterBody.MoveWorld via StepWorld to collide against a physics.World's bodies, tiles and static rigid bodies alongside slopes and moving platforms). Coordinates follow package physics: X increases to the right and Y increases downward.
Timings are measured in step time. An event (being on the ground, pressing jump, touching a wall, wall jumping) opens a window that stays open on later steps while less than its duration has passed since the event's step, so the same Config plays alike at any fixed rate and a test can count the steps a window lasts. Each step is allowed 1ns of slack because game.Loop's dt is truncated to whole nanoseconds, so 100ms at 60 Hz is 6 steps, not 7.
Config holds the tunables and is a versioned YAML document read with ParseConfig and written with EncodeConfig: unknown fields are refused, durations are Go duration strings such as "100ms", and encoding is deterministic: EncodeConfig writes a canonical form, every field in a fixed order, and parsing and encoding that form again gives the same bytes.
目次
- 定数
- func EncodeConfig(c Config) ([]byte, error)
- type Config
- type Controller
- func NewController(body *physics.CharacterBody, cfg Config) (*Controller, error)
- func (c *Controller) Config() Config
- func (c *Controller) Reset()
- func (c *Controller) SetConfig(cfg Config) error
- func (c *Controller) Step(in Input, grid physics.TileGrid, slopes []physics.Slope, platforms []*physics.MovingPlatform, dt time.Duration) StepResult
- func (c *Controller) StepWorld(in Input, world *physics.World, slopes []physics.Slope, platforms []*physics.MovingPlatform, dt time.Duration) StepResult
- type Input
- type JumpKind
- type StepResult
定数
const CurrentVersion = 1CurrentVersion is the controller config schema version this package reads and writes.
関数
func EncodeConfig(c Config) ([]byte, error)EncodeConfig validates c and writes it as 2-space YAML with every field in a fixed order, so ParseConfig reads back the same Config and encoding that again gives the same bytes.
型
type Config struct {
// Version is the schema version; it must equal CurrentVersion (1).
Version int `yaml:"version"`
// CoyoteTime is how long after the body was last on the ground a jump still leaves from the ground. Zero allows a ground jump only while grounded.
CoyoteTime time.Duration `yaml:"coyote_time"`
// JumpBuffer is how long a jump press waits for the body to be able to jump. Zero honors a press only on the step it happens.
JumpBuffer time.Duration `yaml:"jump_buffer"`
// JumpSpeed is the upward speed a ground jump starts with.
JumpSpeed float32 `yaml:"jump_speed"`
// JumpCut multiplies the upward speed once when the jump button is let go during a jump, in [0, 1]; 1 turns variable jump height off and 0 stops the rise at once.
JumpCut float32 `yaml:"jump_cut"`
// JumpCutMinSpeed is the upward speed a release must exceed to be cut, so a release near the top of a jump changes nothing.
JumpCutMinSpeed float32 `yaml:"jump_cut_min_speed"`
// Gravity is the downward acceleration applied every step.
Gravity float32 `yaml:"gravity"`
// MaxFallSpeed caps the downward speed; zero leaves it uncapped.
MaxFallSpeed float32 `yaml:"max_fall_speed"`
// RunSpeed is the horizontal speed full input reaches. Zero leaves horizontal velocity to the game, apart from wall jumps.
RunSpeed float32 `yaml:"run_speed"`
// GroundAccel is the horizontal acceleration toward the input's speed on the ground; zero reaches it at once.
GroundAccel float32 `yaml:"ground_accel"`
// GroundDecel is the horizontal deceleration on the ground without input; zero stops at once.
GroundDecel float32 `yaml:"ground_decel"`
// AirAccel is the horizontal acceleration toward the input's speed in the air; zero reaches it at once.
AirAccel float32 `yaml:"air_accel"`
// WallSlideSpeed caps the downward speed while the body falls against a wall it is pushing into; zero turns wall sliding off.
WallSlideSpeed float32 `yaml:"wall_slide_speed"`
// WallCoyoteTime is how long after the body last touched a wall in the air a jump still leaves from that wall.
WallCoyoteTime time.Duration `yaml:"wall_coyote_time"`
// WallJumpSpeedX is the horizontal speed a wall jump starts with, away from the wall.
WallJumpSpeedX float32 `yaml:"wall_jump_speed_x"`
// WallJumpSpeedY is the upward speed a wall jump starts with; zero turns wall jumps off.
WallJumpSpeedY float32 `yaml:"wall_jump_speed_y"`
// WallJumpLock is how long horizontal input is ignored after a wall jump, so holding toward the wall does not cancel the push away from it.
WallJumpLock time.Duration `yaml:"wall_jump_lock"`
}Config holds a Controller's tunables. Speeds are in points per second and accelerations in points per second squared, with Y increasing downward as in package physics; every value is non-negative, and the controller applies the sign. Durations are Go duration strings in YAML, such as "100ms".
func DefaultConfig() ConfigDefaultConfig returns a Config tuned for a 16-point tile platformer: 100 ms of coyote time and jump buffer, a cut-short jump, wall slides and wall jumps.
func ParseConfig(data []byte) (Config, error)ParseConfig decodes and validates a YAML controller config. Unknown fields, a missing or unsupported version, and trailing YAML documents are refused.
func (c Config) Validate() errorValidate reports whether c is a version 1 config with finite, non-negative values and JumpCut in [0, 1].
type Controller struct {
// Body is the character the controller moves.
Body *physics.CharacterBody
// contains filtered or unexported fields
}Controller drives a physics.CharacterBody as a platformer character with coyote time, jump buffering, variable jump height, wall slides and wall jumps. Call Step once per fixed game.Loop step; it reads the contacts the body's previous Move left on its Body, sets the velocity from the input and the Config, and then moves the body.
Every timing is measured in step time: an event on a step opens a window that stays open on later steps while less than its duration has passed since that step, so at a fixed dt a duration D covers ceil(D/dt) steps counting the event's own step, and a zero duration covers only that step. Each step is allowed 1ns of slack for game.Loop's dt being truncated to whole nanoseconds, so at 60 Hz a 100ms window covers 6 steps.
A rising controller jump is cut only while the body's VY is the one the previous Step left; a VY the game sets in between, such as a spring, ends the jump and is never cut. A body that is rising in the air cannot use coyote time either, so a jump press does not replace such a launch with a ground jump.
func NewController(body *physics.CharacterBody, cfg Config) (*Controller, error)NewController returns a Controller moving body with cfg, or an error from cfg.Validate.
func (c *Controller) Config() ConfigConfig returns the controller's tunables.
func (c *Controller) Reset()Reset forgets every timer and the jump in progress, such as after a respawn; it leaves the body as it is.
func (c *Controller) SetConfig(cfg Config) errorSetConfig replaces the controller's tunables after checking them with Validate; running timers keep their elapsed time and are measured against the new durations from the next step.
func (c *Controller) Step(in Input, grid physics.TileGrid, slopes []physics.Slope, platforms []*physics.MovingPlatform, dt time.Duration) StepResultStep runs one fixed step of dt: it advances the timers, applies horizontal input, starts a buffered ground or wall jump when one is allowed, cuts a rising jump whose button is up, applies gravity with the wall-slide and fall caps, and moves the body against grid, slopes and platforms with CharacterBody.Move. A nil controller or body, or dt <= 0, does nothing.
func (c *Controller) StepWorld(in Input, world *physics.World, slopes []physics.Slope, platforms []*physics.MovingPlatform, dt time.Duration) StepResultStepWorld runs one fixed step of dt like Step, moving the body against world (its Bodies, Tiles and static RigidBody colliders in world.RigidWorld) together with slopes and platforms via CharacterBody.MoveWorld.
type Input struct {
// Move is the horizontal axis in [-1, 1], negative to the left; values outside are clamped and NaN reads as 0.
Move float32
// JumpPressed reports that the jump button went down during this step.
JumpPressed bool
// JumpHeld reports that the jump button is down at the end of this step.
JumpHeld bool
}Input is one fixed step's player input.
type JumpKind uint8JumpKind says which jump a step started.
const (
// JumpNone means the step started no jump.
JumpNone JumpKind = iota
// JumpGround means the step jumped from the ground, or within CoyoteTime of leaving it.
JumpGround
// JumpWall means the step jumped off a wall, or within WallCoyoteTime of leaving it.
JumpWall
)func (k JumpKind) String() stringString returns "none", "ground" or "wall".
type StepResult struct {
// Move is the result of the step's CharacterBody.Move.
Move physics.MoveResult
// Jump is the jump the step started, if any.
Jump JumpKind
// JumpCut reports that the step cut a rising jump short because the button was let go.
JumpCut bool
// WallSliding reports that the step capped the fall at WallSlideSpeed.
WallSliding bool
// InputLocked reports that the step ignored horizontal input because a wall jump happened less than WallJumpLock earlier.
InputLocked bool
// WallSide is -1 for a wall on the left, 1 for one on the right and 0 for none: the wall the body touches or can still wall jump from at the start of the step.
WallSide int
}StepResult reports what one Controller.Step did.