feat 稳定300人
This commit is contained in:
@@ -19,26 +19,27 @@ import (
|
||||
|
||||
// Instance 场景类
|
||||
type Instance struct {
|
||||
wg sync.WaitGroup
|
||||
players map[int]*npc.PlayerNode // 存储所有玩家节点 [uid]
|
||||
ctx context.Context // 停止指令
|
||||
cancel context.CancelFunc // 停止函数
|
||||
logger *zap.SugaredLogger // 日志
|
||||
wg sync.WaitGroup
|
||||
players map[int32]*npc.PlayerNode // 存储所有玩家节点 [uid]
|
||||
ctx context.Context // 停止指令
|
||||
cancel context.CancelFunc // 停止函数
|
||||
logger *zap.SugaredLogger // 日志
|
||||
lastLogicTime int64 // 上次逻辑帧执行时间(毫秒时间戳)
|
||||
|
||||
SID int64 // 服务ID
|
||||
InstanceID int // 副本ID
|
||||
InstanceID int32 // 副本ID
|
||||
UniqueNo int64 // 唯一编号
|
||||
EventIn chan proto.Message // 消息入口
|
||||
}
|
||||
|
||||
// NewScene 初始化场景
|
||||
func NewScene(sid int64, instanceID int) *Instance {
|
||||
func NewScene(sid int64, instanceID int32) *Instance {
|
||||
s := &Instance{
|
||||
players: make(map[int]*npc.PlayerNode),
|
||||
players: make(map[int32]*npc.PlayerNode),
|
||||
SID: sid,
|
||||
InstanceID: instanceID,
|
||||
UniqueNo: utils.SnowflakeInstance().Generate().Int64(),
|
||||
EventIn: make(chan proto.Message),
|
||||
EventIn: make(chan proto.Message, 1024),
|
||||
}
|
||||
s.logger = log.GetLogger().Named(fmt.Sprintf("instance %v:%v", s.InstanceID, s.UniqueNo))
|
||||
s.ctx, s.cancel = context.WithCancel(context.Background())
|
||||
@@ -69,12 +70,15 @@ func (i *Instance) Start(ttl int64) {
|
||||
}
|
||||
// 场景心跳
|
||||
tick := time.NewTicker(50 * time.Millisecond)
|
||||
i.lastLogicTime = time.Now().UnixMilli()
|
||||
for {
|
||||
select {
|
||||
case <-i.ctx.Done():
|
||||
return
|
||||
case <-tick.C:
|
||||
i.onLogic()
|
||||
now := time.Now().UnixMilli()
|
||||
i.onLogic(now - i.lastLogicTime)
|
||||
i.lastLogicTime = now
|
||||
case e := <-i.EventIn:
|
||||
i.onEvent(e)
|
||||
}
|
||||
@@ -87,25 +91,27 @@ func (i *Instance) Start(ttl int64) {
|
||||
func (i *Instance) onEvent(e proto.Message) {
|
||||
switch v := e.(type) {
|
||||
case *grpc_pb.EnterReq:
|
||||
i.players[int(v.UID)] = npc.NewPlayerNode(v.GatewaySID, int(v.UID))
|
||||
i.players[v.UID] = npc.NewPlayerNode(v.GatewaySID, v.UID)
|
||||
case *grpc_pb.ActionReq:
|
||||
if node, ok := i.players[int(v.UID)]; ok {
|
||||
if node, ok := i.players[v.UID]; ok {
|
||||
node.AddAction(v)
|
||||
}
|
||||
case *grpc_pb.LeaveReq:
|
||||
delete(i.players, v.UID)
|
||||
}
|
||||
}
|
||||
|
||||
// 逻辑帧
|
||||
func (i *Instance) onLogic() {
|
||||
func (i *Instance) onLogic(delta int64) {
|
||||
positionUpdate := make([]*sc_pb.PositionInfo, 0)
|
||||
sid := int64(0)
|
||||
// 优先处理移动指令
|
||||
// 处理玩家指令
|
||||
for _, node := range i.players {
|
||||
if node.LogicMove() {
|
||||
if node.LogicAction(delta) {
|
||||
positionUpdate = append(positionUpdate, &sc_pb.PositionInfo{
|
||||
UID: int32(node.UID),
|
||||
X: node.Position[0],
|
||||
Y: node.Position[1],
|
||||
X: int32(node.Position[0] * 100),
|
||||
Y: int32(node.Position[1] * 100),
|
||||
})
|
||||
sid = node.GatewaySID
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user