Browse Source

gamemenu now uses balance_util

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

59
games/gamemenu.py

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

Loading…
Cancel
Save