Added ring/elimination mechanic.

This commit is contained in:
2024-12-14 06:36:45 -05:00
parent fe428bee12
commit 2b7bef30fa
7 changed files with 373 additions and 123 deletions

View File

@@ -1,6 +1,14 @@
package gamedata
import "math"
type Coordinates struct {
X float64
Y float64
}
func (c Coordinates) Distance(p Coordinates) float64 {
dx := p.X - c.X
dy := p.Y - c.Y
return math.Sqrt(dx*dx + dy*dy)
}

View File

@@ -132,6 +132,7 @@ type ClientData struct {
Name string `protobuf:"bytes,3,opt,name=Name,proto3" json:"Name,omitempty"`
Coordinates *Coordinates `protobuf:"bytes,4,opt,name=coordinates,proto3" json:"coordinates,omitempty"`
Hit bool `protobuf:"varint,5,opt,name=hit,proto3" json:"hit,omitempty"`
Eliminated bool `protobuf:"varint,6,opt,name=Eliminated,proto3" json:"Eliminated,omitempty"`
}
func (x *ClientData) Reset() {
@@ -199,6 +200,13 @@ func (x *ClientData) GetHit() bool {
return false
}
func (x *ClientData) GetEliminated() bool {
if x != nil {
return x.Eliminated
}
return false
}
type ClientCoordinates struct {
state protoimpl.MessageState
sizeCache protoimpl.SizeCache
@@ -526,7 +534,11 @@ type GameEvent struct {
Instigator int32 `protobuf:"varint,1,opt,name=Instigator,proto3" json:"Instigator,omitempty"`
Target int32 `protobuf:"varint,2,opt,name=Target,proto3" json:"Target,omitempty"`
Slap bool `protobuf:"varint,3,opt,name=Slap,proto3" json:"Slap,omitempty"`
// Types that are assignable to Event:
//
// *GameEvent_Slap
// *GameEvent_Eliminated
Event isGameEvent_Event `protobuf_oneof:"Event"`
}
func (x *GameEvent) Reset() {
@@ -573,13 +585,43 @@ func (x *GameEvent) GetTarget() int32 {
return 0
}
func (m *GameEvent) GetEvent() isGameEvent_Event {
if m != nil {
return m.Event
}
return nil
}
func (x *GameEvent) GetSlap() bool {
if x != nil {
if x, ok := x.GetEvent().(*GameEvent_Slap); ok {
return x.Slap
}
return false
}
func (x *GameEvent) GetEliminated() bool {
if x, ok := x.GetEvent().(*GameEvent_Eliminated); ok {
return x.Eliminated
}
return false
}
type isGameEvent_Event interface {
isGameEvent_Event()
}
type GameEvent_Slap struct {
Slap bool `protobuf:"varint,3,opt,name=Slap,proto3,oneof"`
}
type GameEvent_Eliminated struct {
Eliminated bool `protobuf:"varint,4,opt,name=Eliminated,proto3,oneof"`
}
func (*GameEvent_Slap) isGameEvent_Event() {}
func (*GameEvent_Eliminated) isGameEvent_Event() {}
type ServerEnvelope struct {
state protoimpl.MessageState
sizeCache protoimpl.SizeCache
@@ -694,7 +736,7 @@ var file_clientdata_proto_rawDesc = []byte{
0x74, 0x6f, 0x12, 0x04, 0x6d, 0x61, 0x69, 0x6e, 0x22, 0x29, 0x0a, 0x0b, 0x43, 0x6f, 0x6f, 0x72,
0x64, 0x69, 0x6e, 0x61, 0x74, 0x65, 0x73, 0x12, 0x0c, 0x0a, 0x01, 0x58, 0x18, 0x01, 0x20, 0x01,
0x28, 0x01, 0x52, 0x01, 0x58, 0x12, 0x0c, 0x0a, 0x01, 0x59, 0x18, 0x02, 0x20, 0x01, 0x28, 0x01,
0x52, 0x01, 0x59, 0x22, 0x91, 0x01, 0x0a, 0x0a, 0x43, 0x6c, 0x69, 0x65, 0x6e, 0x74, 0x44, 0x61,
0x52, 0x01, 0x59, 0x22, 0xb1, 0x01, 0x0a, 0x0a, 0x43, 0x6c, 0x69, 0x65, 0x6e, 0x74, 0x44, 0x61,
0x74, 0x61, 0x12, 0x0e, 0x0a, 0x02, 0x49, 0x64, 0x18, 0x01, 0x20, 0x01, 0x28, 0x05, 0x52, 0x02,
0x49, 0x64, 0x12, 0x18, 0x0a, 0x07, 0x41, 0x64, 0x64, 0x72, 0x65, 0x73, 0x73, 0x18, 0x02, 0x20,
0x01, 0x28, 0x09, 0x52, 0x07, 0x41, 0x64, 0x64, 0x72, 0x65, 0x73, 0x73, 0x12, 0x12, 0x0a, 0x04,
@@ -703,7 +745,9 @@ var file_clientdata_proto_rawDesc = []byte{
0x04, 0x20, 0x01, 0x28, 0x0b, 0x32, 0x11, 0x2e, 0x6d, 0x61, 0x69, 0x6e, 0x2e, 0x43, 0x6f, 0x6f,
0x72, 0x64, 0x69, 0x6e, 0x61, 0x74, 0x65, 0x73, 0x52, 0x0b, 0x63, 0x6f, 0x6f, 0x72, 0x64, 0x69,
0x6e, 0x61, 0x74, 0x65, 0x73, 0x12, 0x10, 0x0a, 0x03, 0x68, 0x69, 0x74, 0x18, 0x05, 0x20, 0x01,
0x28, 0x08, 0x52, 0x03, 0x68, 0x69, 0x74, 0x22, 0x5c, 0x0a, 0x11, 0x43, 0x6c, 0x69, 0x65, 0x6e,
0x28, 0x08, 0x52, 0x03, 0x68, 0x69, 0x74, 0x12, 0x1e, 0x0a, 0x0a, 0x45, 0x6c, 0x69, 0x6d, 0x69,
0x6e, 0x61, 0x74, 0x65, 0x64, 0x18, 0x06, 0x20, 0x01, 0x28, 0x08, 0x52, 0x0a, 0x45, 0x6c, 0x69,
0x6d, 0x69, 0x6e, 0x61, 0x74, 0x65, 0x64, 0x22, 0x5c, 0x0a, 0x11, 0x43, 0x6c, 0x69, 0x65, 0x6e,
0x74, 0x43, 0x6f, 0x6f, 0x72, 0x64, 0x69, 0x6e, 0x61, 0x74, 0x65, 0x73, 0x12, 0x12, 0x0a, 0x04,
0x4e, 0x61, 0x6d, 0x65, 0x18, 0x01, 0x20, 0x01, 0x28, 0x09, 0x52, 0x04, 0x4e, 0x61, 0x6d, 0x65,
0x12, 0x33, 0x0a, 0x0b, 0x63, 0x6f, 0x6f, 0x72, 0x64, 0x69, 0x6e, 0x61, 0x74, 0x65, 0x73, 0x18,
@@ -729,33 +773,35 @@ var file_clientdata_proto_rawDesc = []byte{
0x6e, 0x74, 0x12, 0x0e, 0x0a, 0x02, 0x49, 0x64, 0x18, 0x01, 0x20, 0x01, 0x28, 0x05, 0x52, 0x02,
0x49, 0x64, 0x12, 0x1c, 0x0a, 0x09, 0x43, 0x6f, 0x6e, 0x6e, 0x65, 0x63, 0x74, 0x65, 0x64, 0x18,
0x02, 0x20, 0x01, 0x28, 0x08, 0x52, 0x09, 0x43, 0x6f, 0x6e, 0x6e, 0x65, 0x63, 0x74, 0x65, 0x64,
0x22, 0x57, 0x0a, 0x09, 0x47, 0x61, 0x6d, 0x65, 0x45, 0x76, 0x65, 0x6e, 0x74, 0x12, 0x1e, 0x0a,
0x0a, 0x49, 0x6e, 0x73, 0x74, 0x69, 0x67, 0x61, 0x74, 0x6f, 0x72, 0x18, 0x01, 0x20, 0x01, 0x28,
0x05, 0x52, 0x0a, 0x49, 0x6e, 0x73, 0x74, 0x69, 0x67, 0x61, 0x74, 0x6f, 0x72, 0x12, 0x16, 0x0a,
0x06, 0x54, 0x61, 0x72, 0x67, 0x65, 0x74, 0x18, 0x02, 0x20, 0x01, 0x28, 0x05, 0x52, 0x06, 0x54,
0x61, 0x72, 0x67, 0x65, 0x74, 0x12, 0x12, 0x0a, 0x04, 0x53, 0x6c, 0x61, 0x70, 0x18, 0x03, 0x20,
0x01, 0x28, 0x08, 0x52, 0x04, 0x53, 0x6c, 0x61, 0x70, 0x22, 0xdd, 0x01, 0x0a, 0x0e, 0x53, 0x65,
0x72, 0x76, 0x65, 0x72, 0x45, 0x6e, 0x76, 0x65, 0x6c, 0x6f, 0x70, 0x65, 0x12, 0x32, 0x0a, 0x08,
0x69, 0x64, 0x65, 0x6e, 0x74, 0x69, 0x74, 0x79, 0x18, 0x01, 0x20, 0x01, 0x28, 0x0b, 0x32, 0x14,
0x2e, 0x6d, 0x61, 0x69, 0x6e, 0x2e, 0x43, 0x6c, 0x69, 0x65, 0x6e, 0x74, 0x49, 0x64, 0x65, 0x6e,
0x74, 0x69, 0x74, 0x79, 0x48, 0x00, 0x52, 0x08, 0x69, 0x64, 0x65, 0x6e, 0x74, 0x69, 0x74, 0x79,
0x12, 0x30, 0x0a, 0x09, 0x62, 0x72, 0x6f, 0x61, 0x64, 0x63, 0x61, 0x73, 0x74, 0x18, 0x02, 0x20,
0x01, 0x28, 0x0b, 0x32, 0x10, 0x2e, 0x6d, 0x61, 0x69, 0x6e, 0x2e, 0x41, 0x6c, 0x6c, 0x43, 0x6c,
0x69, 0x65, 0x6e, 0x74, 0x73, 0x48, 0x00, 0x52, 0x09, 0x62, 0x72, 0x6f, 0x61, 0x64, 0x63, 0x61,
0x73, 0x74, 0x12, 0x29, 0x0a, 0x05, 0x65, 0x76, 0x65, 0x6e, 0x74, 0x18, 0x03, 0x20, 0x01, 0x28,
0x0b, 0x32, 0x11, 0x2e, 0x6d, 0x61, 0x69, 0x6e, 0x2e, 0x43, 0x6c, 0x69, 0x65, 0x6e, 0x74, 0x45,
0x76, 0x65, 0x6e, 0x74, 0x48, 0x00, 0x52, 0x05, 0x65, 0x76, 0x65, 0x6e, 0x74, 0x12, 0x2f, 0x0a,
0x09, 0x67, 0x61, 0x6d, 0x65, 0x65, 0x76, 0x65, 0x6e, 0x74, 0x18, 0x04, 0x20, 0x01, 0x28, 0x0b,
0x32, 0x0f, 0x2e, 0x6d, 0x61, 0x69, 0x6e, 0x2e, 0x47, 0x61, 0x6d, 0x65, 0x45, 0x76, 0x65, 0x6e,
0x74, 0x48, 0x00, 0x52, 0x09, 0x67, 0x61, 0x6d, 0x65, 0x65, 0x76, 0x65, 0x6e, 0x74, 0x42, 0x09,
0x0a, 0x07, 0x50, 0x61, 0x79, 0x6c, 0x6f, 0x61, 0x64, 0x2a, 0x56, 0x0a, 0x0b, 0x4d, 0x65, 0x73,
0x73, 0x61, 0x67, 0x65, 0x54, 0x79, 0x70, 0x65, 0x12, 0x16, 0x0a, 0x12, 0x4d, 0x53, 0x47, 0x5f,
0x54, 0x59, 0x50, 0x45, 0x5f, 0x42, 0x52, 0x4f, 0x41, 0x44, 0x43, 0x41, 0x53, 0x54, 0x10, 0x00,
0x12, 0x15, 0x0a, 0x11, 0x4d, 0x53, 0x47, 0x5f, 0x54, 0x59, 0x50, 0x45, 0x5f, 0x49, 0x44, 0x45,
0x4e, 0x54, 0x49, 0x54, 0x59, 0x10, 0x01, 0x12, 0x18, 0x0a, 0x14, 0x4d, 0x53, 0x47, 0x5f, 0x54,
0x59, 0x50, 0x45, 0x5f, 0x43, 0x4f, 0x4f, 0x52, 0x44, 0x49, 0x4e, 0x41, 0x54, 0x45, 0x53, 0x10,
0x02, 0x42, 0x06, 0x5a, 0x04, 0x2e, 0x2f, 0x70, 0x62, 0x62, 0x06, 0x70, 0x72, 0x6f, 0x74, 0x6f,
0x33,
0x22, 0x84, 0x01, 0x0a, 0x09, 0x47, 0x61, 0x6d, 0x65, 0x45, 0x76, 0x65, 0x6e, 0x74, 0x12, 0x1e,
0x0a, 0x0a, 0x49, 0x6e, 0x73, 0x74, 0x69, 0x67, 0x61, 0x74, 0x6f, 0x72, 0x18, 0x01, 0x20, 0x01,
0x28, 0x05, 0x52, 0x0a, 0x49, 0x6e, 0x73, 0x74, 0x69, 0x67, 0x61, 0x74, 0x6f, 0x72, 0x12, 0x16,
0x0a, 0x06, 0x54, 0x61, 0x72, 0x67, 0x65, 0x74, 0x18, 0x02, 0x20, 0x01, 0x28, 0x05, 0x52, 0x06,
0x54, 0x61, 0x72, 0x67, 0x65, 0x74, 0x12, 0x14, 0x0a, 0x04, 0x53, 0x6c, 0x61, 0x70, 0x18, 0x03,
0x20, 0x01, 0x28, 0x08, 0x48, 0x00, 0x52, 0x04, 0x53, 0x6c, 0x61, 0x70, 0x12, 0x20, 0x0a, 0x0a,
0x45, 0x6c, 0x69, 0x6d, 0x69, 0x6e, 0x61, 0x74, 0x65, 0x64, 0x18, 0x04, 0x20, 0x01, 0x28, 0x08,
0x48, 0x00, 0x52, 0x0a, 0x45, 0x6c, 0x69, 0x6d, 0x69, 0x6e, 0x61, 0x74, 0x65, 0x64, 0x42, 0x07,
0x0a, 0x05, 0x45, 0x76, 0x65, 0x6e, 0x74, 0x22, 0xdd, 0x01, 0x0a, 0x0e, 0x53, 0x65, 0x72, 0x76,
0x65, 0x72, 0x45, 0x6e, 0x76, 0x65, 0x6c, 0x6f, 0x70, 0x65, 0x12, 0x32, 0x0a, 0x08, 0x69, 0x64,
0x65, 0x6e, 0x74, 0x69, 0x74, 0x79, 0x18, 0x01, 0x20, 0x01, 0x28, 0x0b, 0x32, 0x14, 0x2e, 0x6d,
0x61, 0x69, 0x6e, 0x2e, 0x43, 0x6c, 0x69, 0x65, 0x6e, 0x74, 0x49, 0x64, 0x65, 0x6e, 0x74, 0x69,
0x74, 0x79, 0x48, 0x00, 0x52, 0x08, 0x69, 0x64, 0x65, 0x6e, 0x74, 0x69, 0x74, 0x79, 0x12, 0x30,
0x0a, 0x09, 0x62, 0x72, 0x6f, 0x61, 0x64, 0x63, 0x61, 0x73, 0x74, 0x18, 0x02, 0x20, 0x01, 0x28,
0x0b, 0x32, 0x10, 0x2e, 0x6d, 0x61, 0x69, 0x6e, 0x2e, 0x41, 0x6c, 0x6c, 0x43, 0x6c, 0x69, 0x65,
0x6e, 0x74, 0x73, 0x48, 0x00, 0x52, 0x09, 0x62, 0x72, 0x6f, 0x61, 0x64, 0x63, 0x61, 0x73, 0x74,
0x12, 0x29, 0x0a, 0x05, 0x65, 0x76, 0x65, 0x6e, 0x74, 0x18, 0x03, 0x20, 0x01, 0x28, 0x0b, 0x32,
0x11, 0x2e, 0x6d, 0x61, 0x69, 0x6e, 0x2e, 0x43, 0x6c, 0x69, 0x65, 0x6e, 0x74, 0x45, 0x76, 0x65,
0x6e, 0x74, 0x48, 0x00, 0x52, 0x05, 0x65, 0x76, 0x65, 0x6e, 0x74, 0x12, 0x2f, 0x0a, 0x09, 0x67,
0x61, 0x6d, 0x65, 0x65, 0x76, 0x65, 0x6e, 0x74, 0x18, 0x04, 0x20, 0x01, 0x28, 0x0b, 0x32, 0x0f,
0x2e, 0x6d, 0x61, 0x69, 0x6e, 0x2e, 0x47, 0x61, 0x6d, 0x65, 0x45, 0x76, 0x65, 0x6e, 0x74, 0x48,
0x00, 0x52, 0x09, 0x67, 0x61, 0x6d, 0x65, 0x65, 0x76, 0x65, 0x6e, 0x74, 0x42, 0x09, 0x0a, 0x07,
0x50, 0x61, 0x79, 0x6c, 0x6f, 0x61, 0x64, 0x2a, 0x56, 0x0a, 0x0b, 0x4d, 0x65, 0x73, 0x73, 0x61,
0x67, 0x65, 0x54, 0x79, 0x70, 0x65, 0x12, 0x16, 0x0a, 0x12, 0x4d, 0x53, 0x47, 0x5f, 0x54, 0x59,
0x50, 0x45, 0x5f, 0x42, 0x52, 0x4f, 0x41, 0x44, 0x43, 0x41, 0x53, 0x54, 0x10, 0x00, 0x12, 0x15,
0x0a, 0x11, 0x4d, 0x53, 0x47, 0x5f, 0x54, 0x59, 0x50, 0x45, 0x5f, 0x49, 0x44, 0x45, 0x4e, 0x54,
0x49, 0x54, 0x59, 0x10, 0x01, 0x12, 0x18, 0x0a, 0x14, 0x4d, 0x53, 0x47, 0x5f, 0x54, 0x59, 0x50,
0x45, 0x5f, 0x43, 0x4f, 0x4f, 0x52, 0x44, 0x49, 0x4e, 0x41, 0x54, 0x45, 0x53, 0x10, 0x02, 0x42,
0x06, 0x5a, 0x04, 0x2e, 0x2f, 0x70, 0x62, 0x62, 0x06, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x33,
}
var (
@@ -811,6 +857,10 @@ func file_clientdata_proto_init() {
(*ClientEnvelope_Coordinates)(nil),
(*ClientEnvelope_Slap)(nil),
}
file_clientdata_proto_msgTypes[8].OneofWrappers = []any{
(*GameEvent_Slap)(nil),
(*GameEvent_Eliminated)(nil),
}
file_clientdata_proto_msgTypes[9].OneofWrappers = []any{
(*ServerEnvelope_Identity)(nil),
(*ServerEnvelope_Broadcast)(nil),

View File

@@ -16,18 +16,21 @@ import (
)
const (
boxwidth = 20
boxwidth = 20
boxheight = 20
checkRadius = 200
)
type ClientData struct {
Id int
Address string
Name string
Position gamedata.Coordinates
Hit bool
Target int
Targets []int
Slapping bool
Id int
Address string
Name string
Position gamedata.Coordinates
Hit bool
Target int
Targets []int
Slapping bool
Eliminated bool
}
type Server struct {
@@ -125,6 +128,7 @@ func (s *Server) HandleClient(conn net.Conn, id int) {
Address: conn.RemoteAddr().String(),
Name: payload.Coordinates.Name,
Position: gamedata.Coordinates{X: payload.Coordinates.Coordinates.X, Y: payload.Coordinates.Coordinates.Y},
Hit: false,
}
s.mu.Lock()
@@ -134,18 +138,49 @@ func (s *Server) HandleClient(conn net.Conn, id int) {
dx := client.Position.X - cd.Position.X
dy := client.Position.Y - cd.Position.Y
if math.Abs(dx) < boxwidth && math.Abs(dy) < boxwidth {
if math.Abs(dx) < boxwidth && math.Abs(dy) < boxheight {
//collision, mark it
cd.Hit = true
cd.Target = client.Id
//cd.Target = client.Id
cd.Targets = append(cd.Targets, client.Id)
}
}
}
p1 := gamedata.Coordinates{
X: cd.Position.X - boxwidth/2,
Y: cd.Position.Y - boxheight/2,
}
p2 := gamedata.Coordinates{
X: cd.Position.X - boxwidth/2,
Y: cd.Position.Y + boxheight/2,
}
p3 := gamedata.Coordinates{
X: cd.Position.X + boxwidth/2,
Y: cd.Position.Y + boxheight/2,
}
p4 := gamedata.Coordinates{
X: cd.Position.X + boxwidth/2,
Y: cd.Position.Y - boxheight/2,
}
circle := gamedata.Coordinates{
X: 640 / 2,
Y: 480 / 2,
}
if circle.Distance(p1) > checkRadius &&
circle.Distance(p2) > checkRadius &&
circle.Distance(p3) > checkRadius &&
circle.Distance(p4) > checkRadius {
fmt.Println("client is OUT ", cd.Id)
cd.Eliminated = true
}
//update the client list
s.clientlist[conn] = cd
s.mu.Unlock()
//s.CheckElimination(cd)
case *pb.ClientEnvelope_Slap:
s.mu.Lock()
@@ -213,7 +248,8 @@ func (s *Server) BuildBroadcastMessage() *pb.AllClients {
X: data.Position.X,
Y: data.Position.Y,
},
Hit: data.Hit,
Hit: data.Hit,
Eliminated: data.Eliminated,
}
result.Clients = append(result.Clients, clientdata)
}
@@ -290,12 +326,15 @@ func (s *Server) SendClientEvent(conn net.Conn, id int, connected bool) {
func (s *Server) BroadcastSlap(instigator, target int) {
slap := &pb.GameEvent_Slap{
Slap: true,
}
envelope := &pb.ServerEnvelope{
Payload: &pb.ServerEnvelope_Gameevent{
Gameevent: &pb.GameEvent{
Instigator: int32(instigator),
Target: int32(target),
Slap: true,
Event: slap,
},
},
}
@@ -307,3 +346,51 @@ func (s *Server) BroadcastSlap(instigator, target int) {
s.mu.Unlock()
}
func (s *Server) CheckElimination(cd ClientData) {
p1 := gamedata.Coordinates{
X: cd.Position.X - boxwidth/2,
Y: cd.Position.Y - boxheight/2,
}
p2 := gamedata.Coordinates{
X: cd.Position.X - boxwidth/2,
Y: cd.Position.Y + boxheight/2,
}
p3 := gamedata.Coordinates{
X: cd.Position.X + boxwidth/2,
Y: cd.Position.Y + boxheight/2,
}
p4 := gamedata.Coordinates{
X: cd.Position.X + boxwidth/2,
Y: cd.Position.Y - boxheight/2,
}
circle := gamedata.Coordinates{
X: 640 / 2,
Y: 480 / 2,
}
if circle.Distance(p1) > checkRadius &&
circle.Distance(p2) > checkRadius &&
circle.Distance(p3) > checkRadius &&
circle.Distance(p4) > checkRadius {
fmt.Println("client is OUT ", cd.Id)
elim := &pb.GameEvent_Eliminated{
Eliminated: true,
}
envelope := &pb.ServerEnvelope{
Payload: &pb.ServerEnvelope_Gameevent{
Gameevent: &pb.GameEvent{
Target: int32(cd.Id),
Event: elim,
},
},
}
s.mu.Lock()
for conn := range s.clientlist {
s.SendMessage(conn, envelope)
}
s.mu.Unlock()
}
}