2024-12-10 18:55:23 -05:00
|
|
|
package game
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"client/client"
|
|
|
|
|
"client/elements"
|
|
|
|
|
"client/fonts"
|
|
|
|
|
"client/gamedata"
|
2024-12-10 20:30:51 -05:00
|
|
|
"client/pb"
|
2024-12-11 07:41:21 -05:00
|
|
|
"fmt"
|
2024-12-14 06:36:45 -05:00
|
|
|
"image/color"
|
2024-12-10 18:55:23 -05:00
|
|
|
"maps"
|
2024-12-12 07:12:46 -05:00
|
|
|
"math"
|
2024-12-10 18:55:23 -05:00
|
|
|
"sync"
|
|
|
|
|
"time"
|
|
|
|
|
|
|
|
|
|
"github.com/hajimehoshi/ebiten/v2"
|
2024-12-12 07:12:46 -05:00
|
|
|
"github.com/hajimehoshi/ebiten/v2/inpututil"
|
2024-12-10 18:55:23 -05:00
|
|
|
"github.com/hajimehoshi/ebiten/v2/text/v2"
|
2024-12-14 06:36:45 -05:00
|
|
|
"github.com/hajimehoshi/ebiten/v2/vector"
|
2024-12-10 18:55:23 -05:00
|
|
|
"golang.org/x/exp/rand"
|
|
|
|
|
)
|
|
|
|
|
|
2024-12-14 06:36:45 -05:00
|
|
|
const (
|
2024-12-11 18:18:04 -05:00
|
|
|
screenWidth = 640
|
|
|
|
|
screenHeight = 480
|
|
|
|
|
movementLimit = 5
|
2024-12-14 06:36:45 -05:00
|
|
|
stageRadius = 200
|
|
|
|
|
)
|
2024-12-10 18:55:23 -05:00
|
|
|
|
2024-12-14 06:36:45 -05:00
|
|
|
var (
|
2024-12-12 07:12:46 -05:00
|
|
|
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"}
|
2024-12-10 18:55:23 -05:00
|
|
|
)
|
|
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
|
rand.Seed(uint64(time.Now().UnixNano()))
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
type ClientData struct {
|
2024-12-14 06:36:45 -05:00
|
|
|
Id int
|
|
|
|
|
Address string
|
|
|
|
|
Name string
|
|
|
|
|
Position gamedata.Coordinates
|
|
|
|
|
Hit bool
|
|
|
|
|
Eliminated bool
|
2024-12-10 18:55:23 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
type Game struct {
|
2024-12-11 18:18:04 -05:00
|
|
|
name string
|
|
|
|
|
blocky *elements.Block
|
|
|
|
|
hitblocky *elements.Block
|
2024-12-14 06:36:45 -05:00
|
|
|
elimblocky *elements.Block
|
2024-12-11 18:18:04 -05:00
|
|
|
gameId int
|
|
|
|
|
realclients map[int]ClientData
|
|
|
|
|
gameclient *client.Client
|
|
|
|
|
cycle int
|
|
|
|
|
mu sync.Mutex
|
2024-12-14 06:36:45 -05:00
|
|
|
|
|
|
|
|
//similar fields that we see in the client list, but for us
|
|
|
|
|
eliminated bool
|
|
|
|
|
hit bool
|
2024-12-14 11:32:41 -05:00
|
|
|
|
|
|
|
|
dino *elements.Dino
|
2024-12-10 18:55:23 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func NewGame() *Game {
|
|
|
|
|
g := &Game{
|
|
|
|
|
gameclient: client.NewClient(),
|
|
|
|
|
blocky: elements.NewBlock(),
|
2024-12-11 12:55:07 -05:00
|
|
|
hitblocky: elements.NewBlock(),
|
2024-12-14 06:36:45 -05:00
|
|
|
elimblocky: elements.NewBlock(),
|
2024-12-10 18:55:23 -05:00
|
|
|
cycle: 0,
|
|
|
|
|
name: namelist[rand.Intn(len(namelist))],
|
2024-12-14 11:32:41 -05:00
|
|
|
dino: elements.NewDino(gamedata.DinoTypeGreen),
|
2024-12-10 18:55:23 -05:00
|
|
|
}
|
2024-12-11 12:55:07 -05:00
|
|
|
|
2024-12-14 06:36:45 -05:00
|
|
|
g.blocky.SetColor(color.RGBA{R: 0xff, G: 0x00, B: 0x00, A: 0xff})
|
|
|
|
|
g.hitblocky.SetColor(color.RGBA{R: 0x00, G: 0xff, B: 0xff, A: 0xff})
|
|
|
|
|
g.elimblocky.SetColor(color.RGBA{R: 0x00, G: 0x00, B: 0xff, A: 0xff})
|
2024-12-11 12:55:07 -05:00
|
|
|
|
2024-12-11 18:18:04 -05:00
|
|
|
g.blocky.SetPosition(gamedata.Coordinates{X: float64(screenWidth) / 2, Y: float64(screenHeight) / 2})
|
2024-12-12 07:12:46 -05:00
|
|
|
g.blocky.SetTargetPosition(gamedata.Coordinates{X: float64(screenWidth) / 2, Y: float64(screenHeight) / 2})
|
2024-12-11 18:18:04 -05:00
|
|
|
|
|
|
|
|
g.realclients = make(map[int]ClientData)
|
2024-12-10 22:24:45 -05:00
|
|
|
|
2024-12-11 07:41:21 -05:00
|
|
|
//g.gameId = g.gameclient.GetIdentity()
|
2024-12-10 18:55:23 -05:00
|
|
|
go g.gameclient.ReadData(g.HandleServerData)
|
|
|
|
|
return g
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (g *Game) Update() error {
|
|
|
|
|
|
2024-12-12 07:12:46 -05:00
|
|
|
g.blocky.Update()
|
2024-12-14 11:32:41 -05:00
|
|
|
g.dino.Update()
|
2024-12-12 07:12:46 -05:00
|
|
|
//g.hitblocky.Update()
|
2024-12-11 18:18:04 -05:00
|
|
|
|
2024-12-12 07:12:46 -05:00
|
|
|
g.HandleInput()
|
|
|
|
|
g.SendPosition()
|
2024-12-10 18:55:23 -05:00
|
|
|
|
|
|
|
|
g.cycle++
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (g *Game) Draw(screen *ebiten.Image) {
|
|
|
|
|
screen.Clear()
|
|
|
|
|
|
|
|
|
|
g.blocky.Draw()
|
2024-12-11 12:55:07 -05:00
|
|
|
g.hitblocky.Draw()
|
2024-12-14 06:36:45 -05:00
|
|
|
g.elimblocky.Draw()
|
|
|
|
|
|
|
|
|
|
vector.StrokeCircle(screen, float32(screenWidth)/2, float32(screenHeight)/2, stageRadius, 3, color.White, true)
|
2024-12-11 12:55:07 -05:00
|
|
|
|
2024-12-10 18:55:23 -05:00
|
|
|
op := &ebiten.DrawImageOptions{}
|
|
|
|
|
op.GeoM.Translate(-float64(g.blocky.Sprite.Bounds().Dx())/2, -float64(g.blocky.Sprite.Bounds().Dy())/2)
|
|
|
|
|
op.GeoM.Translate(g.blocky.GetPosition().X, g.blocky.GetPosition().Y)
|
2024-12-14 06:36:45 -05:00
|
|
|
if !g.eliminated {
|
|
|
|
|
if !g.hit {
|
|
|
|
|
screen.DrawImage(g.blocky.Sprite, op)
|
|
|
|
|
} else {
|
|
|
|
|
screen.DrawImage(g.hitblocky.Sprite, op)
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
screen.DrawImage(g.elimblocky.Sprite, op)
|
|
|
|
|
}
|
2024-12-10 18:55:23 -05:00
|
|
|
f2 := &text.GoTextFace{
|
|
|
|
|
Source: fonts.LaunchyFont.New,
|
|
|
|
|
Size: 12,
|
|
|
|
|
}
|
|
|
|
|
top := &text.DrawOptions{}
|
|
|
|
|
top.GeoM.Translate(g.blocky.GetPosition().X-50, g.blocky.GetPosition().Y+15)
|
|
|
|
|
text.Draw(screen, "you ("+g.name+")", f2, top)
|
|
|
|
|
|
|
|
|
|
g.mu.Lock()
|
2024-12-11 18:18:04 -05:00
|
|
|
clientcopy := maps.Clone(g.realclients)
|
2024-12-10 18:55:23 -05:00
|
|
|
g.mu.Unlock()
|
|
|
|
|
for _, client := range clientcopy {
|
|
|
|
|
op := &ebiten.DrawImageOptions{}
|
|
|
|
|
op.GeoM.Translate(-float64(g.blocky.Sprite.Bounds().Dx())/2, -float64(g.blocky.Sprite.Bounds().Dy())/2)
|
|
|
|
|
op.GeoM.Translate(client.Position.X, client.Position.Y)
|
2024-12-14 06:36:45 -05:00
|
|
|
|
|
|
|
|
if client.Eliminated {
|
|
|
|
|
screen.DrawImage(g.elimblocky.Sprite, op)
|
2024-12-11 12:55:07 -05:00
|
|
|
} else {
|
2024-12-14 06:36:45 -05:00
|
|
|
if !client.Hit {
|
|
|
|
|
screen.DrawImage(g.blocky.Sprite, op)
|
|
|
|
|
} else {
|
|
|
|
|
screen.DrawImage(g.hitblocky.Sprite, op)
|
|
|
|
|
}
|
2024-12-11 12:55:07 -05:00
|
|
|
}
|
2024-12-10 18:55:23 -05:00
|
|
|
|
|
|
|
|
f2 := &text.GoTextFace{
|
|
|
|
|
Source: fonts.LaunchyFont.New,
|
|
|
|
|
Size: 12,
|
|
|
|
|
}
|
|
|
|
|
top := &text.DrawOptions{}
|
|
|
|
|
top.GeoM.Translate(client.Position.X, client.Position.Y)
|
|
|
|
|
text.Draw(screen, client.Name, f2, top)
|
|
|
|
|
}
|
|
|
|
|
|
2024-12-14 11:32:41 -05:00
|
|
|
g.dino.Draw()
|
|
|
|
|
dop := &ebiten.DrawImageOptions{}
|
|
|
|
|
dop.GeoM.Translate(-float64(g.dino.Sprite.Bounds().Dx())/2, -float64(g.dino.Sprite.Bounds().Dy())/2)
|
|
|
|
|
dop.GeoM.Translate(g.blocky.GetPosition().X, g.blocky.GetPosition().Y)
|
|
|
|
|
screen.DrawImage(g.dino.Sprite, dop)
|
|
|
|
|
|
2024-12-10 18:55:23 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (g *Game) Layout(outsideWidth, outsideHeight int) (screenwidth, screenheight int) {
|
|
|
|
|
return screenWidth, screenHeight
|
|
|
|
|
}
|
|
|
|
|
|
2024-12-11 07:41:21 -05:00
|
|
|
func (g *Game) HandleServerData(envelope *pb.ServerEnvelope) {
|
|
|
|
|
|
|
|
|
|
switch payload := envelope.Payload.(type) {
|
|
|
|
|
case *pb.ServerEnvelope_Broadcast:
|
|
|
|
|
//fmt.Println("Here comes the broadcast!")
|
|
|
|
|
|
|
|
|
|
for _, client := range payload.Broadcast.Clients {
|
|
|
|
|
if client.Id != int32(g.gameId) {
|
|
|
|
|
update := ClientData{
|
|
|
|
|
Id: int(client.Id),
|
|
|
|
|
Address: client.Address,
|
|
|
|
|
Name: client.Name,
|
|
|
|
|
Position: gamedata.Coordinates{
|
|
|
|
|
X: client.Coordinates.X,
|
|
|
|
|
Y: client.Coordinates.Y,
|
|
|
|
|
},
|
2024-12-14 06:36:45 -05:00
|
|
|
Hit: client.Hit,
|
|
|
|
|
Eliminated: client.Eliminated,
|
2024-12-11 07:41:21 -05:00
|
|
|
}
|
2024-12-10 18:55:23 -05:00
|
|
|
|
2024-12-11 07:41:21 -05:00
|
|
|
g.mu.Lock()
|
2024-12-11 18:18:04 -05:00
|
|
|
g.realclients[int(client.Id)] = update
|
2024-12-11 07:41:21 -05:00
|
|
|
g.mu.Unlock()
|
2024-12-11 18:18:04 -05:00
|
|
|
} else {
|
2024-12-14 06:36:45 -05:00
|
|
|
g.eliminated = client.Eliminated
|
|
|
|
|
g.hit = client.Hit
|
|
|
|
|
//this is us
|
|
|
|
|
|
|
|
|
|
//g.blocky.SetHit(client.Hit)
|
|
|
|
|
|
2024-12-10 20:58:17 -05:00
|
|
|
}
|
|
|
|
|
}
|
2024-12-11 18:18:04 -05:00
|
|
|
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))
|
|
|
|
|
}
|
2024-12-11 07:41:21 -05:00
|
|
|
case *pb.ServerEnvelope_Identity:
|
2024-12-12 07:12:46 -05:00
|
|
|
fmt.Println("Server is trying to give us our id: ", payload.Identity.Id)
|
2024-12-11 07:41:21 -05:00
|
|
|
g.gameId = int(payload.Identity.Id)
|
2024-12-12 07:12:46 -05:00
|
|
|
case *pb.ServerEnvelope_Gameevent:
|
2024-12-14 06:36:45 -05:00
|
|
|
//fmt.Printf("someone slapping! target:%d, instigator:%d isSlap:%d", payload.Gameevent.Target, payload.Gameevent.Instigator, payload.Gameevent.Slap)
|
2024-12-12 07:12:46 -05:00
|
|
|
|
2024-12-14 06:36:45 -05:00
|
|
|
switch payload.Gameevent.Event.(type) {
|
|
|
|
|
case *pb.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()
|
2024-12-12 07:12:46 -05:00
|
|
|
|
2024-12-14 06:36:45 -05:00
|
|
|
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
|
|
|
|
|
}
|
2024-12-12 07:12:46 -05:00
|
|
|
}
|
2024-12-14 06:36:45 -05:00
|
|
|
|
|
|
|
|
cpos := gamedata.Coordinates{}
|
|
|
|
|
cpos.X = g.blocky.GetPosition().X + dx
|
|
|
|
|
cpos.Y = g.blocky.GetPosition().Y + dy
|
|
|
|
|
g.blocky.SetTargetPosition(cpos)
|
2024-12-12 07:12:46 -05:00
|
|
|
}
|
2024-12-14 06:36:45 -05:00
|
|
|
case *pb.GameEvent_Eliminated:
|
|
|
|
|
fmt.Println("someone eliminated...", payload.Gameevent.Target)
|
2024-12-12 07:12:46 -05:00
|
|
|
|
2024-12-14 06:36:45 -05:00
|
|
|
/*
|
|
|
|
|
g.mu.Lock()
|
|
|
|
|
client := g.realclients[int(payload.Gameevent.Target)]
|
|
|
|
|
client.Eliminated = true
|
|
|
|
|
g.realclients[int(payload.Gameevent.Target)] = client
|
|
|
|
|
g.mu.Unlock()
|
|
|
|
|
*/
|
2024-12-12 07:12:46 -05:00
|
|
|
}
|
2024-12-11 07:41:21 -05:00
|
|
|
}
|
2024-12-10 18:55:23 -05:00
|
|
|
}
|
|
|
|
|
|
2024-12-11 18:18:04 -05:00
|
|
|
func (g *Game) HandleInput() {
|
|
|
|
|
|
|
|
|
|
dx := 0
|
|
|
|
|
dy := 0
|
2024-12-14 11:32:41 -05:00
|
|
|
|
2024-12-11 18:18:04 -05:00
|
|
|
if ebiten.IsKeyPressed(ebiten.KeyW) {
|
|
|
|
|
dy = -movementLimit
|
2024-12-10 18:55:23 -05:00
|
|
|
}
|
2024-12-11 18:18:04 -05:00
|
|
|
if ebiten.IsKeyPressed(ebiten.KeyS) {
|
|
|
|
|
dy = +movementLimit
|
|
|
|
|
}
|
|
|
|
|
if ebiten.IsKeyPressed(ebiten.KeyA) {
|
|
|
|
|
dx = -movementLimit
|
2024-12-14 11:32:41 -05:00
|
|
|
g.dino.SetLeft(true)
|
2024-12-11 18:18:04 -05:00
|
|
|
}
|
|
|
|
|
if ebiten.IsKeyPressed(ebiten.KeyD) {
|
|
|
|
|
dx = +movementLimit
|
2024-12-14 11:32:41 -05:00
|
|
|
g.dino.SetLeft(false)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if math.Abs(float64(dx)) > 0 || math.Abs(float64(dy)) > 0 {
|
|
|
|
|
g.dino.SetAction(gamedata.DinoActionWalk)
|
|
|
|
|
} else {
|
|
|
|
|
g.dino.SetAction(gamedata.DinoActionIdle)
|
2024-12-11 18:18:04 -05:00
|
|
|
}
|
2024-12-12 07:12:46 -05:00
|
|
|
|
2024-12-11 18:18:04 -05:00
|
|
|
cpos := g.blocky.GetPosition()
|
|
|
|
|
cpos.X += float64(dx)
|
|
|
|
|
cpos.Y += float64(dy)
|
2024-12-12 07:12:46 -05:00
|
|
|
g.blocky.SetTargetPosition(cpos)
|
|
|
|
|
|
|
|
|
|
if inpututil.IsKeyJustPressed(ebiten.KeySpace) {
|
|
|
|
|
g.SendSlap()
|
2024-12-14 11:32:41 -05:00
|
|
|
g.dino.SetAction(gamedata.DinoActionSlap)
|
2024-12-12 07:12:46 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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)
|
|
|
|
|
}
|
2024-12-10 18:55:23 -05:00
|
|
|
}
|