public WorldContext(MainContext parent, World worldModel) { Main = parent; // Create fighter contexts from fighter models. // Use an anonymous class to associate // the original model with the new context. var newFighterPairs = from model in worldModel.Fighters select new { model, context = new FighterContext(this) { Name = model.Name, Position = new Point(model.PositionX, model.PositionY), Rotation = model.Rotation } }; // For fast lookup, convert the list of model <-> context pairs to a dictionary. Dictionary<Fighter, FighterContext> newFighterMap = newFighterPairs.ToDictionary(pair => pair.model, pair => pair.context); // Set the Target properties (these are possibly cyclically connected) foreach (var pair in newFighterMap) { Fighter targetFighter = pair.Key.Target; if (targetFighter != null) { FighterContext fighterContext = pair.Value; fighterContext.Target = newFighterMap[targetFighter]; } } Fighters = new UndoableCollection<FighterContext>(this, newFighterMap.Values); }
public WorldContext(MainContext parent) { Main = parent; Fighters = new UndoableCollection<FighterContext>(this); }