|
|
|
@ -137,37 +137,43 @@ func (g *Game) startNetworkGame() {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (g *Game) InitNetworking(localPort int, numPlayers int, players []ggpo.Player, numSpectators int) {
|
|
|
|
|
var result error
|
|
|
|
|
var inputBits InputBits = 0
|
|
|
|
|
var inputSize int = len(encodeInputs(inputBits))
|
|
|
|
|
|
|
|
|
|
session := NewGameSession()
|
|
|
|
|
|
|
|
|
|
var inputBits InputBits = 0
|
|
|
|
|
inputSize := len(encodeInputs(inputBits))
|
|
|
|
|
|
|
|
|
|
peer := ggpo.NewPeer(session, localPort, numPlayers, inputSize)
|
|
|
|
|
//peer := ggpo.NewSyncTest(&session, numPlayers, 8, inputSize, true)
|
|
|
|
|
world.Backend = &peer
|
|
|
|
|
session.backend = world.Backend
|
|
|
|
|
peer.InitializeConnection()
|
|
|
|
|
peer.Start()
|
|
|
|
|
session.backend = &peer
|
|
|
|
|
|
|
|
|
|
//session.SetDisconnectTimeout(3000)
|
|
|
|
|
//session.SetDisconnectNotifyStart(1000)
|
|
|
|
|
err := peer.InitializeConnection()
|
|
|
|
|
if err != nil {
|
|
|
|
|
log.Fatalf("failed to initialize connection: %s", err)
|
|
|
|
|
}
|
|
|
|
|
err = peer.SetDisconnectTimeout(3000)
|
|
|
|
|
if err != nil {
|
|
|
|
|
log.Fatalf("failed to set disconnect timeout: %s", err)
|
|
|
|
|
}
|
|
|
|
|
err = peer.SetDisconnectNotifyStart(1000)
|
|
|
|
|
if err != nil {
|
|
|
|
|
log.Fatalf("failed to set disconnect notify start: %s", err)
|
|
|
|
|
}
|
|
|
|
|
peer.Start()
|
|
|
|
|
|
|
|
|
|
for i := 0; i < numPlayers+numSpectators; i++ {
|
|
|
|
|
var handle ggpo.PlayerHandle
|
|
|
|
|
result = peer.AddPlayer(&players[i], &handle)
|
|
|
|
|
if players[i].PlayerType == ggpo.PlayerTypeLocal {
|
|
|
|
|
world.CurrentPlayer = int(handle)
|
|
|
|
|
}
|
|
|
|
|
if result != nil {
|
|
|
|
|
log.Fatalf("There's an issue from AddPlayer")
|
|
|
|
|
err = peer.AddPlayer(&players[i], &handle)
|
|
|
|
|
if err != nil {
|
|
|
|
|
log.Fatalf("failed to add player: %s", err)
|
|
|
|
|
}
|
|
|
|
|
if players[i].PlayerType == ggpo.PlayerTypeLocal {
|
|
|
|
|
peer.SetFrameDelay(handle, frameDelay)
|
|
|
|
|
world.CurrentPlayer = int(handle)
|
|
|
|
|
err = peer.SetFrameDelay(handle, frameDelay)
|
|
|
|
|
if err != nil {
|
|
|
|
|
log.Fatalf("failed to set frame delay: %s", err)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
peer.SetDisconnectTimeout(3000)
|
|
|
|
|
peer.SetDisconnectNotifyStart(1000)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (g *Game) ReadInputs() InputBits {
|
|
|
|
@ -195,7 +201,12 @@ func (g *Game) ReadInputs() InputBits {
|
|
|
|
|
|
|
|
|
|
func (g *Game) applyPhysics() {
|
|
|
|
|
for i := 0; i < 2; i++ {
|
|
|
|
|
opp := 0
|
|
|
|
|
if i == 0 {
|
|
|
|
|
opp = 1
|
|
|
|
|
}
|
|
|
|
|
p := &g.Players[i]
|
|
|
|
|
o := &g.Players[opp]
|
|
|
|
|
|
|
|
|
|
// Apply gravity.
|
|
|
|
|
if p.VY > -world.Gravity {
|
|
|
|
@ -208,11 +219,21 @@ func (g *Game) applyPhysics() {
|
|
|
|
|
// Apply velocity.
|
|
|
|
|
p.X, p.Y = p.X+p.VX, p.Y+p.VY
|
|
|
|
|
|
|
|
|
|
// TODO check player collision.
|
|
|
|
|
// Apply player collision.
|
|
|
|
|
playerRect := world.FloatRect(g.Players[i].X, g.Players[i].Y, g.Players[i].X+float64(component.PlayerSize), g.Players[i].Y+float64(component.PlayerSize))
|
|
|
|
|
oppRect := world.FloatRect(g.Players[opp].X, g.Players[opp].Y, g.Players[opp].X+float64(component.PlayerSize), g.Players[opp].Y+float64(component.PlayerSize))
|
|
|
|
|
|
|
|
|
|
if playerRect.Overlaps(oppRect) {
|
|
|
|
|
if playerRect.Min.X < oppRect.Min.X {
|
|
|
|
|
p.X = o.X - component.PlayerSize
|
|
|
|
|
} else {
|
|
|
|
|
p.X = o.X + component.PlayerSize
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Apply ground collision.
|
|
|
|
|
if p.Y-component.PlayerSize < 0 {
|
|
|
|
|
p.Y = component.PlayerSize
|
|
|
|
|
if p.Y < 0 {
|
|
|
|
|
p.Y = 0
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
@ -240,7 +261,6 @@ func (g *Game) UpdateByInputs(inputs []InputBits) {
|
|
|
|
|
if grounded {
|
|
|
|
|
g.Players[i].VY = 20
|
|
|
|
|
}
|
|
|
|
|
log.Println("JUMP", grounded)
|
|
|
|
|
}
|
|
|
|
|
if input.isButtonOn(ButtonDown) && !component.TranslateRect(playerRect, 0, 1).Overlaps(oppRect) {
|
|
|
|
|
//g.Players[i].VY = -1
|
|
|
|
@ -265,12 +285,12 @@ func (g *Game) UpdateByInputs(inputs []InputBits) {
|
|
|
|
|
// TODO player starts in idle action?
|
|
|
|
|
if g.Players[i].ActionTicksLeft != 0 {
|
|
|
|
|
// Run current frame logic.
|
|
|
|
|
log.Printf("apply hitbox for player %d action %d ticks left %d", i, g.Players[i].Action, g.Players[i].ActionTicksLeft)
|
|
|
|
|
|
|
|
|
|
// TODO handle invulnerability and blocking
|
|
|
|
|
|
|
|
|
|
allFrameData := component.FrameDataForActionTick(g.Players[i].Action, g.Players[i].ActionTicksLeft)
|
|
|
|
|
for _, frame := range allFrameData {
|
|
|
|
|
// Apply hitbox.
|
|
|
|
|
if frame.T == component.HitboxHurt {
|
|
|
|
|
// Hit opponent.
|
|
|
|
|
if oppRect.Overlaps(component.TranslateRect(frame.R, int(player.X), int(player.Y))) {
|
|
|
|
@ -284,8 +304,6 @@ func (g *Game) UpdateByInputs(inputs []InputBits) {
|
|
|
|
|
// Stun the opponent.
|
|
|
|
|
opponent.Action = component.ActionStunned
|
|
|
|
|
opponent.ActionTicksLeft = 7
|
|
|
|
|
|
|
|
|
|
log.Println("HIT")
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
@ -377,6 +395,32 @@ func (g *Game) Update() error {
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if inpututil.IsKeyJustPressed(ebiten.KeyMinus) && ebiten.IsKeyPressed(ebiten.KeyControl) {
|
|
|
|
|
for i, preset := range world.TPSPresets {
|
|
|
|
|
if preset == world.TPS {
|
|
|
|
|
if i > 0 {
|
|
|
|
|
world.TPS = world.TPSPresets[i-1]
|
|
|
|
|
ebiten.SetTPS(world.TPS)
|
|
|
|
|
log.Printf("Set TPS to %d", world.TPS)
|
|
|
|
|
break
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if inpututil.IsKeyJustPressed(ebiten.KeyEqual) && ebiten.IsKeyPressed(ebiten.KeyControl) {
|
|
|
|
|
for i, preset := range world.TPSPresets {
|
|
|
|
|
if preset == world.TPS {
|
|
|
|
|
if i < len(world.TPSPresets)-1 {
|
|
|
|
|
world.TPS = world.TPSPresets[i+1]
|
|
|
|
|
ebiten.SetTPS(world.TPS)
|
|
|
|
|
log.Printf("Set TPS to %d", world.TPS)
|
|
|
|
|
break
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if world.ConnectPromptConfirmed && !world.ConnectionActive {
|
|
|
|
|
if world.ConnectPromptText == "" {
|
|
|
|
|
g.startLocalGame()
|
|
|
|
|