// Метод для отрисовки модели в зависимости от направления // p - центральная координата public void show(Point2D p, Direction d) { //generate(p, d); Console.BackgroundColor = color; Console.ForegroundColor = Program.foregroundColor; Console.SetCursorPosition(p.X, p.Y); Console.Write(label); Console.BackgroundColor = Program.backgroundColor; //Console.SetCursorPosition(Program.X, Program.Y); }
public TankModel() { this.color = ConsoleColor.White; this.label = ' '; this.model = new Point2D[6]; this.oldModel = new Point2D[6]; for (int i = 0; i < this.model.Length; ++i) { model[i] = new Point2D(); oldModel[i] = new Point2D(); } }
public TankModel(Point2D p, Direction d, ConsoleColor color, char label) { this.color = color; this.label = label; this.model = new Point2D[6]; this.oldModel = new Point2D[6]; for (int i = 0; i < this.model.Length; ++i) { model[i] = new Point2D(); oldModel[i] = new Point2D(); } generate(p, d); }
public Bullet(Point2D point, ConsoleColor color, Direction direction = Direction.up, char icon = '∙', int speed = 980, int damage = 1) { this.point = point; this.oldPoint = new Point2D(this.point.X, this.point.Y); this.direction = direction; this.color = color; this.icon = icon; this.speed = speed; this.damage = damage; this.alive = false; this.Changed = false; }
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(); }
public void generate(Point2D p, Direction d) { center.X = p.X; center.Y = p.Y; for (int i = 0; i < model.Length; ++i) { oldModel[i].X = model[i].X; oldModel[i].Y = model[i].Y; } if (d == Direction.up) { #region up generate // [+] //[ ][ ][ ] //[ ] [ ] model[0].X = p.X; model[0].Y = p.Y - 1; // [ ] //[+][ ][ ] //[ ] [ ] model[1].X = p.X - 1; model[1].Y = p.Y; // [ ] //[ ][+][ ] //[ ] [ ] model[2].X = p.X; model[2].Y = p.Y; // [ ] //[ ][ ][+] //[ ] [ ] model[3].X = p.X + 1; model[3].Y = p.Y; // [ ] //[ ][ ][ ] //[+] [ ] model[4].X = p.X - 1; model[4].Y = p.Y + 1; // [ ] //[ ][ ][ ] //[ ] [+] model[5].X = p.X + 1; model[5].Y = p.Y + 1; #endregion } else if (d == Direction.down) { #region down generate //[ ] [ ] //[ ][ ][ ] // [+] model[0].X = p.X; model[0].Y = p.Y + 1; //[ ] [ ] //[+][ ][ ] // [ ] model[1].X = p.X - 1; model[1].Y = p.Y; //[ ] [ ] //[ ][+][ ] // [ ] model[2].X = p.X; model[2].Y = p.Y; //[ ] [ ] //[ ][ ][+] // [ ] model[3].X = p.X + 1; model[3].Y = p.Y; //[+] [ ] //[ ][ ][ ] // [ ] model[4].X = p.X - 1; model[4].Y = p.Y - 1; //[ ] [+] //[ ][ ][ ] // [ ] model[5].X = p.X + 1; model[5].Y = p.Y - 1; #endregion } else if (d == Direction.left) { #region left generate // [+][ ] //[ ][ ] // [ ][ ] model[0].X = p.X; model[0].Y = p.Y - 1; // [ ][+] //[ ][ ] // [ ][ ] model[1].X = p.X + 1; model[1].Y = p.Y - 1; // [ ][ ] //[+][ ] // [ ][ ] model[2].X = p.X - 1; model[2].Y = p.Y; // [ ][ ] //[ ][+] // [ ][ ] model[3].X = p.X; model[3].Y = p.Y; // [ ][ ] //[ ][ ] // [+][ ] model[4].X = p.X; model[4].Y = p.Y + 1; // [ ][ ] //[ ][ ] // [ ][+] model[5].X = p.X + 1; model[5].Y = p.Y + 1; #endregion } else if (d == Direction.right) { #region right generate //[+][ ] // [ ][ ] //[ ][ ] model[0].X = p.X - 1; model[0].Y = p.Y - 1; //[ ][+] // [ ][ ] //[ ][ ] model[1].X = p.X; model[1].Y = p.Y - 1; //[ ][ ] // [+][ ] //[ ][ ] model[2].X = p.X; model[2].Y = p.Y; //[ ][ ] // [ ][+] //[ ][ ] model[3].X = p.X + 1; model[3].Y = p.Y; //[ ][ ] // [ ][ ] //[+][ ] model[4].X = p.X - 1; model[4].Y = p.Y + 1; //[ ][ ] // [ ][ ] //[ ][+] model[5].X = p.X; model[5].Y = p.Y + 1; #endregion } }
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); } } }
public void revive(Point2D p) { oldPoint.X = X; oldPoint.Y = Y; oldDirection = direction; this.point.X = p.X; this.point.Y = p.Y; this.direction = Direction.up; this.Changed = true; }
public void generate(Point2D p, Direction d) { if (d == Direction.up) { #region up generate // [+] //[ ][ ][ ] //[ ] [ ] model[0].X = p.X; model[0].Y = p.Y - 1; // [ ] //[+][ ][ ] //[ ] [ ] model[1].X = p.X - 1; model[1].Y = p.Y; // [ ] //[ ][+][ ] //[ ] [ ] model[2].X = p.X; model[2].Y = p.Y; // [ ] //[ ][ ][+] //[ ] [ ] model[3].X = p.X + 1; model[3].Y = p.Y; // [ ] //[ ][ ][ ] //[+] [ ] model[4].X = p.X - 1; model[4].Y = p.Y + 1; // [ ] //[ ][ ][ ] //[ ] [+] model[5].X = p.X + 1; model[5].Y = p.Y + 1; #endregion } else if (d == Direction.down) { #region down generate //[ ] [ ] //[ ][ ][ ] // [+] model[0].X = p.X; model[0].Y = p.Y + 1; //[ ] [ ] //[+][ ][ ] // [ ] model[1].X = p.X - 1; model[1].Y = p.Y; //[ ] [ ] //[ ][+][ ] // [ ] model[2].X = p.X; model[2].Y = p.Y; //[ ] [ ] //[ ][ ][+] // [ ] model[3].X = p.X + 1; model[3].Y = p.Y; //[+] [ ] //[ ][ ][ ] // [ ] model[4].X = p.X - 1; model[4].Y = p.Y - 1; //[ ] [+] //[ ][ ][ ] // [ ] model[5].X = p.X + 1; model[5].Y = p.Y - 1; #endregion } else if (d == Direction.left) { #region left generate // [+][ ] //[ ][ ] // [ ][ ] model[0].X = p.X; model[0].Y = p.Y - 1; // [ ][+] //[ ][ ] // [ ][ ] model[1].X = p.X + 1; model[1].Y = p.Y - 1; // [ ][ ] //[+][ ] // [ ][ ] model[2].X = p.X - 1; model[2].Y = p.Y; // [ ][ ] //[ ][+] // [ ][ ] model[3].X = p.X; model[3].Y = p.Y; // [ ][ ] //[ ][ ] // [+][ ] model[4].X = p.X; model[4].Y = p.Y + 1; // [ ][ ] //[ ][ ] // [ ][+] model[5].X = p.X + 1; model[5].Y = p.Y + 1; #endregion } else if (d == Direction.right) { #region right generate //[+][ ] // [ ][ ] //[ ][ ] model[0].X = p.X - 1; model[0].Y = p.Y - 1; //[ ][+] // [ ][ ] //[ ][ ] model[1].X = p.X; model[1].Y = p.Y - 1; //[ ][ ] // [+][ ] //[ ][ ] model[2].X = p.X; model[2].Y = p.Y; //[ ][ ] // [ ][+] //[ ][ ] model[3].X = p.X + 1; model[3].Y = p.Y; //[ ][ ] // [ ][ ] //[+][ ] model[4].X = p.X - 1; model[4].Y = p.Y + 1; //[ ][ ] // [ ][ ] //[ ][+] model[5].X = p.X; model[5].Y = p.Y + 1; #endregion } }
public void clear(Point2D oldPoint) { Console.BackgroundColor = Program.backgroundColor; Console.SetCursorPosition(oldPoint.X, oldPoint.Y); Console.Write(" "); //Console.SetCursorPosition(Program.X, Program.Y); }