/// <summary>
        /// Search up a tree of nodes looking for a node which can answer a given question.
        /// </summary>
        /// <typeparam name="TResult">The type of the result</typeparam>
        /// <typeparam name="TNode">The type of the intermediate node which can answer the question</typeparam>
        /// <param name="start">The node to start at</param>
        /// <param name="queryNode">Take an intermediate node and produce a result</param>
        /// <param name="stopTypes">A set of types to stop at if encountered and cancel the search</param>
        /// <returns></returns>
        public static TResult SearchUp <TResult, TNode>(this ISubdivisionContext start, Func <TNode, TResult> queryNode, params Type[] stopTypes)
            where TResult : class
            where TNode : class
        {
            Contract.Requires(start != null);
            Contract.Requires(queryNode != null);
            Contract.Requires(stopTypes != null);

            ISubdivisionContext node = start.Parent;

            while (node != null)
            {
                //Once we find a provider take suggestions from it
                var p = node as TNode;
                if (p != null)
                {
                    var f = queryNode(p);
                    return(f);
                }

// ReSharper disable AccessToModifiedClosure
                if (stopTypes.Any(t => t.IsInstanceOfType(node)))
                {
// ReSharper restore AccessToModifiedClosure
                    break;
                }

                //Search further up
                node = node.Parent;
            }

            //Found nothing
            return(null);
        }
Beispiel #2
0
        public NeighbourInfo(ISubdivisionContext neighbour, LineSegment2 neighbourSegment, float neighbourStart, float neighbourEnd, LineSegment2 segment, float start, float end)
        {
            Neighbour = neighbour;

            NeighbourSegment = neighbourSegment;
            NeighbourStart   = neighbourStart;
            NeighbourEnd     = neighbourEnd;

            Segment = segment;
            Start   = start;
            End     = end;
        }
Beispiel #3
0
        private KeyValuePair <float, BuildingSideInfo.NeighbourInfo.Resource[]> ExtractDataFromUnknownNode(ISubdivisionContext neighbour)
        {
            #region fields
            //Wouldn't it be great if we were using C#7 and we could use a match statement on type...
            //...Instead we're going to hide the dirty details in here
            IBuildingContainer ibc;
            IRoad ir;
            #endregion

            if ((ibc = neighbour as IBuildingContainer) != null)
            {
                return(new KeyValuePair <float, BuildingSideInfo.NeighbourInfo.Resource[]>(ibc.Height, new BuildingSideInfo.NeighbourInfo.Resource[0]));
            }
            else if ((ir = neighbour as IRoad) != null)
            {
                return(new KeyValuePair <float, BuildingSideInfo.NeighbourInfo.Resource[]>(0, new BuildingSideInfo.NeighbourInfo.Resource[] {
                    new BuildingSideInfo.NeighbourInfo.Resource(0, 0, "road")
                }));
            }

            return(new KeyValuePair <float, BuildingSideInfo.NeighbourInfo.Resource[]>(0, new BuildingSideInfo.NeighbourInfo.Resource[0]));
        }
Beispiel #4
0
 void ISubdivisionContext.AddPrerequisite(ISubdivisionContext prerequisite, bool recursive)
 {
 }