Пример #1
0
        private void DataReceived(byte[] data)
        {
            int startingIndex = 0;

            while (startingIndex < data.Length)
            {
                switch ((CommandType)data[startingIndex + 1])
                {
                case CommandType.Tick:
                {
                    float deltaTime = BitConverter.ToSingle(data.SubArray(startingIndex + 2, 4), 0);
                    worldGrid.UpdateCrops(deltaTime);
                    worldGrid.UpdateStructures(deltaTime);
                    break;
                }

                case CommandType.AddCrop:
                {
                    byte     y        = data[startingIndex + 2];
                    byte     x        = data[startingIndex + 3];
                    CropType cropType = (CropType)data[startingIndex + 4];
                    worldGrid.AddCrop(y, x, cropType);
                    break;
                }

                case CommandType.RemoveCrop:
                {
                    byte y = data[startingIndex + 2];
                    byte x = data[startingIndex + 3];
                    worldGrid.RemoveCrop(y, x, out _);
                    break;
                }

                case CommandType.AddStructure:
                {
                    byte          y             = data[startingIndex + 2];
                    byte          x             = data[startingIndex + 3];
                    StructureType structureType = (StructureType)data[startingIndex + 4];
                    worldGrid.AddStructure(y, x, structureType);
                    break;
                }

                case CommandType.RemoveStructure:
                {
                    byte y = data[startingIndex + 2];
                    byte x = data[startingIndex + 3];
                    worldGrid.RemoveStructure(y, x, out _);
                    break;
                }

                case CommandType.AddItemToInventory:
                {
                    byte playerId = data[startingIndex + 2];
                    if (this.playerId != playerId)
                    {
                        break;
                    }
                    ItemType itemType = (ItemType)data[startingIndex + 3];
                    int      amount   = BitConverter.ToInt32(data.SubArray(startingIndex + 4, 4), 0);
                    playerInventory.AddItem(itemType, amount);
                    break;
                }

                case CommandType.RemoveItemFromInventory:
                {
                    byte playerId = data[startingIndex + 2];
                    if (this.playerId != playerId)
                    {
                        break;
                    }
                    ItemType itemType = (ItemType)data[startingIndex + 3];
                    int      amount   = BitConverter.ToInt32(data.SubArray(startingIndex + 4, 4), 0);
                    playerInventory.RemoveItem(itemType, amount);
                    break;
                }

                case CommandType.SpawnItemDrop:
                {
                    float    y        = BitConverter.ToSingle(data.SubArray(startingIndex + 2, 4), 0);
                    float    x        = BitConverter.ToSingle(data.SubArray(startingIndex + 6, 4), 0);
                    ItemType itemType = (ItemType)data[startingIndex + 10];
                    byte     amount   = data[startingIndex + 11];
                    itemDrops.Add(y, x, itemType, amount);
                    break;
                }

                case CommandType.RemoveItemDrop:
                {
                    int index = BitConverter.ToInt32(data.SubArray(startingIndex + 2, 4), 0);
                    itemDrops.Remove(index);
                    break;
                }

                case CommandType.UpdatePlayerLocation:
                {
                    byte  playerId  = data[startingIndex + 2];
                    float y         = BitConverter.ToSingle(data.SubArray(startingIndex + 3, 4), 0);
                    float x         = BitConverter.ToSingle(data.SubArray(startingIndex + 7, 4), 0);
                    float yVelocity = BitConverter.ToSingle(data.SubArray(startingIndex + 11, 4), 0);
                    float xVelocity = BitConverter.ToSingle(data.SubArray(startingIndex + 15, 4), 0);
                    players.SetPlayerPositionAndVelocity(playerId, y, x, yVelocity, xVelocity);
                    break;
                }

                case CommandType.SetPlayerId:
                {
                    playerId = data[startingIndex + 2];

                    break;
                }
                }

                startingIndex += data[startingIndex];
            }
        }
Пример #2
0
        private static void DataReceived(byte[] data)
        {
            //Console.WriteLine($"Data received Length: {data.Length}");

            int startingIndex = 0;

            while (startingIndex < data.Length)
            {
                switch ((CommandType)data[startingIndex + 1])
                {
                case CommandType.PlantCrop:
                {
                    byte     playerId = data[startingIndex + 2];
                    byte     y        = data[startingIndex + 3];
                    byte     x        = data[startingIndex + 4];
                    CropType cropType = (CropType)data[startingIndex + 5];

                    inventories.GetInventory(playerId, out Inventory.Inventory inventory);
                    ItemType cropItemType = (ItemType)((int)cropType + 50);        //Convert crop to seed item

                    //Check if player has enough seeds to plant a crop
                    if (inventory.RemoveItem(cropItemType, 1))
                    {
                        worldGrid.AddCrop(y, x, cropType);
                        AddDataToCommandList(Commands.RemoveItemFromInventory(playerId, cropItemType, 1));
                        AddDataToCommandList(Commands.AddCrop(y, x, cropType));
                    }
                    break;
                }

                case CommandType.HarvestCrop:
                {
                    byte playerId = data[startingIndex + 2];
                    byte y        = data[startingIndex + 3];
                    byte x        = data[startingIndex + 4];

                    if (worldGrid.RemoveCrop(y, x, out Crop crop))
                    {
                        AddDataToCommandList(Commands.RemoveCrop(y, x));

                        inventories.GetInventory(playerId, out Inventory.Inventory inventory);
                        ItemType cropItemType = (ItemType)((int)crop.Type + (50 * Convert.ToInt32(crop.Growth != 1)));         //Convert crop to crop seed or item

                        //Check if player has enough space in their inventory
                        if (inventory.AddItem(cropItemType, 1))
                        {
                            AddDataToCommandList(Commands.AddItemToInventory(playerId, cropItemType, 1));
                        }
                        else
                        {
                            AddDataToCommandList(Commands.SpawnItemDrop(y, x, cropItemType, 1));
                        }
                    }
                    break;
                }

                case CommandType.PlaceStructure:
                {
                    byte          playerId      = data[startingIndex + 2];
                    byte          y             = data[startingIndex + 3];
                    byte          x             = data[startingIndex + 4];
                    StructureType structureType = (StructureType)data[startingIndex + 5];

                    //TODO: Check if player has enough items

                    worldGrid.AddStructure(y, x, structureType);
                    AddDataToCommandList(Commands.AddStructure(y, x, structureType));
                    break;
                }

                case CommandType.DestroyStructure:
                {
                    byte playerId = data[startingIndex + 2];
                    byte y        = data[startingIndex + 3];
                    byte x        = data[startingIndex + 4];

                    worldGrid.RemoveStructure(y, x, out Structure structure);
                    AddDataToCommandList(Commands.RemoveStructure(y, x));

                    //TODO: Give back item to player
                    break;
                }

                case CommandType.DropItem:
                {
                    byte     playerId = data[startingIndex + 2];
                    float    y        = BitConverter.ToSingle(data.SubArray(startingIndex + 3, 4), 0);
                    float    x        = BitConverter.ToSingle(data.SubArray(startingIndex + 7, 4), 0);
                    ItemType itemType = (ItemType)data[startingIndex + 11];
                    int      amount   = BitConverter.ToInt32(data.SubArray(startingIndex + 12, 4), 0);

                    inventories.GetInventory(playerId, out Inventory.Inventory inventory);

                    if (inventory.RemoveItem(itemType, amount))
                    {
                        itemDrops.Add(y, x, itemType, amount);
                        AddDataToCommandList(Commands.RemoveItemFromInventory(playerId, itemType, amount));
                        AddDataToCommandList(Commands.SpawnItemDrop(y, x, itemType, amount));
                    }
                    break;
                }

                case CommandType.PickupItem:
                {
                    byte  playerId = data[startingIndex + 2];
                    float y        = BitConverter.ToSingle(data.SubArray(startingIndex + 3, 4), 0);
                    float x        = BitConverter.ToSingle(data.SubArray(startingIndex + 7, 4), 0);

                    inventories.GetInventory(playerId, out Inventory.Inventory inventory);
                    IList itemDropsInRange = itemDrops.GetInRange(y, x, 1);

                    //Add as many items in range to the player inventory
                    foreach (int itemDropId in itemDropsInRange)
                    {
                        ItemDrop itemDrop = itemDrops.GetItemDropSnapshot(itemDropId);
                        if (inventory.AddItem(itemDrop.Item, itemDrop.Amount))
                        {
                            AddDataToCommandList(Commands.AddItemToInventory(playerId, itemDrop.Item, itemDrop.Amount));
                        }
                    }

                    break;
                }

                case CommandType.UpdatePlayerLocation:
                {
                    byte  playerId  = data[startingIndex + 2];
                    float y         = BitConverter.ToSingle(data.SubArray(startingIndex + 3, 4), 0);
                    float x         = BitConverter.ToSingle(data.SubArray(startingIndex + 7, 4), 0);
                    float yVelocity = BitConverter.ToSingle(data.SubArray(startingIndex + 11, 4), 0);
                    float xVelocity = BitConverter.ToSingle(data.SubArray(startingIndex + 15, 4), 0);

                    players.SetPlayerPositionAndVelocity(playerId, y, x, yVelocity, xVelocity);
                    AddDataToCommandList(Commands.UpdatePlayerLocation(playerId, y, x, yVelocity, xVelocity));
                    //TODO Collision check
                    break;
                }
                }

                byte dataSize = data[startingIndex];
                startingIndex += dataSize;
            }
        }