Added ring/elimination mechanic.
This commit is contained in:
@@ -7,6 +7,7 @@ import (
|
||||
"client/gamedata"
|
||||
"client/pb"
|
||||
"fmt"
|
||||
"image/color"
|
||||
"maps"
|
||||
"math"
|
||||
"sync"
|
||||
@@ -15,14 +16,18 @@ import (
|
||||
"github.com/hajimehoshi/ebiten/v2"
|
||||
"github.com/hajimehoshi/ebiten/v2/inpututil"
|
||||
"github.com/hajimehoshi/ebiten/v2/text/v2"
|
||||
"github.com/hajimehoshi/ebiten/v2/vector"
|
||||
"golang.org/x/exp/rand"
|
||||
)
|
||||
|
||||
var (
|
||||
const (
|
||||
screenWidth = 640
|
||||
screenHeight = 480
|
||||
movementLimit = 5
|
||||
stageRadius = 200
|
||||
)
|
||||
|
||||
var (
|
||||
namelist = []string{"slappy", "mick", "rodney", "george", "ringo",
|
||||
"robin", "temitry", "evangeline", "ron", "abigail", "lester",
|
||||
"maynard", "agnes", "stacey", "wendell", "susanne", "myrtle",
|
||||
@@ -37,22 +42,28 @@ func init() {
|
||||
}
|
||||
|
||||
type ClientData struct {
|
||||
Id int
|
||||
Address string
|
||||
Name string
|
||||
Position gamedata.Coordinates
|
||||
Hit bool
|
||||
Id int
|
||||
Address string
|
||||
Name string
|
||||
Position gamedata.Coordinates
|
||||
Hit bool
|
||||
Eliminated bool
|
||||
}
|
||||
|
||||
type Game struct {
|
||||
name string
|
||||
blocky *elements.Block
|
||||
hitblocky *elements.Block
|
||||
elimblocky *elements.Block
|
||||
gameId int
|
||||
realclients map[int]ClientData
|
||||
gameclient *client.Client
|
||||
cycle int
|
||||
mu sync.Mutex
|
||||
|
||||
//similar fields that we see in the client list, but for us
|
||||
eliminated bool
|
||||
hit bool
|
||||
}
|
||||
|
||||
func NewGame() *Game {
|
||||
@@ -60,11 +71,14 @@ func NewGame() *Game {
|
||||
gameclient: client.NewClient(),
|
||||
blocky: elements.NewBlock(),
|
||||
hitblocky: elements.NewBlock(),
|
||||
elimblocky: elements.NewBlock(),
|
||||
cycle: 0,
|
||||
name: namelist[rand.Intn(len(namelist))],
|
||||
}
|
||||
|
||||
g.hitblocky.SetHit(true)
|
||||
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})
|
||||
|
||||
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})
|
||||
@@ -93,11 +107,22 @@ func (g *Game) Draw(screen *ebiten.Image) {
|
||||
|
||||
g.blocky.Draw()
|
||||
g.hitblocky.Draw()
|
||||
g.elimblocky.Draw()
|
||||
|
||||
vector.StrokeCircle(screen, float32(screenWidth)/2, float32(screenHeight)/2, stageRadius, 3, color.White, true)
|
||||
|
||||
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)
|
||||
screen.DrawImage(g.blocky.Sprite, op)
|
||||
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)
|
||||
}
|
||||
f2 := &text.GoTextFace{
|
||||
Source: fonts.LaunchyFont.New,
|
||||
Size: 12,
|
||||
@@ -113,10 +138,15 @@ func (g *Game) Draw(screen *ebiten.Image) {
|
||||
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)
|
||||
if !client.Hit {
|
||||
screen.DrawImage(g.blocky.Sprite, op)
|
||||
|
||||
if client.Eliminated {
|
||||
screen.DrawImage(g.elimblocky.Sprite, op)
|
||||
} else {
|
||||
screen.DrawImage(g.hitblocky.Sprite, op)
|
||||
if !client.Hit {
|
||||
screen.DrawImage(g.blocky.Sprite, op)
|
||||
} else {
|
||||
screen.DrawImage(g.hitblocky.Sprite, op)
|
||||
}
|
||||
}
|
||||
|
||||
f2 := &text.GoTextFace{
|
||||
@@ -150,14 +180,20 @@ func (g *Game) HandleServerData(envelope *pb.ServerEnvelope) {
|
||||
X: client.Coordinates.X,
|
||||
Y: client.Coordinates.Y,
|
||||
},
|
||||
Hit: client.Hit,
|
||||
Hit: client.Hit,
|
||||
Eliminated: client.Eliminated,
|
||||
}
|
||||
|
||||
g.mu.Lock()
|
||||
g.realclients[int(client.Id)] = update
|
||||
g.mu.Unlock()
|
||||
} else {
|
||||
g.blocky.SetHit(client.Hit)
|
||||
g.eliminated = client.Eliminated
|
||||
g.hit = client.Hit
|
||||
//this is us
|
||||
|
||||
//g.blocky.SetHit(client.Hit)
|
||||
|
||||
}
|
||||
}
|
||||
case *pb.ServerEnvelope_Event:
|
||||
@@ -174,35 +210,48 @@ func (g *Game) HandleServerData(envelope *pb.ServerEnvelope) {
|
||||
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)
|
||||
//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()
|
||||
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()
|
||||
|
||||
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
|
||||
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)
|
||||
cpos := gamedata.Coordinates{}
|
||||
cpos.X = g.blocky.GetPosition().X + dx
|
||||
cpos.Y = g.blocky.GetPosition().Y + dy
|
||||
g.blocky.SetTargetPosition(cpos)
|
||||
}
|
||||
case *pb.GameEvent_Eliminated:
|
||||
fmt.Println("someone eliminated...", payload.Gameevent.Target)
|
||||
|
||||
/*
|
||||
g.mu.Lock()
|
||||
client := g.realclients[int(payload.Gameevent.Target)]
|
||||
client.Eliminated = true
|
||||
g.realclients[int(payload.Gameevent.Target)] = client
|
||||
g.mu.Unlock()
|
||||
*/
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user