Example #1
0
        public MapObject(TmxMap map)
        {
            Map = map;
            Graphics = new MapGraphicsComponent(map);

            foreach (var platform in map.ObjectGroups["Solid"].Objects)
                Platforms.Add(new PlatformObject(ConvertUnits.ToSimUnits(platform.X),
                                                 ConvertUnits.ToSimUnits(platform.Y),
                                                 ConvertUnits.ToSimUnits(platform.Width),
                                                 ConvertUnits.ToSimUnits(platform.Height)));

            // "implicit" walls to prevent leaving the screen
            Platforms.Add(new WallObject(ConvertUnits.ToSimUnits(-map.TileWidth),
                                             0,
                                             ConvertUnits.ToSimUnits(map.TileWidth),
                                             ConvertUnits.ToSimUnits(map.Height * map.TileHeight)));

            Platforms.Add(new WallObject(ConvertUnits.ToSimUnits(map.Width * map.TileWidth),
                                             0,
                                             ConvertUnits.ToSimUnits(map.TileWidth),
                                             ConvertUnits.ToSimUnits(map.Height * map.TileHeight)));

            foreach (var candy in map.ObjectGroups["Candy"].Objects)
            {
                CandyKind kind;
                if (Enum.TryParse(candy.Name, out kind))
                {
                    CandyObject cobj;
                    if (kind == CandyKind.Chocolate)
                        cobj = new CandyObject(kind, new Vector2(candy.X, candy.Y), new Vector2(candy.Width, candy.Height));
                    else
                        cobj = new CandyObject(kind, new Vector2(candy.X + candy.Width / 2, candy.Y + candy.Height / 2));
                    Candies.Add(cobj);
                }
            }

            foreach (var obj in map.ObjectGroups["Misc"].Objects)
            {
                if (obj.Name == "Text")
                {
                    Hints.Add(new TextComponent
                              {
                                  Text =
                                      new Text(obj.Properties["Text"], Content.Font, 16)
                                      {
                                          Position = new Vector2f(obj.X, obj.Y)
                                      }
                              });
                }
                else if (obj.Name == "Powerup")
                {
                    Powerups.Add(new PowerupObject(new Vector2f(obj.X + obj.Width / 2, obj.Y + obj.Height / 2)));
                }
                else if (obj.Name == "Spawn")
                    Spawn = new Vector2(ConvertUnits.ToSimUnits(obj.X + obj.Width / 2), ConvertUnits.ToSimUnits(obj.Y + obj.Height / 2));
                else if (obj.Name == "Exit")
                    Exit = new ExitObject(new Vector2(ConvertUnits.ToSimUnits(obj.X + obj.Width/2), ConvertUnits.ToSimUnits(obj.Y + obj.Height/2)));
            }
        }
Example #2
0
        public void Slice(Vector2 entryPoint, Vector2 exitPoint, bool crush = false, Vector2 direction = default(Vector2))
        {
            HitPoints = Math.Min(HitPoints, 1);

            var textureOrigin = Sprite.Sprite.Texture;
            Texture textureA, textureB;
            var spriteOrigin = Sprite.Sprite.Origin;
            var relativeEntryPoint =
                new Vector2f(ConvertUnits.ToDisplayUnits(entryPoint.X - Physics.Position.X),
                                ConvertUnits.ToDisplayUnits(entryPoint.Y - Physics.Position.Y));
            var rotation = Transform.Identity;
            rotation.Rotate(-Sprite.Sprite.Rotation);
            var rotatedEntryPoint = rotation.TransformPoint(relativeEntryPoint);
            var startPoint = spriteOrigin + rotatedEntryPoint;
            var relativeExitPoint =
                new Vector2f(ConvertUnits.ToDisplayUnits(exitPoint.X - Physics.Position.X),
                                ConvertUnits.ToDisplayUnits(exitPoint.Y - Physics.Position.Y));
            var rotatedExitPoint = rotation.TransformPoint(relativeExitPoint);
            var endPoint = spriteOrigin + rotatedExitPoint;
            TextureSlicer.SliceAndDice(startPoint, endPoint, textureOrigin, out textureA,
                out textureB, RepeatsX, RepeatsY);
            Vertices first;
            Vertices second;
            var fixture = Physics.Body.FixtureList[0];
            FarseerPhysics.Common.PolygonManipulation.CuttingTools.SplitShape(fixture, entryPoint, exitPoint, out first, out second);
            //Delete the original shape and create two new. Retain the properties of the body.
            if (first.GetArea() < 0.005f ||
                second.GetArea() < 0.005f || first.CheckPolygon() != PolygonError.NoError ||
                second.CheckPolygon() != PolygonError.NoError)
                return;

            Body firstFixture = BodyFactory.CreatePolygon(PhysicsSubsystem.Instance.World, first, 1f,
                                                            fixture.Body.Position);
            firstFixture.Rotation = fixture.Body.Rotation;
            firstFixture.LinearVelocity = fixture.Body.LinearVelocity;
            firstFixture.AngularVelocity = fixture.Body.AngularVelocity;
            firstFixture.BodyType = BodyType.Dynamic;
            firstFixture.UserData = this;
            Physics.Body = firstFixture;
            Sprite.Sprite.Texture = textureA;
            RepeatsX = RepeatsY = 1; // 1WEEK

            if (crush)
            {
                Physics.Body.IgnoreCollisionWith(GameplayState.Player.Torso.Body);
                Physics.Body.IgnoreCollisionWith(GameplayState.Player.Legs.Body);
                Physics.Body.IgnoreCollisionWith(GameplayState.Player.Piston.Body);
                if (first.GetArea() > 0.25f)
                    Crush(direction);
            }

            Body secondFixture = BodyFactory.CreatePolygon(PhysicsSubsystem.Instance.World, second, 1f,
                                                            fixture.Body.Position);
            secondFixture.Rotation = fixture.Body.Rotation;
            secondFixture.LinearVelocity = fixture.Body.LinearVelocity;
            secondFixture.AngularVelocity = fixture.Body.AngularVelocity;
            secondFixture.BodyType = BodyType.Dynamic;
            var secondCandy = new CandyObject(secondFixture, textureB, secondFixture.Position) { HitPoints = HitPoints };
            secondFixture.UserData = secondCandy;

            if (crush)
            {
                secondCandy.Physics.Body.IgnoreCollisionWith(GameplayState.Player.Torso.Body);
                secondCandy.Physics.Body.IgnoreCollisionWith(GameplayState.Player.Legs.Body);
                secondCandy.Physics.Body.IgnoreCollisionWith(GameplayState.Player.Piston.Body);
                if (second.GetArea() > 0.25f)
                    secondCandy.Crush(direction);
            }

            PhysicsSubsystem.Instance.World.RemoveBody(fixture.Body);

            if(OnSlice != null)
                OnSlice();
        }