Ejemplo n.º 1
0
 private void NotifyPlayersCharacterMoved(RealCharacter character)
 {
     foreach (var netplayer in gameState.NetPlayers.Values)
     {
         netplayer.ServerPacket.PlayersPositions[character.Id] = (ProtoVector2)character.Position;
     }
 }
Ejemplo n.º 2
0
        public Communicator(Settings settings, Battlefield battlefield, GameStateClient gameState, out RealCharacter player)
        {
            _battlefield = battlefield;
            _gameState = gameState;
            // подключаемся к серверу
            ServerConnectionPacket connMsg = Connect(settings, ref _clientPort);
            if (connMsg == null)
            {
                _battlefield.Game.Exit();
            }
            _messenger = new UdpMessenger(settings.ServerAddress, settings.ServerPort, _clientPort, _receiveTimeout);
            _gameState.SetLevel((Level)connMsg.Level);
            // обновление состояния игрока
            _gameState.CreateCharacter(connMsg.PlayerId);
            player = _gameState.Players[connMsg.PlayerId];
            player.Position = (Vector2)connMsg.PlayerPosition;
            player.Health = connMsg.Health;
            player.Speed = connMsg.Speed;
            player.MoveBehavior = new PlayerMove1(player);
            _gameState.Player = player;

            _gameState.NetPlayer = new NetPlayer(_messenger, player);
            new Thread(StartReceiving).Start();
            new Thread(StartSending).Start();
        }
Ejemplo n.º 3
0
 public void CheckForCollisions(RealCharacter character)
 {
     if (character.MoveBehavior.NewPosition == character.Position)
         return;
     gameState.Level.CurrectNewPositionIfOut(character);
     gameState.Grid.Relocate(character);
     CheckNewPositionForCollisionsWithMapObjects(character);
     character.Position = character.MoveBehavior.NewPosition;
 }
Ejemplo n.º 4
0
 public void CurrectNewPositionIfOut(RealCharacter character)
 {
     Vector2 newPos = character.MoveBehavior.NewPosition;
     if (newPos.X - (character.CollisionOffset.X >> 1) < 0)
         newPos.X = (character.CollisionOffset.X >> 1);
     else if (newPos.X + (character.CollisionOffset.X >> 1) > Rect.Width)
         newPos.X = Rect.Width - (character.CollisionOffset.X >> 1);
     if (newPos.Y - (character.CollisionOffset.Y >> 1) < 0)
         newPos.Y = (character.CollisionOffset.Y >> 1);
     else if (newPos.Y + (character.CollisionOffset.Y >> 1) > Rect.Height)
         newPos.Y = Rect.Height - (character.CollisionOffset.Y >> 1);
     character.MoveBehavior.NewPosition = newPos;
 }
Ejemplo n.º 5
0
 private void CheckNewPositionForCollisionsWithMapObjects(RealCharacter character)
 {
     Point newZone = gameState.Grid.GetZone(character.MoveBehavior.NewPosition);
     for (int i = newZone.X - 1; i <= newZone.X + 1; i++)
     {
         for (int j = newZone.Y - 1; j <= newZone.Y + 1; j++)
         {
             if (i < 0 || j < 0 || i >= gameState.Grid.Solids.Count || j >= gameState.Grid.Solids[i].Count)
             {
                 continue;
             }
             foreach (var solid in gameState.Grid.Solids[i][j])
             {
                 DeepCorrection(character, solid);
             }
         }
     }
 }
Ejemplo n.º 6
0
 protected void NotifyCharacterAdded(RealCharacter character)
 {
     CharacterAdded(character);
 }
Ejemplo n.º 7
0
 public bool IsCollide(RealCharacter character2)
 {
     return CollisionRect.Intersects(character2.CollisionRect);
 }
Ejemplo n.º 8
0
 private void CreateCharacter(RealCharacter character)
 {
     character.Sprite = _playerSprite;
 }
Ejemplo n.º 9
0
 private void OnCharacterAdd(RealCharacter character)
 {
     gameState.Grid.Put(character);
 }
Ejemplo n.º 10
0
 public RealCharacter CreateCharacter(bool isPlayer)
 {
     RealCharacter character = new RealCharacter(_curCharacterId++);
     Scores.TryAdd((byte)character.Id, new Score());
     character.Health = Character.MaxHealth;
     character.HealthRegen = 1;
     character.Speed = 400;
     character.DamagePerSecond = 120;
     character.Mana = Character.MaxMana;
     character.ManaRegen = 10;
     character.Position = Level.GetRespawnPosition();
     if (isPlayer)
     {
         character.MoveBehavior = new EmptyBehavior(character);
         Players.TryAdd(character.Id, character);
         lock (Bots)
         {
             foreach (Character monster in Bots)
             {
                 if (monster.MoveBehavior is StupidMonsterMove)
                 {
                     (monster.MoveBehavior as StupidMonsterMove).AddTarget(character);
                 }
             }
         }
     }
     else
     {
         // todo: temp, move behavior shouldn't be initialized here
         character.MoveBehavior = new MonsterMoveAStar(character, Level);
         lock (Bots)
         {
             Bots.Add(character);
             foreach (Character player in Players.Values)
             {
                 if (character.MoveBehavior is StupidMonsterMove)
                 {
                     (character.MoveBehavior as StupidMonsterMove).AddTarget(player);
                 }
             }
         }
     }
     NotifyCharacterAdded(character);
     NotifyOthersNewPlayerCreated(character);
     return character;
 }
Ejemplo n.º 11
0
 private void NotifyOthersNewPlayerCreated(RealCharacter character)
 {
     foreach (var netplayer in NetPlayers.Values)
     {
         netplayer.ServerPacket.PlayersConnections.TryAdd((byte)character.Id, ConnectionState.Connected);
     }
 }