Пример #1
0
        public override void prePhysicsUpdate(GameTime time)
        {
            base.prePhysicsUpdate(time);

            if (!isAnchor)
            {
                if (pin != null && Vector2.Distance(location, pin.location) > Chunk.tileDrawWidth * .3f)
                {
                    if (pin is Player)
                    {
                        Player    player    = (Player)pin;
                        Item_Rope item_rope = (Item_Rope)player.inventory.getItemOfType(new Item_Rope(1));
                        if (item_rope != null)
                        {
                            float maxRopeLength = item_rope.uses * Chunk.tileDrawWidth * 1.1f;
                            if (Vector2.Distance(location, hook.location) > maxRopeLength)
                            {
                                this.isAnchor = true;
                            }
                            else
                            {
                                EntityRopeSegment ropeSegment = new EntityRopeSegment(pin.location, world, this, pin, hook);
                                pin   = null;
                                child = ropeSegment;
                                world.addEntity(ropeSegment);
                            }
                        }
                    }
                    else
                    {
                        EntityRopeSegment ropeSegment = new EntityRopeSegment(pin.location, world, this, pin, hook);
                        pin   = null;
                        child = ropeSegment;
                        world.addEntity(ropeSegment);
                    }
                }

                impulse += (parent.location - location) * .02f;
                if (child != null)
                {
                    impulse += (child.location - location) * .02f;
                }
            }
            else
            {
                velocity = new Vector2();
            }
        }
        public override void prePhysicsUpdate(GameTime time)
        {
            base.prePhysicsUpdate(time);

            if (collideBottom || collideLeft || collideRight || collideTop)
            {
                if (!anchored)
                {
                    pinLocation = location;
                    anchored    = true;
                    EntityRopeSegment endRopeSegment = (EntityRopeSegment)child;
                    while (endRopeSegment != null && endRopeSegment.child != null)
                    {
                        endRopeSegment = endRopeSegment.child;
                    }

                    if (endRopeSegment != null)
                    {
                        endRopeSegment.isAnchor = true;


                        Player    player    = (Player)parent;
                        Item_Rope item_rope = (Item_Rope)player.inventory.getItemOfType(new Item_Rope(1));
                        if (item_rope != null)
                        {
                            int theoreticallyConsumedRopes = (int)(Vector2.Distance(location, endRopeSegment.location) / (Chunk.tileDrawWidth * 1.1f));
                            int consumedRopes = Math.Min(item_rope.uses, theoreticallyConsumedRopes); // ensure that the number of ropes consumed does not exceed the number of ropes the player has.
                            player.inventory.consume(item_rope, consumedRopes);
                        }
                        else
                        {
                            Console.WriteLine("grappling hook error: the player does not have rope!");
                        }
                    }


                    /*Player player = (Player)pin;
                     * Item_Rope item_rope = (Item_Rope)player.inventory.getItemOfType(new Item_Rope(1));
                     * float maxRopeLength = item_rope.uses * Chunk.tileDrawWidth * 1.1f;
                     * if (Vector2.Distance(location, hook.location) > maxRopeLength)
                     * {
                     * this.isAnchor = true;
                     * }
                     * if (item_rope != null)
                     * {
                     * player.inventory.consume(item_rope, 1);
                     *
                     * EntityRopeSegment ropeSegment = new EntityRopeSegment(pin.location, world, this, pin, hook);
                     * pin = null;
                     * child = ropeSegment;
                     * world.addEntity(ropeSegment);
                     * }else
                     * {
                     * this.isAnchor = true;
                     * }*/
                }
            }

            if (anchored)
            {
                location = pinLocation; velocity = new Vector2();
            }
            else if (child != null)
            {
                if (parent is Player && ((Player)parent).inventory.getItemOfType(new Item_Rope(1)) == null)
                {
                    impulse += (child.location - location) * .01f;
                }
                else
                {
                    impulse += (child.location - location) * .001f;
                }
            }

            if (pin != null && Vector2.Distance(pin.location, location) > Chunk.tileDrawWidth)
            {
                if (parent is Player && pin != null)
                {
                    EntityRopeSegment ropeSegment = new EntityRopeSegment(location, world, this, pin, this);
                    this.pin   = null;
                    this.child = ropeSegment;
                    world.addEntity(ropeSegment);
                }
                else
                {
                    EntityRopeSegment ropeSegment = new EntityRopeSegment(location, world, this, pin, this);
                    this.pin   = null;
                    this.child = ropeSegment;
                    world.addEntity(ropeSegment);
                }
            }
        }