Upgrade to Pro — share decks privately, control downloads, hide ads and more …

ebitenのマルチプレイ対応 | WebSocketの活用

ponyo877
March 29, 2024

ebitenのマルチプレイ対応 | WebSocketの活用

【ゲーム開発/Go】Ebitengine ぷちConf #2【ゆるいLTと交流会】
https://gocon.connpass.com/event/306765/

ponyo877

March 29, 2024
Tweet

More Decks by ponyo877

Other Decks in Technology

Transcript

  1. ॲཧͷྲྀΕ ೖࣨ ΫϥΠΞϯτ" ৽نೖࣨ 8FC4PDLFUαʔό ΫϥΠΞϯτ# ΫϥΠΞϯτ"ΑΓલʹೖࣨࡁΈ ೖࣨ ೖࣨड෇ ৽Ϣʔβ

    ࣗ਎ ొ࿥ Ϣʔβొ࿥ ৽Ϣʔβొ࿥ ΫϥΠΞϯτʹೖࣨ ௨஌ طଘϢʔβ ϝοηʔ δཤྺऔಘ௨஌ طଘϢʔβ ϝοηʔ δཤྺొ࿥ อଘ ྖҬ ʹ͋ Δ΋ ͷΛ ৗʹ ࠶ඳ ը อଘ ྖҬ ʹ͋ Δ΋ ͷΛ ৗʹ ࠶ඳ ը %# %# ϒϥ΢βͷ อଘྖҬ ϒϥ΢βͷ อଘྖҬ Ϣʔβ*% ࠲ඪ Y Z  ޲͖
  2. ॲཧͷྲྀΕ ϝοηʔδૹ৴ ΫϥΠΞϯτ" ೖࣨࡁΈ 8FC4PDLFUαʔό ΫϥΠΞϯτ# ೖࣨࡁΈ ϝοηʔδΛૹ৴ ϝοηʔδड෇ ϝοηʔδొ࿥

    ϝοηʔδ௨஌ ϝοηʔδ ࣗ෼ͷ ొ ࿥ ϝοηʔδొ࿥ Ϣʔβ*% ϝοηʔδ %# ϒϥ΢βͷ อଘྖҬ ϒϥ΢βͷ อଘྖҬ อଘ ྖҬ ʹ͋ Δ΋ ͷΛ ৗʹ ࠶ඳ ը อଘ ྖҬ ʹ͋ Δ΋ ͷΛ ৗʹ ࠶ඳ ը ϝοηʔδͷ9࠲ඪൃݴΩϟϥΫλͷ9࠲ඪͱಉ͡ ϝοηʔδͷ:࠲ඪൃݴ࣌ࠁݱࡏ࣌ࠁΛ΋ͱʹܭࢉ ු͔ͼ্͕Δ෩
  3. ෇࿥c࣮૷ྫ FCJUFOΫϥΠΞϯτ w ը໘ͷૢ࡞͝ͱʹԿΛૹΔ͔ܾΊͯૹΔɺجຊతʹ(BNF6QEBUF಺Ͱ࣮૷ func (g *Game) Update() error {

    // ΩϟϥΫλʔͷҠಈ if dir != entity.DirUnknown { x, y := g.characters[g.id].Point() g.wss.Send(entity.NewMessage("move", entity.NewMoveBody(g.id, x, y, dir))) } // ϝοηʔδͷૹ৴ if ebiten.IsKeyPressed(ebiten.KeyEnter) { text := g.textField.Text() g.textField.Clear() g.wss.Send(entity.NewMessage("say", entity.NewSayBody(g.id, text))) } return nil }
  4. ෇࿥c࣮૷ྫ 8FC4PDLFUαʔό func readMessage(session *session, service socket.UseCase) { ɹ for

    { var mp presenter.MessagePresenter if err := session.Read(&mp); err != nil { return } switch mp.MessageType { // ΩϟϥΫλʔͷҠಈ ΩϟϥΫλʔͷ࠲ඪΛӬଓԽ case "move": u := entity.NewUser(mp.Body.ID, mp.Body.X, mp.Body.Y, entity.Dir(mp.Body. if err := service.UpdateUser(u); err != nil { return } body := entity.NewEnterBody(users) mp := presenter.NewMessagePresenter(entity.NewMessage("enter", body)) if err := session.Write(&mp); err != nil { return } continue } // ϝοηʔδͷૹ৴ ϝοηʔδͷ಺༰ΛӬଓԽ case "say": if err := service.AddMessage(mp.Unmarshal()); err != nil { return } // ΩϟϥΫλʔͷҠಈ or ϝοηʔδͷૹ৴ΛଞͷϢʔβʹ఻ൖ if err := service.Publish(mp.Unmarshal()); err != nil { return } } } w 8FCTPDLFUͷϦΫΤετ ͷ۠෼ΛಡΈऔΓɺ۠෼ ಛ༗ͷૢ࡞Λ࣮ࢪ w ͦͷޙɺଞϢʔβ΁ૢ࡞ ಺༰Λૹ৴