Skip to main content

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.System implementations with functions. An existing system's Update method can be registered as game.AddSystem(phase, name, system.Update).
  • Replace AddSystemToGroup(phase, group, name, system) with AddSystem(phase, name, system, castrum.InGroup(group)).
  • Remove WithAnimation, WithCollision and WithTimer: these systems now run by default. Remove any duplicate manual registrations.
  • Replace WithDefaultSystems with WithScenes() if your game uses scenes. Animation, collision and timers are already enabled.

Entities and queries​

  • Replace *core.Entity fields, slices and parameters with core.Entity.
  • Replace entity.IsAlive() with world.HasEntity(entity). The new check tests world membership; the old check tested the handle's local state. Kill did not destroy the entity; use world.DestroyEntity(entity) to destroy it.
  • Replace query With with Require and Without with Exclude. Require no longer restricts entry access to the listed components; use Omit if you need to hide a component.
  • Replace query entry Update and SetComponent writes with entry.ComponentPtr[T]() writes, or use a typed query. Pointer writes bypass component validation; use entity.Update or entity.SetComponent when 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.30.4
ctx.Inputctx.World.Resource[*input.Snapshot]()
ctx.Actionsctx.World.Resource[input.Actions]()
ctx.LogicalWidth, ctx.LogicalHeightrender.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.30.4
timer.NewTimergametime.NewTimer
timer.NewSystemgametime.NewTimerSystem
timer.Timer, timer.Groups, timer.Completedgametime.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 drawable0.4 component
render.TextureSourcesprite.Sprite with Asset
render.AtlasSourcesprite.Sprite with sheet Asset and Region
render.TextSourcesprite.Text
render.RectShape, render.CircleShape, render.LineShapesprite.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.SpawnCamera with render.NewCamera and Camera.MovePosition with Camera.Translate.
  • Replace render.CameraView reads with the selected render.Camera component's conversion and framing methods. They return false until a view has been collected.

Animation​

0.30.4
animation.ClipStoreanimation.Sets resource
animation.Clipanimation.Track in an animation.State
animation.Animationanimation.Animator
Animation.ClipAnimator.Set and Animator.State
Animation.PlaybackMultiplierAnimator.Speed
Animation.Loop: LoopNone / LoopForeverState.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 GroupMusic and GroupSFX with named buses, updating players and mixer calls together. Replace SetGroupVolume with SetBusVolume and SetMaster with SetMasterVolume.
  • Replace LoopForever with LoopTrack for 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.Handle instead 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.Sheet documents and file-backed sheet references. Preserve the region names used by sprites and animations.
  • Move loader.RegisterDecoder calls to codecs.SetDecoder. For JSON types, register the encoder and decoder with codecs.SetJSON[T]().
  • Pass the codecs to the game with castrum.WithAssetCodecs(codecs).
  • For manually constructed loaders, replace asset.NewLoader(filesystem) with asset.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.