Client list no longer dependent on cleanup; server will alert clients upon connection/disconnection.
This commit is contained in:
@@ -17,8 +17,9 @@ import (
|
||||
)
|
||||
|
||||
var (
|
||||
screenWidth = 640
|
||||
screenHeight = 480
|
||||
screenWidth = 640
|
||||
screenHeight = 480
|
||||
movementLimit = 5
|
||||
|
||||
namelist = []string{"slappy", "mick", "rodney", "george", "ringo", "robin", "temitry "}
|
||||
)
|
||||
@@ -36,21 +37,14 @@ type ClientData struct {
|
||||
}
|
||||
|
||||
type Game struct {
|
||||
name string
|
||||
blocky *elements.Block
|
||||
hitblocky *elements.Block
|
||||
gameId int
|
||||
|
||||
//players map[client.Identity]
|
||||
|
||||
clients map[string]ClientData
|
||||
|
||||
gameclient *client.Client
|
||||
cycle int
|
||||
|
||||
position gamedata.Coordinates
|
||||
|
||||
mu sync.Mutex
|
||||
name string
|
||||
blocky *elements.Block
|
||||
hitblocky *elements.Block
|
||||
gameId int
|
||||
realclients map[int]ClientData
|
||||
gameclient *client.Client
|
||||
cycle int
|
||||
mu sync.Mutex
|
||||
}
|
||||
|
||||
func NewGame() *Game {
|
||||
@@ -64,7 +58,9 @@ func NewGame() *Game {
|
||||
|
||||
g.hitblocky.SetHit(true)
|
||||
|
||||
g.clients = make(map[string]ClientData)
|
||||
g.blocky.SetPosition(gamedata.Coordinates{X: float64(screenWidth) / 2, Y: float64(screenHeight) / 2})
|
||||
|
||||
g.realclients = make(map[int]ClientData)
|
||||
|
||||
//g.gameId = g.gameclient.GetIdentity()
|
||||
go g.gameclient.ReadData(g.HandleServerData)
|
||||
@@ -73,47 +69,29 @@ func NewGame() *Game {
|
||||
|
||||
func (g *Game) Update() error {
|
||||
|
||||
x, y := ebiten.CursorPosition()
|
||||
g.position.X = float64(x)
|
||||
g.position.Y = float64(y)
|
||||
|
||||
g.blocky.SetPosition(g.position)
|
||||
g.HandleInput()
|
||||
|
||||
//broadcast our position
|
||||
if g.cycle%2 == 0 {
|
||||
if g.gameclient.IsConnected() {
|
||||
//g.gameclient.SendData(fmt.Sprintf("%s,%.0f,%.0f\n", g.name, g.position.X, g.position.Y))
|
||||
|
||||
cd := &pb.ClientCoordinates{
|
||||
Name: g.name,
|
||||
Coordinates: &pb.Coordinates{
|
||||
X: g.position.X,
|
||||
Y: g.position.Y,
|
||||
},
|
||||
}
|
||||
|
||||
envelope := &pb.ClientEnvelope{
|
||||
Payload: &pb.ClientEnvelope_Coordinates{
|
||||
Coordinates: cd,
|
||||
},
|
||||
}
|
||||
|
||||
g.gameclient.SendMessage(envelope)
|
||||
|
||||
//g.gameclient.SendProtoData(cd)
|
||||
/*
|
||||
cd := *client.ClientData{
|
||||
Name: g.name,
|
||||
Address: g.
|
||||
}*/
|
||||
|
||||
//if g.cycle%2 == 0 {
|
||||
if g.gameclient.IsConnected() {
|
||||
cd := &pb.ClientCoordinates{
|
||||
Name: g.name,
|
||||
Coordinates: &pb.Coordinates{
|
||||
X: g.blocky.GetPosition().X, //g.position.X,
|
||||
Y: g.blocky.GetPosition().Y, //g.position.Y,
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
//cleanup client list every 2 seconds
|
||||
if g.cycle%120 == 0 {
|
||||
go g.CleanupClients()
|
||||
envelope := &pb.ClientEnvelope{
|
||||
Payload: &pb.ClientEnvelope_Coordinates{
|
||||
Coordinates: cd,
|
||||
},
|
||||
}
|
||||
|
||||
g.gameclient.SendMessage(envelope)
|
||||
|
||||
}
|
||||
//}
|
||||
|
||||
g.cycle++
|
||||
return nil
|
||||
@@ -138,7 +116,7 @@ func (g *Game) Draw(screen *ebiten.Image) {
|
||||
text.Draw(screen, "you ("+g.name+")", f2, top)
|
||||
|
||||
g.mu.Lock()
|
||||
clientcopy := maps.Clone(g.clients)
|
||||
clientcopy := maps.Clone(g.realclients)
|
||||
g.mu.Unlock()
|
||||
for _, client := range clientcopy {
|
||||
op := &ebiten.DrawImageOptions{}
|
||||
@@ -185,21 +163,46 @@ func (g *Game) HandleServerData(envelope *pb.ServerEnvelope) {
|
||||
}
|
||||
|
||||
g.mu.Lock()
|
||||
g.clients[update.Address] = update
|
||||
g.realclients[int(client.Id)] = update
|
||||
g.mu.Unlock()
|
||||
} else {
|
||||
g.blocky.SetHit(client.Hit)
|
||||
}
|
||||
}
|
||||
|
||||
case *pb.ServerEnvelope_Event:
|
||||
//add or remove client from client list
|
||||
if payload.Event.Connected && payload.Event.Id != int32(g.gameId) {
|
||||
realclient := ClientData{
|
||||
Id: int(payload.Event.Id),
|
||||
}
|
||||
g.realclients[int(payload.Event.Id)] = realclient
|
||||
} else {
|
||||
delete(g.realclients, int(payload.Event.Id))
|
||||
}
|
||||
case *pb.ServerEnvelope_Identity:
|
||||
fmt.Println("Server is trying to give us our id.")
|
||||
g.gameId = int(payload.Identity.Id)
|
||||
}
|
||||
}
|
||||
|
||||
func (g *Game) CleanupClients() {
|
||||
g.mu.Lock()
|
||||
for k := range g.clients {
|
||||
delete(g.clients, k)
|
||||
func (g *Game) HandleInput() {
|
||||
|
||||
dx := 0
|
||||
dy := 0
|
||||
if ebiten.IsKeyPressed(ebiten.KeyW) {
|
||||
dy = -movementLimit
|
||||
}
|
||||
g.mu.Unlock()
|
||||
if ebiten.IsKeyPressed(ebiten.KeyS) {
|
||||
dy = +movementLimit
|
||||
}
|
||||
if ebiten.IsKeyPressed(ebiten.KeyA) {
|
||||
dx = -movementLimit
|
||||
}
|
||||
if ebiten.IsKeyPressed(ebiten.KeyD) {
|
||||
dx = +movementLimit
|
||||
}
|
||||
cpos := g.blocky.GetPosition()
|
||||
cpos.X += float64(dx)
|
||||
cpos.Y += float64(dy)
|
||||
g.blocky.SetPosition(cpos)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user