/// <summary> /// Event-handler for when a Thing is added to the PigWorld. /// </summary> public void WorldThingAddedEvent(Thing thing) { if (thing is LifeForm) { Position position = thing.Cell.Position; LifeFormView view = (LifeFormView)CreateView(thing); cellViews[position.Row, position.Column].AddLifeFormView(view); } }
/// <summary> /// Removes a LifeFormView from this CellView. /// This happens when a LifeForm is removed from the Cell, either by the user, /// or when an animal moves from one cell to another. /// </summary> /// <param name="view"> the LifeFormView to remove. </param> public void RemoveLifeForm() { // When the life-form changes or is destroyed, no longer tell this CellView about it. this.lifeFormView.LifeForm.thingChangedEvent -= ThingChangedEvent; this.lifeFormView.LifeForm.thingDestroyedEvent -= ThingDestroyedEvent; this.lifeFormView = null; this.Image = null; this.ContextMenuStrip = null; // Remove the context-menu. }
/// <summary> /// Adds a LifeFormView to this CellView. /// This happens when a LifeForm is added to a Cell, either by the user, /// or when an animal moves from one cell to another. /// </summary> /// <param name="view"> the LifeFormView to add. </param> public void AddLifeFormView(LifeFormView lifeFormView) { // Check that the cell has no life-form already. (It may have nonLivingThings, e.g. pig food.) Debug.Assert(this.lifeFormView == null); this.lifeFormView = lifeFormView; // Update this cell's image. this.Image = lifeFormView.PaintImage(); // If the life-form changes or is destroyed, tell this CellView about it. this.lifeFormView.LifeForm.thingChangedEvent += ThingChangedEvent; this.lifeFormView.LifeForm.thingDestroyedEvent += ThingDestroyedEvent; this.ContextMenuStrip = this.lifeFormView.contextMenuStrip; // Assign the context-menu to this CellView. }