Exemplo n.º 1
0
        public bool TryDeserialize(string data, [NotNullWhen(true)] out IEntity?entity)
        {
            entity = null;
            var bits = data.Split('.', 2);

            if (bits.Length != 2)
            {
                return(false);
            }

            if (!bits[0].Equals(nameof(CrossTrack)))
            {
                return(false);
            }

            var track = new CrossTrack()
            {
                Happy = bool.Parse(bits[1])
            };

            entity = track;
            return(true);
        }
Exemplo n.º 2
0
        public bool TryCreateEntity(int column, int row, bool isPartOfDrag, [NotNullWhen(true)] out Track?entity)
        {
            var neighbours = TrackNeighbors.GetConnectedNeighbours(_layout.ToLayout(), column, row, emptyIsConsideredConnected: true);

            // if they click and its the perfect spot for a cross track, just do it
            if (!isPartOfDrag && neighbours.Count == 4)
            {
                entity = new CrossTrack();
                return(true);
            }

            if (isPartOfDrag)
            {
                if (neighbours.Count == 4)
                {
                    entity = new CrossTrack();
                    return(true);
                }

                // if they're dragging, we're looking for them to complete an intersection
                neighbours = TrackNeighbors.GetConnectedNeighbours(_layout.ToLayout(), column - 1, row);
                if (neighbours.Count == 3 && neighbours.Right is null)
                {
                    entity = new Track()
                    {
                        Direction = TrackDirection.Horizontal
                    };
                    _layout.Set(column - 1, row, new CrossTrack());
                    return(true);
                }

                neighbours = TrackNeighbors.GetConnectedNeighbours(_layout.ToLayout(), column, row - 1);
                if (neighbours.Count == 3 && neighbours.Down is null)
                {
                    entity = new Track()
                    {
                        Direction = TrackDirection.Vertical
                    };
                    _layout.Set(column, row - 1, new CrossTrack());
                    return(true);
                }

                neighbours = TrackNeighbors.GetConnectedNeighbours(_layout.ToLayout(), column + 1, row);
                if (neighbours.Count == 3 && neighbours.Left is null)
                {
                    entity = new Track()
                    {
                        Direction = TrackDirection.Horizontal
                    };
                    _layout.Set(column + 1, row, new CrossTrack());
                    return(true);
                }

                neighbours = TrackNeighbors.GetConnectedNeighbours(_layout.ToLayout(), column, row + 1);
                if (neighbours.Count == 3 && neighbours.Up is null)
                {
                    entity = new Track()
                    {
                        Direction = TrackDirection.Vertical
                    };
                    _layout.Set(column, row + 1, new CrossTrack());
                    return(true);
                }
            }

            entity = null;
            return(false);
        }