コード例 #1
0
        private void onClothTest(int groupId, CmdArgs args)
        {
            string arg = args.PopWord();

            if (arg == "clear")
            {
                clothSystems.Clear();
                nextClothId = 1;
                return;
            }

            float xsize = 0.5f + (float)capi.World.Rand.NextDouble() * 3;
            float zsize = 0.5f + (float)capi.World.Rand.NextDouble() * 3;

            if (arg == "cloth")
            {
                ClothSystem sys = ClothSystem.CreateCloth(capi, this, capi.World.Player.Entity.Pos.AsBlockPos.Add(-1, 2, -1), xsize, zsize);
                RegisterCloth(sys);



                EntityAgent byEntity = capi.World.Player.Entity;
                Vec3d       pos      = byEntity.ServerPos.XYZ.Add(0, byEntity.LocalEyePos.Y, 0);
                Vec3f       leftPos  = pos.AheadCopy(0.5, byEntity.SidedPos.Pitch, byEntity.SidedPos.Yaw + GameMath.PIHALF).ToVec3f();
                Vec3f       rightPos = pos.AheadCopy(0.5, byEntity.SidedPos.Pitch, byEntity.SidedPos.Yaw + GameMath.PIHALF).ToVec3f();

                // pin the top right and top left.
                //sys.Points[0][0].PinTo(byEntity, leftPos);
                //sys.Points[0][numxpoints - 1].PinTo(byEntity, rightPos);
            }

            if (arg == "rope")
            {
                xsize = 5;
                ClothSystem sys = ClothSystem.CreateRope(capi, this, capi.World.Player.Entity.Pos.AsBlockPos.Add(-1, 2, -1), xsize, null);
                RegisterCloth(sys);


                EntityAgent byEntity = capi.World.Player.Entity;
                Vec3d       pos      = new Vec3d(0, byEntity.LocalEyePos.Y, 0);
                Vec3d       aheadPos = pos.AheadCopy(1, byEntity.SidedPos.Pitch, byEntity.SidedPos.Yaw);

                sys.WalkPoints(p => p.Pos.Add(pos.X, pos.Y + p.PointIndex / 100f, pos.Z));

                sys.FirstPoint.PinTo(byEntity, aheadPos.ToVec3f());
            }
        }
コード例 #2
0
        public override void OnHeldInteractStart(ItemSlot slot, EntityAgent byEntity, BlockSelection blockSel, EntitySelection entitySel, bool firstEvent, ref EnumHandHandling handling)
        {
            // Disabled outside of creative mode because its too broken
            if ((byEntity as EntityPlayer)?.Player?.WorldData.CurrentGameMode != EnumGameMode.Creative)
            {
                return;
            }

            // sneak = attach
            // non-sneak = detach

            int clothId = slot.Itemstack.Attributes.GetInt("clothId");

            handling = EnumHandHandling.PreventDefault;
            ClothSystem sys = null;


            if (clothId != 0)
            {
                sys = cm.GetClothSystem(clothId);
                if (sys == null)
                {
                    clothId = 0;
                }
            }


            // Detach
            if (!byEntity.Controls.Sneak)
            {
                if (clothId != 0)
                {
                    //Console.WriteLine(api.World.Side + ", clothid {0}, detach", clothId);

                    detach(sys, slot, byEntity, entitySel?.Entity, blockSel?.Position);
                }

                return;
            }


            // Attach
            if (clothId == 0)
            {
                float xsize = 2;

                sys = ClothSystem.CreateRope(api, cm, byEntity.Pos.AsBlockPos.Add(0, 1, 0), xsize, null);

                Vec3d     lpos     = new Vec3d(0, byEntity.LocalEyePos.Y - 0.3f, 0);
                Vec3d     aheadPos = lpos.AheadCopy(0.1f, byEntity.SidedPos.Pitch, byEntity.SidedPos.Yaw).AheadCopy(0.4f, byEntity.SidedPos.Pitch, byEntity.SidedPos.Yaw - GameMath.PIHALF);
                EntityPos pos      = byEntity.SidedPos;

                //Console.WriteLine(api.World.Side + ", clothid {0}, new cloth. attach to self", sys.ClothId);

                sys.FirstPoint.PinTo(byEntity, aheadPos.ToVec3f());

                cm.RegisterCloth(sys);

                slot.Itemstack.Attributes.SetLong("ropeHeldByEntityId", byEntity.EntityId);
                slot.Itemstack.Attributes.SetInt("clothId", sys.ClothId);
                slot.MarkDirty();
            }


            ClothPoint[] pEnds = sys.Ends;

            if (blockSel != null)
            {
                Block block = api.World.BlockAccessor.GetBlock(blockSel.Position);

                if (blockSel.Position.Equals(pEnds[0].PinnedToBlockPos) || blockSel.Position.Equals(pEnds[1].PinnedToBlockPos))
                {
                    //Console.WriteLine(api.World.Side + ", clothid {0}, detach from block", sys.ClothId);

                    detach(sys, slot, byEntity, null, blockSel.Position);
                    return;
                }


                if (block.HasBehavior <BlockBehaviorRopeTieable>())
                {
                    //Console.WriteLine(api.World.Side + ", clothid {0}, attach to block", sys.ClothId);

                    attachToBlock(byEntity, blockSel.Position, sys, slot);
                }
            }

            if (entitySel != null)
            {
                if (entitySel.Entity.EntityId == pEnds[0].PinnedToEntity?.EntityId || entitySel.Entity.EntityId == pEnds[1].PinnedToEntity?.EntityId)
                {
                    //Console.WriteLine(api.World.Side + ", clothid {0}, detach from entity", sys.ClothId);
                    detach(sys, slot, byEntity, entitySel.Entity, null);
                    return;
                }

                //Console.WriteLine(api.World.Side + ", clothid {0}, attach to entity", sys.ClothId);
                attachToEntity(byEntity, entitySel.Entity, sys, slot);
            }


            if (clothId == 0)
            {
                sys.WalkPoints(p => p.update(0));

                Vec3d startPos = sys.FirstPoint.Pos;
                Vec3d endPos   = sys.LastPoint.Pos;

                double dx = endPos.X - startPos.X;
                double dy = endPos.Y - startPos.Y;
                double dz = endPos.Z - startPos.Z;

                sys.WalkPoints(p => {
                    float f = p.PointIndex / (float)sys.Length;

                    if (!p.Pinned)
                    {
                        p.Pos.Set(startPos.X + dx * f, startPos.Y + dy * f, startPos.Z + dz * f);
                    }
                });

                sys.setRenderCenterPos();
            }


            // No longer pinned to ourselves
            if (pEnds[0].PinnedToEntity?.EntityId != byEntity.EntityId && pEnds[1].PinnedToEntity?.EntityId != byEntity.EntityId)
            {
                //Console.WriteLine(api.World.Side + ", clothid {0}, rope assigned. removed one from inv.", sys.ClothId);

                slot.Itemstack.Attributes.RemoveAttribute("clothId");
                slot.TakeOut(1);
                slot.MarkDirty();
            }
        }