コード例 #1
0
        public bool AddPlayer(int damage, char bulletSkin, int hp, char skin, ConsoleColor color,
                              System.ConsoleKey foreward, System.ConsoleKey backward, System.ConsoleKey left,
                              System.ConsoleKey right, System.ConsoleKey shout)
        {
            if ((bulletSkin != ' ') && (hp > 0) && (skin != ' '))
            {
                TankOnMap newTank = new TankOnMap();
                int       guid    = this.GUID;
                newTank.Tank = new Tank(guid, damage, bulletSkin, hp, skin, color);


                //  find an empty place on the map
                //
                //
                bool founded = false;// indecates if the position was found


                for (int i = 0; i < map.xLenth; i++)
                {
                    //  to stop if the position already founded
                    if (founded)
                    {
                        break;
                    }
                    for (int j = 0; j < map.yLenth; j++)
                    {
                        if (map.IsEmpty(i, j))
                        {
                            newTank.Position.X = i;
                            newTank.Position.Y = j;
                            founded            = true;
                            break;
                        }
                    }
                }

                //  add event handlers
                newTank.Tank.OnShot    += OnShotHandler;
                newTank.Tank.Moved     += OnMoveHandler;
                newTank.Tank.HPChanged += OnHPCHangedHandler;

                //  add to the collection
                tanksCollection.Add(guid, newTank);
                playerController             = new PlayerController(foreward, backward, right, left, shout);
                playerController.Tank        = newTank.Tank;
                playerController.KeyPressed += OnKeyPressed;


                return(true);
            }
            else
            {
                return(false);
            }
        }
コード例 #2
0
        private void OnMoveHandler(object sender, Directions direction)
        {
            //  check if we can move (there arent wall or another tank)

            Tank tank = sender as Tank;

            if (tank != null)
            {
                Position desirePosition = new Position();

                //  get the desire position
                if (GetDesirePosition(ref desirePosition, tank, direction))
                {
                    //  if desire position doesnt overhead the map size
                    if ((desirePosition.X < map.xLenth) && (desirePosition.X >= 0) &&
                        (desirePosition.Y < map.yLenth) && (desirePosition.Y >= 0))
                    {
                        //  if desire position doesnt intersects with the map(wall)
                        if (map.IsEmpty(desirePosition.X, desirePosition.Y))
                        {
                            //  if desire position doesnt intersects with ohther tanks
                            if (!IntersectsWithObjects(tank, desirePosition.X, desirePosition.Y))
                            {
                                //  if desire position intersects with the bullet
                                foreach (var bullet in bulletsCollection)
                                {
                                    if ((bullet.Value.Position.X == desirePosition.X) && (bullet.Value.Position.Y == desirePosition.Y))
                                    {
                                        //  decrement hp
                                        tank.HPImprove(bullet.Value.Bullet.GetDamage() * (-1));
                                        bulletsCollection.Remove(bullet.Key);
                                        break;
                                    }
                                }



                                //  so desire position is OK
                                //  find the tank and change current position to desire


                                if (tanksCollection.ContainsKey(tank.GetHashCode()))
                                {
                                    var tmp = tanksCollection[tank.GetHashCode()];

                                    if (tmp.Tank.GetHashCode() == tank.GetHashCode())
                                    {
                                        TankOnMap newValue = new TankOnMap();

                                        newValue.Position.X = desirePosition.X;
                                        newValue.Position.Y = desirePosition.Y;

                                        newValue.Tank = tmp.Tank;

                                        //  Render
                                        //  Delete old position from map
                                        Visualizer.Render(' ', tmp.Position.X, tmp.Position.Y, System.Console.BackgroundColor);
                                        //  Add new position
                                        Visualizer.Render(newValue.Tank.GetSkin(), newValue.Position.X, newValue.Position.Y, newValue.Tank.GetColor());


                                        tanksCollection[tmp.Tank.GetHashCode()] = newValue;
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }