Slapping in the mix.

This commit is contained in:
2024-12-12 07:12:46 -05:00
parent ad6a061e5a
commit 913d5e93d4
7 changed files with 601 additions and 139 deletions

View File

@@ -8,10 +8,12 @@ import (
"client/pb"
"fmt"
"maps"
"math"
"sync"
"time"
"github.com/hajimehoshi/ebiten/v2"
"github.com/hajimehoshi/ebiten/v2/inpututil"
"github.com/hajimehoshi/ebiten/v2/text/v2"
"golang.org/x/exp/rand"
)
@@ -21,7 +23,13 @@ var (
screenHeight = 480
movementLimit = 5
namelist = []string{"slappy", "mick", "rodney", "george", "ringo", "robin", "temitry "}
namelist = []string{"slappy", "mick", "rodney", "george", "ringo",
"robin", "temitry", "evangeline", "ron", "abigail", "lester",
"maynard", "agnes", "stacey", "wendell", "susanne", "myrtle",
"teresa", "kristi", "genos", "felton", "lawrence", "rosie",
"nigel", "constance", "maryellen", "dollie", "markus",
"dorthy", "lazaro", "willa", "dino", "gustavo", "conrad",
"georgia", "lucinda", "saitama"}
)
func init() {
@@ -59,6 +67,7 @@ func NewGame() *Game {
g.hitblocky.SetHit(true)
g.blocky.SetPosition(gamedata.Coordinates{X: float64(screenWidth) / 2, Y: float64(screenHeight) / 2})
g.blocky.SetTargetPosition(gamedata.Coordinates{X: float64(screenWidth) / 2, Y: float64(screenHeight) / 2})
g.realclients = make(map[int]ClientData)
@@ -69,29 +78,11 @@ func NewGame() *Game {
func (g *Game) Update() error {
g.blocky.Update()
//g.hitblocky.Update()
g.HandleInput()
//broadcast our position
//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,
},
}
envelope := &pb.ClientEnvelope{
Payload: &pb.ClientEnvelope_Coordinates{
Coordinates: cd,
},
}
g.gameclient.SendMessage(envelope)
}
//}
g.SendPosition()
g.cycle++
return nil
@@ -180,8 +171,39 @@ func (g *Game) HandleServerData(envelope *pb.ServerEnvelope) {
delete(g.realclients, int(payload.Event.Id))
}
case *pb.ServerEnvelope_Identity:
fmt.Println("Server is trying to give us our id.")
fmt.Println("Server is trying to give us our id: ", payload.Identity.Id)
g.gameId = int(payload.Identity.Id)
case *pb.ServerEnvelope_Gameevent:
fmt.Printf("someone slapping! target:%d, instigator:%d isSlap:%d", payload.Gameevent.Target, payload.Gameevent.Instigator, payload.Gameevent.Slap)
if payload.Gameevent.Target == int32(g.gameId) {
g.mu.Lock()
dx := g.blocky.GetPosition().X - g.realclients[int(payload.Gameevent.Instigator)].Position.X
dy := g.blocky.GetPosition().Y - g.realclients[int(payload.Gameevent.Instigator)].Position.Y
g.mu.Unlock()
if dx != 0 {
dx = (dx / math.Abs(dx)) * 100
}
if dy != 0 {
dy = (dy / math.Abs(dy)) * 100
}
if dx == 0 && dy == 0 {
b := rand.Intn(2)
if b == 0 {
dx = 100
dy = 100
} else {
dx = -100
dy = -100
}
}
cpos := gamedata.Coordinates{}
cpos.X = g.blocky.GetPosition().X + dx
cpos.Y = g.blocky.GetPosition().Y + dy
g.blocky.SetTargetPosition(cpos)
}
}
}
@@ -201,8 +223,50 @@ func (g *Game) HandleInput() {
if ebiten.IsKeyPressed(ebiten.KeyD) {
dx = +movementLimit
}
cpos := g.blocky.GetPosition()
cpos.X += float64(dx)
cpos.Y += float64(dy)
g.blocky.SetPosition(cpos)
g.blocky.SetTargetPosition(cpos)
if inpututil.IsKeyJustPressed(ebiten.KeySpace) {
g.SendSlap()
}
}
func (g *Game) SendPosition() {
//broadcast our position
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,
},
}
envelope := &pb.ClientEnvelope{
Payload: &pb.ClientEnvelope_Coordinates{
Coordinates: cd,
},
}
g.gameclient.SendMessage(envelope)
}
}
func (g *Game) SendSlap() {
if g.gameclient.IsConnected() {
slap := &pb.SlapEvent{
Slap: true,
}
envelope := &pb.ClientEnvelope{
Payload: &pb.ClientEnvelope_Slap{
Slap: slap,
},
}
g.gameclient.SendMessage(envelope)
}
}