LifeSimulation.Core.World.Draw C# (CSharp) Method

Draw() public method

public Draw ( Graphics g ) : void
g System.Drawing.Graphics
return void
        public void Draw(Graphics g)
        {
            // Init
            Lifelet lifeToHighlight = _highlightedLifelet;
            if(_selectedLifelet != null) lifeToHighlight = _selectedLifelet;

            // Background
            g.Clear(Color.Black);

            // Food
            foreach(Food food in _food) {
                food.Draw(g);
            }

            // Loop life forms
            foreach(Lifelet life in _lifelets) {
                // Tell life to draw
                life.Draw(g);

                // Debugging?
                if(Config.Debug) {

                }

                // Highlight / debugging infos
                if(life == lifeToHighlight || Config.Debug) {

                    System.Drawing.Drawing2D.GraphicsContainer gc1 = g.BeginContainer(); {

                        g.TranslateTransform((int)life.X,(int)life.Y);

                        // Draw a crosshair
                        Pen pen = new Pen( life == lifeToHighlight ? Color.White : life.Color );
                        int crosshairSize = life == lifeToHighlight ? (int)(Config.DisplayCrosshairSize*1.5) : Config.DisplayCrosshairSize;
                        g.DrawLine(pen,0,-crosshairSize,0,crosshairSize);
                        g.DrawLine(pen,-crosshairSize,0,crosshairSize,0);

                        // Focus Circle
                        g.DrawEllipse(new Pen(Color.White),
                                      (int)(-life.Visibility),
                                      (int)(-life.Visibility),
                                      (int)(life.Visibility*2),
                                      (int)(life.Visibility*2) );

                        // Health bar
                        g.FillRectangle(Brushes.Red,
                                        0,
                                        -(Config.DisplayCrosshairSize*3),
                                        (int)((life.Health/100.0) * Config.DisplayPseudoPercentBarSize),
                                        2 );

                        // Energy bar
                        g.FillRectangle(Brushes.Yellow,
                                        0,
                                        -(Config.DisplayCrosshairSize*3) - 4,
                                        (int)((life.Energy/100.0) * Config.DisplayPseudoPercentBarSize),
                                        2 );

                    } g.EndContainer(gc1);
                }
            }

            // Message
            foreach(Message message in _messages) {
                message.Draw(g);
            }

            // Highlight infos
            string infos = "";
            if(lifeToHighlight != null) {

                // Infos via reflection. We only show information for properties. Note that
                // we do this operation on the virtual type, not the Life type, so any
                // properties in an implemented life are also shown.
                Type t = lifeToHighlight.GetType();
                foreach(System.Reflection.PropertyInfo p in t.GetProperties()) {
                    object val = p.GetValue(lifeToHighlight,null);
                    if(val != null) infos += p.Name + ": " + val.ToString() + "\n";
                }
                infos += "\n";
            }

            // Show debug infos
            if(Config.Debug) {
                // World...
                infos += "Cursor: " + _cursor + "\n";

                // Statistics
                foreach(object k in _stats.Keys) {
                    infos += k + ": " + _stats[k] + "\n";
                }
                infos += "\n";
            }

            // Draw world boundaries
            if(Config.Debug) {
                Pen pen = new Pen(Color.White);
                g.DrawLine(pen,0,-Config.DisplayCrosshairSize*3,0,Config.DisplayCrosshairSize*3);
                g.DrawLine(pen,-Config.DisplayCrosshairSize*3,0,Config.DisplayCrosshairSize*3,0);
                g.DrawEllipse(pen,-Config.WorldRadius,-Config.WorldRadius,Config.WorldRadius*2,Config.WorldRadius*2);
            }

            // Draw debug infos - no world drawing after this
            if (infos != "") {
                g.ResetTransform(); //NOTE: On Windows this doesnt reset through all the containers, so we do it in the current container and restrict any drawing after
                System.Drawing.Drawing2D.GraphicsContainer gc2 = g.BeginContainer();
                {
                    // g.ResetTransform();
                    g.DrawString(infos, Config.DisplayTextFont, Config.DisplayTextBrush, new PointF(30, 30));
                } g.EndContainer(gc2);
            }
        }