player.go 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. package game
  2. import (
  3. "github.com/gorilla/websocket"
  4. )
  5. //玩家
  6. type Player struct {
  7. Conn *websocket.Conn
  8. IsValid bool
  9. //当前所在的房间
  10. Room *Room
  11. //用户信息(需要客户端上传)
  12. userInfo interface{}
  13. }
  14. func (player *Player) OnLoad() {
  15. player.IsValid = true
  16. }
  17. func (player *Player) OnDestroy() {
  18. player.IsValid = false
  19. //断开连接时,玩家就要离开房间
  20. if player.Room != nil {
  21. player.Room.Leave(player)
  22. }
  23. }
  24. //RPC接口-请求进入房间
  25. func (player *Player) RequestEnterRoom(userInfo interface{}) {
  26. player.userInfo = userInfo
  27. if player.Room == nil {
  28. MatchAndEnterRoom(player)
  29. }
  30. }
  31. //RPC接口-上传数据帧
  32. func (player *Player) UploadFrames(frames []interface{}) {
  33. if player.Room != nil {
  34. player.Room.CacheFrames(frames)
  35. }
  36. }
  37. //通知客户端-匹配完成
  38. func (player *Player) Notify_onMatchComplete(id int, playerInfos map[int]interface{}, uuid_ string, timestamp_ int64) {
  39. SendPackFunction(player.Conn, "onMatchComplete", id, playerInfos, uuid_, timestamp_)
  40. }
  41. //通知客户端-游戏开始
  42. func (player *Player) Notify_onGameStart() {
  43. SendPackFunction(player.Conn, "onGameStart")
  44. }
  45. //通知客户端-下发帧同步数据
  46. func (player *Player) Notify_onFrameSync(frames []interface{}) {
  47. SendPackFunction(player.Conn, "onFrameSync", frames)
  48. }