예제 #1
0
        public static void AddCollider(Unit unit, UnitData unitData, bool isSensor)
        {
            PBaseColliderHelper pBaseColliderHelper = unit.GameObject.GetComponentInChildren <PBaseColliderHelper>();
            PBaseShape          pBaseShape          = null;


            switch (pBaseColliderHelper)
            {
            case PBoxColliderHelper pBoxCollider:
                PBoxShape pBoxShape = new PBoxShape()
                {
                    eulerAnglesY = pBoxCollider.transform.eulerAngles.y,
                    offset       = pBoxCollider.offset,
                    size         = pBoxCollider.size,
                    bodyType     = pBoxCollider.bodyType,
                    unitData     = unitData,
                    isSensor     = isSensor
                };

                pBaseShape = pBoxShape;
                break;

            case PCircleColliderHelper pCircleCollider:
                PCircleShape pCircleShape = new PCircleShape()
                {
                    eulerAnglesY = pCircleCollider.transform.eulerAngles.y,
                    offset       = pCircleCollider.offset,
                    radius       = pCircleCollider.radius,
                    halfHeight   = pCircleCollider.height,
                    bodyType     = pCircleCollider.bodyType,
                    unitData     = unitData,
                    isSensor     = isSensor
                };
                pBaseShape = pCircleShape;
                break;
            }
            var bodyCom = unit.AddComponent <P2DBodyComponent, PBaseShape>(pBaseShape);

            pBaseColliderHelper.bodyComponent = bodyCom;
        }
예제 #2
0
        public void Awake(PBaseShape pShape)
        {
            baseShape = pShape;
            Shape shape = null;
            Unit  unit  = GetParent <Unit>();

            unit.UnitData = pShape.unitData;
            unit.OffsetY  = pShape.offset.y;

            switch (pShape)
            {
            case PBoxShape boxShape:
                //根据配置的信息,先设置Unit的一堆属性

                unit.HalfHeight = boxShape.size.y / 2;


                //接着设置形状的数据
                var box = new PolygonShape();
                box.SetAsBox(boxShape.size.x, boxShape.size.z, pShape.offset.ToVector2(), BOX2DMathfUtils.ToAngle(boxShape.eulerAnglesY));
                shape = box;
                break;

            case PCircleShape pCircleShape:
                unit.HalfHeight = pCircleShape.halfHeight;

                var circle = new CircleShape();
                circle.Radius   = pCircleShape.radius;
                circle.Position = pShape.offset.ToVector2();
                shape           = circle;
                break;
            }

            World world = PhysicWorldComponent.Instance.world;

            var bd = new BodyDef
            {
                Position      = GetParent <Unit>().Position.ToVector2(),
                BodyType      = pShape.bodyType,
                FixedRotation = true,
            };

            if (bd.BodyType != BodyType.StaticBody)
            {
                bd.AllowSleep = false;
            }
            else
            {
                bd.AllowSleep = true;
            }

            body = world.CreateBody(bd);

            // Log.Debug(GetParent<Unit>().GameObject.name + " 在2D物理世界的位置是" + body.GetPosition());

            var fd = new FixtureDef
            {
                Shape    = shape,
                Density  = 20.0f,
                UserData = GetParent <Unit>(),
                Friction = 0,
                IsSensor = pShape.isSensor,
                Filter   = new Filter()
                {
                    CategoryBits = (ushort)GetParent <Unit>().UnitData.unitLayer,
                    MaskBits     = (ushort)GetParent <Unit>().UnitData.layerMask,
                    GroupIndex   = (short)GetParent <Unit>().UnitData.groupIndex,
                }
            };


            fixture = body.CreateFixture(fd);
            //Log.Debug(fixture.GetAABB(0).LowerBound.ToString() + fixture.GetAABB(0).UpperBound.ToString());

            GetParent <Unit>().OnPositionUpdate += UpdatePos;
            GetParent <Unit>().OnRotationUpdate += UpdateRot;
        }