private void PushBlock()
    {
        if (rigidbody.velocity.y != 0.0f)
        {
            return;
        }

        animator.SetBool(PushingBlock, (state & (int)PlayerState.PUSHING_BLOCK) != 0);
        var dirRay = Vec3f.right * (transform.localScale.x * 1.0f);
        var hit    = Physics2D.Raycast(transform.position, dirRay, grabMaxRange, blockLayer);

        if (hit)
        {
            if (Input.GetButtonDown("Interact"))
            {
                heldBlock = new PushableBlock(hit.collider.gameObject);

                var blockSize  = heldBlock.Collider.size;
                var playerSize = GetComponent <BoxCollider2D>().size;
                if (heldBlock.Transform.position.x - transform.position.x > 0)
                {
                    transform.position = new Vec2f(
                        heldBlock.Transform.position.x - blockSize.x / 2.0f - playerSize.x / 2.0f,
                        transform.position.y);
                }
                else
                {
                    transform.position = new Vec2f(
                        heldBlock.Transform.position.x + blockSize.x / 2.0f + playerSize.x / 2.0f,
                        transform.position.y);
                }
                rigidbody.velocity = Vec2f.zero;

                state = 0;
                return;
            }

            if (hit && Input.GetButton("Interact"))
            {
                heldBlock.Rigidbody.mass      = 1.0f;
                heldBlock.Joint.enabled       = true;
                heldBlock.Joint.connectedBody = rigidbody;

                state = (int)PlayerState.PUSHING_BLOCK | (int)PlayerState.CAN_MOVE;
                return;
            }
        }

        if (heldBlock != null)
        {
            rigidbody.velocity = Vec2f.zero;

            heldBlock.Rigidbody.mass     = 1000.0f;
            heldBlock.Rigidbody.velocity = Vec2f.zero;
            heldBlock.Joint.enabled      = false;

            heldBlock = null;
            state     = (int)PlayerState.CAN_MOVE;
        }
    }
    public void TryPush(bool pushingRight, bool overridePushTime = false)
    {
        if (CanPush)
        {
            _pushTimer -= Time.deltaTime;
            if (_pushTimer <= 0 || overridePushTime)
            {
                Vector2 direction = Vector2.right;
                if (!pushingRight)
                {
                    direction = Vector2.left;
                }

                if (!BoxcastHelper.BoxcastInDirection(direction, 1, GroundLayers))
                {
                    StartCoroutine(Push(direction.x));
                    //check for boxes on top of this one and push them too if we can
                    RaycastHit2D result;
                    if (BoxcastHelper.GetFirstBoxcastHit(Vector2.up, 1, GroundLayers, out result))
                    {
                        PushableBlock pb = result.transform.GetComponent <PushableBlock>();
                        if (pb != null)
                        {
                            pb.TryPush(pushingRight, true);
                        }
                    }
                }
            }
        }
    }
Exemple #3
0
        public void Handle(ICollidable mover, ICollidable target, CollisionDirection.DirectionTag direction)
        {
            if (typeof(PushableBlock).IsInstanceOfType(mover) && typeof(PushableBlock).IsInstanceOfType(target))
            {
                item1 = (PushableBlock)mover;
                item2 = (PushableBlock)target;
                switch (direction)
                {
                case CollisionDirection.DirectionTag.Top:
                    item1.Physics.yPosition = item2.GetTopSide() - item1.GetHeight();
                    item1.Ground();
                    break;

                case CollisionDirection.DirectionTag.Bottom:
                    item1.Physics.yPosition = item2.GetBottomSide() + 1;
                    break;

                case CollisionDirection.DirectionTag.Left:
                    SwapXVelocity(item1, item2);
                    item1.Physics.xPosition = item2.GetLeftSide() - item1.GetWidth();
                    break;

                case CollisionDirection.DirectionTag.Right:
                    SwapXVelocity(item1, item2);
                    item1.Physics.xPosition = item2.GetRightSide() + 1;
                    break;

                default:
                    Console.WriteLine(Config.GetCollisionResolutionException() + GetType().ToString());
                    break;
                }
            }
        }
    // ================================================================

    public override void _Ready()
    {
        partner = GetNode <PushableBlock>(partnerBlock);

        player    = GetTree().GetRoot().GetNode <Spatial>("Scene").GetNode <Player>("Player");
        timerPush = GetNode <Timer>("TimerPush");
    }
Exemple #5
0
 private void OnCollisionExit2D(Collision2D collision)
 {
     if (collision.gameObject.tag == "PushBlock")
     {
         PushableBlock pb = collision.gameObject.GetComponent <PushableBlock>();
         pb.ResetPushTimer();
     }
 }
Exemple #6
0
 private void SpawnBlock(Vector2 position)
 {
     if (LastSpawnedBlock == null)
     {
         LastSpawnedBlock = Instantiate(BlockPrefab);
     }
     LastSpawnedBlock.transform.position = position;
     LastSpawnedBlock.CheckGround();
 }
Exemple #7
0
        void addBlock(String line)
        {
            String[] split = line.Split(',');
            IObject  block;
            //Debug.WriteLine(split[0]);
            float x = ((Int32.Parse(split[1]) - 1) * blockBaseDimension * blockSizeMod) + screenX + (2 * blockBaseDimension * blockSizeMod);
            float y = ((Int32.Parse(split[2]) - 1) * blockBaseDimension * blockSizeMod) + screenY + (2 * blockBaseDimension * blockSizeMod);

            switch (split[0])
            {
            case "bluesandblock":
                block = new BlueSandBlock(sprites["tileset"], new Vector2(x, y));
                break;

            case "dragonblock":
                block = new DragonBlock(sprites["tileset"], new Vector2(x, y));
                break;

            case "dungeonblock":
                block = new DungeonBlock(sprites["tileset"], new Vector2(x, y));
                break;

            case "pushableblock":
                block = new PushableBlock(sprites["tileset"], new Vector2(x, y), split[3]);
                break;

            case "stairsblock":
                block = new StairsBlock(sprites["tileset"], new Vector2(x, y));
                break;

            case "fireblock":
                block = new FireBlock(sprites["fire"], new Vector2(x + 8, y));
                break;

            case "fishblock":
                block = new FishBlock(sprites["tileset"], new Vector2(x, y));
                break;

            case "solidblock":
                Color c = Color.Transparent;
                if (split[3] == "blue")
                {
                    c = Color.Blue;
                }
                block = new SolidBlock(sprites["Backgrounds"], new Vector2(x, y), c);
                break;

            default:
                block = new DungeonBlock(sprites["tileset"], new Vector2(x, y));
                break;
            }
            Blocks.Add(block);
        }
Exemple #8
0
 private void OnCollisionStay2D(Collision2D collision)
 {
     if (collision.gameObject.tag == "PushBlock")
     {
         if (OnGround && Mathf.Abs(transform.position.y - collision.transform.position.y) < .5f && CanPush)
         {
             float         xInput = _controls.GetAxis(ActionNames.HorizontalMovement);
             PushableBlock pb     = collision.gameObject.GetComponent <PushableBlock>();
             if (transform.position.x < collision.transform.position.x && xInput > 0)
             {
                 pb.TryPush(true);
             }
             else if (transform.position.x > collision.transform.position.x && xInput < 0)
             {
                 pb.TryPush(false);
             }
         }
     }
 }
 public void SetParent(GameObject _parent)
 {
     parent = _parent;
     pb = parent.GetComponent<PushableBlock>();
 }
Exemple #10
0
 public void SetParent(GameObject _parent)
 {
     parent = _parent;
     pb     = parent.GetComponent <PushableBlock>();
 }