private void Window_Closed(object sender, EventArgs e) { //TODO: Now that ship creation is async, this can run before ships are finished building. Disposing world //early causes bad things. See FlyingBeansWindow.Window_Closed for a hacked solution try { _world.Pause(); _updateManager.Dispose(); _selectionLogic.UnselectItem(); _selectionLogic = null; // Explosions //foreach (ExplodingBot explosion in _explosions.ToArray()) //{ // DisposeExplosion(explosion); //} _map.Dispose(); // this will dispose the physics bodies _map = null; if (_cameraPool != null) { _cameraPool.Dispose(); _cameraPool = null; } _world.Dispose(); _world = null; } catch (Exception ex) { MessageBox.Show(ex.ToString(), this.Title, MessageBoxButton.OK, MessageBoxImage.Error); } }
private void Window_Loaded(object sender, RoutedEventArgs e) { try { _itemOptions = new ItemOptions(); #region Init World _boundryMin = new Point3D(-BOUNDRYSIZEHALF, -BOUNDRYSIZEHALF, -BOUNDRYSIZEHALF); _boundryMax = new Point3D(BOUNDRYSIZEHALF, BOUNDRYSIZEHALF, BOUNDRYSIZEHALF); _world = new World(); _world.Updating += new EventHandler <WorldUpdatingArgs>(World_Updating); _world.SetCollisionBoundry(_boundryMin, _boundryMax); //TODO: Only draw the boundry lines if options say to #endregion #region Materials _materialManager = new MaterialManager(_world); // Wall Game.Newt.v2.NewtonDynamics.Material material = new Game.Newt.v2.NewtonDynamics.Material(); material.Elasticity = .1d; _material_Wall = _materialManager.AddMaterial(material); // Bot material = new Game.Newt.v2.NewtonDynamics.Material(); _material_Bot = _materialManager.AddMaterial(material); // Exploding Bot material = new Game.Newt.v2.NewtonDynamics.Material(); material.IsCollidable = false; _material_ExplodingBot = _materialManager.AddMaterial(material); // Food material = new Game.Newt.v2.NewtonDynamics.Material(); material.Elasticity = .1d; _material_Food = _materialManager.AddMaterial(material); // Egg material = new Game.Newt.v2.NewtonDynamics.Material(); material.Elasticity = .5d; _material_Egg = _materialManager.AddMaterial(material); // Projectile material = new Game.Newt.v2.NewtonDynamics.Material(); _material_Projectile = _materialManager.AddMaterial(material); // Collisions _materialManager.RegisterCollisionEvent(_material_Bot, _material_Bot, Collision_BotBot); _materialManager.RegisterCollisionEvent(_material_Bot, _material_Food, Collision_BotFood); //TODO: May want to listen to projectile collisions #endregion #region Trackball // Trackball _trackball = new TrackBallRoam(_camera); _trackball.KeyPanScale = 15d; _trackball.EventSource = grdViewPort; //NOTE: If this control doesn't have a background color set, the trackball won't see events (I think transparent is ok, just not null) _trackball.AllowZoomOnMouseWheel = true; _trackball.Mappings.AddRange(TrackBallMapping.GetPrebuilt(TrackBallMapping.PrebuiltMapping.MouseComplete_NoLeft)); _trackball.Mappings.AddRange(TrackBallMapping.GetPrebuilt(TrackBallMapping.PrebuiltMapping.Keyboard_ASDW_In)); _trackball.ShouldHitTestOnOrbit = true; //_trackball.UserMovedCamera += new EventHandler<UserMovedCameraArgs>(Trackball_UserMovedCamera); //_trackball.GetOrbitRadius += new EventHandler<GetOrbitRadiusArgs>(Trackball_GetOrbitRadius); #endregion #region Camera Pool //TODO: Make the number of threads more configurable, look at how many processors there are //_cameraPool = new CameraPool(2, Colors.Black); _cameraPool = new CameraPool(1, Colors.Black); #endregion #region Map _map = new Map(_viewport, _cameraPool, _world) { SnapshotFequency_Milliseconds = 250, // 125 SnapshotMaxItemsPerNode = 10, ShouldBuildSnapshots = true, ShouldShowSnapshotLines = false, ShouldSnapshotCentersDrift = true, }; _updateManager = new UpdateManager( new Type[] { typeof(Swimbot) }, new Type[] { typeof(Swimbot) }, _map); #endregion #region Fields _radiation = new RadiationField() { AmbientRadiation = 0d, }; //_gravity = new GravityFieldUniform() //{ // Gravity = new Vector3D(0, 0, 0), //}; //TODO: Support a uniform fluid //FluidField #endregion #region ItemSelectDragLogic _selectionLogic = new ItemSelectDragLogic(_map, _camera, _viewport, grdViewPort) { ShouldMoveItemWithSpring = true, ShouldSpringCauseTorque = false, SpringColor = null, // Colors.Chartreuse ShowDebugVisuals = false, // true }; _selectionLogic.SelectableTypes.Add(typeof(Bot)); _selectionLogic.SelectableTypes.Add(typeof(Mineral)); _selectionLogic.SelectableTypes.Add(typeof(Egg <ShipDNA>)); _selectionLogic.ItemSelected += new EventHandler <ItemSelectedArgs>(SelectionLogic_ItemSelected); #endregion _world.UnPause(); } catch (Exception ex) { MessageBox.Show(ex.ToString(), this.Title, MessageBoxButton.OK, MessageBoxImage.Error); } }