This repository has been archived on 2026-01-07. You can view files and clone it. You cannot open issues or pull requests or push a commit.
Files
Game/Server/scene/npc/player.go
2025-07-04 23:41:19 +08:00

93 lines
1.9 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 npc
import (
"common/proto/gen/grpc_pb"
)
// PlayerNode 定义玩家节点结构体
type PlayerNode struct {
UID int // 用户ID
GatewaySID int64 // 网关服务ID
Position [2]float64 // 二维坐标 [x, y]
MoveCross int8 // 移动十字1 上 2 下 4 左 8 右
Action []*grpc_pb.ActionReq // 其他操作
}
func NewPlayerNode(gatewaySID int64, uid int) *PlayerNode {
return &PlayerNode{
UID: uid,
GatewaySID: gatewaySID,
Position: [2]float64{0, 0},
MoveCross: 0,
Action: make([]*grpc_pb.ActionReq, 0),
}
}
// AddAction 将指令存储到玩家身上
func (p *PlayerNode) AddAction(e *grpc_pb.ActionReq) {
if e.Action < 16 {
p.MoveCross = int8(e.Action)
} else {
p.Action = append(p.Action, e)
}
}
const (
DirUp int8 = 1 << iota // 1 (00000001)
DirDown // 2 (00000010)
DirLeft // 4 (00000100)
DirRight // 8 (00001000)
)
// LogicMove 逻辑帧-移动
func (p *PlayerNode) 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
}