Esempio n. 1
0
        /// <summary>
        /// Adds a new client to the list
        /// </summary>
        /// <param name="ipEndPoint"></param>
        public void AddClient(NetConnection _connection, IPEndPoint _ipEndPoint)
        {
            if (!clients.ContainsKey(_ipEndPoint))
            {
                //TODO get the username of the player that is logging in and fetch the data
                ClientData newClient = new ClientData(_connection, ClientData.GetFreeID(clients));
                clients.Add(_ipEndPoint, newClient);

                foreach (var client in clients.Values)
                {
                    if (client.Equals(newClient))
                    {
                        continue;
                    }
                    //Tell all clients, a new client connected
                    PacketController.getInstance().SendNewClientConnected(newClient);
                }

                PacketController.getInstance().SendClientConnectAnswer(newClient);

                if (debugMode)
                {
                    Debug.Log("Created client with id '" + newClient.ID + "'!");
                }
            }
        }
Esempio n. 2
0
        private void UpdatePosition()
        {
            if (transform.position == LastPosition)
            {
                return;
            }
            LastPosition = transform.position;
            Position     = transform.position;

            PacketController.getInstance().SendNetworkObjectPosition(this);
        }
Esempio n. 3
0
        private void Start()
        {
            ID = Utils.GetFreeID();
            Debug.Log("PrefabID: " + ID);
            Server.getInstance().netObjs.Add(ID, this);
            Position = transform.position;
            Rotation = transform.rotation.eulerAngles;

            if (!Server.getInstance().isStarted)
            {
                return;
            }
            PacketController.getInstance().SendNetworkObjectSpawn(this);
        }
Esempio n. 4
0
        /// <summary>
        /// Removes a client from the list and also removes the transform if one exists
        /// </summary>
        /// <param name="ipEndPoint"></param>
        public void RemoveClient(IPEndPoint ipEndPoint)
        {
            if (clients.ContainsKey(ipEndPoint))
            {
                ClientData _client = clients[ipEndPoint];
                //inform all players, that a client disconnected
                PacketController.getInstance().SendClientDisconnect(clients[ipEndPoint]);
                if (clientsTransform.ContainsKey(_client.ID))
                {
                    GameServerCycle.getInstance()
                    .DestroyNetObject(clientsTransform[clients[ipEndPoint].ID]);
                }

                clientsTransform.Remove(clients[ipEndPoint].ID);
                clients.Remove(ipEndPoint);
            }
        }
Esempio n. 5
0
        private void CalculatePlayerDeathTime()
        {
            foreach (ClientData _client in Server.getInstance().clients.Values)
            {
                if (_client == null || !_client.IsDead)
                {
                    continue;
                }

                if (_client.DeathTime > 0)
                {
                    _client.DeathTime -= Time.deltaTime;
                }
                else
                {
                    Server.getInstance().clients[_client.Connection.RemoteEndPoint] =
                        new ClientData(_client.Connection, _client.ID);
                    SpawnPlayer(_client);
                    PacketController.getInstance().SendPlayerRespawn(_client);
                }
            }
        }
Esempio n. 6
0
        public void UpdatePlayerPosition(ClientData _client)
        {
            Vector3 dir;

            switch (_client.MoveDir)
            {
            case MoveDirs.UP:
                dir = Vector3.forward;
                break;

            case MoveDirs.UPRIGHT:
                dir = Vector3.forward + Vector3.right;
                break;

            case MoveDirs.RIGHT:
                dir = Vector3.right;
                break;

            case MoveDirs.RIGHTDOWN:
                dir = Vector3.back + Vector3.right;
                break;

            case MoveDirs.DOWN:
                dir = Vector3.back;
                break;

            case MoveDirs.DOWNLEFT:
                dir = Vector3.back + Vector3.left;
                break;

            case MoveDirs.LEFT:
                dir = Vector3.left;
                break;

            case MoveDirs.LEFTUP:
                dir = Vector3.left + Vector3.forward;
                break;

            case MoveDirs.NONE:
                dir = Vector3.zero;
                break;

            default:
                throw new ArgumentOutOfRangeException();
            }

            Transform player = Server.getInstance().clientsTransform[_client.ID];

            player.rotation = Quaternion.Euler(_client.Rotation);

            player.Translate(dir.normalized * _client.Speed * Time.deltaTime, Space.World);
            _client.Position = player.transform.position;

            if (_client.LastPosition == _client.Position)
            {
                return;
            }
            if (_client.WantsPredict)
            {
                _client.moveTime++;
            }

            _client.LastPosition = _client.Position;
            PacketController.getInstance().SendPlayerPosition(_client);
        }
Esempio n. 7
0
 private void OnDestroy()
 {
     Server.getInstance().netObjs.Remove(ID);
     PacketController.getInstance().SendNetworkObjectDestroy(this);
 }
Esempio n. 8
0
        private void ProcessMessage(byte id, NetIncomingMessage message)
        {
            //Protokoll : [Byte], [Value], [Value], ...
            ClientData _client = clients[message.SenderEndPoint];

            switch (id)
            {
            case 0x0:     //positionupdate
                _client.MoveDir  = (MoveDirs)message.ReadInt32();
                _client.Rotation = new Vector3(message.ReadFloat(), message.ReadFloat(), message.ReadFloat());
                if (_client.WantsPredict)
                {
                    float movetime = message.ReadFloat();
                    if (movetime <= _client.moveTime)
                    {
                        if (debugMode)
                        {
                            Debug.Log("Client movetime mismatch! Client(" + _client.ID + "):" + movetime + "  -- " +
                                      _client.moveTime);
                        }
                    }
                }

                break;

            case 0x1:     //gamestate
                List <ClientData> _clients = (from client in clients
                                              where client.Value.ID != clients[message.SenderEndPoint].ID
                                              select client.Value).ToList();
                foreach (ClientData c in _clients)
                {
                    if (c.Connection == null)
                    {
                        continue;
                    }
                    PacketController.getInstance().SendPlayerList(c, clients[message.SenderEndPoint].Connection);
                }

                foreach (var netObject in netObjs.Values)
                {
                    PacketController.getInstance()
                    .SendNetworkObjectSpawn(netObject, clients[message.SenderEndPoint].Connection);
                }

                Debug.Log("Sent PLAYERLIST to " + clients[message.SenderEndPoint].ID);

                /*Takes me one step closer to the
                 * edge and I'm about to */
                break;

            case 0x3:     //statsUpdate
                short      defenderId = message.ReadInt16();
                ClientData defender   =
                    (from client in clients where client.Value.ID == defenderId select client.Value).ToList()[0];

                ClientData attacker = clients[message.SenderEndPoint];
                HealthController.getInstance().CalculatePlayerHitpoints(attacker, defender);

                foreach (ClientData client in clients.Values)
                {
                    PacketController.getInstance().SendPlayerHPUpdate(defender, client.Connection);
                }

                break;

            case 0x4:     //update animation
                ClientData animPlayer = clients[message.SenderEndPoint];
                animPlayer.AnimationState = (AnimationStates)message.ReadInt32();
                List <ClientData> _others = Utils.GetOtherClients(animPlayer, clients);

                foreach (ClientData c in _others)
                {
                    PacketController.getInstance().SendPlayerAnimationState(animPlayer, c.Connection);
                }

                break;

            case 0x5:     //chat event
                //TODO add chat controller and check message for different parameter
                //TODO add differenct chat channels
                //TODO make it possible to write a personal message to other players
                //TODO add commands
                foreach (ClientData rec in clients.Values)
                {
                    PacketController.getInstance().SendChatMessage(clients[message.SenderEndPoint], rec.Connection,
                                                                   message.ReadString());
                }

                break;

            case 0x6:     //team join
                int teamId = message.ReadInt32();
                if (TeamController.getInstance().AddToTeam(clients[message.SenderEndPoint], teamId))
                {
                    GameServerCycle.getInstance().SpawnPlayer(clients[message.SenderEndPoint]);
                }
                foreach (var receiver in clients.Values)
                {
                    PacketController.getInstance()
                    .SendPlayerTeamJoin(clients[message.SenderEndPoint], receiver.Connection);
                }

                break;

            case 0x7:     //farming
                short   interactEntityID = message.ReadInt16();
                Vector3 targetPos        = new Vector3(message.ReadFloat(), message.ReadFloat(), message.ReadFloat());

                if (netObjs.ContainsKey(interactEntityID) &&
                    Utils.CanInteract(clients[message.SenderEndPoint], targetPos))
                {
                    //TODO add multiple ressources and dont return on another prefab as tree
                    //TODO get a list of interactable types / mineable types
                    //TODO add a interact function to every type of entity and call the function of the networkobject
                    if (netObjs[interactEntityID].prefabType != PrefabTypes.TREE)
                    {
                        return;
                    }

                    Vector3 dropLocation = Utils.CalculateDropLocation(netObjs[interactEntityID].Position,
                                                                       (int)GameConstants.dropRange);

                    GameServerCycle.getInstance().DestroyNetObject(netObjs[interactEntityID]);
                    PrefabController.getInstance().SpawnPrefab(PrefabTypes.WOOD, dropLocation);
                }

                break;

            case 0x8:     //addItemRequest / pickup Item
                //TODO add picker
                //short pickerID = message.ReadInt16();
                short netId = message.ReadInt16();

                if (netObjs.ContainsKey(netId) &&
                    Utils.CanInteract(clients[message.SenderEndPoint], netObjs[netId].Position))
                {
                    //TODO move into own class as an interact event and check what happens if a player interacts with an item (pickup, destroy etc.)
                    //GameServerCycle.getInstance().DestroyNetObject(netObjs[netId].gameObject);
                    //var response = server.CreateMessage();
                    //response.Write((byte) PacketTypes.PICKUP);
                    //response.Write(netId);
                    //server.SendToAll(response, NetDeliveryMethod.ReliableUnordered);
                    //TODO check add inventory attribute to the clientdata
                }


                break;

            case 0x9:     //weapon change event
                _client.WeaponState = (WeaponStates)message.ReadInt32();

                if (debugMode)
                {
                    Debug.Log("Changed weapon of " + _client.ID + " to " + _client.WeaponState);
                }

                PacketController.getInstance().SendWeaponChange(_client);
                break;

            case 0x10:     //spawn
//                    netId = message.ReadInt16();
                break;

            case 0x11:     //attack request (spells, projectiles)

                PrefabController.getInstance().SpawnPrefab(PrefabTypes.ARROW, _client.Position);
                break;
            }
        }