예제 #1
0
        public ColoredChar GetChar(int row, int col, Action <IEnumerable <Collision> > hit = null)
        {
            if (row - Row < 0 || row - Row >= Height)
            {
                throw new ArgumentException($"row must be between 0 and {Height - 1}");
            }

            if (col - Col < 0 || col - Col >= Width)
            {
                throw new ArgumentException($"col must be between 0 and {Width - 1}");
            }

            ColoredChar currChar = new ColoredChar
            {
                Background = Background
            };

            lock (Sprites)
            {
                Dictionary <int, Collision> collisionTable = null;
                if (hit != null)
                {
                    collisionTable = new Dictionary <int, Collision>();
                }

                foreach (var sprite in Sprites)
                {
                    if (sprite.Contains(row - Row, col - Col))
                    {
                        var spriteChar = sprite.GetChar(row - Row, col - Col);
                        currChar.Import(spriteChar, currChar.Background);

                        if (collisionTable != null && spriteChar != null && !spriteChar.IsNull)
                        {
                            // Builds the collision table
                            if (!collisionTable.ContainsKey(sprite.ZIndex))
                            {
                                collisionTable.Add(ZIndex, new Collision());
                            }
                            collisionTable[sprite.ZIndex].Add(sprite);
                        }
                    }
                }

                if (hit != null)
                {
                    var collisions =
                        from c in collisionTable.Values
                        where c.Count > 1
                        select c;

                    if (collisions.Count() > 0)
                    {
                        hit(collisions);
                    }
                }
            }

            return(currChar);
        }
예제 #2
0
        public void Import(ColoredChar other, ConsoleColor background)
        {
            if (other == null)
            {
                return;
            }

            Foreground = other.Foreground;
            Char       = other.Char;
            Background = other.Background;

            if (Background == ConsoleColor.Black)
            {
                if (other.Background == ConsoleColor.Black)
                {
                    Background = background;
                }
            }
        }