提交战斗系统

This commit is contained in:
1
2025-08-28 02:27:14 +00:00
parent b36ff6d0f1
commit 7dddc0400d
11 changed files with 151 additions and 165 deletions

View File

@@ -3,12 +3,10 @@ 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"
)
@@ -36,7 +34,7 @@ func (h *Controller) MapEnter(data *maps.InInfo, c *entity.Player) (result *maps
time.After(5 * time.Second)
// 首次刷新
if !c.IsFighting && c.MapId != 0 {
spawnMonsters(c)
data.SpawnMonsters(c, true)
}
//循环刷新怪物
@@ -51,7 +49,7 @@ func (h *Controller) MapEnter(data *maps.InInfo, c *entity.Player) (result *maps
case <-ticker.C:
// 刷新当前地图的怪物
if !c.IsFighting && c.MapId != 0 {
spawnMonsters(c)
data.SpawnMonsters(c, false)
}
}
@@ -92,102 +90,3 @@ func (h *Controller) MapList(data *maps.ListMapPlayerInboundInfo, c *entity.Play
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
}