Example #1
0
        public override void InitializePhysics(bool force, IComponentContext engineRegistrations)
        {
            if (force || !initialized)
            {
                var cache = engineRegistrations.Resolve<IResourceCache<Texture2D>>();
                var texture = cache.GetResource(TextureName);
                var world = engineRegistrations.Resolve<PhysicsManager>().World;
                game = engineRegistrations.ResolveOptional<EngineGame>();

                if (game != null && game.GamepadEnabled)
                {
                    ACTION_POPUP = InputManager.Instance.GamepadTextures[InputManager.ButtonActions.Pickup];
                }

                //Width = texture.Width;
                //Height = texture.Height;
                Physics = BodyFactory.CreateRectangle(
                    world,
                    PhysicsConstants.PixelsToMeters(Width),
                    PhysicsConstants.PixelsToMeters(Height),
                    1,
                    Position);
                Physics.FixedRotation = true;
                Physics.BodyType = BodyType.Static;
                Physics.UserData = this;

                var fix = Physics.FixtureList[0];
                fix.CollisionCategories = Category.Cat3;
                fix.CollidesWith = Category.Cat1;

                HitSensor = BodyFactory.CreateRectangle(
                    world,
                    PhysicsConstants.PixelsToMeters(Width) * 2,
                    PhysicsConstants.PixelsToMeters(Height),
                    1,
                    Position);
                HitSensor.IsSensor = true;
                HitSensor.CollisionCategories = Category.Cat2;
                HitSensor.CollidesWith = Category.Cat2;

                HitSensor.RegisterOnCollidedListener<UserControlledCharacter>(OnCollidedWith);
                HitSensor.RegisterOnSeparatedListener<UserControlledCharacter>(OnSeparation);

                popup = new ItemPopup(ACTION_POPUP,
                    Physics.Position + new Vector2(0, -PhysicsConstants.PixelsToMeters(Height / 2 + POPUP_OFFSET)),
                    cache);

                initialized = true;
            }

            base.InitializePhysics(false, engineRegistrations);
        }