Example #1
0
        public Structure(Rectangle rectangle, StructurePrefab sp, Submarine submarine)
            : base(sp, submarine)
        {
            if (rectangle.Width == 0 || rectangle.Height == 0)
            {
                return;
            }
            System.Diagnostics.Debug.Assert(rectangle.Width > 0 && rectangle.Height > 0);

            rect   = rectangle;
            prefab = sp;

            isHorizontal = (rect.Width > rect.Height);

            StairDirection = prefab.StairDirection;

            if (prefab.HasBody)
            {
                bodies = new List <Body>();
                //gaps = new List<Gap>();

                Body newBody = BodyFactory.CreateRectangle(GameMain.World,
                                                           ConvertUnits.ToSimUnits(rect.Width),
                                                           ConvertUnits.ToSimUnits(rect.Height),
                                                           1.5f);
                newBody.BodyType = BodyType.Static;
                newBody.Position = ConvertUnits.ToSimUnits(new Vector2(rect.X + rect.Width / 2.0f, rect.Y - rect.Height / 2.0f));
                newBody.Friction = 0.5f;

                newBody.OnCollision += OnWallCollision;

                newBody.UserData = this;

                newBody.CollisionCategories = (prefab.IsPlatform) ? Physics.CollisionPlatform : Physics.CollisionWall;

                bodies.Add(newBody);

                WallList.Add(this);

                CreateSections();
            }
            else
            {
                sections    = new WallSection[1];
                sections[0] = new WallSection(rect);

                if (StairDirection != Direction.None)
                {
                    CreateStairBodies();
                }
            }

            if (prefab.CastShadow)
            {
                GenerateConvexHull();
            }

            InsertToList();
        }
Example #2
0
        private void CreateSections()
        {
            int xsections = 1, ysections = 1;
            int width = rect.Width, height = rect.Height;

            if (!HasBody)
            {
                if (flippedX && isHorizontal)
                {
                    xsections = (int)Math.Ceiling((float)rect.Width / prefab.sprite.SourceRect.Width);
                    width     = prefab.sprite.SourceRect.Width;
                }
                else
                {
                    xsections = 1;
                    ysections = 1;
                }
                sections = new WallSection[xsections];
            }
            else
            {
                if (isHorizontal)
                {
                    xsections = (int)Math.Ceiling((float)rect.Width / wallSectionSize);
                    sections  = new WallSection[xsections];
                    width     = (int)wallSectionSize;
                }
                else
                {
                    ysections = (int)Math.Ceiling((float)rect.Height / wallSectionSize);
                    sections  = new WallSection[ysections];
                    height    = (int)wallSectionSize;
                }
            }

            for (int x = 0; x < xsections; x++)
            {
                for (int y = 0; y < ysections; y++)
                {
                    if (flippedX)
                    {
                        Rectangle sectionRect = new Rectangle(rect.Right - (x + 1) * width, rect.Y - y * height, width, height);

                        int over = Math.Max(rect.X - sectionRect.X, 0);

                        sectionRect.X     += over;
                        sectionRect.Width -= over;

                        sectionRect.Height -= (int)Math.Max((rect.Y - rect.Height) - (sectionRect.Y - sectionRect.Height), 0.0f);

                        sections[xsections - 1 - x + y] = new WallSection(sectionRect);
                    }

                    else
                    {
                        Rectangle sectionRect = new Rectangle(rect.X + x * width, rect.Y - y * height, width, height);
                        sectionRect.Width  -= (int)Math.Max(sectionRect.Right - rect.Right, 0.0f);
                        sectionRect.Height -= (int)Math.Max((rect.Y - rect.Height) - (sectionRect.Y - sectionRect.Height), 0.0f);

                        sections[x + y] = new WallSection(sectionRect);
                    }
                }
            }
        }
Example #3
0
        private void CreateSections()
        {
            int xsections = 1, ysections = 1;
            int width = rect.Width, height = rect.Height;

            if (!HasBody)
            {
                if (FlippedX && IsHorizontal)
                {
                    xsections = (int)Math.Ceiling((float)rect.Width / prefab.sprite.SourceRect.Width);
                    width     = prefab.sprite.SourceRect.Width;
                }
                else if (FlippedY && !IsHorizontal)
                {
                    ysections = (int)Math.Ceiling((float)rect.Height / prefab.sprite.SourceRect.Height);
                    width     = prefab.sprite.SourceRect.Height;
                }
                else
                {
                    xsections = 1;
                    ysections = 1;
                }
                Sections = new WallSection[xsections];
            }
            else
            {
                if (IsHorizontal)
                {
                    xsections = (int)Math.Ceiling((float)rect.Width / WallSectionSize);
                    Sections  = new WallSection[xsections];
                    width     = (int)WallSectionSize;
                }
                else
                {
                    ysections = (int)Math.Ceiling((float)rect.Height / WallSectionSize);
                    Sections  = new WallSection[ysections];
                    height    = (int)WallSectionSize;
                }
            }

            for (int x = 0; x < xsections; x++)
            {
                for (int y = 0; y < ysections; y++)
                {
                    if (FlippedX || FlippedY)
                    {
                        Rectangle sectionRect = new Rectangle(
                            FlippedX ? rect.Right - (x + 1) * width : rect.X + x * width,
                            FlippedY ? rect.Y - rect.Height + (y + 1) * height : rect.Y - y * height,
                            width, height);

                        if (FlippedX)
                        {
                            int over = Math.Max(rect.X - sectionRect.X, 0);
                            sectionRect.X     += over;
                            sectionRect.Width -= over;
                        }
                        else
                        {
                            sectionRect.Width -= (int)Math.Max(sectionRect.Right - rect.Right, 0.0f);
                        }
                        if (FlippedY)
                        {
                            int over = Math.Max(sectionRect.Y - rect.Y, 0);
                            sectionRect.Y      -= over;
                            sectionRect.Height -= over;
                        }
                        else
                        {
                            sectionRect.Height -= (int)Math.Max((rect.Y - rect.Height) - (sectionRect.Y - sectionRect.Height), 0.0f);
                        }

                        //sectionRect.Height -= (int)Math.Max((rect.Y - rect.Height) - (sectionRect.Y - sectionRect.Height), 0.0f);
                        int xIndex = FlippedX && IsHorizontal ? (xsections - 1 - x) : x;
                        int yIndex = FlippedY && !IsHorizontal ? (ysections - 1 - y) : y;
                        Sections[xIndex + yIndex] = new WallSection(sectionRect);
                    }
                    else
                    {
                        Rectangle sectionRect = new Rectangle(rect.X + x * width, rect.Y - y * height, width, height);
                        sectionRect.Width  -= (int)Math.Max(sectionRect.Right - rect.Right, 0.0f);
                        sectionRect.Height -= (int)Math.Max((rect.Y - rect.Height) - (sectionRect.Y - sectionRect.Height), 0.0f);

                        Sections[x + y] = new WallSection(sectionRect);
                    }
                }
            }
        }
Example #4
0
        public Structure(Rectangle rectangle, StructurePrefab sp, Submarine submarine)
            : base(sp, submarine)
        {
            if (rectangle.Width == 0 || rectangle.Height == 0)
            {
                return;
            }
            System.Diagnostics.Debug.Assert(rectangle.Width > 0 && rectangle.Height > 0);

            rect = rectangle;
#if CLIENT
            TextureScale = sp.TextureScale;
#endif
            spriteColor = prefab.SpriteColor;
            if (ResizeHorizontal && !ResizeVertical)
            {
                IsHorizontal = true;
            }
            else if (ResizeVertical && !ResizeHorizontal)
            {
                IsHorizontal = false;
            }
            else
            {
                if (BodyWidth > 0.0f && BodyHeight > 0.0f)
                {
                    IsHorizontal = BodyWidth > BodyHeight;
                }
                else
                {
                    IsHorizontal = (rect.Width > rect.Height);
                }
            }

            StairDirection         = Prefab.StairDirection;
            SerializableProperties = SerializableProperty.GetProperties(this);

            InitProjSpecific();

            if (Prefab.Body)
            {
                Bodies = new List <Body>();
                WallList.Add(this);

                CreateSections();
                UpdateSections();
            }
            else
            {
                Sections    = new WallSection[1];
                Sections[0] = new WallSection(rect);

                if (StairDirection != Direction.None)
                {
                    CreateStairBodies();
                }
            }

            // Only add ai targets automatically to walls
            if (aiTarget == null && HasBody && Tags.Contains("wall"))
            {
                aiTarget = new AITarget(this);
            }

            InsertToList();
        }