Browse Source

gamemenu now uses balance_util

feature/balanceutils
informaniac 7 years ago
parent
commit
16e895e390
  1. 51
      games/gamemenu.py

51
games/gamemenu.py

@ -8,6 +8,7 @@ import argparse @@ -8,6 +8,7 @@ import argparse
import subprocess
import socket
import struct
import blup.balance_util
import blup.frame
import blup.output
@ -210,9 +211,8 @@ class TestInput(AbstractInput): @@ -210,9 +211,8 @@ class TestInput(AbstractInput):
return None
class BalanceInput(AbstractInput, threading.Thread):
def __init__(self, addr, player_id):
self.addr = addr
self.player_id = player_id
def __init__(self, balance_util):
self.balance_util = balance_util
self.evt = None
self.lastevt = None
self.player_present = False
@ -221,27 +221,13 @@ class BalanceInput(AbstractInput, threading.Thread): @@ -221,27 +221,13 @@ class BalanceInput(AbstractInput, threading.Thread):
self.start()
def run(self):
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
try:
sock.connect(self.addr)
except ConnectionRefusedError:
print('could not connect to balance server', file=sys.stderr)
self.evt = InputEvent.QUIT
return
self.running = True
evt = None
oldevt = None
lastchange = 0
while self.running:
sock.send(b'a')
data = sock.recv(4)
p0x, p0y, p1x, p1y = struct.unpack('bbbb', data)
if self.player_id == 0:
xbal, ybal = p0x, p0y
elif self.player_id == 1:
xbal, ybal = p1x, p1y
THRESHOLD = 40
direction = self.balance_util.get_4dir(THRESHOLD)
MIN_TIMES = {
InputEvent.LEFT: 0.05,
InputEvent.RIGHT: 0.05,
@ -249,24 +235,21 @@ class BalanceInput(AbstractInput, threading.Thread): @@ -249,24 +235,21 @@ class BalanceInput(AbstractInput, threading.Thread):
InputEvent.DOWN: 0.05,
}
if self.player_present:
if xbal == -128 or ybal == -128:
if direction == blup.balance_util.Direction.QUIT:
self.evt = InputEvent.QUIT
self.player_present = False
continue
if abs(xbal) < THRESHOLD and abs(ybal) < THRESHOLD:
elif direction == blup.balance_util.Direction.CENTER:
evt = None
else:
if abs(xbal) < abs(ybal):
if ybal > 0:
elif direction == blup.balance_util.Direction.UP:
evt = InputEvent.UP
else:
elif direction == blup.balance_util.Direction.DOWN:
evt = InputEvent.DOWN
else:
if xbal > 0:
elif direction == blup.balance_util.Direction.RIGHT:
evt = InputEvent.RIGHT
else:
elif direction == blup.balance_util.Direction.LEFT:
evt = InputEvent.LEFT
if evt != oldevt:
lastchange = time.time()
oldevt = evt
@ -281,7 +264,7 @@ class BalanceInput(AbstractInput, threading.Thread): @@ -281,7 +264,7 @@ class BalanceInput(AbstractInput, threading.Thread):
#print('player_id=%d event=%s' % (self.player_id, self.evt))
time.sleep(0.005)
else:
if xbal != -128 and ybal != -128:
if direction != blup.balance_utils.Direction.QUIT:
self.evt = InputEvent.STEP_ON
self.player_present = True
continue
@ -325,8 +308,14 @@ if __name__ == '__main__': @@ -325,8 +308,14 @@ if __name__ == '__main__':
inp2 = TestInput()
elif args.balance is not None:
host, port = args.balance.split(':')
inp1 = BalanceInput((host, int(port)), 0)
inp2 = BalanceInput((host, int(port)), 1)
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
try:
sock.connect((host, int(port)))
inp1 = BalanceInput(blup.balance_util.BalanceUtil(sock, 0))
inp2 = BalanceInput(blup.balance_util.BalanceUtil(sock, 1))
except ConnectionRefusedError:
print('could not connect to balance server', file=sys.stderr)
sys.exit(1)
while True:
evt = inp1.get_event()

Loading…
Cancel
Save