static void actionSynSerialize(byte[] data) { int offset = 0; int actionNums = ByteUtil.BytesToInt2(data, offset); offset += 4; //Debug.Log("update move"); if (actionNums >= 0) { for (int i = 0; i < actionNums; i++) { int id = ByteUtil.BytesToInt2(data, offset); offset += 4; int action = ByteUtil.BytesToInt2(data, offset); offset += 4; switch (action) { case ActionType.keyboardMove: int dir = ByteUtil.BytesToInt2(data, offset); NetScene.getInstance().BallMove(id, dir); offset += 4; break; case ActionType.shoot: break; case ActionType.dragMove: break; default: break; } } } }
private void Update() { if (NetScene.getInstance().queMes.Count > 0) { MessageVO vo = NetScene.getInstance().queMes.Dequeue(); Serializer.Deserialize(vo.protocol, vo.data); if (NetScene.getInstance().frame % 350 == 0) { ModelFactory.getStance().spawn(); txtLog.text = ModelFactory.getStance().curSpawn + "/" + ModelFactory.maxSpawn; //txtLog.text = Random.Range(1, 100).ToString(); } } if (NetMgr.GetInstance().client.GetReadMsg() != null && NetMgr.GetInstance().client.GetReadMsg().Count > 0) { string s = NetMgr.GetInstance().client.GetReadMsg().Dequeue(); txtLog.text = s; Debug.Log(s); } if (NetMgr.GetInstance().client.GetLog() != null && NetMgr.GetInstance().client.GetLog().Count > 0) { string s = NetMgr.GetInstance().client.GetLog().Dequeue(); Debug.Log(s); } keyboardMove(); shoot(); }
void shoot() { if (Input.GetKeyDown(KeyCode.J)) { NetScene.getInstance().Shoot(); } }
private void OnTriggerEnter2D(Collider2D collision) { if (collision.tag.CompareTo(ObjectType.EnemyBullet) == 0) { NetObject netObject = NetScene.getInstance().GetBall(uid); netObject.isDead = true; TrashMan.despawn(this.gameObject); return; } }
// Update is called once per frame void Connect() { if (!NetMgr.GetInstance().client.IsConnected()) { NetMgr.GetInstance().client.Connect("192.168.1.102", 1500); NetScene.getInstance().StartTreadUpdateByNetWork(); } else { txtLog.text = "已建立连接"; } }
void onPointerUp(int dir) { if (NetScene.getInstance().IsDeath(NetScene.getInstance().MyId)) { return; } byte[] action = ByteUtil.IntToBytes2(ActionType.keyboardMove); byte[] content = ByteUtil.IntToBytes2(0); byte[] send = ByteUtil.BytesCombine(action, content); lastDir = 0; Debug.Log("send dir3:"); NetMgr.GetInstance().Send(Protocol.Update, send); }
public void createBallView(int id) { GameObject obj = TrashMan.spawn(ballPrefab.gameObject); BallView ball = obj.GetComponent <BallView>(); listBallView.Add(ball); ball.uid = id; obj.transform.SetParent(this.transform, false); obj.transform.localPosition = Vector3.zero; NetObject data = NetScene.getInstance().GetBall(ball.uid); ball.setData(data); }
// Update is called once per frame void Update() { if (start) { angle += Time.deltaTime * speed; //rTran.anchoredPosition = new Vector2(rTran.anchoredPosition.x, startY + Mathf.Sin(angle) * max); rTran.anchoredPosition = new Vector2(pos.center.x, pos.center.y); if (lastFrame != NetScene.getInstance().frame && NetScene.getInstance().frame % 100 == 0) { lastFrame = NetScene.getInstance().frame; //shoot(); } } }
public void shoot() { GameObject obj = TrashMan.spawn("Bullet"); obj.tag = ObjectType.EnemyBullet; BulletView bulletView = obj.GetComponent <BulletView>(); obj.transform.parent = shooter.transform.parent; obj.transform.position = shooter.transform.position; BulletLeftRunner bulletLeftRunner = new BulletLeftRunner(); Rectangle body = new Rectangle(shooterPos.origin.x, shooterPos.origin.y, 20, 20); NetScene.getInstance().collisionTree.Insert(body); bulletView.SetRunner(body); bulletView.valid = true; }
void keyboardMove() { int dir = 0; if (Input.GetKey(KeyCode.D)) { dir = 1; } else if (Input.GetKey(KeyCode.S)) { dir = 2; } else if (Input.GetKey(KeyCode.A)) { dir = 3; } else if (Input.GetKey(KeyCode.W)) { dir = 4; } //Debug.Log("send dir1"); if (lastDir == dir) { return; } if (NetScene.getInstance().IsDeath(NetScene.getInstance().MyId)) { return; } //Debug.Log("send dir1:"); byte[] action = ByteUtil.IntToBytes2(ActionType.keyboardMove); byte[] content = ByteUtil.IntToBytes2(dir); byte[] send = ByteUtil.BytesCombine(action, content); lastDir = dir; NetMgr.GetInstance().Send(Protocol.Update, send); }
public static void SynDeserialize(int protocol, byte[] data) { if (protocol == Protocol.Login) { int id = ByteUtil.BytesToInt2(data, 0); NetScene.getInstance().SetMyId(id); NetScene.getInstance().CreateBall(id); } else if (protocol == Protocol.PlayerJoin) { int id = ByteUtil.BytesToInt2(data, 0); NetScene.getInstance().CreateBall(id); } else if (protocol == Protocol.PlayerLeave) { int id = ByteUtil.BytesToInt2(data, 0); NetScene.getInstance().DelBall(id); } else if (protocol == Protocol.AddPlayer) { int num = data.Length / 4; int offset = 0; for (int i = 0; i < num; i++) { int id = ByteUtil.BytesToInt2(data, offset); NetScene.getInstance().CreateBall(id); offset += 4; } } else if (protocol == Protocol.Move) { //Debug.Log("update move"); int offset = 0; int num = ByteUtil.BytesToInt2(data, offset); for (int i = 0; i < num; i++) { offset += 4; int id = ByteUtil.BytesToInt2(data, offset); offset += 4; int actions = ByteUtil.BytesToInt2(data, offset); for (int j = 0; j < actions; j++) { offset += 4; int dir = ByteUtil.BytesToInt2(data, offset); NetScene.getInstance().BallMove(id, dir); } } NetScene.getInstance().UpdatePos(); } else if (protocol == Protocol.StartGame) { NetScene.getInstance().StartGame(); } else if (protocol == Protocol.Update) { actionSynSerialize(data); NetScene.getInstance().UpdatePos(); NetScene.getInstance().UpdateBulletPos(); NetScene.getInstance().UpdateEnemyPos(); return; int offset = 0; int haveData = ByteUtil.BytesToInt2(data, offset); //Debug.Log("update move"); if (haveData == 1) { offset = 4; int num = data.Length / 8; for (int i = 0; i < num; i++) { int id = ByteUtil.BytesToInt2(data, offset); offset += 4; int dir = ByteUtil.BytesToInt2(data, offset); NetScene.getInstance().BallMove(id, dir); offset += 4; } } NetScene.getInstance().UpdatePos(); } }
// Start is called before the first frame update void Start() { Application.targetFrameRate = 60; btnConnect = this.transform.Find("BtnConnect").GetComponent <Button>(); btnClose = this.transform.Find("BtnClose").GetComponent <Button>(); btnLogin = this.transform.Find("BtnLogin").GetComponent <Button>(); btnStart = this.transform.Find("BtnStart").GetComponent <Button>(); btnShoot = this.transform.Find("BtnShoot").GetComponent <Button>(); btnRight = this.transform.Find("BtnRight").GetComponent <DoubleClickButton>(); btnDown = this.transform.Find("BtnDown").GetComponent <DoubleClickButton>(); btnLeft = this.transform.Find("BtnLeft").GetComponent <DoubleClickButton>(); btnUp = this.transform.Find("BtnUp").GetComponent <DoubleClickButton>(); txtLog = this.transform.Find("TxtLog").GetComponent <Text>(); ballPrefab = this.transform.Find("Ball"); bulletPrefab = this.transform.Find("Bullet"); NetMgr.GetInstance().client = new SocketClient(); btnConnect.onClick.AddListener(delegate() { Connect(); }); btnClose.onClick.AddListener(delegate() { NetScene.getInstance().StopGame(); stopGame(); }); btnLogin.onClick.AddListener(delegate() { Debug.Log("click login"); //client.ClientWrite(Protocol.Move, "client send" + client.getLocalPort()); NetMgr.GetInstance().client.ClientWrite(Protocol.Login, "login");; }); btnStart.onClick.AddListener(delegate() { //client.ClientWrite(Protocol.Move, "client send" + client.getLocalPort()); Debug.Log("click start"); ModelFactory.getStance().startSpawn = true; NetMgr.GetInstance().client.ClientWrite(Protocol.StartGame, "start game");; }); btnShoot.onClick.AddListener(delegate() { //AlgorithmsUtil.test(); //client.ClientWrite(Protocol.Move, "client send" + client.getLocalPort()); Debug.Log("click shoot"); NetMgr.GetInstance().Send(Protocol.Update, ByteUtil.IntToBytes2(ActionType.shoot)); //byte[] action = ByteUtil.intToBytes2(ActionType.keyboardMove); //byte[] content = ByteUtil.intToBytes2(3); //byte[] send = ByteUtil.bytesCombine(action, content); //NetMgr.getInstance().send(Protocol.Update, send); }); btnRight.addPointerUp(delegate(object sender, EventArgs e) { onPointerUp(1); }); btnRight.addPointerDown(delegate(object sender, EventArgs e) { onPointerDown(1); }); btnDown.addPointerUp(delegate(object sender, EventArgs e) { onPointerUp(2); }); btnDown.addPointerDown(delegate(object sender, EventArgs e) { onPointerDown(2); }); btnLeft.addPointerUp(delegate(object sender, EventArgs e) { onPointerUp(3); }); btnLeft.addPointerDown(delegate(object sender, EventArgs e) { onPointerDown(3); }); btnUp.addPointerUp(delegate(object sender, EventArgs e) { onPointerUp(4); }); btnUp.addPointerDown(delegate(object sender, EventArgs e) { onPointerDown(4); }); Screen.fullScreenMode = FullScreenMode.Windowed; ModelFactory.getStance().init(); UnityEngine.Random.InitState(1); }