Пример #1
0
        public float CalculateXOffset(AxisAlignedBb other, float offset)
        {
            //x
            if (other.Max.Y > Min.Y && other.Min.Y < Max.Y && other.Max.Z > Min.Z && other.Min.Z < Max.Z)
            {
                if (offset > 0.0D && other.Max.X <= Min.X)
                {
                    float d1 = Min.X - other.Max.X;

                    if (d1 < offset)
                    {
                        offset = d1;
                    }
                }
                else if (offset < 0.0D && other.Min.X >= Max.X)
                {
                    float d0 = Max.X - other.Min.X;

                    if (d0 > offset)
                    {
                        offset = d0;
                    }
                }
            }

            return(offset);
        }
Пример #2
0
        public virtual void Move()
        {
            AxisAlignedBb bbO = BoundingBox.Union(BoundingBox.Offset(Motion));

            List <AxisAlignedBb> list = SharpCraft.Instance.World.GetBlockCollisionBoxes(bbO);

            Vector3 mOrig = Motion;

            for (int i = 0; i < list.Count; i++)
            {
                AxisAlignedBb blockBb = list[i];
                Motion.Y = blockBb.CalculateYOffset(BoundingBox, Motion.Y);
            }
            BoundingBox = BoundingBox.Offset(Motion * Vector3.UnitY);

            for (int i = 0; i < list.Count; i++)
            {
                AxisAlignedBb blockBb = list[i];
                Motion.X = blockBb.CalculateXOffset(BoundingBox, Motion.X);
            }
            BoundingBox = BoundingBox.Offset(Motion * Vector3.UnitX);

            for (int i = 0; i < list.Count; i++)
            {
                AxisAlignedBb blockBb = list[i];
                Motion.Z = blockBb.CalculateZOffset(BoundingBox, Motion.Z);
            }
            BoundingBox = BoundingBox.Offset(Motion * Vector3.UnitZ);

            SetPositionToBb();

            bool stoppedX = Math.Abs(mOrig.X - Motion.X) > 0.00001f;
            bool stoppedY = Math.Abs(mOrig.Y - Motion.Y) > 0.00001f;
            bool stoppedZ = Math.Abs(mOrig.Z - Motion.Z) > 0.00001f;

            OnGround = stoppedY && mOrig.Y < 0.0D;

            bool onCeiling = stoppedY && mOrig.Y > 0.0D;

            if (stoppedX)
            {
                Motion.X = 0;
            }

            if (stoppedZ)
            {
                Motion.Z = 0;
            }

            if (onCeiling)
            {
                Motion.Y *= 0.15f;
            }
            else if (OnGround)
            {
                Motion.Y = 0;
            }
        }
Пример #3
0
        protected Entity(World world, Vector3 pos, Vector3 motion = new Vector3())
        {
            World  = world;
            Pos    = LastPos = pos;
            Motion = motion;

            CollisionBoundingBox = AxisAlignedBb.BlockFull;
            BoundingBox          = CollisionBoundingBox.Offset(pos - CollisionBoundingBox.Size / 2);
        }
Пример #4
0
        public AxisAlignedBb Union(AxisAlignedBb other)
        {
            int minX = (int)Math.Floor(MathUtil.Min(Min.X, Max.X, other.Min.X, other.Max.X));
            int minY = (int)Math.Floor(MathUtil.Min(Min.Y, Max.Y, other.Min.Y, other.Max.Y));
            int minZ = (int)Math.Floor(MathUtil.Min(Min.Z, Max.Z, other.Min.Z, other.Max.Z));

            int maxX = (int)Math.Ceiling(MathUtil.Max(Min.X, Max.X, other.Min.X, other.Max.X));
            int maxY = (int)Math.Ceiling(MathUtil.Max(Min.Y, Max.Y, other.Min.Y, other.Max.Y));
            int maxZ = (int)Math.Ceiling(MathUtil.Max(Min.Z, Max.Z, other.Min.Z, other.Max.Z));

            return(new AxisAlignedBb(minX, minY, minZ, maxX, maxY, maxZ));
        }
Пример #5
0
        public EntityItem(World world, Vector3 pos, Vector3 motion, ItemStack stack, bool noDelay = false) : base(world, pos, motion)
        {
            if (noDelay)
            {
                _entityAge = 14;
            }

            _stack = stack;

            CollisionBoundingBox = new AxisAlignedBb(0.25f);
            BoundingBox          = CollisionBoundingBox.Offset(pos - Vector3.One * CollisionBoundingBox.Size / 2);

            Gravity = 1.25f;

            IsAlive = stack != null && !stack.IsEmpty;
        }
Пример #6
0
        public virtual void Update()
        {
            LastPos = Pos;

            Motion.Y -= 0.045f * Gravity;

            Vector3 motion = Motion;

            motion.Y = 0;

            if (OnGround && Motion.Xz.Length > 0.0001f)
            {
                AxisAlignedBb bbO = BoundingBox.Union(BoundingBox.Offset(motion));

                var list = SharpCraft.Instance.World.GetBlockCollisionBoxes(bbO).OrderBy(box => (box.Min - new BlockPos(box.Min).ToVec() + box.Size).Y);

                foreach (var bb in list)
                {
                    var blockPos = new BlockPos(bb.Min);
                    var bbTop    = bb.Min + bb.Size;
                    var b        = bbTop - blockPos.ToVec();

                    var step = bbTop.Y - Pos.Y;

                    if (step <= StepHeight && step > 0)
                    {
                        Motion.Y = 0;
                        Pos.Y    = blockPos.Y + b.Y;

                        TeleportTo(Pos);
                    }
                }
            }

            Move();

            Motion.Xz *= 0.864021f;

            if (OnGround)
            {
                Motion.Xz *= 0.6676801f;
            }
        }
Пример #7
0
        public void TeleportTo(Vector3 pos)
        {
            Pos = LastPos = pos;

            BoundingBox = CollisionBoundingBox.Offset(pos - Vector3.UnitX * CollisionBoundingBox.Size.X / 2 - Vector3.UnitZ * CollisionBoundingBox.Size.Z / 2);
        }
Пример #8
0
 public bool IntersectsWith(AxisAlignedBb other)
 {
     return(Min.X < other.Max.X && Max.X > other.Min.X &&
            Min.Y < other.Max.Y && Max.Y > other.Min.Y &&
            Min.Z < other.Max.Z && Max.Z > other.Min.Z);
 }