func (e *Engine) broadcastChanges() { if e.ctx.Entity.IsDirty() { log.Println("Entity diff dirty, broadcasting to clients...") err := e.srv.Write(func(w io.Writer) error { return builder.SendEntitiesDiffToClients(w, e.ctx.Entity.Flush()) }) if err != nil { log.Println("Cannot broadcast entities diff:", err) } } if e.ctx.Action.IsDirty() { log.Println("Actions diff dirty, broadcasting to clients...") err := e.srv.Write(func(w io.Writer) error { return builder.SendActionsDone(w, e.clock.GetRelativeTick(), e.ctx.Action.Flush()) }) if err != nil { log.Println("Cannot broadcast actions:", err) } } }
return err } } // Register new entity req := ctx.Entity.Add(session.Entity, ctx.Clock.GetAbsoluteTick()) // TODO: move this elsewhere? err = req.Wait() log.Println("Entity registered!") if err != nil { return err } // Broadcast new entity to other clients log.Println("Flushing entities diff") err = conn.Broadcast(func(w io.Writer) error { return builder.SendEntitiesDiffToClients(w, ctx.Entity.Flush()) }) if err != nil { return err } // Send new entity to this client err = conn.Write(func(w io.Writer) error { return builder.SendEntityCreate(w, ctx.Clock.GetRelativeTick(), session.Entity) }) if err != nil { return err } // Mark the io as ready // It will now receive all broadcasts