Files
bl/logic/controller/map.go
2025-11-15 22:17:43 +00:00

72 lines
2.4 KiB
Go
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

package controller
import (
"blazing/common/socket/errorcode"
"blazing/logic/service/common"
"blazing/logic/service/maphot"
"blazing/logic/service/maps"
"blazing/logic/service/maps/info"
"blazing/logic/service/player"
"blazing/logic/service/space"
"github.com/jinzhu/copier"
)
func (h *Controller) MapEnter(data *maps.InInfo, c *player.Player) (result *info.OutInfo, err errorcode.ErrorCode) { //这个时候player应该是空的
c.Info.MapID = data.MapId //登录地图
space.GetSpace(c.Info.MapID).User.Store(c.Info.UserID, c) //添加玩家
result = info.NewOutInfo()
c.Info.Pos = data.Point
copier.Copy(result, c.Info)
go space.GetSpace(c.Info.MapID).EnterMap(c) //玩家进入地图
return result, -1
}
func (h Controller) MapHot(data *maphot.InInfo, c *player.Player) (result *maphot.OutInfo, err errorcode.ErrorCode) {
result = &maphot.OutInfo{
HotInfos: space.GetMapHot(),
}
return
}
func (h *Controller) MapLeave(data *maps.LeaveMapInboundInfo, c *player.Player) (result *info.LeaveMapOutboundInfo, err errorcode.ErrorCode) { //这个时候player应该是空的
//result = &maps.LeaveMapOutboundInfo{UserID: c.GetUserID()}
c.Canmon = false
c.Changemap = true //可以刷怪
//data.Broadcast(c.Info.MapID, info.LeaveMapOutboundInfo{UserID: c.Info.UserID}) //同步广播
go space.GetSpace(c.Info.MapID).LeaveMap(c) //玩家离开地图
// 如果有正在运行的刷怪协程,发送停止信号
c.Info.MapID = 0 // 重置当前地图
return &info.LeaveMapOutboundInfo{UserID: c.Info.UserID}, -1
}
func (h *Controller) MapList(data *maps.ListMapPlayerInboundInfo, c *player.Player) (result *maps.ListMapPlayerOutboundInfo, err errorcode.ErrorCode) { //这个时候player应该是空的
result = &maps.ListMapPlayerOutboundInfo{}
result.Player = make([]info.OutInfo, 0)
space.GetSpace(c.Info.MapID).User.Range(func(playerID uint32, player common.PlayerI) bool {
result1 := info.NewOutInfo()
copier.CopyWithOption(result1, player.GetInfo(), copier.Option{DeepCopy: true})
result.Player = append(result.Player, *result1)
result.Player = LastFourElements(result.Player)
return false
})
c.Canmon = true //可以刷怪
return
}
func LastFourElements[T any](s []T) []T {
n := len(s)
if n <= 30 {
// 切片长度小于等于4时返回整个切片
return s
}
// 切片长度大于4时返回最后4个元素从n-4索引到末尾
return s[n-30:]
}