143 lines
3.4 KiB
Go
143 lines
3.4 KiB
Go
package controller
|
||
|
||
import (
|
||
"blazing/common/data/entity"
|
||
"blazing/common/socket/errorcode"
|
||
"blazing/common/socket/handler"
|
||
"blazing/logic/service/maphot"
|
||
"blazing/logic/service/maps"
|
||
"blazing/logic/service/space"
|
||
mservice "blazing/modules/blazing/service"
|
||
"time"
|
||
)
|
||
|
||
func (h *Controller) MapEnter(data *maps.InInfo, c *entity.Player) (result *maps.OutInfo, err errorcode.ErrorCode) { //这个时候player应该是空的
|
||
|
||
c.MapId = data.MapId //登录地图
|
||
space.GetSpace(c.MapId).Set(c.UserID, c) //添加玩家
|
||
tt := maps.NewOutInfo()
|
||
tt.UserID = c.UserID
|
||
tt.Nick = c.Nick
|
||
tt.Pos = data.Point
|
||
data.Broadcast(c.MapId, *tt) //同步广播
|
||
// 如果是无怪地图,直接返回
|
||
|
||
if mservice.NewMonsterService().GetId(c.MapId) == 0 {
|
||
return nil, -1
|
||
}
|
||
|
||
// 创建新的停止通道
|
||
c.StopChan = make(chan struct{})
|
||
|
||
// 启动刷怪协程
|
||
go func(stopChan chan struct{}, currentMap int) {
|
||
ticker := time.NewTicker(5 * time.Second)
|
||
defer ticker.Stop()
|
||
|
||
for {
|
||
select {
|
||
case <-stopChan:
|
||
// 收到停止信号,退出协程
|
||
return
|
||
case <-ticker.C:
|
||
// 刷新当前地图的怪物
|
||
if !c.IsFighting && c.MapId != 0 {
|
||
spawnMonsters(c)
|
||
}
|
||
|
||
}
|
||
}
|
||
}(c.StopChan, int(c.MapId))
|
||
return nil, -1
|
||
}
|
||
func (h Controller) MapHot(data *maphot.InInfo, c *entity.Player) (result *maphot.OutInfo, err errorcode.ErrorCode) {
|
||
|
||
result = &maphot.OutInfo{
|
||
|
||
HotInfos: space.GetMapHot(),
|
||
}
|
||
|
||
return
|
||
}
|
||
func (h *Controller) MapLeave(data *maps.LeaveMapInboundInfo, c *entity.Player) (result *maps.LeaveMapOutboundInfo, err errorcode.ErrorCode) { //这个时候player应该是空的
|
||
//result = &maps.LeaveMapOutboundInfo{UserID: c.GetUserID()}
|
||
data.Broadcast(c.MapId, maps.LeaveMapOutboundInfo{UserID: c.GetUserID()}) //同步广播
|
||
space.GetSpace(c.MapId).Delete(c.UserID)
|
||
// 如果有正在运行的刷怪协程,发送停止信号
|
||
if c.StopChan != nil {
|
||
close(c.StopChan)
|
||
c.StopChan = nil
|
||
}
|
||
c.MapId = 0 // 重置当前地图
|
||
return nil, -1
|
||
}
|
||
func (h *Controller) MapList(data *maps.ListMapPlayerInboundInfo, c *entity.Player) (result *maps.ListMapPlayerOutboundInfo, err errorcode.ErrorCode) { //这个时候player应该是空的
|
||
|
||
result = &maps.ListMapPlayerOutboundInfo{}
|
||
result.Player = make([]maps.OutInfo, 0)
|
||
result1 := maps.NewOutInfo()
|
||
|
||
result1.UserID = c.UserID
|
||
//result.Pos = model.Pos{X: 500, Y: 400}
|
||
result1.Nick = c.Nick
|
||
result.Player = append(result.Player, *result1)
|
||
return
|
||
}
|
||
|
||
// 刷怪具体实现
|
||
func spawnMonsters(c *entity.Player) {
|
||
// 获取当前地图的怪物配置
|
||
|
||
if c == nil || mservice.NewMonsterService().GetId(c.MapId) == 0 { //用户离线
|
||
return
|
||
}
|
||
if !c.IsLogin {
|
||
defer func() {
|
||
|
||
if c.StopChan != nil {
|
||
close(c.StopChan)
|
||
c.StopChan = nil
|
||
}
|
||
}()
|
||
}
|
||
|
||
// 创建数据包
|
||
tt := handler.NewTomeeHeader(2004, c.UserID)
|
||
t1 := genMonster(c.MapId)
|
||
|
||
c.SendPack(tt.Pack(&t1))
|
||
}
|
||
|
||
func genMonster(id uint32) maps.OgreInfo {
|
||
// 设置怪物信息
|
||
t1 := maps.OgreInfo{}
|
||
var localMenu = make([]int, 0)
|
||
for i := 7; i <= 448; i++ {
|
||
if bitsCount(i) == 3 {
|
||
localMenu = append(localMenu, i)
|
||
}
|
||
}
|
||
|
||
for i := 0; i < 3; i++ {
|
||
|
||
ttt := maps.OgrePetInfo{}
|
||
ttt.Id = mservice.NewMonsterService().GetId(id)
|
||
|
||
ttt.Shiny = uint32(i + 1)
|
||
//t1.Data[i] = mservice.NewMonsterService().GetId(c.MapId)
|
||
t1.Data[i] = ttt
|
||
}
|
||
|
||
return t1
|
||
}
|
||
|
||
// 计算整数的二进制1的个数(对应Java的Integer.bitCount)
|
||
func bitsCount(n int) int {
|
||
count := 0
|
||
for n > 0 {
|
||
count += n & 1
|
||
n >>= 1
|
||
}
|
||
return count
|
||
}
|