API 레퍼런스는 소스 코드의 Go 문서 주석에서 추출되며 영어로 작성되어 있습니다.
package framedata
import "github.com/nsgo-dev/game/pkg/framedata"개요
Package framedata gives fighting games per-frame hitboxes, hurtboxes and hit properties for their moves, authored as versioned YAML and resolved deterministically once per fixed step.
Documents
A Doc lists a character's moves. Each Move lasts Length frames (one frame per fixed step, numbered from 1 like published frame data) and holds Boxes, each active on an inclusive range of frames: hurtboxes (KindHurt) where the character can be hit and hitboxes (KindHit) that strike, with damage, hitstun, blockstun, knockback and a hit Group. Cancels open windows where a move may be cut short into other moves, on any frame or only after the move hit, was blocked or made contact. Parse decodes and validates a version 1 document, refusing unknown fields; Encode writes deterministic YAML that Parse reads back unchanged.
Boxes are in points relative to the fighter's origin (usually between its feet), facing right, with Y growing downward like a core.Drawing. A fighter facing left mirrors them about its origin.
Resolving hits
A Fighter plays one move at a time from its Doc and returns to its Neutral move when a move that does not loop ends. A World holds the fighters of a match; World.Step advances every fighter one frame, then tests each active hitbox against every other fighter's active hurtboxes. All hits of a step are found from the positions before any of them is applied, so two fighters that strike each other on the same frame trade. Fighters are visited in slice order and boxes in document order, so the same inputs always produce the same hits in the same order.
A move instance hits each opponent at most once per hit Group (a multi-hit move gives its hits different groups) and at most once per step. A hit puts the opponent in hitstun, or blockstun when it is Blocking, interrupts its move with its Neutral move, and lets the attacker's cancel windows that wait for contact open.
The debug package's ShapeOverlay.DrawFighterBoxes draws a fighter's active boxes as wireframes.
색인
- 상수
- func Encode(doc *Doc) ([]byte, error)
- func Validate(doc *Doc) error
- type Box
- type Cancel
- type Condition
- type Doc
- type Fighter
- func (f *Fighter) Actionable() bool
- func (f *Fighter) Boxes(dst []WorldBox) []WorldBox
- func (f *Fighter) CanStart(name string) bool
- func (f *Fighter) Contact() (hit, blocked bool)
- func (f *Fighter) Force(name string) error
- func (f *Fighter) Frame() int
- func (f *Fighter) Move() *Move
- func (f *Fighter) Start(name string) bool
- func (f *Fighter) Stun() int
- type FighterState
- type FrameData
- type Hit
- type Kind
- type Landed
- type Move
- type World
- type WorldBox
- type WorldState
상수
const CurrentVersion = 1CurrentVersion is the frame data schema version this package reads and writes.
const MaxGroup = 63MaxGroup is the highest hit group a hitbox may use; groups run from 0 to MaxGroup.
const MaxLength = 3600MaxLength is the most frames a move may last: a minute at 60 steps a second.
함수
func Encode(doc *Doc) ([]byte, error)Encode validates doc and writes it as deterministic 2-space YAML that Parse reads back unchanged.
func Validate(doc *Doc) errorValidate checks that doc follows the version 1 schema: unique move names, lengths from 1 to MaxLength, boxes and cancel windows inside their moves, positive finite box sizes, hit properties only on hitboxes, and cancels into moves that exist.
타입
type Box struct {
// Kind is KindHurt or KindHit.
Kind Kind `yaml:"kind"`
// Start and End are the first and last frames the box is active on, with 1 <= Start <= End <= the move's Length.
Start int `yaml:"start"`
End int `yaml:"end"`
// X and Y are the box's top-left corner in points from the fighter's origin; Y grows downward.
X float32 `yaml:"x,omitempty"`
Y float32 `yaml:"y,omitempty"`
// Width and Height are the box's size in points; both must be positive.
Width float32 `yaml:"width"`
Height float32 `yaml:"height"`
// Group is a hitbox's hit group, from 0 to MaxGroup. A move instance hits each opponent once per group, so hitboxes of one hit share a group and each hit of a multi-hit move has its own. Hurtboxes leave it 0.
Group int `yaml:"group,omitempty"`
// Damage is how much damage a hitbox deals. Hurtboxes leave it 0.
Damage int `yaml:"damage,omitempty"`
// Hitstun is how many frames an opponent hit by the hitbox cannot act. Hurtboxes leave it 0.
Hitstun int `yaml:"hitstun,omitempty"`
// Blockstun is how many frames an opponent that blocks the hitbox cannot act. Hurtboxes leave it 0.
Blockstun int `yaml:"blockstun,omitempty"`
// KnockbackX and KnockbackY push an opponent hit by the hitbox, in points per frame; positive X is away from the attacker. Hurtboxes leave them 0.
KnockbackX float32 `yaml:"knockbackX,omitempty"`
KnockbackY float32 `yaml:"knockbackY,omitempty"`
}Box is a rectangle active on the frames Start to End inclusive, relative to the fighter's origin when it faces right.
func (b Box) Active(frame int) boolActive reports whether the box is active on frame.
type Cancel struct {
// Start and End are the first and last frames of the window, with 1 <= Start <= End <= the move's Length.
Start int `yaml:"start"`
End int `yaml:"end"`
// When is when the window is open: OnAny (the default), OnHit, OnBlock or OnContact.
When Condition `yaml:"when,omitempty"`
// Into names the moves the window may cancel into; each must be a move of the same Doc.
Into []string `yaml:"into,flow"`
}Cancel is a window of frames, Start to End inclusive, in which a move may be cut short into the moves named in Into.
type Condition stringCondition says when a cancel window is open.
const (
// OnAny opens the window whether or not the move connected. It is the empty string, the default.
OnAny Condition = ""
// OnHit opens the window only after the move hit an opponent that was not blocking.
OnHit Condition = "hit"
// OnBlock opens the window only after the move was blocked.
OnBlock Condition = "block"
// OnContact opens the window after the move hit or was blocked.
OnContact Condition = "contact"
)type Doc struct {
// Version is the schema version; it must equal CurrentVersion (1).
Version int `yaml:"version"`
// Moves are the character's moves. Names are unique.
Moves []Move `yaml:"moves,omitempty"`
}Doc is the root of a versioned YAML frame data document: one character's moves.
func Parse(data []byte) (*Doc, error)Parse decodes a YAML frame data document and validates it. Unknown fields and trailing YAML documents are refused.
func (d *Doc) Move(name string) (*Move, bool)Move returns the move named name and true, or nil and false when the document has none.
type Fighter struct {
// Moves is the fighter's frame data. It should have passed Validate, as Parse ensures: a hitbox whose Group is outside 0 to MaxGroup never hits.
Moves *Doc
// Neutral names the looping move the fighter returns to when a move that does not loop ends or when it is hit, such as "idle". Empty, or a name Moves does not have, leaves the fighter with no move and so no boxes.
Neutral string
// X and Y place the fighter's origin in world points.
X, Y float32
// FacingLeft mirrors the fighter's boxes and knockback about its origin.
FacingLeft bool
// Team keeps fighters from hitting each other: two fighters with the same non-zero Team never hit. Fighters on team 0 can hit everyone.
Team int
// Blocking turns hits on the fighter into blocks: blockstun instead of hitstun. The game sets it each step from input when the fighter may block.
Blocking bool
// contains filtered or unexported fields
}Fighter is one character in a World: where it stands, which way it faces and which move of its Doc it is playing on which frame.
A Fighter plays its Neutral move when it has no other, so it always has hurtboxes to be hit on. Start begins a move when the fighter can act or when a cancel window of its current move allows it; Force begins one regardless.
func (f *Fighter) Actionable() boolActionable reports whether the fighter may start any move: it is not stunned and it has no move, a looping move or a move on its last frame.
func (f *Fighter) Boxes(dst []WorldBox) []WorldBoxBoxes appends the boxes of the current move active on the fighter's current frame to dst, in document order, and returns the extended slice.
func (f *Fighter) CanStart(name string) boolCanStart reports whether Start(name) would begin the move: the move exists and the fighter is actionable, or a cancel window of its current move open on this frame lists the move.
func (f *Fighter) Contact() (hit, blocked bool)Contact reports whether the current move instance has hit an opponent and whether it has been blocked.
func (f *Fighter) Force(name string) errorForce begins the move named name on its first frame regardless of stun and cancel windows, for hit reactions, throws and scripted moves. It returns an error when Moves has no such move.
func (f *Fighter) Frame() intFrame returns the frame of the current move the fighter shows, from 1, or 0 when it has no move.
func (f *Fighter) Move() *MoveMove returns the move the fighter is playing, or nil when it has none.
func (f *Fighter) Start(name string) boolStart begins the move named name on its first frame, which the next World.Step resolves, when CanStart allows it, and reports whether it did.
func (f *Fighter) Stun() intStun returns how many more steps the fighter stays in hitstun or blockstun.
type FighterState struct {
// X, Y, FacingLeft and Blocking are the fighter's fields of the same names.
X, Y float32
FacingLeft bool
Blocking bool
// Move names the move the fighter plays, or is empty when it has none.
Move string
// Frame is the frame of Move the fighter shows, from 1, or 0 when it has no move.
Frame int
// Fresh reports that Move began after the last Step advanced the fighter, by Start, Force or a hit, so the next Step resolves its first frame instead of advancing it.
Fresh bool
// Stun is the steps of hitstun or blockstun left.
Stun int
// Hit and Blocked are the current move instance's contact, as Fighter.Contact reports it.
Hit, Blocked bool
// Landed lists, per opponent, the hit groups the current move instance has already struck it with.
Landed []Landed
}FighterState is the state of one Fighter in a WorldState.
type FrameData struct {
// Startup is the first frame a hitbox is active on.
Startup int
// Active is how many frames pass from the first frame a hitbox is active on to the last, inclusive.
Active int
// Recovery is how many frames the move lasts after its last active frame.
Recovery int
// OnHit and OnBlock are the attacker's frame advantage when the earliest hitbox connects on its first frame and the opponent is hit or blocks: positive means the attacker can act first.
OnHit int
OnBlock int
}FrameData summarizes a move the way fighting game frame data tables do.
type Hit struct {
// Attacker and Victim are the fighters that struck and were struck.
Attacker, Victim *Fighter
// Move is the attacker's move and Frame the frame of it that connected.
Move *Move
Frame int
// Box is the index of the connecting hitbox in Move.Boxes, and Group its hit group.
Box int
Group int
// Blocked reports whether the victim was blocking.
Blocked bool
// Damage is the hitbox's damage, dealt in full on a hit and left to the game to scale on a block.
Damage int
// Stun is the frames of stun the victim took: the hitbox's hitstun, or its blockstun when Blocked.
Stun int
// KnockbackX and KnockbackY are the hitbox's knockback in world space, with X pointing away from the attacker: toward the side of the attacker's origin the victim's origin is on, or the attacker's facing when both share an X.
KnockbackX, KnockbackY float32
// X and Y are the center of the overlap of the hitbox and the hurtbox it struck, where a game draws a hit spark.
X, Y float32
}Hit is one hitbox connecting with an opponent during a World.Step.
type Kind stringKind says whether a box can be hit or strikes.
const (
// KindHurt is a hurtbox: where the fighter can be hit.
KindHurt Kind = "hurt"
// KindHit is a hitbox: where the fighter strikes.
KindHit Kind = "hit"
)type Landed struct {
// Victim is the opponent's index in World.Fighters.
Victim int
// Groups has bit g set when hit group g has struck the opponent.
Groups uint64
}Landed records the hit groups a move instance has struck one opponent with.
type Move struct {
// Name identifies the move within its Doc.
Name string `yaml:"name"`
// Animation optionally names the sprite animation the game plays alongside the move, such as an entry of a sprite.Atlas's Animations.
Animation string `yaml:"animation,omitempty"`
// Length is how many frames the move lasts, from 1 to MaxLength.
Length int `yaml:"length"`
// Loop starts the move over after its last frame instead of ending it, for stances such as idle, walk and crouch.
Loop bool `yaml:"loop,omitempty"`
// Boxes are the move's hurtboxes and hitboxes, each active on a range of frames. Their order is the order hits are tested in.
Boxes []Box `yaml:"boxes,omitempty"`
// Cancels are the windows where the move may be cut short into other moves.
Cancels []Cancel `yaml:"cancels,omitempty"`
}Move is one animation's frame data: how long it lasts, its boxes on each frame and where it may be cancelled.
func (m *Move) FrameData() (FrameData, bool)FrameData returns the move's startup, active and recovery frames and its advantage, and false when the move has no hitbox.
type World struct {
// Fighters are the fighters of the match. Their order is the order hits are found and reported in.
Fighters []*Fighter
// contains filtered or unexported fields
}World resolves the hits between the fighters of a match, one fixed step at a time.
A World is not safe for concurrent use. A Fighter belongs to at most one World.
func (w *World) Restore(src *WorldState) errorRestore sets every fighter to the state in src, which Save took from this World or from one with the same fighters. It copies src, which the caller may keep and restore again.
It returns an error, and changes nothing, when src has a different number of fighters, a state for a nil fighter, a move its fighter's Moves does not have, a frame outside that move, or a Landed victim that is not another fighter of the World.
func (w *World) Save(dst *WorldState)Save copies the state of every fighter into dst, reusing dst's slices so that saving on every step allocates nothing once warm. The copy shares no memory with the World.
func (w *World) Step() []HitStep advances every fighter one frame, finds every hitbox that overlaps an opponent's hurtbox on that frame, and applies the hits: the attacker's contact opens its cancel windows, and the victim takes hitstun or blockstun and returns to its Neutral move.
Hits are found from the fighters' positions and moves before any hit of the step is applied, so fighters that strike each other on the same frame trade. A move instance hits each opponent once per hit group and at most once per step. A victim struck by several hits in one step takes the longest stun.
The returned hits are ordered by attacker, then victim, in Fighters order, and stay valid until the next Step.
type WorldBox struct {
// Kind is KindHurt or KindHit.
Kind Kind
// Rect is the box in world points, mirrored when the fighter faces left.
Rect math2d.Rect
// Index is the box's index in the current move's Boxes.
Index int
}WorldBox is one active box of a fighter placed in world points.
type WorldState struct {
// Fighters holds one state per entry of World.Fighters, in the same order. A nil fighter has a zero state.
Fighters []FighterState
}WorldState is a copy of everything World.Step reads and changes in a World's fighters, for replay and rollback net code to save a match and restore it later.
It names moves by name and fighters by their index in World.Fighters, so it holds no pointers and a game can encode it, compare it or hash it for a checksum. The fighters' configuration (Moves, Neutral and Team) is not part of it: a restore expects the World to have the same fighters with the same configuration.