Compare commits
2 Commits
develop
...
f91f72c05d
Author | SHA1 | Date | |
---|---|---|---|
f91f72c05d | |||
1d6d3ab2ea |
@ -1,7 +1,7 @@
|
|||||||
// Code generated by protoc-gen-go. DO NOT EDIT.
|
// Code generated by protoc-gen-go. DO NOT EDIT.
|
||||||
// versions:
|
// versions:
|
||||||
// protoc-gen-go v1.28.1
|
// protoc-gen-go v1.36.2
|
||||||
// protoc v3.21.12
|
// protoc v5.29.2
|
||||||
// source: actions.proto
|
// source: actions.proto
|
||||||
|
|
||||||
package actions
|
package actions
|
||||||
@ -64,23 +64,20 @@ func (Action_ActionType) EnumDescriptor() ([]byte, []int) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
type Action struct {
|
type Action struct {
|
||||||
state protoimpl.MessageState
|
state protoimpl.MessageState `protogen:"open.v1"`
|
||||||
sizeCache protoimpl.SizeCache
|
Type Action_ActionType `protobuf:"varint,1,opt,name=type,proto3,enum=actions.Action_ActionType" json:"type,omitempty"`
|
||||||
|
X int32 `protobuf:"varint,2,opt,name=x,proto3" json:"x,omitempty"`
|
||||||
|
Y int32 `protobuf:"varint,3,opt,name=y,proto3" json:"y,omitempty"`
|
||||||
|
PlayerId int32 `protobuf:"varint,4,opt,name=player_id,json=playerId,proto3" json:"player_id,omitempty"`
|
||||||
unknownFields protoimpl.UnknownFields
|
unknownFields protoimpl.UnknownFields
|
||||||
|
sizeCache protoimpl.SizeCache
|
||||||
Type Action_ActionType `protobuf:"varint,1,opt,name=type,proto3,enum=actions.Action_ActionType" json:"type,omitempty"`
|
|
||||||
X int32 `protobuf:"varint,2,opt,name=x,proto3" json:"x,omitempty"`
|
|
||||||
Y int32 `protobuf:"varint,3,opt,name=y,proto3" json:"y,omitempty"`
|
|
||||||
PlayerId int32 `protobuf:"varint,4,opt,name=player_id,json=playerId,proto3" json:"player_id,omitempty"`
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (x *Action) Reset() {
|
func (x *Action) Reset() {
|
||||||
*x = Action{}
|
*x = Action{}
|
||||||
if protoimpl.UnsafeEnabled {
|
mi := &file_actions_proto_msgTypes[0]
|
||||||
mi := &file_actions_proto_msgTypes[0]
|
ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x))
|
||||||
ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x))
|
ms.StoreMessageInfo(mi)
|
||||||
ms.StoreMessageInfo(mi)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (x *Action) String() string {
|
func (x *Action) String() string {
|
||||||
@ -91,7 +88,7 @@ func (*Action) ProtoMessage() {}
|
|||||||
|
|
||||||
func (x *Action) ProtoReflect() protoreflect.Message {
|
func (x *Action) ProtoReflect() protoreflect.Message {
|
||||||
mi := &file_actions_proto_msgTypes[0]
|
mi := &file_actions_proto_msgTypes[0]
|
||||||
if protoimpl.UnsafeEnabled && x != nil {
|
if x != nil {
|
||||||
ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x))
|
ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x))
|
||||||
if ms.LoadMessageInfo() == nil {
|
if ms.LoadMessageInfo() == nil {
|
||||||
ms.StoreMessageInfo(mi)
|
ms.StoreMessageInfo(mi)
|
||||||
@ -135,21 +132,18 @@ func (x *Action) GetPlayerId() int32 {
|
|||||||
}
|
}
|
||||||
|
|
||||||
type ServerMessage struct {
|
type ServerMessage struct {
|
||||||
state protoimpl.MessageState
|
state protoimpl.MessageState `protogen:"open.v1"`
|
||||||
sizeCache protoimpl.SizeCache
|
PlayerId int32 `protobuf:"varint,1,opt,name=player_id,json=playerId,proto3" json:"player_id,omitempty"` // Only used when initially assigning player ID
|
||||||
|
Players []*PlayerState `protobuf:"bytes,2,rep,name=players,proto3" json:"players,omitempty"` // Player state updates
|
||||||
unknownFields protoimpl.UnknownFields
|
unknownFields protoimpl.UnknownFields
|
||||||
|
sizeCache protoimpl.SizeCache
|
||||||
PlayerId int32 `protobuf:"varint,1,opt,name=player_id,json=playerId,proto3" json:"player_id,omitempty"` // Only used when initially assigning player ID
|
|
||||||
Players []*PlayerState `protobuf:"bytes,2,rep,name=players,proto3" json:"players,omitempty"` // Player state updates
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (x *ServerMessage) Reset() {
|
func (x *ServerMessage) Reset() {
|
||||||
*x = ServerMessage{}
|
*x = ServerMessage{}
|
||||||
if protoimpl.UnsafeEnabled {
|
mi := &file_actions_proto_msgTypes[1]
|
||||||
mi := &file_actions_proto_msgTypes[1]
|
ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x))
|
||||||
ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x))
|
ms.StoreMessageInfo(mi)
|
||||||
ms.StoreMessageInfo(mi)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (x *ServerMessage) String() string {
|
func (x *ServerMessage) String() string {
|
||||||
@ -160,7 +154,7 @@ func (*ServerMessage) ProtoMessage() {}
|
|||||||
|
|
||||||
func (x *ServerMessage) ProtoReflect() protoreflect.Message {
|
func (x *ServerMessage) ProtoReflect() protoreflect.Message {
|
||||||
mi := &file_actions_proto_msgTypes[1]
|
mi := &file_actions_proto_msgTypes[1]
|
||||||
if protoimpl.UnsafeEnabled && x != nil {
|
if x != nil {
|
||||||
ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x))
|
ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x))
|
||||||
if ms.LoadMessageInfo() == nil {
|
if ms.LoadMessageInfo() == nil {
|
||||||
ms.StoreMessageInfo(mi)
|
ms.StoreMessageInfo(mi)
|
||||||
@ -190,22 +184,19 @@ func (x *ServerMessage) GetPlayers() []*PlayerState {
|
|||||||
}
|
}
|
||||||
|
|
||||||
type PlayerState struct {
|
type PlayerState struct {
|
||||||
state protoimpl.MessageState
|
state protoimpl.MessageState `protogen:"open.v1"`
|
||||||
sizeCache protoimpl.SizeCache
|
PlayerId int32 `protobuf:"varint,1,opt,name=player_id,json=playerId,proto3" json:"player_id,omitempty"`
|
||||||
|
X int32 `protobuf:"varint,2,opt,name=x,proto3" json:"x,omitempty"`
|
||||||
|
Y int32 `protobuf:"varint,3,opt,name=y,proto3" json:"y,omitempty"`
|
||||||
unknownFields protoimpl.UnknownFields
|
unknownFields protoimpl.UnknownFields
|
||||||
|
sizeCache protoimpl.SizeCache
|
||||||
PlayerId int32 `protobuf:"varint,1,opt,name=player_id,json=playerId,proto3" json:"player_id,omitempty"`
|
|
||||||
X int32 `protobuf:"varint,2,opt,name=x,proto3" json:"x,omitempty"`
|
|
||||||
Y int32 `protobuf:"varint,3,opt,name=y,proto3" json:"y,omitempty"`
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (x *PlayerState) Reset() {
|
func (x *PlayerState) Reset() {
|
||||||
*x = PlayerState{}
|
*x = PlayerState{}
|
||||||
if protoimpl.UnsafeEnabled {
|
mi := &file_actions_proto_msgTypes[2]
|
||||||
mi := &file_actions_proto_msgTypes[2]
|
ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x))
|
||||||
ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x))
|
ms.StoreMessageInfo(mi)
|
||||||
ms.StoreMessageInfo(mi)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (x *PlayerState) String() string {
|
func (x *PlayerState) String() string {
|
||||||
@ -216,7 +207,7 @@ func (*PlayerState) ProtoMessage() {}
|
|||||||
|
|
||||||
func (x *PlayerState) ProtoReflect() protoreflect.Message {
|
func (x *PlayerState) ProtoReflect() protoreflect.Message {
|
||||||
mi := &file_actions_proto_msgTypes[2]
|
mi := &file_actions_proto_msgTypes[2]
|
||||||
if protoimpl.UnsafeEnabled && x != nil {
|
if x != nil {
|
||||||
ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x))
|
ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x))
|
||||||
if ms.LoadMessageInfo() == nil {
|
if ms.LoadMessageInfo() == nil {
|
||||||
ms.StoreMessageInfo(mi)
|
ms.StoreMessageInfo(mi)
|
||||||
@ -295,7 +286,7 @@ func file_actions_proto_rawDescGZIP() []byte {
|
|||||||
|
|
||||||
var file_actions_proto_enumTypes = make([]protoimpl.EnumInfo, 1)
|
var file_actions_proto_enumTypes = make([]protoimpl.EnumInfo, 1)
|
||||||
var file_actions_proto_msgTypes = make([]protoimpl.MessageInfo, 3)
|
var file_actions_proto_msgTypes = make([]protoimpl.MessageInfo, 3)
|
||||||
var file_actions_proto_goTypes = []interface{}{
|
var file_actions_proto_goTypes = []any{
|
||||||
(Action_ActionType)(0), // 0: actions.Action.ActionType
|
(Action_ActionType)(0), // 0: actions.Action.ActionType
|
||||||
(*Action)(nil), // 1: actions.Action
|
(*Action)(nil), // 1: actions.Action
|
||||||
(*ServerMessage)(nil), // 2: actions.ServerMessage
|
(*ServerMessage)(nil), // 2: actions.ServerMessage
|
||||||
@ -316,44 +307,6 @@ func file_actions_proto_init() {
|
|||||||
if File_actions_proto != nil {
|
if File_actions_proto != nil {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
if !protoimpl.UnsafeEnabled {
|
|
||||||
file_actions_proto_msgTypes[0].Exporter = func(v interface{}, i int) interface{} {
|
|
||||||
switch v := v.(*Action); i {
|
|
||||||
case 0:
|
|
||||||
return &v.state
|
|
||||||
case 1:
|
|
||||||
return &v.sizeCache
|
|
||||||
case 2:
|
|
||||||
return &v.unknownFields
|
|
||||||
default:
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
}
|
|
||||||
file_actions_proto_msgTypes[1].Exporter = func(v interface{}, i int) interface{} {
|
|
||||||
switch v := v.(*ServerMessage); i {
|
|
||||||
case 0:
|
|
||||||
return &v.state
|
|
||||||
case 1:
|
|
||||||
return &v.sizeCache
|
|
||||||
case 2:
|
|
||||||
return &v.unknownFields
|
|
||||||
default:
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
}
|
|
||||||
file_actions_proto_msgTypes[2].Exporter = func(v interface{}, i int) interface{} {
|
|
||||||
switch v := v.(*PlayerState); i {
|
|
||||||
case 0:
|
|
||||||
return &v.state
|
|
||||||
case 1:
|
|
||||||
return &v.sizeCache
|
|
||||||
case 2:
|
|
||||||
return &v.unknownFields
|
|
||||||
default:
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
type x struct{}
|
type x struct{}
|
||||||
out := protoimpl.TypeBuilder{
|
out := protoimpl.TypeBuilder{
|
||||||
File: protoimpl.DescBuilder{
|
File: protoimpl.DescBuilder{
|
||||||
|
38
main.go
38
main.go
@ -21,8 +21,11 @@ type Player struct {
|
|||||||
X, Y int // Position on the game grid
|
X, Y int // Position on the game grid
|
||||||
}
|
}
|
||||||
|
|
||||||
var players = make(map[int]*Player)
|
var (
|
||||||
var actionQueue = make(map[int][]*pb.Action) // Queue to store actions for each player
|
players = make(map[int]*Player)
|
||||||
|
actionQueue = make(map[int][]*pb.Action) // Queue to store actions for each player
|
||||||
|
playerConns = make(map[int]net.Conn) // Map to store player connections
|
||||||
|
)
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
ln, err := net.Listen("tcp", port)
|
ln, err := net.Listen("tcp", port)
|
||||||
@ -47,7 +50,6 @@ func main() {
|
|||||||
for {
|
for {
|
||||||
if time.Since(lastTick) >= tickRate {
|
if time.Since(lastTick) >= tickRate {
|
||||||
lastTick = time.Now()
|
lastTick = time.Now()
|
||||||
// log.Printf("Last tick: %s", lastTick)
|
|
||||||
processActions()
|
processActions()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -59,6 +61,7 @@ func handleConnection(conn net.Conn) {
|
|||||||
// Assign a new player ID and add the player to the game state
|
// Assign a new player ID and add the player to the game state
|
||||||
playerID := len(players) + 1
|
playerID := len(players) + 1
|
||||||
players[playerID] = &Player{ID: playerID, X: 5, Y: 5} // Start at default position
|
players[playerID] = &Player{ID: playerID, X: 5, Y: 5} // Start at default position
|
||||||
|
playerConns[playerID] = conn // Store the connection
|
||||||
fmt.Printf("Player %d connected\n", playerID)
|
fmt.Printf("Player %d connected\n", playerID)
|
||||||
|
|
||||||
// Send player ID to the client
|
// Send player ID to the client
|
||||||
@ -82,6 +85,7 @@ func handleConnection(conn net.Conn) {
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
log.Printf("Error reading from player %d: %v", playerID, err)
|
log.Printf("Error reading from player %d: %v", playerID, err)
|
||||||
delete(players, playerID)
|
delete(players, playerID)
|
||||||
|
delete(playerConns, playerID) // Remove connection on error
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -97,6 +101,7 @@ func handleConnection(conn net.Conn) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func processActions() {
|
func processActions() {
|
||||||
|
// Update players based on queued actions
|
||||||
for playerID, actions := range actionQueue {
|
for playerID, actions := range actionQueue {
|
||||||
player := players[playerID]
|
player := players[playerID]
|
||||||
for _, action := range actions {
|
for _, action := range actions {
|
||||||
@ -106,6 +111,31 @@ func processActions() {
|
|||||||
fmt.Printf("Player %d moved to (%d, %d)\n", playerID, player.X, player.Y)
|
fmt.Printf("Player %d moved to (%d, %d)\n", playerID, player.X, player.Y)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
actionQueue[playerID] = nil // Clear the queue after processing
|
actionQueue[playerID] = nil // Clear the action queue after processing
|
||||||
|
}
|
||||||
|
|
||||||
|
// Prepare and broadcast the positions of all players
|
||||||
|
for playerID := range players {
|
||||||
|
state := &pb.ServerMessage{}
|
||||||
|
for id, p := range players {
|
||||||
|
state.Players = append(state.Players, &pb.PlayerState{
|
||||||
|
PlayerId: int32(id),
|
||||||
|
X: int32(p.X),
|
||||||
|
Y: int32(p.Y),
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
data, err := proto.Marshal(state)
|
||||||
|
if err != nil {
|
||||||
|
log.Printf("Failed to marshal player state: %v", err)
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
// Send to each connected player
|
||||||
|
for _, conn := range playerConns {
|
||||||
|
if _, err := conn.Write(data); err != nil {
|
||||||
|
log.Printf("Failed to send update to player %d: %v", playerID, err)
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user