Esempio n. 1
0
        public Player(string name, Point2D point, ConsoleColor color, Statistics stat, Direction direction = Direction.up) {
            this.name = name;
            this.point = point;
            this.oldPoint = new Point2D(this.point.X, this.point.Y);
            this.color = color;
            this.stat = stat;
            this.oldDirection = direction;
            this.direction = direction;
            this.Changed = true;
            this.isShooter = false;
            this.model = new TankModel(new Point2D(point.X, point.Y), direction, color, name[0]);
            this.bullet = new Bullet(new Point2D(X, Y), model.Color, direction);
            this.timeToMove = new Stopwatch();
            this.timeToShoot = new Stopwatch();

            timeToMove.Start();
            timeToShoot.Start();
        }
Esempio n. 2
0
        public void Handler() {
            while(true) {
                if (packages.Count > 0) {
                    Package temp = packages.Dequeue();

                    IPEndPoint remoteIp = temp.Ip;
                    string message = temp.Message;
                    string[] package = message.Split('¶');

                    #region Обработчик пакетов
                    if (package[0] == "0" && package.Length == 4) {
                        string name = package[1];
                        ConsoleColor color = (ConsoleColor)Int32.Parse(package[2]);
                        int port = Int32.Parse(package[3]);

                        
                        

                        Point2D point;
                        TankModel tankModel;
                        lock(locker) {                            
                            while (true) {
                                bool flag = true;
                                point = new Point2D(rand.Next(3, Program.width - 3), rand.Next(3, Program.height - 3));
                                tankModel = new TankModel();
                                tankModel.generate(point, Direction.up);
                                foreach (var i in clients) {
                                    if(i.Value.Player.Model == tankModel) {
                                        flag = false;
                                    }
                                }
                                if (flag) {
                                    break;
                                }
                            }
                        }
                        Statistics stat = new Statistics(0, 0, 5, 5);
                        Direction direction = Direction.up;
                        
                        IPEndPoint reallyRemoteIp = new IPEndPoint(remoteIp.Address, port);
                        ClientObject newClient = new ClientObject(reallyRemoteIp, new Player(name, point, color, stat, direction));

                        clients.Add(++generateID, newClient);

                        Console.WriteLine("{0} | Игрок: {1}: {2} - подключился ", DateTime.Now.ToLongTimeString(), name, remoteIp);
                        updateTitle();

                        byte[] data = Encoding.Unicode.GetBytes(String.Format("4¶{0}¶{1}", point.X, point.Y));
                        sender.Send(data, data.Length, clients[generateID].IP);

                        message = "0¶" + clients[generateID].Player.Info;
                        // отослать всех игроков, только что зашедшему
                        BroadcastMessage(message + "¶" + generateID, generateID, true);

                        BroadcastMessage(message + "¶" + generateID, generateID);

                    } else if (package[0] == "1" && package.Length == 2) {

                        int id = 0;
                        foreach (var i in clients) {
                            if (i.Value.IP.Address.ToString() == remoteIp.Address.ToString() &&
                                i.Value.IP.Port.ToString() == (remoteIp.Port - 1).ToString()) {

                                i.Value.Player.move((Direction)Int32.Parse(package[1]));
                                id = i.Key;
                                break;
                            }
                        }
                        BroadcastMessage(message + "¶" + id, id);

                    } else if (package[0] == "2" && package.Length == 8) {

                        int id = 0;
                        foreach (var i in clients) {
                            if (i.Value.IP.Address.ToString() == remoteIp.Address.ToString() &&
                                i.Value.IP.Port.ToString() == (remoteIp.Port - 1).ToString()) {

                                id = i.Key;
                                break;
                            }
                        }
                        BroadcastMessage(message + "¶" + id, id);

                    } else if (package[0] == "3" && package.Length == 2) { // попадание
                        int idWhom = Int32.Parse(package[1]);
                        int idShooter = 0;
                        foreach (var i in clients) {
                            if (i.Value.IP.Address.ToString() == remoteIp.Address.ToString() &&
                                i.Value.IP.Port.ToString() == (remoteIp.Port - 1).ToString()) {

                                idShooter = i.Key;
                                break;
                            }
                        }

                        BroadcastMessage(message + "¶" + idShooter, idShooter);

                        clients[idWhom].Player.Stat.Health -= clients[idShooter].Player.Bullet.Damage;
                        if (clients[idWhom].Player.Stat.Health <= 0) {
                            clients[idWhom].Player.Stat.Health = clients[idWhom].Player.Stat.MaxHealth;
                            clients[idWhom].Player.Stat.Deaths++;
                            if (clients[idShooter].Player.Stat.Health + 2 <= clients[idShooter].Player.Stat.MaxHealth) {
                                clients[idShooter].Player.Stat.Health += 2;
                            } else {
                                clients[idShooter].Player.Stat.Health = clients[idShooter].Player.Stat.MaxHealth;
                            }
                            clients[idShooter].Player.Stat.Kills++;

                            Point2D point;
                            TankModel tankModel;
                            lock (locker) {
                                while (true) {
                                    bool flag = true;
                                    point = new Point2D(rand.Next(3, Program.width - 3), rand.Next(3, Program.height - 3));
                                    tankModel = new TankModel();
                                    tankModel.generate(point, Direction.up);
                                    foreach (var i in clients) {
                                        if (i.Value.Player.Model == tankModel) {
                                            flag = false;
                                        }
                                    }
                                    if (flag) {
                                        break;
                                    }
                                }
                            }

                            clients[idWhom].Player.revive(new Point2D(point.X, point.Y));

                            BroadcastMessage(String.Format("4¶{0}¶{1}¶{2}", point.X, point.Y, idWhom), idWhom);
                            byte[] data = Encoding.Unicode.GetBytes(String.Format("4¶{0}¶{1}", point.X, point.Y));
                            sender.Send(data, data.Length, clients[idWhom].IP);

                            Console.WriteLine("{0} | Игрок {1} убил игрока {2}", DateTime.Now.ToLongTimeString(), clients[idShooter].Player.Name, clients[idWhom].Player.Name);

                        }

                    } else if(package[0] == "5" && package.Length == 1) {
                        
                        for (int i = 0; i < clients.Count; ++i) {
                            if (clients.ElementAt(i).Value.IP.Address.ToString() == remoteIp.Address.ToString() &&
                                clients.ElementAt(i).Value.IP.Port.ToString() == (remoteIp.Port - 1).ToString()) {

                                clients.ElementAt(i).Value.Connected = true;
                            }
                        }
                    }
                    #endregion
                    continue;
                } else {
                    Thread.Sleep(1);
                }
            }
        }