Example #1
0
 public SharedWorker(SharedWorker worker) : this(worker.Name, worker.Surename, worker.Gender)
 {
     this.ID             = worker.ID;
     this.Salary         = worker.Salary;
     this.Abilites       = worker.Abilites;
     this.ExperienceTime = worker.ExperienceTime;
     this.Avatar         = worker.Avatar;
     this.NameIndex      = worker.NameIndex;
     this.SurenameIndex  = worker.SurenameIndex;
     this.AvatarIndex    = worker.AvatarIndex;
 }
Example #2
0
        private void RemoveCharacter(SharedWorker companyWorker)
        {
            SharedWorker removedChar = SpawnedCharacters.Find(x => x == companyWorker);

            if (null == CharactersPool)
            {
                CharactersPool = new ObjectPool <GameObject>();
            }

            removedChar.PhysicalCharacter.gameObject.SetActive(false);
            CharactersPool.AddObject(companyWorker.PhysicalCharacter);
            removedChar.PhysicalCharacter = null;
        }
Example #3
0
        public static object Deserialize(byte[] workerBytes)
        {
            if (null == Resources)
            {
                GameObject scriptsObject = GameObject.FindGameObjectWithTag("ScriptsGameObject");
                Resources = scriptsObject.GetComponent <ResourceHolder>();;
            }

            int offset = 0;

            int nameIndex = BitConverter.ToInt32(workerBytes, offset);

            offset += sizeof(int);
            int surenameIndex = BitConverter.ToInt32(workerBytes, offset);

            offset += sizeof(int);
            int avatarIndex = BitConverter.ToInt32(workerBytes, offset);

            offset += sizeof(int);
            int abilitiesSize = BitConverter.ToInt32(workerBytes, offset);

            offset += sizeof(int);

            Dictionary <ProjectTechnology, SafeFloat> abilities = new Dictionary <ProjectTechnology, SafeFloat>();

            for (int i = 0; i < abilitiesSize; i++)
            {
                ProjectTechnology abilityType = (ProjectTechnology)BitConverter.ToInt32(workerBytes, offset);
                offset += sizeof(int);
                float abilityValue = BitConverter.ToSingle(workerBytes, offset);
                offset += sizeof(float);
                abilities.Add(abilityType, new SafeFloat(abilityValue));
            }

            int expierienceTime = BitConverter.ToInt32(workerBytes, offset);

            offset += sizeof(int);
            int salary = BitConverter.ToInt32(workerBytes, offset);

            offset += sizeof(int);
            int workerID = BitConverter.ToInt32(workerBytes, offset);

            offset += sizeof(int);
            Gender workerGender = (Gender)BitConverter.ToInt32(workerBytes, offset);

            string name     = (Gender.Male == workerGender) ? WorkerData.MaleNames[nameIndex] : WorkerData.FemaleNames[nameIndex];
            string surename = WorkerData.Surenames[surenameIndex];
            Sprite avatar   = (Gender.Male == workerGender) ? Resources.MaleCharactersAvatars[avatarIndex] : Resources.FemaleCharactersAvatars[avatarIndex];

            SharedWorker deserializedWorker = new SharedWorker(name, surename, workerGender);

            deserializedWorker.Abilites       = abilities;
            deserializedWorker.ExperienceTime = expierienceTime;
            deserializedWorker.Salary         = salary;
            deserializedWorker.ID             = workerID;
            deserializedWorker.NameIndex      = nameIndex;
            deserializedWorker.SurenameIndex  = surenameIndex;
            deserializedWorker.AvatarIndex    = avatarIndex;
            deserializedWorker.Avatar         = avatar;

            return(deserializedWorker);
        }
Example #4
0
        public static byte[] Serialize(object workerObject)
        {
            SharedWorker workerToSerialize = (SharedWorker)workerObject;

            byte[] nameIndexBytes       = BitConverter.GetBytes(workerToSerialize.NameIndex);
            byte[] surenameIndexBytes   = BitConverter.GetBytes(workerToSerialize.SurenameIndex);
            byte[] avatarIndexBytes     = BitConverter.GetBytes(workerToSerialize.AvatarIndex);
            byte[] expierienceTimeBytes = BitConverter.GetBytes(workerToSerialize.ExperienceTime);
            byte[] salaryBytes          = BitConverter.GetBytes(workerToSerialize.Salary);
            byte[] abilitiesBytes;
            byte[] IDBytes     = BitConverter.GetBytes(workerToSerialize.ID);
            byte[] genderBytes = BitConverter.GetBytes((int)workerToSerialize.Gender);

            //This is used so receiving client knows how much bytes of name and surename should be read
            byte[] abilitiesSize = BitConverter.GetBytes(workerToSerialize.Abilites.Count);
            //Serialize ability type and its value (2*size)
            int abilitiesSizeInBytes = workerToSerialize.Abilites.Count * 2 * sizeof(int);

            abilitiesBytes = new byte[abilitiesSizeInBytes];

            int abilitiesBytesOffset = 0;

            foreach (var ability in workerToSerialize.Abilites)
            {
                byte[] abilityTypeBytes  = BitConverter.GetBytes((int)ability.Key);
                byte[] abilityValueBytes = BitConverter.GetBytes(ability.Value.Value);

                Array.Copy(abilityTypeBytes, 0, abilitiesBytes, abilitiesBytesOffset, abilityTypeBytes.Length);
                abilitiesBytesOffset += abilityTypeBytes.Length;
                Array.Copy(abilityValueBytes, 0, abilitiesBytes, abilitiesBytesOffset, abilityValueBytes.Length);
                abilitiesBytesOffset += abilityValueBytes.Length;
            }

            int workerBytesSize = nameIndexBytes.Length
                                  + surenameIndexBytes.Length
                                  + avatarIndexBytes.Length
                                  + abilitiesSize.Length
                                  + abilitiesBytes.Length
                                  + expierienceTimeBytes.Length
                                  + salaryBytes.Length
                                  + IDBytes.Length
                                  + genderBytes.Length;

            byte[] workerBytes = new byte[workerBytesSize];
            int    offset      = 0;

            Array.Copy(nameIndexBytes, 0, workerBytes, offset, nameIndexBytes.Length);
            offset += nameIndexBytes.Length;
            Array.Copy(surenameIndexBytes, 0, workerBytes, offset, surenameIndexBytes.Length);
            offset += surenameIndexBytes.Length;
            Array.Copy(avatarIndexBytes, 0, workerBytes, offset, avatarIndexBytes.Length);
            offset += avatarIndexBytes.Length;
            Array.Copy(abilitiesSize, 0, workerBytes, offset, abilitiesSize.Length);
            offset += abilitiesSize.Length;
            Array.Copy(abilitiesBytes, 0, workerBytes, offset, abilitiesBytes.Length);
            offset += abilitiesBytes.Length;
            Array.Copy(expierienceTimeBytes, 0, workerBytes, offset, expierienceTimeBytes.Length);
            offset += expierienceTimeBytes.Length;
            Array.Copy(salaryBytes, 0, workerBytes, offset, expierienceTimeBytes.Length);
            offset += salaryBytes.Length;
            Array.Copy(IDBytes, 0, workerBytes, offset, IDBytes.Length);
            offset += IDBytes.Length;
            Array.Copy(genderBytes, 0, workerBytes, offset, genderBytes.Length);

            return(workerBytes);
        }
Example #5
0
 public LocalWorker(SharedWorker worker) : base(worker)
 {
 }