Migrating from 0.3.0 to 0.4.0
go get github.com/Leonard-Atorough/castrum@v0.4.0
go mod tidy
Apply the changes below that affect your game. Code snippets are excerpts.
Game setup and systems
Replace game and runner creation:
// 0.3
game, err := castrum.New(castrum.WithTitle("My game"))
if err != nil {
return err
}
runner, err := ebitrun.New(game)
if err != nil {
return err
}
return game.Run(runner)
// 0.4
game := castrum.New(castrum.WithTitle("My game"))
ebitrun.New().Run(game)
castrum.New, ebitrun.New and Runner.Run now panic on failure. Window and
graphics options belong on the runner. Game.AddSystem and
Game.SetTimeScale still return errors.
- Replace
core.Systemimplementations with functions. An existing system'sUpdatemethod can be registered asgame.AddSystem(phase, name, system.Update). - Replace
AddSystemToGroup(phase, group, name, system)withAddSystem(phase, name, system, castrum.InGroup(group)). - Remove
WithAnimation,WithCollisionandWithTimer: these systems now run by default. Remove any duplicate manual registrations. - Replace
WithDefaultSystemswithWithScenes()if your game uses scenes. Animation, collision and timers are already enabled.
Entities and queries
- Replace
*core.Entityfields, slices and parameters withcore.Entity. - Replace
entity.IsAlive()withworld.HasEntity(entity). The new check tests world membership; the old check tested the handle's local state.Killdid not destroy the entity; useworld.DestroyEntity(entity)to destroy it. - Replace query
WithwithRequireandWithoutwithExclude.Requireno longer restricts entry access to the listed components; useOmitif you need to hide a component. - Replace query entry
UpdateandSetComponentwrites withentry.ComponentPtr[T]()writes, or use a typed query. Pointer writes bypass component validation; useentity.Updateorentity.SetComponentwhen validation is required. - Keep entity creation, destruction and component additions or removals outside query passes.
For example:
// 0.3
query := core.NewQuery(world).With(Position{}, Velocity{})
for entry := range query.Execute() {
velocity, _ := entry.Component[Velocity]()
entry.Update(func(position *Position) {
position.X += velocity.X
})
}
// 0.4
query := core.NewQuery2[Position, Velocity](world)
for _, row := range query.Execute() {
row.A.X += row.B.X
}
Input, window and time
Replace removed context fields with world resources. Resource returns a
value and an error; handle the error at the call site.
| 0.3 | 0.4 |
|---|---|
ctx.Input | ctx.World.Resource[*input.Snapshot]() |
ctx.Actions | ctx.World.Resource[input.Actions]() |
ctx.LogicalWidth, ctx.LogicalHeight | render.Window resource, then window.Viewport() |
ctx.Frame | *gametime.Real resource, then real.Frame |
input.Actions requires configured bindings. Convert binding values such as
input.KeyInput{Key: input.KeySpace} to
input.KeyInput{Key: input.KeySpace}.Binding(), including members passed to
input.Chord. input.Chord(...) itself already returns an input.Input.
Replace imports of timer with gametime, including:
| 0.3 | 0.4 |
|---|---|
timer.NewTimer | gametime.NewTimer |
timer.NewSystem | gametime.NewTimerSystem |
timer.Timer, timer.Groups, timer.Completed | gametime.Timer, gametime.Groups, gametime.Completed |
gametime.Completed.Entity is an entity handle rather than an ID.
Rendering and cameras
Replace render.Sprite and its Drawable with components from sprite:
| 0.3 drawable | 0.4 component |
|---|---|
render.TextureSource | sprite.Sprite with Asset |
render.AtlasSource | sprite.Sprite with sheet Asset and Region |
render.TextSource | sprite.Text |
render.RectShape, render.CircleShape, render.LineShape | sprite.Primitive |
Move visual fields such as Layer, Color and flips into sprite.Style.
Style.Color is a *color.RGBA, not a color.Color.
- Replace
render.SpawnCamerawithrender.NewCameraandCamera.MovePositionwithCamera.Translate. - Replace
render.CameraViewreads with the selectedrender.Cameracomponent's conversion and framing methods. They returnfalseuntil a view has been collected.
Animation
| 0.3 | 0.4 |
|---|---|
animation.ClipStore | animation.Sets resource |
animation.Clip | animation.Track in an animation.State |
animation.Animation | animation.Animator |
Animation.Clip | Animator.Set and Animator.State |
Animation.PlaybackMultiplier | Animator.Speed |
Animation.Loop: LoopNone / LoopForever | State.Loop: LoopOnce / Loop |
Convert each clip's atlas and frame names to a track referencing a sheet document and regions. Register the sets as a world resource.
Remove any sprite used solely to display the old animation: the animator
draws its own frames. Update completion handling to use
animation.Completed or Animator.Finished. See the
animation example
for a complete conversion.
Audio
Replace the old audio.Source component with an audio.Player:
player, err := audio.NewPlayer(
audio.Track("audio/music.mp3"),
audio.Settings{Bus: "music", Volume: 1, Load: audio.LoadStream, Loop: audio.LoopTrack},
)
if err != nil {
return err
}
if _, err := world.NewEntity(player); err != nil {
return err
}
- Move source playback settings to
Player.Settings. Set a volume explicitly: the zero value is silent. - Replace
GroupMusicandGroupSFXwith named buses, updating players and mixer calls together. ReplaceSetGroupVolumewithSetBusVolumeandSetMasterwithSetMasterVolume. - Replace
LoopForeverwithLoopTrackfor a single repeating track. - Update saved playback data: serialized players restore desired state, not the exact playback position.
Scenes
Replace scene.Blueprint and scene.EntitySpec with a scene.Scene builder
and scene.Node tree. Replace each entity spec's Name with a node Key:
room := scene.Of(func(name string) scene.Node {
return scene.Node{
Key: name,
Components: []any{core.Transform{}},
}
})
director, err := world.Resource[*scene.Director]()
if err != nil {
return err
}
_, err = director.Load(room, "hangar", scene.WithName("hangar"))
if err != nil {
return err
}
- Account for the root node creating an entity; child nodes are not a transform hierarchy.
- Move blueprint hooks to
scene.WithHooks; their callbacks now receive*scene.Handleinstead of*scene.Instance. - Replace blueprint asset IDs with
scene.Warm[T]declarations on nodes. These now load assets when the scene enters. - Convert nested blueprints to nodes or separately loaded instances.
Assets and codecs
- Replace atlas registration with
asset.Sheetdocuments and file-backed sheet references. Preserve the region names used by sprites and animations. - Move
loader.RegisterDecodercalls tocodecs.SetDecoder. For JSON types, register the encoder and decoder withcodecs.SetJSON[T](). - Pass the codecs to the game with
castrum.WithAssetCodecs(codecs). - For manually constructed loaders, replace
asset.NewLoader(filesystem)withasset.NewLoader(filesystem, asset.LoaderOptions{Codecs: codecs})and handle the returned error. - Check saved data against the new file formats: JSON decoding now rejects additional values after the first.
Verify the upgrade
Run your game's tests and build its modules. Check scene transitions, rendering, animation, audio and input in the running game.