You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

42 lines
997 B

  1. package api
  2. import (
  3. "fmt"
  4. "github.com/golang/protobuf/proto"
  5. "zinx/ziface"
  6. "zinx/zinx_app_demo/mmo_game/core"
  7. "zinx/zinx_app_demo/mmo_game/pb"
  8. "zinx/znet"
  9. )
  10. //玩家移动
  11. type MoveApi struct {
  12. znet.BaseRouter
  13. }
  14. func (*MoveApi) Handle(request ziface.IRequest) {
  15. //1. 将客户端传来的proto协议解码
  16. msg := &pb.Position{}
  17. err := proto.Unmarshal(request.GetData(), msg)
  18. if err != nil {
  19. fmt.Println("Move: Position Unmarshal error ", err)
  20. return
  21. }
  22. //2. 得知当前的消息是从哪个玩家传递来的,从连接属性pid中获取
  23. pid, err := request.GetConnection().GetProperty("pid")
  24. if err != nil {
  25. fmt.Println("GetProperty pid error", err)
  26. request.GetConnection().Stop()
  27. return
  28. }
  29. fmt.Printf("user pid = %d , move(%f,%f,%f,%f)", pid, msg.X, msg.Y, msg.Z, msg.V)
  30. //3. 根据pid得到player对象
  31. player := core.WorldMgrObj.GetPlayerByPid(pid.(int32))
  32. //4. 让player对象发起移动位置信息广播
  33. player.UpdatePos(msg.X, msg.Y, msg.Z, msg.V)
  34. }