Пример #1
0
 public ThingChangeEvent(Position pOld, Position pNew)
 {
     Old = pOld;
     New = pNew;
 }
Пример #2
0
 public virtual IThing Neighbour(Position pos, Direction direction)
 {
     return Get(pos.Move(direction));
 }
Пример #3
0
 public virtual IThing Get(Position pos)
 {
     return _map.Get(pos);
 }
Пример #4
0
 private void Recognize(char letter, Position pos)
 {
     SetMap map = (target, cor, obj) => target[cor.X, cor.Y] = obj;
     Destination destination;
     Coffin coffin;
     switch (letter)
     {
         case '#':
             map(_staticMap, pos, new Wall());
             break;
         case 'o':
             coffin = new Coffin();
             Coffins.Add(coffin);
             map(_dynamicMap, pos, coffin);
             break;
         case 'O':
             coffin = new Coffin {OnDestination = true};
             Coffins.Add(coffin);
             map(_dynamicMap, pos, coffin);
             destination = new Destination();
             Destinations.Add(destination);
             map(_staticMap, pos, destination);
             break;
         case 'x':
             destination = new Destination();
             Destinations.Add(destination);
             map(_staticMap, pos, destination);
             break;
         case '@':
             map(_dynamicMap, pos, new Forklift());
             Player = pos;
             break;
     }
 }
Пример #5
0
        private bool CheckForPercolation()
        {
            var isOpen = new Func<Position, bool>(thing => !(_staticMap[thing.X, thing.Y] is Wall));
            var getUnionId = new Func<Position, int>(thing =>
                {
                    if (Width >= Height)
                    {
                        return thing.Y % Height + thing.X * Height;
                    }
                    else
                    {
                        return thing.X % Width + thing.Y * Width;
                    }
                });
            var uf = new UnionFind(Width * Height);
            for (var i = 0; i < Width; i++)
            {
                for (var j = 0; j < Height; j++)
                {
                    var pos = new Position(i, j);
                    if (!isOpen(pos)) continue;

                    Direction[] directions = {Direction.Down, Direction.Up, Direction.Left, Direction.Right};
                    foreach (var tmp in directions
                        .Select(pos.Move)
                        .Where(tmp => InBounds(tmp) && isOpen(tmp)))
                    {
                        uf.Union(getUnionId(pos), getUnionId(tmp));
                    }
                }
            }
            var percolates = false;
            var playerId = getUnionId(Player);
            for (var i = 0; i < Width; i++)
            {
                var top = new Position(i, 0);
                var bottom = new Position(i, Height - 1);
                if (uf.IsConnected(playerId, getUnionId(top)))
                    percolates = true;
                if (uf.IsConnected(playerId, getUnionId(bottom)))
                    percolates = true;
            }
            for (var j = 0; j < Height; j++)
            {
                var left = new Position(0, j);
                var right = new Position(Width - 1, j);
                if (uf.IsConnected(playerId, getUnionId(left)))
                    percolates = true;

                if (uf.IsConnected(playerId, getUnionId(right)))
                    percolates = true;
            }
            return percolates;
        }
Пример #6
0
        public void Move(Position o, Position n, EventHandler<ThingChangeEvent> moved)
        {
            if (!InBounds(o)) return;
            if (!InBounds(n)) return;

            if (_dynamicMap[o.X, o.Y] == null) return;
            _dynamicMap[n.X, n.Y] = _dynamicMap[o.X, o.Y];
            _dynamicMap[o.X, o.Y] = null;

            if (moved == null) return;
            moved(this, new ThingChangeEvent(o, n));
        }
Пример #7
0
 public bool InBounds(Position pos)
 {
     return (pos.X >= 0 && pos.Y >= 0 && pos.Y < Height && pos.X < Width);
 }
Пример #8
0
        public virtual IThing Get(Position pos)
        {
            // check for out of bounds
            if (!InBounds(pos)) return null;

            return _dynamicMap[pos.X, pos.Y] ?? _staticMap[pos.X, pos.Y];
        }