Files
bl/logic/controller/map.go
2025-08-27 20:52:15 +00:00

194 lines
4.7 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/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"
"math/rand"
"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) {
time.After(5 * time.Second)
// 首次刷新
if !c.IsFighting && c.MapId != 0 {
spawnMonsters(c)
}
//循环刷新怪物
ticker := time.NewTicker(10 * 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, generateThreeUniqueNumbers())
c.SendPack(tt.Pack(&t1))
}
// 应该根据怪物信息决定后端生成
func genMonster(mapid uint32, wz [3]int) maps.OgreInfo {
// 设置怪物信息
t1 := maps.OgreInfo{}
for i := 0; i < 3; i++ {
ttt := maps.OgrePetInfo{}
ttt.Id = mservice.NewMonsterService().GetId(mapid) //待修改成xml获取
ttt.Shiny = uint32(i + 1) //异色概率,待实现自定义
//t1.Data[i] = mservice.NewMonsterService().GetId(c.MapId)
t1.Data[wz[i]] = ttt
}
return t1
}
// 计算整数的二进制1的个数Integer.bitCount
func bitsCount(n int) int {
count := 0
for n > 0 {
count += n & 1
n >>= 1
}
return count
}
// 生成0-9之间三个不重复的随机数 进地图5s
func generateThreeUniqueNumbers() [3]int {
rand.Seed(time.Now().UnixNano())
selected := make(map[int]bool)
var result [3]int
index := 0
for index < 3 {
num := rand.Intn(10)
if !selected[num] {
selected[num] = true
result[index] = num
index++
}
}
return result
}
// 从三个数字中移除一个并从剩余6个数字中选一个补充 10s
func replaceOneNumber(original [3]int) ([3]int, int, int) {
// 随机选择要移除的索引0-2
removeIndex := rand.Intn(3)
removedNum := original[removeIndex]
// 找出所有不在原始数组中的数字(候选数字)
candidates := []int{}
originalMap := make(map[int]bool)
for _, num := range original {
originalMap[num] = true
}
for i := 0; i < 10; i++ {
if !originalMap[i] {
candidates = append(candidates, i)
}
}
// 从候选数字中随机选择一个
newNum := candidates[rand.Intn(len(candidates))]
// 创建新数组并替换数字
newNumbers := original
newNumbers[removeIndex] = newNum
return newNumbers, removedNum, newNum
}