|
|
|
@ -49,6 +49,7 @@ func NewGame() (*Game, error) {
|
|
|
|
|
|
|
|
|
|
if !addedGame {
|
|
|
|
|
entity.NewOnceEntity()
|
|
|
|
|
gohan.AddSystem(&system.MapSystem{})
|
|
|
|
|
gohan.AddSystem(&system.PlayerSystem{})
|
|
|
|
|
gohan.AddSystem(&system.UISystem{})
|
|
|
|
|
addedGame = true
|
|
|
|
@ -134,48 +135,11 @@ func (g *Game) startNetworkGame() {
|
|
|
|
|
|
|
|
|
|
world.ConnectionActive = true
|
|
|
|
|
}
|
|
|
|
|
func (g *Game) Update() error {
|
|
|
|
|
if ebiten.IsWindowBeingClosed() || (!world.WASM && ebiten.IsKeyPressed(ebiten.KeyEscape)) {
|
|
|
|
|
g.Exit()
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if world.ConnectPromptConfirmed && !world.ConnectionActive {
|
|
|
|
|
if world.ConnectPromptText == "" {
|
|
|
|
|
g.startLocalGame()
|
|
|
|
|
} else {
|
|
|
|
|
g.startNetworkGame()
|
|
|
|
|
}
|
|
|
|
|
g.playerStateUpdated()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if world.ConnectionActive {
|
|
|
|
|
err := world.Backend.Idle(0)
|
|
|
|
|
if err != nil {
|
|
|
|
|
panic(err)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
g.RunFrame()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return gohan.Update()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (g *Game) playerStateUpdated() {
|
|
|
|
|
world.Player1, world.Player2 = g.Players[0], g.Players[1]
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (g *Game) Draw(screen *ebiten.Image) {
|
|
|
|
|
err := gohan.Draw(screen)
|
|
|
|
|
if err != nil {
|
|
|
|
|
log.Fatal(err)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (g *Game) Exit() {
|
|
|
|
|
os.Exit(0)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (g *Game) InitNetworking(localPort int, numPlayers int, players []ggpo.Player, numSpectators int) {
|
|
|
|
|
var result error
|
|
|
|
|
var inputBits InputBits = 0
|
|
|
|
@ -249,18 +213,37 @@ func (g *Game) AdvanceFrame(inputs []InputBits, disconnectFlags int) {
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (g *Game) applyPhysics() {
|
|
|
|
|
// Apply gravity.
|
|
|
|
|
for i := 0; i < 2; i++ {
|
|
|
|
|
p := &g.Players[i]
|
|
|
|
|
|
|
|
|
|
p.Y -= world.Gravity
|
|
|
|
|
if p.Y-component.PlayerSize < 0 {
|
|
|
|
|
p.Y = component.PlayerSize
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (g *Game) UpdateByInputs(inputs []InputBits) {
|
|
|
|
|
for i, input := range inputs {
|
|
|
|
|
if input.isButtonOn(ButtonUp) {
|
|
|
|
|
opp := 0
|
|
|
|
|
if i == 0 {
|
|
|
|
|
opp = 1
|
|
|
|
|
}
|
|
|
|
|
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 input.isButtonOn(ButtonUp) && !component.TranslateRect(playerRect, 0, -1).Overlaps(oppRect) {
|
|
|
|
|
g.Players[i].Y--
|
|
|
|
|
}
|
|
|
|
|
if input.isButtonOn(ButtonDown) {
|
|
|
|
|
if input.isButtonOn(ButtonDown) && !component.TranslateRect(playerRect, 0, 1).Overlaps(oppRect) {
|
|
|
|
|
g.Players[i].Y++
|
|
|
|
|
}
|
|
|
|
|
if input.isButtonOn(ButtonLeft) {
|
|
|
|
|
if input.isButtonOn(ButtonLeft) && !component.TranslateRect(playerRect, -1, 0).Overlaps(oppRect) {
|
|
|
|
|
g.Players[i].X--
|
|
|
|
|
}
|
|
|
|
|
if input.isButtonOn(ButtonRight) {
|
|
|
|
|
if input.isButtonOn(ButtonRight) && !component.TranslateRect(playerRect, 1, 0).Overlaps(oppRect) {
|
|
|
|
|
g.Players[i].X++
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
@ -287,6 +270,8 @@ func (g *Game) UpdateByInputs(inputs []InputBits) {
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
g.applyPhysics()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (g *Game) ReadInputs() InputBits {
|
|
|
|
@ -316,7 +301,6 @@ func (g *Game) ReadInputsP2() InputBits {
|
|
|
|
|
var in InputBits
|
|
|
|
|
|
|
|
|
|
// TODO Support local multiplayer?
|
|
|
|
|
|
|
|
|
|
return in
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
@ -331,3 +315,48 @@ func (g *Game) Checksum() int {
|
|
|
|
|
}
|
|
|
|
|
return sum
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (g *Game) Update() error {
|
|
|
|
|
if ebiten.IsWindowBeingClosed() || (!world.WASM && ebiten.IsKeyPressed(ebiten.KeyEscape)) {
|
|
|
|
|
g.Exit()
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if inpututil.IsKeyJustPressed(ebiten.KeyV) && ebiten.IsKeyPressed(ebiten.KeyControl) {
|
|
|
|
|
world.Debug++
|
|
|
|
|
if world.Debug > world.MaxDebug {
|
|
|
|
|
world.Debug = 0
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if world.ConnectPromptConfirmed && !world.ConnectionActive {
|
|
|
|
|
if world.ConnectPromptText == "" {
|
|
|
|
|
g.startLocalGame()
|
|
|
|
|
} else {
|
|
|
|
|
g.startNetworkGame()
|
|
|
|
|
}
|
|
|
|
|
g.playerStateUpdated()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if world.ConnectionActive {
|
|
|
|
|
err := world.Backend.Idle(0)
|
|
|
|
|
if err != nil {
|
|
|
|
|
panic(err)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
g.RunFrame()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return gohan.Update()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (g *Game) Draw(screen *ebiten.Image) {
|
|
|
|
|
err := gohan.Draw(screen)
|
|
|
|
|
if err != nil {
|
|
|
|
|
log.Fatal(err)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (g *Game) Exit() {
|
|
|
|
|
os.Exit(0)
|
|
|
|
|
}
|
|
|
|
|