提交战斗系统

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

@@ -8,3 +8,4 @@ type OgrePetInfo struct {
Id uint32
Shiny uint32
}

View File

@@ -5,6 +5,8 @@ import (
"blazing/common/socket/handler"
"blazing/logic/service/space"
"blazing/modules/blazing/model"
"math/rand"
"time"
"github.com/creasty/defaults"
)
@@ -16,8 +18,8 @@ type InInfo struct {
MapId uint32
// Point: 直接给坐标xy
Point model.Pos `fieldDesc:"直接给坐标xy"`
Point model.Pos `fieldDesc:"直接给坐标xy"`
monsters [3]int
// Reverse2: 暂定 占位字符2
//Reverse2 string `struc:"[2]byte"`
}
@@ -32,6 +34,112 @@ func (t *InInfo) Broadcast(mapid uint32, o OutInfo) {
})
}
// 刷怪具体实现
func (t *InInfo) SpawnMonsters(c *entity.Player, isfrist bool) {
// 获取当前地图的怪物配置
// 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)
if isfrist {
t.monsters = generateThreeUniqueNumbers()
} else {
t.monsters, _, _ = replaceOneNumber(t.monsters)
}
t1 := t.genMonster(c.MapId)
c.SendPack(tt.Pack(&t1))
}
// 应该根据怪物信息决定后端生成
func (t *InInfo) genMonster(mapid uint32) OgreInfo {
// 设置怪物信息
t1 := OgreInfo{}
for i := 0; i < 3; i++ {
ttt := OgrePetInfo{}
ttt.Id = mservice.NewMonsterService().GetId(mapid) //待修改成xml获取
ttt.Shiny = uint32(i + 1) //异色概率,待实现自定义
//t1.Data[i] = mservice.NewMonsterService().GetId(c.MapId)
t1.Data[t.monsters[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
}
// 这里存储星球的map
//var planetmap utils.SyncMap[] //= space.NewSyncMap()