Ejemplo n.º 1
0
        public PanelFile(string sessionFolder, FlyingBeanSession session, FlyingBeanOptions options, ItemOptions itemOptions, SortedList<string, ShipDNA> defaultBeanList)
        {
            InitializeComponent();

            _defaultBeanList = defaultBeanList;

            this.SessionFolder = sessionFolder;
            this.Session = session;
            this.Options = options;
            this.ItemOptions = itemOptions;

            _isInitialized = true;
        }
        private void PanelFile_SessionChanged(object sender, EventArgs e)
        {
            try
            {
                //NOTE: This method has many statements, and isn't very threadsafe, but even if there were several threads hitting these options there shouldn't
                //be any damage with the panels being momentarily out of sync

                // Kill all current
                if (_selectedBean != null && _selectedBean.Viewer != null)
                {
                    _selectedBean.Viewer.Close();
                }

                _selectedBean = null;
                foreach (Bean bean in _beans.ToArray())		// using toarray, because DisposeBean removes from the list
                {
                    DisposeBean(bean);
                }
                foreach (ExplodingBean explosion in _explosions.ToArray())
                {
                    DisposeExplosion(explosion);
                }

                // Get the new session
                _session = _panelFile.Session;
                _options = _panelFile.Options;
                _itemOptions = _panelFile.ItemOptions;

                #region Refresh options panels

                if (_panelBeanTypes != null)
                {
                    _panelBeanTypes.Options = _options;
                }

                if (_panelBeanProps != null)
                {
                    _panelBeanProps.Options = _options;
                    _panelBeanProps.ItemOptions = _itemOptions;
                }

                if (_panelMutation != null)
                {
                    _panelMutation.Options = _options;
                }

                if (_panelTracking != null)
                {
                    _panelTracking.Options = _options;
                }

                if (_panelSimulation != null)
                {
                    _panelSimulation.Options = _options;
                }

                #endregion

                // Refresh winner manager
                if (_winnerManager != null)
                {
                    _winnerManager.Live = _options.WinnersLive;
                    _winnerManager.Candidates = _options.WinnerCandidates;
                    _winnerManager.Final = _options.WinnersFinal;
                }

                RefreshStats();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString(), this.Title, MessageBoxButton.OK, MessageBoxImage.Error);
            }
        }
Ejemplo n.º 3
0
        private void FinishLoad(FlyingBeanSession session, FlyingBeanOptions options, ItemOptions itemOptions, ShipDNA[] winningBeans, string saveFolder, bool startEmpty)
        {
            // Manually instantiate some of the properties that didn't get serialized
            options.DefaultBeanList = _defaultBeanList;

            if (options.NewBeanList == null)		// if this was called by new, it will still be null
            {
                options.NewBeanList = new SortedList<string, ShipDNA>();

                if (!startEmpty)
                {
                    string[] beanKeys = options.DefaultBeanList.Keys.ToArray();

                    foreach (int keyIndex in UtilityCore.RandomRange(0, beanKeys.Length, Math.Min(3, beanKeys.Length)))		// only do 3 of the defaults
                    {
                        string key = beanKeys[keyIndex];
                        options.NewBeanList.Add(key, options.DefaultBeanList[key]);
                    }
                }
            }

            options.MutateArgs = PanelMutation.BuildMutateArgs(options);

            options.GravityField = new GravityFieldUniform();
            options.Gravity = options.Gravity;		// the property set modifies the gravity field

            options.WinnersLive = new WinnerList(true, options.TrackingMaxLineagesLive, options.TrackingMaxPerLineageLive);

            if (options.WinnersFinal == null)		// if a previous save had winning ships, this will already be loaded with them
            {
                options.WinnersFinal = new WinnerList(false, options.TrackingMaxLineagesFinal, options.TrackingMaxPerLineageFinal);
            }

            options.WinnerCandidates = new CandidateWinners();
            // These are already in the final list, there's no point in putting them in the candidate list as well
            //if (winningBeans != null)
            //{
            //    foreach (ShipDNA dna in winningBeans)
            //    {
            //        options.WinnerCandidates.Add(dna);
            //    }
            //}

            // Make sure the session folder is up to date
            if (saveFolder == null)
            {
                this.SessionFolder = null;
            }
            else
            {
                string dirChar = Regex.Escape(System.IO.Path.DirectorySeparatorChar.ToString());

                string pattern = dirChar + Regex.Escape(FlyingBeansWindow.SESSIONFOLDERPREFIX) + "[^" + dirChar + "]+(?<upto>" + dirChar + ")" + Regex.Escape(FlyingBeansWindow.SAVEFOLDERPREFIX);
                Match match = Regex.Match(saveFolder, pattern, RegexOptions.IgnoreCase);
                if (match.Success)
                {
                    this.SessionFolder = saveFolder.Substring(0, match.Groups["upto"].Index);		// the session folder is everything before the save subfolder
                }
                else
                {
                    // They may have chosen a folder that they unziped onto their desktop, or wherever.  Leaving this null so that if they hit save, it will be saved
                    // in the appropriate location
                    this.SessionFolder = null;
                }
            }

            // Swap out the settings
            this.Session = session;
            this.Options = options;
            this.ItemOptions = itemOptions;

            // Inform the world
            if (this.SessionChanged != null)
            {
                this.SessionChanged(this, new EventArgs());
            }
        }
        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            try
            {
                double terrainHeight = TERRAINRADIUS / 20d;

                #region Load last save

                _session = new FlyingBeanSession();
                _options = new FlyingBeanOptions();
                _itemOptions = new ItemOptions();

                _panelFile = new PanelFile(null, _session, _options, _itemOptions, GetDefaultBeans());
                _panelFile.SessionChanged += new EventHandler(PanelFile_SessionChanged);
                if (!_panelFile.TryLoadLastSave(false))
                {
                    _panelFile.New(false, false);		// by calling new, all of the options initialization is done by the file panel instead of doing it here
                }

                #endregion

                #region Winners

                _winnerManager = new WinnerManager(_options.WinnersLive, _options.WinnerCandidates, _options.WinnersFinal, _options.FinalistCount);

                #endregion
                #region Init World

                double boundryXY = TERRAINRADIUS * 1.25d;

                _boundryMin = new Point3D(-boundryXY, -boundryXY, terrainHeight * -2d);
                _boundryMax = new Point3D(boundryXY, boundryXY, TERRAINRADIUS * 25d);

                _world = new World();
                _world.Updating += new EventHandler<WorldUpdatingArgs>(World_Updating);

                List<Point3D[]> innerLines, outerLines;
                _world.SetCollisionBoundry(out innerLines, out outerLines, _boundryMin, _boundryMax);

                // Draw the lines
                _boundryLines = new ScreenSpaceLines3D(true)
                {
                    Thickness = 1d,
                    Color = _colors.BoundryLines,
                };
                _viewport.Children.Add(_boundryLines);

                foreach (Point3D[] line in innerLines)
                {
                    _boundryLines.AddLine(line[0], line[1]);
                }

                #endregion
                #region Materials

                _materialManager = new MaterialManager(_world);

                // Terrain
                Game.Newt.v2.NewtonDynamics.Material material = new Game.Newt.v2.NewtonDynamics.Material();
                material.Elasticity = .1d;
                _material_Terrain = _materialManager.AddMaterial(material);

                // Bean
                material = new Game.Newt.v2.NewtonDynamics.Material();
                _material_Bean = _materialManager.AddMaterial(material);

                // Exploding Bean
                material = new Game.Newt.v2.NewtonDynamics.Material();
                material.IsCollidable = false;
                _material_ExplodingBean = _materialManager.AddMaterial(material);

                // Projectile
                material = new Game.Newt.v2.NewtonDynamics.Material();
                _material_Projectile = _materialManager.AddMaterial(material);

                _materialManager.RegisterCollisionEvent(0, _material_Bean, Collision_BeanTerrain);		// zero should be the boundry (it should be the default material if no other is applied)
                _materialManager.RegisterCollisionEvent(_material_Terrain, _material_Bean, Collision_BeanTerrain);
                _materialManager.RegisterCollisionEvent(_material_Bean, _material_Bean, Collision_BeanBean);

                #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 Map

                _map = new Map(_viewport, null, _world)
                {
                    SnapshotFequency_Milliseconds = 125,
                    SnapshotMaxItemsPerNode = 10,
                    ShouldBuildSnapshots = false,
                    ShouldShowSnapshotLines = false,
                    ShouldSnapshotCentersDrift = true,
                };

                _updateManager = new UpdateManager(
                    new Type[] { typeof(Bean) },
                    new Type[] { typeof(Bean) },
                    _map);

                #endregion
                #region Terrain

                //TODO: Texture map this so it's not so boring
                #region WPF Model (plus collision hull)

                // Material
                MaterialGroup materials = new MaterialGroup();
                materials.Children.Add(new DiffuseMaterial(new SolidColorBrush(_colors.Terrain)));
                materials.Children.Add(_colors.TerrainSpecular);

                // Geometry Model
                GeometryModel3D geometry = new GeometryModel3D();
                geometry.Material = materials;
                geometry.BackMaterial = materials;

                geometry.Geometry = UtilityWPF.GetCylinder_AlongX(100, TERRAINRADIUS, terrainHeight);
                CollisionHull hull = CollisionHull.CreateCylinder(_world, 0, TERRAINRADIUS, terrainHeight, null);

                // Transform
                Transform3DGroup transform = new Transform3DGroup();		// rotate needs to be added before translate
                transform.Children.Add(new RotateTransform3D(new AxisAngleRotation3D(new Vector3D(0, 1, 0), -90)));
                transform.Children.Add(new TranslateTransform3D(new Vector3D(0, 0, terrainHeight / -2d)));		// I want the objects to be able to add to z=0

                // Model Visual
                ModelVisual3D model = new ModelVisual3D();
                model.Content = geometry;
                model.Transform = transform;

                // Add to the viewport
                _viewport.Children.Add(model);

                #endregion

                // Make a physics body that represents this shape
                _terrain = new Body(hull, transform.Value, 0, new Visual3D[] { model });		// using zero mass tells newton it's static scenery (stuff bounces off of it, but it will never move)
                hull.Dispose();
                _terrain.MaterialGroupID = _material_Terrain;

                #endregion
                #region Fields

                // gravity was done by the file panel

                _radiation = new RadiationField()
                {
                    AmbientRadiation = 0d,
                };

                _boundryField = new BoundryField(.5d, 7500d, 2d, _boundryMin, _boundryMax);

                #endregion

                // Doing this so that if they hit the import button, it will call a method in beantypes (not the best design, but it works)
                _panelBeanTypes = new PanelBeanTypes(_options, _world);
                _panelFile.BeanTypesPanel = _panelBeanTypes;

                this.TotalBeansText = _options.TotalBeans.ToString("N0");

                _world.UnPause();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString(), this.Title, MessageBoxButton.OK, MessageBoxImage.Error);
            }
        }