feat: westworldwithmessaging. add entitymanager, telegram, messagehandlers, delayedmessages, expand baseentity
This commit is contained in:
1 parent
0e7482e04c
commit
129dd5d8b8
18 files changed
+505
-19
No files matched your search
@@ -1,4 +1,4 @@
|
||||
package westworldwithwoman
|
||||
package westworldwithmessaging
|
||||
|
||||
// State is the Go version of the C++ class template:
|
||||
//
|
||||
@@ -39,6 +39,14 @@ type State[T any] interface {
|
||||
|
||||
// Exit runs once, the moment the entity leaves this state.
|
||||
Exit(entity *T)
|
||||
|
||||
// OnMessage runs when the entity receives a Telegram while this
|
||||
// state is active (current or global). It returns true if this
|
||||
// state actually dealt with the message, false if it doesn't
|
||||
// care about that kind of message - StateMachine.HandleMessage
|
||||
// uses that return value to decide whether to also try routing
|
||||
// the message to the global state.
|
||||
OnMessage(entity *T, msg Telegram) bool
|
||||
}
|
||||
|
||||
// locationType is a temporary stand-in for the C++ enum
|
||||
@@ -116,6 +124,13 @@ func NewMiner(id int) *Miner {
|
||||
m.stateMachine.SetCurrentState(GoHomeAndSleepTilRested())
|
||||
m.stateMachine.SetGlobalState(MinerGlobalState())
|
||||
|
||||
// The book registers an entity as a separate step at the call
|
||||
// site ("Miner* Bob = new Miner(...); EntityMgr->RegisterEntity(Bob);").
|
||||
// We're folding that into the constructor instead, so every miner
|
||||
// is automatically registered and reachable by ID the moment it
|
||||
// exists - nobody can forget the second step.
|
||||
EntityMgr.RegisterEntity(m)
|
||||
|
||||
return m
|
||||
}
|
||||
|
||||
@@ -246,3 +261,11 @@ func (m *Miner) Update() {
|
||||
func (m *Miner) ChangeState(newState State[Miner]) {
|
||||
m.stateMachine.ChangeState(newState)
|
||||
}
|
||||
|
||||
// HandleMessage is the Go version of Miner::HandleMessage - it does
|
||||
// nothing itself, just routes the telegram straight to the state
|
||||
// machine, which knows how to try the current state first and the
|
||||
// global state second.
|
||||
func (m *Miner) HandleMessage(msg Telegram) bool {
|
||||
return m.stateMachine.HandleMessage(msg)
|
||||
}
|
||||
Reference in new issue
Block a user