feat 结构调整
This commit is contained in:
64
Server/scene/internal/npc/player.go
Normal file
64
Server/scene/internal/npc/player.go
Normal file
@@ -0,0 +1,64 @@
|
||||
package npc
|
||||
|
||||
import (
|
||||
"common/proto/sc/sc_pb"
|
||||
"common/proto/ss/grpc_pb"
|
||||
)
|
||||
|
||||
// PlayerNode 定义玩家节点结构体
|
||||
type PlayerNode struct {
|
||||
UID int32 // 用户ID
|
||||
GatewaySID int64 // 网关服务ID
|
||||
MoveSpeed float32 // 移动速度
|
||||
|
||||
Position [2]float32 // 二维坐标 [x, y]
|
||||
MoveDirection [2]int8 // 移动方向 [x, y]
|
||||
MoveCount int8 // 移动指令使用计数
|
||||
Action []*grpc_pb.ActionReq // 其他操作
|
||||
}
|
||||
|
||||
func NewPlayerNode(gatewaySID int64, uid int32) *PlayerNode {
|
||||
return &PlayerNode{
|
||||
UID: uid,
|
||||
GatewaySID: gatewaySID,
|
||||
MoveSpeed: 5 * 0.00001,
|
||||
Position: [2]float32{0, 0},
|
||||
Action: make([]*grpc_pb.ActionReq, 0),
|
||||
}
|
||||
}
|
||||
|
||||
// AddAction 将指令存储到玩家身上
|
||||
func (p *PlayerNode) AddAction(e *grpc_pb.ActionReq) {
|
||||
p.Action = append(p.Action, e)
|
||||
}
|
||||
|
||||
// LogicAction 处理玩家操作指令
|
||||
// return 是否更新玩家状态
|
||||
func (p *PlayerNode) LogicAction(delta int64) bool {
|
||||
for _, a := range p.Action {
|
||||
switch sc_pb.ActionID(a.Action) {
|
||||
case sc_pb.ActionID_ACTION_ID_MOVE:
|
||||
p.MoveDirection = [2]int8{int8(a.DirX), int8(a.DirY)}
|
||||
p.MoveCount = 0
|
||||
case sc_pb.ActionID_ACTION_ID_ATTACK:
|
||||
}
|
||||
}
|
||||
p.Action = make([]*grpc_pb.ActionReq, 0)
|
||||
|
||||
return p.move(delta)
|
||||
}
|
||||
|
||||
func (p *PlayerNode) move(delta int64) bool {
|
||||
if p.MoveDirection[0] == 0 && p.MoveDirection[1] == 0 {
|
||||
return false
|
||||
}
|
||||
// 移动指令只能用2秒,客户端需要定期续上
|
||||
if p.MoveCount >= 40 {
|
||||
p.MoveDirection = [2]int8{0, 0}
|
||||
return false
|
||||
}
|
||||
p.Position[0] += float32(p.MoveDirection[0]) * float32(delta) * p.MoveSpeed
|
||||
p.Position[1] += float32(p.MoveDirection[1]) * float32(delta) * p.MoveSpeed
|
||||
p.MoveCount++
|
||||
return true
|
||||
}
|
||||
Reference in New Issue
Block a user