Documentation
¶
Index ¶
- Constants
- func RegisterSystem[T any](w *World, system ecs.System[T], opts ...ecs.SystemOption)
- func WithHook(hook ecs.SystemHook) ecs.SystemOption
- type BaseCommand
- type BaseEvent
- type BaseSystemState
- type Contains
- type Exact
- type OtherWorld
- type Ref
- type WithCommand
- type WithEvent
- type WithSystemEventEmitter
- type WithSystemEventReceiver
- type World
- type WorldOptions
Constants ¶
const Init = ecs.Init
Init runs once during world initialization.
const PostUpdate = ecs.PostUpdate
PostUpdate runs after the main update.
const PreUpdate = ecs.PreUpdate
PreUpdate runs before the main update.
const Update = ecs.Update
Update runs during the main update phase.
Variables ¶
This section is empty.
Functions ¶
func RegisterSystem ¶
RegisterSystem registers a system and its state with the world. By default, systems are registered to the Update hook. This can be overridden with the optional WithHook option.
Example:
type RegenSystemState struct {
cardinal.BaseSystemState
Players ecs.Exact[struct {
PlayerTag ecs.Ref[PlayerTag]
Health ecs.Ref[Health]
}]
}
world := cardinal.NewWorld()
cardinal.RegisterSystem(world, func(state *RegenSystemState) error {
// System logic here
return nil
})
func WithHook ¶
func WithHook(hook ecs.SystemHook) ecs.SystemOption
WithHook returns an option to set the system hook.
Types ¶
type BaseCommand ¶
type BaseCommand struct{}
BaseCommand is a base command type that all commands should embed.
type BaseEvent ¶
type BaseEvent struct{}
BaseEvent is a base event type that all events should embed.
type BaseSystemState ¶
type BaseSystemState struct {
ecs.BaseSystemState
// contains filtered or unexported fields
}
BaseSystemState provides common functionality for system state types. It should be embedded in your system state types to allow your systems to access the world state.
Example:
// Define your system state by embedding BaseState.
type DebugSystemState struct {
cardinal.BaseSystemState
// Other fields...
}
// Your system function receives a pointer to your system state.
func DebugSystem(state *DebugSystemState) error {
state.Logger().Debug().Int("tick", state.Tick()).Msg("...")
return nil
}
func (*BaseSystemState) Logger ¶
func (b *BaseSystemState) Logger() *zerolog.Logger
Logger returns the logger for the world.
func (*BaseSystemState) Tick ¶
func (b *BaseSystemState) Tick() uint64
Tick returns the current tick of the world.
func (*BaseSystemState) Timestamp ¶
func (b *BaseSystemState) Timestamp() time.Time
Timestamp returns the current timestamp of the world.
type OtherWorld ¶
OtherWorld is a type that represents the address of an external service.
func (OtherWorld) Send ¶
func (o OtherWorld) Send(state *BaseSystemState, command ecs.Command)
Send sends a command to an external service.
Example:
import external "another-game-shard/system"
// Define a shard address of one of your game shards. const MatchmakingService ecs.ServiceAddress = "world.argus.rampage.matchmaking"
func GameSystem(state *GameSystem) error {
MatchmakingService.Send(state, &external.EndGameCommand{
Winner: "Team 1",
})
}
type WithCommand ¶
type WithCommand[T ecs.Command] struct { ecs.WithCommand[T] }
WithCommand is a generic system state field that allows systems to receive commands of type T during each tick. Commands must embed a cardinal.BaseCommand that provides common methods for commands. The commands are automatically registered when the systems are registered.
Example:
// Define a command type for spawning players.
type SpawnPlayerCommand struct {
cardinal.BaseCommand
Name string
}
func (SpawnPlayerCommand) Name() string { return "spawn-player" }
// Define your system state.
type SpawnSystemState struct {
cardinal.BaseSystemState
SpawnPlayerCommands cardinal.WithCommand[SpawnPlayerCommand]
// Other fields...
}
// Your system function receives a pointer to your system state.
func SpawnSystem(state *SpawnSystemState) error {
// Process all spawn commands for the current tick.
for command := range state.SpawnPlayerCommands.Iter() {
state.Players.Create(PlayerTag{Name: command.Name}, Health{Value: 100})
}
return nil
}
type WithEvent ¶
WithEvent is a generic system state field that allows systems to emit events of type T. Events must embed a cardinal.BaseEvent that provides common methods for events. At the end of each tick, events are published to their respective subjects.
Example:
// Define an event type for player deaths.
type LevelUpEvent struct {
cardinal.BaseEvent
Nickname string
}
func (LevelUpEvent) Name() string { return "level-up" }
type LevelUpSystemState struct {
cardinal.BaseSystemState
LevelUpEvents cardinal.WithEvent[LevelUpEvent]
// Other fields...
}
// Emit a level up event from one system.
func LevelUpSystem(state *LevelUpSystemState) error {
state.LevelUpEvents.Emit(LevelUpEvent{Nickname: "Player1"})
return nil
}
type WithSystemEventEmitter ¶
type WithSystemEventEmitter[T ecs.SystemEvent] struct { ecs.WithSystemEventEmitter[T] }
WithSystemEventEmitter is a generic system state field that allows systems to emit system events of type T. System events don't have to be registered, and can be used to communicate between systems.
Example:
// Define a system event for player deaths.
type PlayerDeathEvent struct { Nickname string }
func (PlayerDeathEvent) Name() string { return "player-death" }
type CombatSystemState struct {
PlayerDeathEvents ecs.WithSystemEventEmitter[PlayerDeathEvent]
// Other fields...
}
// Emit a player death event from one system.
func CombatSystem(state *CombatSystemState) error {
state.PlayerDeathEvents.Emit(PlayerDeathEvent{Nickname: "Player1"})
return nil
}
type WithSystemEventReceiver ¶
type WithSystemEventReceiver[T ecs.SystemEvent] struct { ecs.WithSystemEventReceiver[T] }
WithSystemEventReceiver is a generic system state field that allows systems to receive system events of type T. System events don't have to be registered, and can be used to communicate between systems.
Example:
// Define a system event for player deaths.
type PlayerDeathEvent struct { Nickname string }
func (PlayerDeathEvent) Name() string { return "player-death" }
type GraveyardSystemState struct {
cardinal.BaseSystemState
PlayerDeathEvents cardinal.WithSystemEventReceiver[PlayerDeathEvent]
// Other fields...
}
// Receive a player death event from another system.
func GraveyardSystem(state *GraveyardSystemState) error {
for event := range state.PlayerDeathEvents.Iter() {
state.Logger().Info().Msgf("Player %s died", event.Nickname)
}
return nil
}
type World ¶
type World struct {
*micro.Shard // Embedded base shard functionalities
// contains filtered or unexported fields
}
World represents your game world and serves as the main entry point for Cardinal.
func NewWorld ¶
func NewWorld(opts WorldOptions) (*World, error)
NewWorld creates a new game world with the specified configuration.
type WorldOptions ¶
type WorldOptions struct {
Region string // Region the shard is deployed to
Organization string // The organization that owns this world
Project string // Name of the project within the organization
ShardID string // Unique ID for of world's instance
PrivateKey string // Hex-encoded Ed25519 private key for signing commands
PersonaFilePath string // Path to persist the persona ID file
EpochFrequency uint32 // Number of ticks per epoch
TickRate float64 // Number of ticks per second
SnapshotStorageType micro.SnapshotStorageType // Snapshot storage type
SnapshotStorageOptions micro.SnapshotStorageOptions // Optional snapshot storage options
}