This commit is contained in:
2025-07-04 23:41:19 +08:00
parent 7c2c32a31a
commit f0fd00d706
27 changed files with 1206 additions and 163 deletions

76
Server/scene/npc/npc.go Normal file
View File

@@ -0,0 +1,76 @@
package npc
import (
"common/proto/gen/grpc_pb"
)
// NPCNode 定义NPC节点
type NPCNode struct {
UID int // 用户ID
GatewaySID int64 // 网关服务ID
Position [2]float64 // 二维坐标 [x, y]
MoveCross int8 // 移动十字1 上 2 下 4 左 8 右
Action []*grpc_pb.ActionReq // 其他操作
}
func NewNPCNode(gatewaySID int64, uid int) *NPCNode {
return &NPCNode{
UID: uid,
GatewaySID: gatewaySID,
Position: [2]float64{0, 0},
MoveCross: 0,
Action: make([]*grpc_pb.ActionReq, 0),
}
}
// 逻辑帧-移动
func (p *NPCNode) logicMove() bool {
if p.MoveCross&DirUp != 0 && p.MoveCross&DirDown != 0 {
p.MoveCross &^= DirUp | DirDown
}
if p.MoveCross&DirLeft != 0 && p.MoveCross&DirRight != 0 {
p.MoveCross &^= DirLeft | DirRight
}
var moveX, moveY float64
if p.MoveCross&DirUp != 0 {
moveY += 1
}
if p.MoveCross&DirDown != 0 {
moveY -= 1
}
if p.MoveCross&DirLeft != 0 {
moveX -= 1
}
if p.MoveCross&DirRight != 0 {
moveX += 1
}
// 没有移动
if moveX == 0 && moveY == 0 {
return false
}
if moveX != 0 && moveY != 0 {
const diagonalFactor = 0.7071
moveX *= diagonalFactor
moveY *= diagonalFactor
}
speed := 10.0
p.Position[0] += moveX * speed
p.Position[1] += moveY * speed
if p.Position[0] < 0 {
p.Position[0] = 0
}
if p.Position[0] > 400 {
p.Position[0] = 400
}
if p.Position[1] < 0 {
p.Position[1] = 0
}
if p.Position[1] > 400 {
p.Position[1] = 400
}
return true
}