Exemple #1
0
 public static Point HexCornerOffset(Layout layout, int corner)
 {
     Orientation M = layout.orientation;
     Point size = layout.size;
     double angle = 2.0 * Math.PI * (corner + M.start_angle) / 6;
     return new Point(size.x * Math.Cos(angle), size.y * Math.Sin(angle));
 }
Exemple #2
0
        public static PostVoteResponse VoteAction(PostVoteRequest model)
        {
            var gameStateData = SocialWarGames.Instance.StateData;

            if (model.Generation != gameStateData.Generation)
            {
                throw new ValidationException("Generation invalid");
            }

            var unit = gameStateData.GetUnitById(model.UnitId);
            if (unit == null) throw new ValidationException("Unit not found");
            Layout n = new Layout(Layout.flat, new Point(1, 1), new Point(50, 50));

            var h = FractionalHex.HexRound(Layout.PixelToHex(n, new Point(unit.X, unit.Y)));
            var c = FractionalHex.HexRound(Layout.PixelToHex(n, new Point(model.X, model.Y)));

            var distance = Hex.Distance(h, c);

            switch (unit.UnitType)
            {
                case GameUnitType.Infantry:
                    switch (model.Action)
                    {
                        case "Move":
                            if (distance != 1)
                            {
                                throw new ValidationException("Distance must be 1");
                            }

                            SocialWarGames.Instance.VoteAction(new MoveInfantryVote()
                            {
                                X = model.X,
                                Y = model.Y,
                                UnitId = model.UnitId,
                                Generation=model.Generation
                            });
                            break;

                        case "Attack":
                            if (distance != 1)
                            {
                                throw new ValidationException("Distance must be 1");
                            }

                            SocialWarGames.Instance.VoteAction(new AttackInfantryVote()
                            {
                                X = model.X,
                                Y = model.Y,
                                UnitId = model.UnitId,
                                Generation = model.Generation
                            });
                            break;
                    }
                    break;
            }

            return new PostVoteResponse();
        }
Exemple #3
0
 public static Point HexToPixel(Layout layout, Hex h)
 {
     Orientation M = layout.orientation;
     Point size = layout.size;
     Point origin = layout.origin;
     double x = (M.f0 * h.q + M.f1 * h.r) * size.x;
     double y = (M.f2 * h.q + M.f3 * h.r) * size.y;
     return new Point(x + origin.x, y + origin.y);
 }
Exemple #4
0
 public static FractionalHex PixelToHex(Layout layout, Point p)
 {
     Orientation M = layout.orientation;
     Point size = layout.size;
     Point origin = layout.origin;
     Point pt = new Point((p.x - origin.x) / size.x, (p.y - origin.y) / size.y);
     double q = M.b0 * pt.x + M.b1 * pt.y;
     double r = M.b2 * pt.x + M.b3 * pt.y;
     return new FractionalHex(q, r, -q - r);
 }
Exemple #5
0
 public static List<Point> PolygonCorners(Layout layout, Hex h)
 {
     List<Point> corners = new List<Point> { };
     Point center = Layout.HexToPixel(layout, h);
     for (int i = 0; i < 6; i++)
     {
         Point offset = Layout.HexCornerOffset(layout, i);
         corners.Add(new Point(center.x + offset.x, center.y + offset.y));
     }
     return corners;
 }
Exemple #6
0
 public static void TestLayout()
 {
     Hex h = new Hex(3, 4, -7);
     Layout flat = new Layout(Layout.flat, new Point(10, 15), new Point(35, 71));
     Tests.EqualHex("layout", h, FractionalHex.HexRound(Layout.PixelToHex(flat, Layout.HexToPixel(flat, h))));
     Layout pointy = new Layout(Layout.pointy, new Point(10, 15), new Point(35, 71));
     Tests.EqualHex("layout", h, FractionalHex.HexRound(Layout.PixelToHex(pointy, Layout.HexToPixel(pointy, h))));
 }