Beispiel #1
0
        private Node GridDistribution(Rectangle area, Color[] colors, BodyRenderer renderer)
        {
            var hexagons = new Node();

            var r = 8;
            var x = r * (float)Math.Sqrt(3);
            var y = r * 2f;
            var n = area.Width / x - 1;
            var m = area.Height / y - 1;
            var o = new Vector2(r + 3, r);

            var d = r;

            for (var i = 0; i < n; i++)
            {
                o += new Vector2(0, d);
                d = -d;

                for (var j = 0; j < m; j++)
                {
                    if (Rand.Float() < .6f)
                        continue;

                    var pos = o + new Vector2(i * x, j * y);
                    var color = colors.RandomItem();
                    hexagons.Add(BuildHexagon(renderer, color, pos, r, r));
                }
            }

            return hexagons;
        }
Beispiel #2
0
        private Body BuildHexagon(BodyRenderer renderer, Color color, Vector2 pos, float radius, float spacing)
        {
            var poly = ShapeFactory.Hexagon(Vector2.Zero, radius);
            var body = Physic.World.CreatePolygon(new Vertices(poly), 1, pos);
            body.SetCollisionType(HexPhysicTypes.Ground);

            if (radius < 5)
            {
                body.BodyType = BodyType.Dynamic;
                body.AngularDamping = 5f;
                /*var distanceJoint = K2.Physic.JointFactory.CreateFixedDistanceJoint(body, Vector2.Zero, pos);
                    distanceJoint.Frequency = 10;
                    distanceJoint.Length = radius * .2f;
                    distanceJoint.DampingRatio = 1;*/

                var j = new FixedMouseJoint(body, pos)
                        {
                            Frequency = 1000,
                            MaxForce = 100,
                            DampingRatio = 1,
                            Breakpoint = 150,
                        };
                Physic.World.AddJoint(j);
            }

            renderer.Add(body, color);

            return body;
        }
Beispiel #3
0
 private Body BuildHexagon(BodyRenderer renderer, Color[] colors, Hexagon hex, float radius, float spacing)
 {
     var pos = hex.GetPosition(spacing);
     pos += new Vector2(radius) * Rand.Sign();
     var color = colors.RandomItem();
     return BuildHexagon(renderer, color, pos, radius, spacing);
 }
Beispiel #4
0
        public HexScene(BodyRenderer bodyRenderer, Rectangle area)
        {
            var palette = Palette.FromXmlFile(DataPack.Colors.Browns);
            var colors = palette.GetColors().Take(4).ToArray();

            // background
            //var bgColor = palette.ColorSets[0].Colors[4];
            //var shape = vectoRendererBg.Add(new VectoShape());
            //shape.DrawRectangle(Vector2.Zero, area.Width, area.Height, true, bgColor);

            AddRange(TestDistribution(area, bodyRenderer, colors));
            Add(Physic.World.CreateLoopShape(area.ToVertices()));
        }
Beispiel #5
0
        public HexActor(BodyRenderer renderer, Rectangle area)
        {
            var palette = Palette.FromXmlFile(DataPack.Colors.RedAndBlue);
            var color = palette.GetColors(3).ToArray()[2];

            var poly = ShapeFactory.Hexagon(Vector2.Zero, .5f);

            // body
            var body = Add(new Body(Physic.World)
                           {
                               BodyType = BodyType.Dynamic,
                               Position = area.Center.ToVector2(),
                               //LinearDamping = 10,
                               AngularDamping = 5,
                               IsBullet = true
                           });
            Add(new BodyStateRecorder(body, "cameraTarget"));
            Add(new SoundListener(body));

            // main fixture
            var bodyFixture = body.AttachPolygon(new Vertices(poly), 1);
            bodyFixture.Restitution = 0;
            bodyFixture.Friction = 0f;
            bodyFixture.SetCollisionType(HexPhysicTypes.Actor);

            // control
            var ui = Add(new GameUI());
            var surfaceController = Add(new SurfaceMovementControl(body, 200f, 300f));
            var boost = ui.Add(new BoostController(body) { BoostMin = 500, BoostMax = 10000 });
            ui.Add(new MovementKeyboardInput(surfaceController, boost));
            Add(new GroundConstraint(body, bodyFixture, surfaceController));
            ui.Add(new Cursor(DataPack.Textures.Square) { Color = color, Size = 5f });

            // camera

            var tracker = ui.Add(new BodyMouseTracker(body, .4f));
            ui.Add(new TrackingBodyCameraController(tracker, UpdateTime.Sim));
            ui.Add(new CameraStateRecorder());
            var camConstraint = ui.Add(new CameraConstraint
                                       {
                                           TopLeft = Vector2.Zero,
                                           BottomRight = new Vector2(area.Width, area.Height)
                                       });
            ui.Add(new MouseZoomController(.1f, .5f) { WheelRange = 3 });
            Render.Camera.Position = body.Position;

            // render
            renderer.Add(body, color);
            Add(new Disposer(() => renderer.Remove(body)));
        }
Beispiel #6
0
        private Node TreeDistribution(Rectangle area, Color[] colors, BodyRenderer renderer)
        {
            var hexagons = new Node();

            var radius = 2;
            var spacing = 4;
            var mapWidth = area.Width / spacing - 3;
            var mapHeight = area.Height / spacing - 1;
            var map = new HexagonMap<bool>(mapWidth, mapHeight);

            var first = new Hexagon(Rand.Floor(mapWidth), Rand.Floor(mapHeight));
            map[first] = true;
            hexagons.Add(BuildHexagon(renderer, colors, first, radius, spacing));

            var current = first;
            var n = 5 + Rand.Floor(10);
            for (int i = 0; i < n; i++)
            {
                var freeNeighbors = current.GetNeighborsWhere(hexagon => map.IsInBound(hexagon) && !map[hexagon]).ToArray();
                if (!freeNeighbors.Any())
                    break;

                current = freeNeighbors.RandomItem();
                map[current] = true;

                hexagons.Add(BuildHexagon(renderer, colors, current, radius, spacing));
            }

            return hexagons;
        }
Beispiel #7
0
        private IEnumerable<Body> TestDistribution(Rectangle area, BodyRenderer renderer, Color[] colors)
        {
            const int size = 12;
            const float spacing = 14f;

            var origin = new Vector2(area.Width / 2, area.Height / 2);

            return SpiralRingDistribution(renderer, colors, origin, size, spacing, 4, .4f);
        }
Beispiel #8
0
        private IEnumerable<Body> SpiralRingDistribution(BodyRenderer renderer, Color[] colors, Vector2 origin, float size, float spacing, int spiralRadius, float chance)
        {
            var hexagons = new List<Body>();

            var center = new Hexagon(0, 0);

            var color = colors.RandomItem();
            var pos = origin + center.GetPosition(spacing);
            //hexagons.Add(BuildHexagon(render, color, pos, size, spacing));

            var ring = center.GetSpiralRing(spiralRadius);

            foreach (var hexagon in ring)
            {
                pos = origin + hexagon.GetPosition(spacing);

                var rand = Rand.Float();

                if (rand < chance)
                {
                    if (rand > chance / 2)
                        continue;

                    var newSize = size / 3;
                    var newSpacing = spacing / 3;
                    if (newSpacing > .5f)
                    {
                        hexagons.AddRange(SpiralRingDistribution(renderer, colors, pos, newSize, newSpacing, 1, chance));
                    }

                    continue;
                }

                color = colors.RandomItem();
                hexagons.Add(BuildHexagon(renderer, color, pos, size, spacing));
            }

            return hexagons;
        }