public void Flip(bool inLeft, bool inActive) { if (!testing) { if (inLeft == this.left) { myFlipper.Flip(inActive); if (testing) { otherFlipper.Flip(inActive); } } else { otherFlipper.Flip(inActive); } } else { myFlipper.Flip(inActive); otherFlipper.Flip(inActive); } }
private IEnumerator ServeUDP(int port) { int curCode = -1; NetDataWriter writer = new NetDataWriter(); EventBasedNetListener listener = new EventBasedNetListener(); NetManager server = new NetManager(listener); Transform ball = null; Rigidbody ballBody = null; bool syncBall = false; Console.WriteLine("Starting Server on {0}", port); Debug.Log("Starting Server on " + port); server.Start(port); //left and right flipper for this port Transform left, right; FlipperButton fbLeft = null, fbRight = null; NetPeer lClient = null, rClient = null; bool bothPlayers = true; Transform playField = null; IEnumerator timeout = null;; if (port != 3000) { timeout = WaitForTimeout(port, server); StartCoroutine(timeout); Console.WriteLine("Finding play field"); playField = tables.Find(port.ToString()); Console.WriteLine("Finding ball"); ball = playField.Find("Ball"); ballBody = ball.GetComponent <Rigidbody>(); Console.WriteLine("Finding FlipperButton left"); fbLeft = playField.Find("PlayerInput").Find("PlayerLeft").Find("ButtonParent").GetChild(0).GetChild(0).GetComponent <FlipperButton>(); fbLeft.enabled = true; Console.WriteLine("Finding FlipperButton right"); fbRight = playField.Find("PlayerInput").Find("PlayerRight").Find("ButtonParent").GetChild(0).GetChild(0).GetComponent <FlipperButton>(); fbRight.enabled = true; Console.WriteLine("Found both flippers"); } listener.ConnectionRequestEvent += request => { if (server.PeersCount < 10) { request.AcceptIfKey("TestKey"); } else { request.Reject(); } }; listener.PeerConnectedEvent += peer => { Console.WriteLine("PeerConnectedEvent"); if (testMode) { lClient = peer; } }; listener.PeerDisconnectedEvent += (peer, info) => { //remove player from queue for (int i = 0; i < mQueue.Count; i++) { NetPeer nxt = mQueue.Dequeue(); if (nxt.ConnectionNum != peer.ConnectionNum) { mQueue.Enqueue(nxt); } else { Console.WriteLine("Dequeueing [client] due to disconnect event"); } } //remove player from game lobby if (lClient != null && rClient != null) { if (peer.ConnectionNum == lClient.ConnectionNum) { if (bothPlayers) { Console.WriteLine("dispatch lClient leave"); fbLeft.enabled = false; bothPlayers = false; } else { TeardownServer(server, port, 1); } } else if (peer.ConnectionNum == rClient.ConnectionNum) { if (bothPlayers) { Console.WriteLine("dispatch rClient leave"); fbRight.enabled = false; bothPlayers = false; } else { TeardownServer(server, port, 1); } } } }; listener.NetworkReceiveEvent += (peer, reader, delivery) => { curCode = -1; try{ curCode = reader.GetInt(); } catch (Exception e) { Console.WriteLine("error. malformatted packet"); } switch (curCode) { case 0: //handshake if (port == 3000) { Console.WriteLine("Client Handshake Received"); UnityEngine.Debug.Log("Client Handshake Received"); PacketLib.SendMessage(peer, writer, (int)PostCode.SHAKE); } break; case 1: //queue //add peer to queue mQueue.Enqueue(peer); StartCoroutine(ManageQueue(writer)); PacketLib.SendMessage(peer, writer, (int)PostCode.QUEUE); break; case 2: //invite Console.WriteLine("[client] has joined game lobby on port: " + port); Debug.Log("[client] has joined game lobby on port: " + port); //assign left and right flipper if (reader.GetBool()) { lClient = peer; if (rClient != null) { StartCoroutine(StartGame(lClient, rClient, writer, timeout, port, ball)); syncBall = true; } } else { rClient = peer; if (lClient != null) { StartCoroutine(StartGame(lClient, rClient, writer, timeout, port, ball)); syncBall = true; } } //get a handle to the table/playfield for this port break; case 4: //flip //rotate server paddle if (port != 3000) { bool isLeft = reader.GetBool(); bool active = reader.GetBool(); if (rClient != null) { PacketLib.SendMessage(rClient, writer, (int)PostCode.FLIP, isLeft, active); } if (lClient != null) { PacketLib.SendMessage(lClient, writer, (int)PostCode.FLIP, isLeft, active); } //move paddles Debug.Log("Animating paddle movement server-side"); if (fbLeft != null && isLeft) { fbLeft.Flip(active); } else if (fbRight != null && !isLeft) { fbRight.Flip(active); } } break; case 5: //log Console.WriteLine(reader.GetString()); break; case 6: //test if (timeout == null) { SystemTest(); } else { StopCoroutine(timeout); } break; case -1: default: break; } }; while (!Console.KeyAvailable) { server.PollEvents(); if (syncBall && port != 3000) { PacketLib.SendMessage(lClient, writer, (int)PostCode.BALL, ball.position, ballBody.angularVelocity); PacketLib.SendMessage(rClient, writer, (int)PostCode.BALL, ball.position, ballBody.angularVelocity); } yield return(new WaitForSeconds(0.015f)); } Console.WriteLine("Gracefully killing server"); server.Stop(); yield return(new WaitForSeconds(5f)); // wait 5 seconds after stopping server to kill app Application.Quit(); }