Beispiel #1
0
        public CodeIsland(
            VisionContent vContent,
            Archipelag archipelag,
            Matrix world,
            VAssembly vassembly)
            : base(vContent)
        {
            Archipelag = archipelag;
            VAssembly = vassembly;
            World = world;

            if (VAssembly.Is3DParty)
            {
                DrawableBox = new DrawableBox(vContent, World, new Vector3(50, 20, 50), 0.01f);
                foreach (var vclass in vassembly.VClasses)
                {
                    var vc = new VisionClass(this, vclass, 75, 75, 5) {Height = 10};
                    Classes.Add(vclass.FullName, vc);
                }
                return;
            }

            var rnd = new Random();

            var interfaceClasses = new List<VClass>();
            var implementationClasses = new List<VClass>();
            foreach (var vclass in vassembly.VClasses)
                if (vclass.IsInterface)
                    interfaceClasses.Add(vclass);
                else
                    implementationClasses.Add(vclass);

            var circleMaster = new CircleMaster<VClass>();
            var q = 0;
            foreach (var vclass in interfaceClasses)
                circleMaster.Drop(q += 10, 0, 10, vclass);

            foreach (var vclass in implementationClasses)
                circleMaster.Drop(q += 5, 0, 4 + (int) Math.Sqrt(vclass.InstructionCount), vclass);

            int left, top, right, bottom;
            circleMaster.GetBounds(out left, out top, out right, out bottom);
            foreach (var c in circleMaster.Circles)
            {
                var vc = new VisionClass(this, c.Tag, ClassSide/2 + c.X - left, ClassSide/2 + c.Y - top, c.R);
                Classes.Add(c.Tag.FullName, vc);
            }

            var surfaceWidth = 1 << (1 + (int) (Math.Log(right - left)/Math.Log(2)));
            var surfaceHeight = 1 << (1 + (int) (Math.Log(bottom - top)/Math.Log(2)));

            System.Diagnostics.Debug.Print("{0}: {1} {2}", vassembly.Name, surfaceWidth, surfaceHeight);

            //var qq = Math.Max(surfaceWidth, surfaceHeight);
            var ground = new GroundMap(surfaceWidth, surfaceHeight);

            BoundingSphere = new BoundingSphere(new Vector3(
                world.TranslationVector.X + ground.Width / 2f,
                world.TranslationVector.Y,
                world.TranslationVector.Z + ground.Height / 2f),
                (float)Math.Sqrt(ground.Width * ground.Width + ground.Height * ground.Height) / 2);

            foreach (var vc in Classes.Values)
            {
                var instructHeight = (vc.VClass.IsInterface ? 40 : 10) + (float) Math.Pow(vc.VClass.InstructionCount, 0.3);
                var maintainabilityFactor = 3*(10 - vc.MaintainabilityIndex/10);
                var radius = vc.R;
                var middleX = vc.X - radius;
                var middleY = vc.Y - radius;
                var bellShapeFactor = 2f/(radius*1.7f);
                ground.AlterValues(
                    middleX, middleY,
                    radius*2, radius*2,
                    (px, py,  h) =>
                    {
                        var dx = px - radius;
                        var dy = py - radius;
                        var d = (dx*dx + dy*dy)*bellShapeFactor*bellShapeFactor;
                        var sharpness = (px & 1) != (py & 1) ? maintainabilityFactor : 0;
                        return h + instructHeight*(float) Math.Exp(-d*d) + sharpness*(float) rnd.NextDouble();
                    });

                var height = ground[vc.X, vc.Y];
                vc.Height = height;
            }

            // raise the point where the sign is
            foreach (var vc in Classes.Values)
                ground[vc.X, vc.Y] += 6;

            //...and lower it...
            ground.Soften(2);

            //make GroundMap slices seamless
            for (var x = 64; x < surfaceWidth; x += TerrainPlane.SquareSize)
                for (var y = 0; y < surfaceHeight; y++)
                    ground[x, y] = ground[x - 1, y] = (ground[x, y] + ground[x - 1, y])/2;
            for (var y = 64; y < surfaceHeight; y += TerrainPlane.SquareSize)
                for (var x = 0; x < surfaceWidth; x++)
                    ground[x, y] = ground[x, y - 1] = (ground[x, y] + ground[x, y - 1])/2;

            foreach (var vc in Classes.Values)
                vc.Height = ground[vc.X, vc.Y];

            var normals = ground.CreateNormalsMap(ref world);

            var signs = new Signs(
                vContent,
                world*Matrix.Translation(0, -0.1f, 0),
                vContent.Load<Texture2D>("billboards/woodensign"),
                Classes.Values.ToList(),
                16,
                4);
            Children.Add(signs);

            var qqq = world; //*Matrix.Translation(0, 0.05f, 0);
            var ms = new CxBillboard(vContent, Matrix.Identity, vContent.Load<Texture2D>("billboards/grass"), 1, 1, 0.5f);
            foreach (var vc in Classes.Values)
            {
                for (var i = (vc.CyclomaticComplexity - 1)*2; i > 0; i--)
                {
                    var gx = vc.X + ((float) rnd.NextDouble() - 0.5f)*(ClassSide - 3);
                    var gy = vc.Y + ((float) rnd.NextDouble() - 0.5f)*(ClassSide - 3);
                    ms.Add(
                        Vector3.TransformCoordinate(new Vector3(gx, ground.GetExactHeight(gx, gy), gy), qqq),
                        normals.AsVector3(vc.X, vc.Y));
                }
            }
            ms.CreateVertices();
            Children.Add(ms);

            var weights = ground.CreateWeigthsMap(new[] {0, 0.40f, 0.60f, 0.9f});
            //weights.DrawLine(0, 0, 20, 20, 1, (_, mt) => new Mt9Surface.Mt9 { I = 100 });
            //weights.AlterValues(20, 20, 20, 20, (x, y, mt) =>
            //{
            //    mt.B = 1;
            //    return mt;
            //});
            //weights.AlterValues(40, 40, 20, 20, (x, y, mt) => new Mt9Surface.Mt9 { C = 100 });
            //weights.AlterValues(60, 60, 20, 20, (x, y, mt) => new Mt9Surface.Mt9 { D = 10 });
            //weights.AlterValues(80, 80, 20, 20, (x, y, mt) => new Mt9Surface.Mt9 { E = 10 });
            //weights.AlterValues(100, 100, 20, 20, (x, y, mt) => new Mt9Surface.Mt9 { F = 10 });
            //weights.AlterValues(120, 120, 20, 20, (x, y, mt) => new Mt9Surface.Mt9 { G = 10 });
            //weights.AlterValues(140, 140, 20, 20, (x, y, mt) => new Mt9Surface.Mt9 { H = 10 });
            //weights.AlterValues(160, 160, 20, 20, (x, y, mt) => new Mt9Surface.Mt9 { I = 100, A = (float)Math.Sqrt(x * x + y * y) });

            //foreach (var vc in Classes.Values.Where(_ => !_.CalledClasses.Any()))
            //{
            //    weights.AlterValues(vc.X - vc.R, vc.Y - vc.R, vc.R*2, vc.R*2, (x, y, mt) => new Mt9Surface.Mt9 {I = 100});
            //}

            initialize(ground, weights, normals);
        }
        protected override void LoadContent()
        {
            base.LoadContent();

            _spriteBatch = ToDisposeContent(new SpriteBatch(GraphicsDevice));

            _vContent = new VisionContent(_graphicsDeviceManager.GraphicsDevice, Content);
            _arial16Font = Content.Load<SpriteFont>("Fonts/Arial16");

            _basicEffect = new VBasicEffect(_graphicsDeviceManager.GraphicsDevice);
            _basicEffect.EnableDefaultLighting();

            _ball = new SpherePrimitive<VertexPositionNormalTexture>(GraphicsDevice, (p, n, t, tx) => new VertexPositionNormalTexture(p, n, tx), 1);
            var x = _vContent.LoadEffect("effects/simpletextureeffect");
            x.Texture = _vContent.Load<Texture2D>("terraintextures/sand");
            _ballInstance = new VDrawableInstance(x, _ball, Matrix.Translation(10, 2, 10));

            Sky = new SkySphere(_vContent, _vContent.Load<TextureCube>(@"Textures\clouds"));
            _movingShip = new MovingShip(new ShipModel(_vContent));

            _water = WaterFactory.Create(_vContent);
            _water.ReflectedObjects.Add(_movingShip._shipModel);
            _water.ReflectedObjects.Add(_ballInstance);
            _water.ReflectedObjects.Add(Sky);

            _camera = new Camera(
                _vContent.ClientSize,
                new KeyboardManager(this),
                new MouseManager(this),
                null, //new PointerManager(this),
                new Vector3(0, 15, 0),
                new Vector3(-10, 15, 0));

            _rasterizerState = RasterizerState.New(GraphicsDevice, new RasterizerStateDescription
            {
                FillMode = FillMode.Solid,
                CullMode = CullMode.Back,
                IsFrontCounterClockwise = false,
                DepthBias = 0,
                SlopeScaledDepthBias = 0.0f,
                DepthBiasClamp = 0.0f,
                IsDepthClipEnabled = true,
                IsScissorEnabled = false,
                IsMultisampleEnabled = false,
                IsAntialiasedLineEnabled = false
            });

            _shadow = new ShadowMap(_vContent, 1024, 1024);
            //_shadow.ShadowCastingObjects.Add(_sailingShip);
            //_shadow.ShadowCastingObjects.Add(reimersTerrain);
            //_shadow.ShadowCastingObjects.Add(generatedTerrain);
            //_shadow.ShadowCastingObjects.Add(bridge);

            //_archipelag = new Archipelag(_vContent, _water, _shadow);

            _data.VContent = _vContent;
            _data.Camera = _camera;
            _data.Water = _water;
            _data.Shadow = _shadow;

            _q = new CxBillboard(_vContent, Matrix.Identity, _vContent.Load<Texture2D>("billboards/wheat_billboard"), 20, 10, 0.5f);
            _q.AddPositionsWithSameNormal(Vector3.Up,
                Vector3.Zero,
                Vector3.Left*10.5f, Vector3.Up,
                Vector3.Right*10.4f, Vector3.Up,
                Vector3.ForwardRH*3.45f, Vector3.Up,
                Vector3.BackwardRH*2.9f, Vector3.Up);
            _q.CreateVertices();
        }