private void OnMessage(object sender, Message m) { Console.WriteLine(m.Type); data.Parse(m); switch (m.Type) { case MessageType.Init: bgcolor = data.BackgroundColor; ReloadPicturebox(); listBox1.Invoke((Action) delegate() { listBox1.Items.Clear(); zones.Clear(); pictureBoxWithInterpolationMode1.Image = b2 = new Bitmap(data.Width, data.Height); g2 = Graphics.FromImage(b2); foreach (var item in data.Zones.Values) { var z = new MyZone(item.Id, (int)item.Type); zones.Add(item.Id, z); listBox1.Items.Add(z); } }); DrawZone(-1); break; case MessageType.ZoneEdit: DrawZone(-1); break; case MessageType.ZoneCreate: { int id = -1; listBox1.Invoke((Action) delegate() { id = m.GetInt(0); var z = new MyZone(id, m.GetInt(1)); zones.Add(id, z); listBox1.Items.Add(z); }); DrawZone(-1); break; } case MessageType.ZoneDelete: { listBox1.Invoke((Action) delegate() { var id = m.GetInt(0); var z = zones[id]; zones.Remove(id); listBox1.Items.Remove(z); }); DrawZone(-1); break; } case MessageType.PlaceBlock: this.Invoke((Action) delegate() { int l = m.GetInt(1), x = m.GetInt(2), y = m.GetInt(3), id = m.GetInt(4); if (l == 0) { b.SetPixel(x, y, GetColor((int)data.Blocks[1, x, y].Id, id, bgcolor)); } else { b.SetPixel(x, y, GetColor(id, (int)data.Blocks[0, x, y].Id, bgcolor)); } pictureBox1.Invalidate(); }); break; case MessageType.BgColor: bgcolor = m.GetInt(0); ReloadPicturebox(); break; case MessageType.Clear: bgcolor = -1; ReloadPicturebox(); listBox1.Invoke((Action) delegate() { listBox1.Items.Clear(); }); zones.Clear(); DrawZone(-1); break; case MessageType.PlayerMove: { var id = m.GetInt(0); if (pid == id) { var p = data.Players[id]; var f = p.Keys; bool space = f.HasFlag(MovementKeys.Spacebar), up = f.HasFlag(MovementKeys.Up), down = f.HasFlag(MovementKeys.Down), left = f.HasFlag(MovementKeys.Left), right = f.HasFlag(MovementKeys.Right); var x = p.X; var y = p.Y; this.Invoke((Action) delegate { var b = Color.Black; var g = Color.LimeGreen; pictureBoxSpace.BackColor = space ? g : b; pictureBoxUp.BackColor = up ? g : b; pictureBoxDown.BackColor = down ? g : b; pictureBoxLeft.BackColor = left ? g : b; pictureBoxRight.BackColor = right ? g : b; label4.Text = $"X: {x}\nY: {y}"; }); } } break; case MessageType.PlayerAdd: case MessageType.PlayerJoin: this.Invoke((Action) delegate { var id = m.GetInt(0); var p = new MyPlayer(id, m.GetString(1)); players.Add(id, p); listBox2.Items.Add(p); }); break; case MessageType.PlayerExit: this.Invoke((Action) delegate { var id = m.GetInt(0); var p = players[id]; players.Remove(id); listBox2.Items.Remove(p); }); break; } }
//CountdownEvent ce = new CountdownEvent(1); private void OnMessage(object sender, EEUniverse.Library.Message m) { void die() { cli.Dispose(); panelMain.InvokeX(() => EnableMainPanel()); } switch (m.Type) { case MessageType.Init: re.Set(); if (m.GetString(7) != m.GetString(1)) { die(); MessageBox.Show("not world owner", ".w.", MessageBoxButtons.OK, MessageBoxIcon.Error); return; } world = new RoomData(parsePlayerData: false); world.Parse(m); int ww = world.Width, wh = world.Height; Stopwatch s = Stopwatch.StartNew(); //ce.Reset(); Task.Run(() => { //void PlaceBlock(int layer, int x, int y, int id) //{ // ce.AddCount(); // con.SendAsync(MessageType.PlaceBlock, layer, x, y, id).ContinueWith((_) => ce.Signal()); //} for (int j = 0; j < imgh; j++) //y { int yy = j + y; if (yy >= wh) { break; //out of bounds } for (int i = 0; i < imgw; i++) //x { int xx = i + x; if (xx >= ww) { break; //out of bounds } int b = idgrid[i, j]; if (skiptransparent && b == (int)BlockId.Black) { continue; } int l = ((BlockId)b).IsBackground() ? 0 : 1; if (world[l, xx, yy].Id != b) { con.PlaceBlock(l, xx, yy, b); } if (l == 0) { if (glass) { if (world[1, xx, yy].Id != (int)BlockId.Clear) { con.PlaceBlock(1, xx, yy, (int)BlockId.Clear); } } else if (world[1, xx, yy].Id != 0) { con.PlaceBlock(1, xx, yy, 0); } } //else if (i == 1) //{ // PlaceBlock(0, xx, yy, 0);//get rid of bg behind fg //} } } //Console.WriteLine(s.Elapsed); //ce.Signal(); //ce.Wait(); s.Stop(); Console.WriteLine(s.Elapsed); die(); }); break; } }