IEnumerator Start()
    {
        if (roomId == -1)
        {
            roomId = 0;
        }
        else
        {
            roomId = PlayerPrefs.GetInt("_room");
            score  = PlayerPrefs.GetInt("_score");
        }

        var store        = GetComponent <RoomStore>();
        var roomRenderer = GetComponent <RoomRenderer>();

        while (!store.IsReady)
        {
            yield return(null);
        }

        roomData = store.Rooms[roomId];

        // Get Miner Willy data from store and from the room
        minerWilly = new MinerWilly(store.MinerWillySprites, roomData.MinerWillyStart.X, roomData.MinerWillyStart.Y, 4, 0, 0, 7);

        // Set up the horizontal guardians
        roomData.HorizontalGuardians.ForEach(g => mobs.Add(new Mob(g)));

        // Set up the conveyor shape
        foreach (var block in roomData.Blocks.Values)
        {
            if (block.BlockType == BlockType.Conveyor)
            {
                roomData.ConveyorShape = block.Shape;
                break;
            }
        }

        // Set the border colour
        mainCamera.backgroundColor = ZXColour.Get(roomData.BorderColour);

        // HACK: Make portal available
        // REMOVE THIS LATER
        roomData.Portal.Attr.Flashing = true;

        StartCoroutine(DrawScreen(roomRenderer, roomData));
        StartCoroutine(LoseAir(roomData));
        StartCoroutine(MoveWilly(minerWilly, roomData));
        StartCoroutine(CycleColours(roomData.RoomKeys));
        StartCoroutine(UpdateConveyor(roomData));
        StartCoroutine(CheckPortalCollision(roomData));
        StartCoroutine(EndOfCavernCheck(roomData));

        if ((roomId >= 0 && roomId <= 6) || roomId == 9 || roomId == 15)
        {
            StartCoroutine(BidirectionalSprites());
        }
    }
    private void DrawMinerWilly(MinerWilly m, RoomData data)
    {
        int attr = data.Attributes[m.Y * 32 + m.X];

        attr &= 0xF8; // XXXXX--- - bit pattern
        attr |= 7;    // Miner Willy is always white on whatever background we have

        ZXAttribute attribute = new ZXAttribute((byte)attr);

        screen.FillAttribute(m.X, m.Y, 2, 2, attribute);
        screen.DrawSprite(m.X, m.Y, 2, 2, m.Frames[m.Frame]);
    }
    public void DrawScreen(RoomData data, MinerWilly minerWilly, IList <Mob> mobs, string playerScore)
    {
        screen.Clear(7, 0, false);

        DrawMinerWilly(minerWilly, data);
        DrawRoom(data);
        DrawItems(data);
        DrawHorizontalGuardians(mobs, data);
        DrawPortal(data); // Make this the last thing drawn in the room
        DrawRoomTitle(data);
        DrawAirSupply(data);
        DrawScore(playerScore);
    }
    IEnumerator MoveWilly(MinerWilly minerWilly, RoomData data)
    {
        while (state == GameState.Playing)
        {
            int attrRight = data.Attributes[minerWilly.Y * 32 + (minerWilly.X + 1)];
            int attrLeft  = data.Attributes[minerWilly.Y * 32 + (minerWilly.X - 1)];

            bool wallToRight = data.Blocks[attrRight].BlockType == BlockType.Wall;
            bool wallToLeft  = data.Blocks[attrLeft].BlockType == BlockType.Wall;

            bool didMove = false;

            if (Input.GetKey(KeyCode.W))
            {
                if (minerWilly.Frame > 3)
                {
                    minerWilly.Frame -= 4;
                }

                if (!wallToRight || (wallToRight && minerWilly.Frame != 3))
                {
                    minerWilly.Frame += 1;
                }

                if (minerWilly.Frame > 3)
                {
                    minerWilly.Frame = 0;

                    if (!wallToRight)
                    {
                        minerWilly.X++;
                    }
                }
                didMove = true;
            }
            else if (Input.GetKey(KeyCode.Q))
            {
                if (minerWilly.Frame < 4)
                {
                    minerWilly.Frame += 4;
                }

                if (!wallToLeft || (wallToLeft && minerWilly.Frame != 4))
                {
                    minerWilly.Frame -= 1;
                }

                if (minerWilly.Frame < 4)
                {
                    minerWilly.Frame = 7;
                    if (!wallToLeft)
                    {
                        minerWilly.X--;
                    }
                }
                didMove = true;
            }

            if (didMove)
            {
                yield return(new WaitForSeconds(MINER_WILLY_SPEED));
            }
            else
            {
                yield return(null);
            }
        }
    }