|
|
|
@ -136,10 +136,6 @@ func (g *Game) startNetworkGame() {
|
|
|
|
|
world.ConnectionActive = true
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (g *Game) playerStateUpdated() {
|
|
|
|
|
world.Player1, world.Player2 = g.Players[0], g.Players[1]
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (g *Game) InitNetworking(localPort int, numPlayers int, players []ggpo.Player, numSpectators int) {
|
|
|
|
|
var result error
|
|
|
|
|
var inputBits InputBits = 0
|
|
|
|
@ -174,51 +170,47 @@ func (g *Game) InitNetworking(localPort int, numPlayers int, players []ggpo.Play
|
|
|
|
|
peer.SetDisconnectNotifyStart(1000)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (g *Game) RunFrame() {
|
|
|
|
|
input := g.ReadInputs()
|
|
|
|
|
buffer := encodeInputs(input)
|
|
|
|
|
|
|
|
|
|
//fmt.Println("Attempting to add local inputs")
|
|
|
|
|
result := world.Backend.AddLocalInput(ggpo.PlayerHandle(world.CurrentPlayer), buffer, len(buffer))
|
|
|
|
|
|
|
|
|
|
//fmt.Println("Attempt to add local inputs complete")
|
|
|
|
|
if result == nil {
|
|
|
|
|
//fmt.Println("Attempt to add local inputs was successful")
|
|
|
|
|
var values [][]byte
|
|
|
|
|
disconnectFlags := 0
|
|
|
|
|
func (g *Game) ReadInputs() InputBits {
|
|
|
|
|
var in InputBits
|
|
|
|
|
|
|
|
|
|
//fmt.Println("Attempting to synchronize inputs")
|
|
|
|
|
values, result = world.Backend.SyncInput(&disconnectFlags)
|
|
|
|
|
if result == nil {
|
|
|
|
|
//fmt.Println("Attempt synchronize inputs was sucessful")
|
|
|
|
|
inputs := decodeInputs(values)
|
|
|
|
|
//fmt.Println("Advancing Frame from game loop")
|
|
|
|
|
g.AdvanceFrame(inputs, disconnectFlags)
|
|
|
|
|
} else {
|
|
|
|
|
//fmt.Printf("Attempt synchronize inputs was unsuccessful: %s\n", result)
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
//fmt.Printf("Attempt to add local inputs unsuccessful: %s\n", result)
|
|
|
|
|
if ebiten.IsKeyPressed(ebiten.KeyArrowUp) || ebiten.IsKeyPressed(ebiten.KeyW) {
|
|
|
|
|
in.setButton(ButtonUp)
|
|
|
|
|
}
|
|
|
|
|
if ebiten.IsKeyPressed(ebiten.KeyArrowDown) || ebiten.IsKeyPressed(ebiten.KeyS) {
|
|
|
|
|
in.setButton(ButtonDown)
|
|
|
|
|
}
|
|
|
|
|
if ebiten.IsKeyPressed(ebiten.KeyArrowLeft) || ebiten.IsKeyPressed(ebiten.KeyA) {
|
|
|
|
|
in.setButton(ButtonLeft)
|
|
|
|
|
}
|
|
|
|
|
if ebiten.IsKeyPressed(ebiten.KeyArrowRight) || ebiten.IsKeyPressed(ebiten.KeyD) {
|
|
|
|
|
in.setButton(ButtonRight)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
g.playerStateUpdated()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (g *Game) AdvanceFrame(inputs []InputBits, disconnectFlags int) {
|
|
|
|
|
g.UpdateByInputs(inputs)
|
|
|
|
|
|
|
|
|
|
err := world.Backend.AdvanceFrame(uint32(g.Checksum()))
|
|
|
|
|
if err != nil {
|
|
|
|
|
panic(err)
|
|
|
|
|
if inpututil.IsKeyJustPressed(ebiten.KeyH) {
|
|
|
|
|
in.setButton(ButtonPunch)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return in
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (g *Game) applyPhysics() {
|
|
|
|
|
// Apply gravity.
|
|
|
|
|
for i := 0; i < 2; i++ {
|
|
|
|
|
p := &g.Players[i]
|
|
|
|
|
|
|
|
|
|
p.Y -= world.Gravity
|
|
|
|
|
// Apply gravity.
|
|
|
|
|
if p.VY > -world.Gravity {
|
|
|
|
|
p.VY -= 1
|
|
|
|
|
if p.VY < -world.Gravity {
|
|
|
|
|
p.VY = -world.Gravity
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Apply velocity.
|
|
|
|
|
p.X, p.Y = p.X+p.VX, p.Y+p.VY
|
|
|
|
|
|
|
|
|
|
// TODO check player collision.
|
|
|
|
|
|
|
|
|
|
// Apply ground collision.
|
|
|
|
|
if p.Y-component.PlayerSize < 0 {
|
|
|
|
|
p.Y = component.PlayerSize
|
|
|
|
|
}
|
|
|
|
@ -234,17 +226,26 @@ func (g *Game) UpdateByInputs(inputs []InputBits) {
|
|
|
|
|
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))
|
|
|
|
|
|
|
|
|
|
g.Players[i].VX = g.Players[i].VX * 0.8
|
|
|
|
|
g.Players[i].VY = g.Players[i].VY * 0.8
|
|
|
|
|
|
|
|
|
|
if input.isButtonOn(ButtonUp) && !component.TranslateRect(playerRect, 0, -1).Overlaps(oppRect) {
|
|
|
|
|
g.Players[i].Y--
|
|
|
|
|
grounded := g.Players[i].Y == float64(component.PlayerSize)
|
|
|
|
|
// TODO check when last jump, grounded
|
|
|
|
|
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].Y++
|
|
|
|
|
//g.Players[i].VY = -1
|
|
|
|
|
// TODO crouch
|
|
|
|
|
}
|
|
|
|
|
if input.isButtonOn(ButtonLeft) && !component.TranslateRect(playerRect, -1, 0).Overlaps(oppRect) {
|
|
|
|
|
g.Players[i].X--
|
|
|
|
|
g.Players[i].VX = -1
|
|
|
|
|
}
|
|
|
|
|
if input.isButtonOn(ButtonRight) && !component.TranslateRect(playerRect, 1, 0).Overlaps(oppRect) {
|
|
|
|
|
g.Players[i].X++
|
|
|
|
|
g.Players[i].VX = 1
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if g.Players[i].Action == component.ActionIdle {
|
|
|
|
@ -274,35 +275,45 @@ func (g *Game) UpdateByInputs(inputs []InputBits) {
|
|
|
|
|
g.applyPhysics()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (g *Game) ReadInputs() InputBits {
|
|
|
|
|
func (g *Game) ReadInputsP2() InputBits {
|
|
|
|
|
var in InputBits
|
|
|
|
|
|
|
|
|
|
if ebiten.IsKeyPressed(ebiten.KeyArrowUp) || ebiten.IsKeyPressed(ebiten.KeyW) {
|
|
|
|
|
in.setButton(ButtonUp)
|
|
|
|
|
}
|
|
|
|
|
if ebiten.IsKeyPressed(ebiten.KeyArrowDown) || ebiten.IsKeyPressed(ebiten.KeyS) {
|
|
|
|
|
in.setButton(ButtonDown)
|
|
|
|
|
}
|
|
|
|
|
if ebiten.IsKeyPressed(ebiten.KeyArrowLeft) || ebiten.IsKeyPressed(ebiten.KeyA) {
|
|
|
|
|
in.setButton(ButtonLeft)
|
|
|
|
|
}
|
|
|
|
|
if ebiten.IsKeyPressed(ebiten.KeyArrowRight) || ebiten.IsKeyPressed(ebiten.KeyD) {
|
|
|
|
|
in.setButton(ButtonRight)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if inpututil.IsKeyJustPressed(ebiten.KeyH) {
|
|
|
|
|
in.setButton(ButtonPunch)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// TODO Support local multiplayer?
|
|
|
|
|
return in
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (g *Game) ReadInputsP2() InputBits {
|
|
|
|
|
var in InputBits
|
|
|
|
|
func (g *Game) playerStateUpdated() {
|
|
|
|
|
world.Player1, world.Player2 = g.Players[0], g.Players[1]
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// TODO Support local multiplayer?
|
|
|
|
|
return in
|
|
|
|
|
func (g *Game) RunFrame() {
|
|
|
|
|
input := g.ReadInputs()
|
|
|
|
|
buffer := encodeInputs(input)
|
|
|
|
|
|
|
|
|
|
//fmt.Println("Attempting to add local inputs")
|
|
|
|
|
result := world.Backend.AddLocalInput(ggpo.PlayerHandle(world.CurrentPlayer), buffer, len(buffer))
|
|
|
|
|
|
|
|
|
|
//fmt.Println("Attempt to add local inputs complete")
|
|
|
|
|
if result == nil {
|
|
|
|
|
//fmt.Println("Attempt to add local inputs was successful")
|
|
|
|
|
var values [][]byte
|
|
|
|
|
disconnectFlags := 0
|
|
|
|
|
|
|
|
|
|
//fmt.Println("Attempting to synchronize inputs")
|
|
|
|
|
values, result = world.Backend.SyncInput(&disconnectFlags)
|
|
|
|
|
if result == nil {
|
|
|
|
|
//fmt.Println("Attempt synchronize inputs was sucessful")
|
|
|
|
|
inputs := decodeInputs(values)
|
|
|
|
|
//fmt.Println("Advancing Frame from game loop")
|
|
|
|
|
g.AdvanceFrame(inputs, disconnectFlags)
|
|
|
|
|
} else {
|
|
|
|
|
//fmt.Printf("Attempt synchronize inputs was unsuccessful: %s\n", result)
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
//fmt.Printf("Attempt to add local inputs unsuccessful: %s\n", result)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
g.playerStateUpdated()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (g *Game) Checksum() int {
|
|
|
|
@ -316,6 +327,15 @@ func (g *Game) Checksum() int {
|
|
|
|
|
return sum
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (g *Game) AdvanceFrame(inputs []InputBits, disconnectFlags int) {
|
|
|
|
|
g.UpdateByInputs(inputs)
|
|
|
|
|
|
|
|
|
|
err := world.Backend.AdvanceFrame(uint32(g.Checksum()))
|
|
|
|
|
if err != nil {
|
|
|
|
|
panic(err)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (g *Game) Update() error {
|
|
|
|
|
if ebiten.IsWindowBeingClosed() || (!world.WASM && ebiten.IsKeyPressed(ebiten.KeyEscape)) {
|
|
|
|
|
g.Exit()
|
|
|
|
|