Esempio n. 1
0
    public byte[] Serialize(ClientTurn turn)
    {
        byte[] serialized = new byte[0];

        using (MemoryStream stream = new MemoryStream(serialized)) {
            stream.WriteByte((byte)turn.actions.Count);

            foreach (ClientAction action in turn.actions)
            {
                stream.WriteByte(action.actionIndex); // 0 = Spawn Pawn, 1 = Move/Attack, 2 = Upgrade, 3 = Heal;
                WriteVector2Int(stream, action.actionFromPos);

                // 0, 2, need new ID | 1 needs actionToPos
                if (action.actionIndex == 0 || action.actionIndex == 2)
                {
                    stream.WriteByte(action.newID);
                }
                else if (action.actionIndex == 1)
                {
                    WriteVector2Int(stream, action.actionToPos);
                }
            }
        }

        return(serialized);
    }
Esempio n. 2
0
    public ClientTurn Parse(byte[] bytes)
    {
        ClientTurn returned = new ClientTurn();

        using (MemoryStream stream = new MemoryStream(bytes)) {
            int actions = stream.ReadByte();
            returned.actions = new System.Collections.Generic.List <ClientAction>(actions);

            for (int i = 0; i < actions; i++)
            {
                ClientAction action = new ClientAction();

                action.actionIndex   = (byte)stream.ReadByte();
                action.actionFromPos = ReadVector2Int(stream);

                if (action.actionIndex == 0 || action.actionIndex == 2)
                {
                    action.newID = (byte)stream.ReadByte();
                }
                else if (action.actionIndex == 1)
                {
                    action.actionToPos = ReadVector2Int(stream);
                }

                returned.actions.Add(action);
            }
        }

        return(returned);
    }