예제 #1
0
        /// <summary>
        /// Sets item on map.
        /// </summary>
        /// <param name="item">The item to set on map.</param>
        public void SetItem(ISquareMapItemElement item)
        {
            if (item != null)
            {
                if (this.ContainsItem(item.Position))
                {
                    this.RemoveItem(item);
                }

                this.AddItem(item);
            }
        }
예제 #2
0
 /// <summary>
 /// Tries to get item at given position.
 /// </summary>
 /// <param name="position">The position to get item for.</param>
 /// <param name="item">The item instance.</param>
 /// <returns>True if retrieval was successful, otherwise false.</returns>
 public bool TryGetItem(Position position, out ISquareMapItemElement item)
 {
     item = null;
     return position.IsInBoundary(this.boundary) && this.items.TryGetValue(position, out item);
 }
예제 #3
0
 /// <summary>
 /// Removes given item.
 /// </summary>
 /// <param name="item">Item to remove.</param>
 public void RemoveItem(ISquareMapItemElement item)
 {
     /* remove by position */
     this.RemoveItem(item.Position);
 }
예제 #4
0
        /// <summary>
        /// Clears square map.
        /// </summary>
        public void Clear()
        {
            /* note on clear:
             * here all items are removed one by one instead of clearing dictionary.
             * why? two reasons:
             * first event detaching has to take place. this is done in remove method.
             * second ui has to be signalled about such an event - we could have made
             * another "clear" command as well, but since the map is not that big,
             * we keep to use item removal.
             * */

            Position[] positions = new Position[this.items.Keys.Count];

            lock (this.items)
            {
                this.items.Keys.CopyTo(positions, 0);
            }

            this.waterSourceItem = null;

            /* remove one by one */
            foreach (Position position in positions)
            {
                this.RemoveItem(position);
            }
        }
예제 #5
0
        /// <summary>
        /// Adds item.
        /// </summary>
        /// <param name="item">The item to add.</param>
        public void AddItem(ISquareMapItemElement item)
        {
            if (item != null)
            {
                /* is water source? */
                if (item.Square is IWaterSourceElement)
                {
                    if (this.waterSourceItem != null)
                    {
                        this.RemoveItem(this.waterSourceItem);
                    }

                    /* mark it */
                    this.waterSourceItem = item;
                }

                /* attach to events */
                item.Enter += new EventHandler(OnItemEnter);
                item.Leave += new EventHandler(OnItemLeave);

                lock (this.items)
                {
                    this.items.Add(item.Position, item);
                }

                /* tell ui we have added item */
                this.ExecuteCommand(PresentationCommands.SquareMap.AddItem, new PresentSquareMapItemParameter(item.Id, item.Position, item.IsActivated, item.IsFixed, item.Square.Id, item.Square.Type));

                /* fire event */
                if (this.ItemAdded != null)
                {
                    this.ItemAdded(this, new EventArgs<ISquareMapItemElement>(item));
                }

                /* check if square is water source. if yes, fix it immediately. */
                if (item.Square is IWaterSourceElement)
                {
                    item.Fix();
                }
            }
        }
예제 #6
0
 /// <summary>
 /// Initializes navigator.
 /// </summary>
 /// <param name="squareMap">The square map this navigator should operate upon.</param>
 /// <param name="current">The square map item this navigator is pointing to.</param>
 public void Initialize(ISquareMapElement squareMap, ISquareMapItemElement current)
 {
     this.squareMap = squareMap;
     this.current = current;
 }
예제 #7
0
파일: Game.cs 프로젝트: ilkerde/pipemania
        /* active item has changed on square map */
        private void OnSquareMapActiveItemChanged(object sender, EventArgs<ISquareMapItemElement> e)
        {
            if (e != null)
            {
                /* we already have an active item, it's obsolete now. */
                if (this.activeItem != null)
                {
                    /* detach from fix event and restore square */
                    lock (this.activeItem)
                    {
                        this.activeItem.Fixed -= new EventHandler(OnActiveItemFixed);
                        this.activeItem.RestoreSquare();
                    }
                }

                /* there's a new active item */
                if (e.Value != null)
                {
                    /* assign */
                    this.activeItem = e.Value;

                    /* attach to fix event and overlay with current pipe */
                    lock (this.activeItem)
                    {
                        this.activeItem.Fixed += new EventHandler(OnActiveItemFixed);
                        this.activeItem.OverlaySquare(this.CurrentPipe);
                    }
                }
            }
        }