Beispiel #1
0
    void AcceptCup(GameObject cup)
    {
        Cuppa           coffee = new Cuppa();
        LayerMask       layer  = 1 << 8;
        ContactFilter2D filter = new ContactFilter2D();

        filter.layerMask = layer;
        BoxCollider2D     box       = cup.GetComponent <BoxCollider2D>();
        List <Collider2D> colliders = new List <Collider2D>();

        transform.localScale = new Vector3(4, 4, 4);
        box.OverlapCollider(filter, colliders);
        foreach (Collider2D col in colliders)
        {
            if (col.GetComponent <CoffeeQuality>() != null)
            {
                CoffeeQuality quality = col.GetComponent <CoffeeQuality>();
                coffee.bitterness    += quality.bitterness;
                coffee.contamination += quality.contamination;
                coffee.quantity      += 1;
            }
            if (col.gameObject.layer == 8 || col.tag == "Cup")
            {
                Destroy(col.gameObject);
            }
        }
        coffee.bitterness    = coffee.bitterness / coffee.quantity;
        coffee.contamination = coffee.contamination / coffee.quantity;
        CheckCoffee(coffee);
        Debug.Log("bitterness " + coffee.bitterness);
        Debug.Log("Quantity " + coffee.quantity);
        transform.localScale = new Vector3(1, 1, 1);
    }
Beispiel #2
0
    public void OnEndDrag()
    {
        List <Collider2D> overlappingColliders = new List <Collider2D>();

        boxCollider.OverlapCollider(new ContactFilter2D(), overlappingColliders);

        // Ignore the invalid targets that overlap with the card.
        if (overlappingColliders.ContainsComponent(out Dictionary <Collider2D, ICardEndDragTarget> validTargets))
        {
            for (int i = validTargets.Count - 1; i >= 0; i--)
            {
                KeyValuePair <Collider2D, ICardEndDragTarget> pair = validTargets.ElementAt(i);
                if (!pair.Value.CardCanBePutOnHere(this))
                {
                    validTargets.Remove(pair.Key);
                }
            }
        }

        // If there is no valid target overlapping with the card, reset the card...
        if (validTargets.Count == 0)
        {
            transform.position = positionOnStartDrag;
            foreach (Card card in GetComponentsInChildren <Card>())
            {
                card.SortingOrder -= 100;
            }
            return;
        }

        // ...otherwise, move the card to the card pile of the closest valid target.
        Move(validTargets.GetClosest(transform.position).Value.CardPile, true, true, true);
    }
Beispiel #3
0
    // Update is called once per frame
    void Update()
    {
        Collider2D[]    r = new Collider2D[10];
        ContactFilter2D f = new ContactFilter2D();

        f.layerMask = LayerMask.GetMask("Ground");
        int a = col.OverlapCollider(f, r);

        if (r[0] != null)
        {
            if ((col.IsTouchingLayers(LayerMask.GetMask("Ground")) == false || IsInside(r[0], col)) && !jumping)
            {
                transform.localScale = new Vector2(-(Mathf.Sign(rb.velocity.x)), transform.localScale.y);
            }
        }
        if (IsFacingRight())
        {
            rb.velocity = new Vector2(speed, rb.velocity.y);
        }
        else
        {
            rb.velocity = new Vector2(-speed, rb.velocity.y);
        }
        if (groundState.isGround() && Time.time > nextJump)
        {
            nextJump = Time.time + JumpRate;
            StartCoroutine(Jump());
        }
    }
Beispiel #4
0
    // Update is called once per frame
    void Update()
    {
        Collider2D[]    r = new Collider2D[10];
        ContactFilter2D f = new ContactFilter2D();

        f.layerMask = LayerMask.GetMask("Ground");
        int a = moveCol.OverlapCollider(f, r);

        if ((moveCol.IsTouchingLayers(LayerMask.GetMask("Ground")) == false || a > 2) && !attacking)
        {
            transform.localScale = new Vector2(-(Mathf.Sign(rb.velocity.x)), transform.localScale.y);
        }
        if (!attacking)
        {
            if (IsFacingRight())
            {
                rb.velocity = new Vector2(speed, rb.velocity.y);
            }
            else
            {
                rb.velocity = new Vector2(-speed, rb.velocity.y);
            }
        }
        if (playerCol.IsTouchingLayers(LayerMask.GetMask("Player")) && Time.time > nextFire)
        {
            nextFire = Time.time + fireRate;
            StartCoroutine(Attack());
        }
    }
Beispiel #5
0
    /* https://medium.com/quick-code/how-to-make-2d-melee-combat-in-unity-practical-tutorials-c5aa0a26e621
     * Adapated the idea of DamageMaker in this url to allow more complex attack action
     * TODO: Location and .enabled of AttackCollider should match the animation in every frame. To be implemented in editor
     * TODO: Add a trigger box collider to Player
     */
    protected override void Attack()
    {
        Debug.Log("SimpleMeleeEnemy is attacking the player");

        if (attackCollider.enabled && !damageMade)
        {
            damageMade = true;
            Collider2D[]    hittedObjects = new Collider2D[100];
            ContactFilter2D playerFilter  = new ContactFilter2D
            {
                layerMask = LayerMask.GetMask("Player")
            };
            attackCollider.OverlapCollider(playerFilter, hittedObjects);
            for (int i = 0; i < hittedObjects.Length; i++)
            {
                //Debug.Log(hittedObjects[i].gameObject);
                if (hittedObjects[i] != null && hittedObjects[i].gameObject == player)
                {
                    player.GetComponent <PlayerControlsScript>().Hit(damage);
                    Debug.Log("Player is hit by an enemy!");
                }
            }
        }
        else if (!attackCollider.enabled && damageMade)
        {
            damageMade = false;
            stateManager.ReturnToIdle(State.Attacking);
        }
    }
    // Checks if the enemy should be dead
    protected bool CheckForDeath()
    {
        // Fell off screen - uses sprite height so the death happens after the player is off-screen
        if (gameObject.transform.position.y < 0 - enemySpriteHeight)
        {
            return(true);
        }

        // Handles enemy collisions
        List <Collider2D> collisions = new List <Collider2D>();

        if (enemyCollider.OverlapCollider(contactFilter, collisions) > 0)
        {
            foreach (Collider2D colliderResult in collisions)
            {
                // Collide with phase block
                if (colliderResult.GetComponent <PhaseBlock>())
                {
                    return(colliderResult.GetComponent <PhaseBlock>().isSolid);
                }
                // Collide with interactable block
                else if (colliderResult.GetComponent <Interactable>())
                {
                    return(colliderResult.GetComponent <Interactable>().isSolid);
                }
            }
        }

        return(false);
    }
Beispiel #7
0
    // Update is called once per frame
    void Update()
    {
        //一つ以上の赤フィルムと接していたら消す
        if (collider.OverlapCollider(redFilmFilter, result) >= 1)
        {
            RedBranchSTAchangeHIDDEN_ON();
        }
        else
        {
            RedBranchSTAchangeHIDDEN_OFF();
        }


        //確認用
        //if(Input.anyKeyDown)
        //      {
        //          if(redbranchsta == 0)
        //          {
        //              RedBranchSTAchangeHIDDEN_OFF();
        //          }
        //          else
        //          {
        //              RedBranchSTAchangeHIDDEN_ON();
        //          }

        //      }
    }
Beispiel #8
0
    private void CheckForCollisions()
    {
        // We only care about the first result
        // Need to create a new array every frame because Unity
        colliderResults = new Collider2D[1];

        // Make sure we're hitting a character
        if (boxCollider.OverlapCollider(colliderContactFilter, colliderResults) == 1)
        {
            // Specify from where is the target being hit
            // Note values are inverted
            int direction = rigidBody.velocity.x < 0 ? 1 : -1;

            // Explode projectile
            Explode();

            // It's an enemy
            if (colliderResults[0].gameObject.name.Contains("Enemy"))
            {
                game.EnemyIsGettingShot(colliderResults[0].gameObject.name, direction);

                // Sh shh, time to sleep little bullet
                Recycle();
            }
            // It's the player
            else if (colliderResults[0].gameObject.name.Contains("Player"))
            {
                // Notify of target hit, specify from where
                game.PlayerIsGettingShot(direction, this);
            }
        }
    }
Beispiel #9
0
    // Update is called once per frame
    void Update()
    {
        if (room && !isClosed)
        {
            room.UpdateWater(rate * Time.deltaTime * 3);
            var emitParams = new ParticleSystem.EmitParams();
            int emitValue  = Mathf.RoundToInt(rate * Time.deltaTime * 3 * 100);
            if (emitValue < 1)
            {
                emitValue = 1;
            }
            waterFlow.Emit(emitParams, emitValue);
        }

        BoxCollider2D col2d = gameObject.GetComponent <BoxCollider2D>();

        Collider2D[]    grabObjects   = new Collider2D[10];
        ContactFilter2D contactFilter = new ContactFilter2D();

        contactFilter.SetLayerMask(LayerMask.GetMask("GrabObject"));
        col2d.OverlapCollider(contactFilter, grabObjects);

        bool isGrabObjectCollided = false;

        //Debug.Log(grabObjects[0]);
        foreach (Collider2D grabObject in grabObjects)
        {
            Debug.Log(grabObject);
            bool isGrabObject = grabObject
                ? (bool)grabObject.gameObject.GetComponent <GrabObject>()
                : false;
            isGrabObjectCollided = isGrabObject ? true : isGrabObjectCollided;
        }
        isClosed = isGrabObjectCollided;
    }
Beispiel #10
0
 // Update is called once per frame
 void Update()
 {
     if (isObtainable == false && area.OverlapCollider(enemiesFilter, enemiesDetected) == 0)
     {
         key.SetActive(true);
         isObtainable = true;
     }
 }
Beispiel #11
0
 public Dragon CheckTouchDragon()
 {
     if (BoxCollider2DThis.OverlapCollider(ContactFilter2DPlayer, Collider2DsPlayer) > 0)
     {
         return(Collider2DsPlayer[0].GetComponent <Dragon>());
     }
     return(null);
 }
Beispiel #12
0
    private void CheckForLatePositionCorrection()
    {
        Collider2D[] stageColliders = new Collider2D[10];
        if (correctMyPosition == false)
        {
            if (bc.OverlapCollider(filter, stageColliders) > 0)
            {
                correctMyPosition = true;
            }
        }

        if (correctMyPosition == true)
        {
            bc.OverlapCollider(filter, stageColliders);
            TryCorrectPosition(stageColliders);
        }
        correctMyPosition = false;
    }
Beispiel #13
0
    void Update()
    {
        if (foot.isTrigger && body.velocity.y < 0 && foot.OverlapCollider(new ContactFilter2D(), new Collider2D[5]) == 0)
        {
            foot.isTrigger = false;
        }
        if (attacking)
        {
            time += Time.deltaTime;
            if (time > .5)
            {
                attacking = false;
                time      = 0;
            }
        }
        else if (Input.touchCount > 0)
        {
            touch = Input.GetTouch(0);

            switch (touch.phase)
            {
            case TouchPhase.Began:
                beginTouchPosition = touch.position;
                break;

            //case TouchPhase.Moved:

            case TouchPhase.Ended:
                endTouchPosition = touch.position;

                //tap
                if (beginTouchPosition == endTouchPosition)
                {
                    hit.enabled = true;
                    attacking   = true;
                    Debug.Log("tapping");
                }
                //swipe
                if (!attacking && grounded && beginTouchPosition != endTouchPosition)
                {
                    foot.isTrigger = true;
                    grounded       = false;
                    gameObject.GetComponent <Rigidbody2D>().velocity = new Vector2(0, 16);
                }
                if (!attacking && (endTouchPosition.y < beginTouchPosition.y) && transform.position.y > -3.5f)
                {
                    grounded       = false;
                    foot.isTrigger = true;
                    gameObject.GetComponent <Rigidbody2D>().velocity = new Vector2(0, -50);
                }
                Debug.Log("swiping");

                break;
            }
        }
    }
Beispiel #14
0
 protected virtual void Update()
 {
     if (!dead)
     {
         if (hitbox.OverlapCollider(contactFilter, player) == 1)
         {
             player[0].GetComponent <Player>().GetHit();
         }
     }
 }
Beispiel #15
0
    protected void findEchoColliders()
    {
        BoxCollider2D   echoCollider = player.GetComponent <BoxCollider2D>();
        ContactFilter2D filter       = new ContactFilter2D
        {
            useTriggers = true
        };

        echoCollider.OverlapCollider(filter, echoColliders);
    }
Beispiel #16
0
 // Update is called once per frame
 void Update()
 {
     if (hitbox.OverlapCollider(contactFilter, player) == 1)
     {
         if (player[0].GetComponent <Animator>().GetFloat("speed") > 0.1f)
         {
             anim.SetTrigger("move");
         }
     }
 }
Beispiel #17
0
    private void FixedUpdate()
    {
        collidingWithPlayer = false;

        //若Player在Enemy原始坐标chaseLength范围内时,可能被追逐
        if (Vector3.Distance(playTransform.position, startingPosition) < chaseLength)
        {
            //再 若Player与Enemy范围过近(triggerLength内),则Enemy开始追逐
            if (Vector3.Distance(playTransform.position, startingPosition) < triggerLength)
            {
                chasing = true;
            }

            //追逐状态:
            if (chasing)
            {
                //若Enemy与Player处于非碰撞状态,则继续进行追逐移动
                //否则保持与Player碰撞状态,无需再移动
                if (!collidingWithPlayer)
                {
                    UpdateMotor((playTransform.position - transform.position).normalized, speedMultiple);
                }
            }
            else
            {
                //非追逐状态: Enemy回到原始坐标
                UpdateMotor((startingPosition - transform.position), speedMultiple);
            }
        }
        else
        {
            //否则Enemy与Player距离过远,不再保持追逐状态,Enemy回到原始坐标
            UpdateMotor((startingPosition - transform.position), speedMultiple);
            chasing = false;
        }


        hitBox.OverlapCollider(filter, hits);

        for (int i = 0; i < hits.Length; i++)
        {
            if (hits[i] == null)
            {
                continue;
            }

            //如果检测到范围内存在Player
            if (hits[i].tag == "Fighter" && hits[i].name == "Player")
            {
                collidingWithPlayer = true;
            }

            hits[i] = null;
        }
    }
    private void FixedUpdate()
    {
        // Is the player in range?
        if (Vector3.Distance(_playerTransform.position, _startingPos) < chaseDistance)
        {
            _hitBox.enabled = true;
            if (Vector3.Distance(_playerTransform.position, _startingPos) < triggerDistance)
            {
                _isChasing = true;
            }

            if (_isChasing)
            {
                if (!_isCollidingWithPLayer)
                {
                    UpdateMotor((_playerTransform.position - transform.position).normalized);
                }
                else
                {
                    UpdateMotor(_startingPos);
                }
            }
        }
        else
        {
            _hitBox.enabled = false;
            UpdateMotor(_startingPos - transform.position);
            _isChasing = false;
        }

        // Check for Movement
        CheckMovement();

        // Check for overlaps
        _isCollidingWithPLayer = false;
        _hitBox.OverlapCollider(filter2D, _hits);

        for (var i = 0; i < _hits.Length; i++)
        {
            if (_hits[i] == null)
            {
                continue;
            }

            if (_hits[i].CompareTag("Fighter") && _hits[i].name == player.name)
            {
                _isCollidingWithPLayer = true;
            }

            // to clean the array
            _hits[i] = null;
        }
    }
    private bool IsGrounded()
    {
        var hit = Physics2D.Raycast(playerTransform.position, Vector2.down, /*distToGround +*/ 0.1f);

        Collider2D[]    res = new Collider2D[10];
        ContactFilter2D cf  = new ContactFilter2D();

        cf.NoFilter();
        var b = collider2D.OverlapCollider(cf, res) >= 1;

        return((rigidbody.velocity.normalized.y <= 0) && b);
    }
Beispiel #20
0
    protected virtual void _SlashStart()
    {
        _Player.Animation.SetTrigger("Slash");
        _CurSlashDamage = SlashDamage;

        Collider2D[] points = new Collider2D[10];
        int          size   = _Collider.OverlapCollider(_Filter, points);

        for (int i = 0; i < size; i++)
        {
            _ApplyDamage(points[i].gameObject);
        }
    }
Beispiel #21
0
    private Room GetRoom()
    {
        BoxCollider2D col2d = gameObject.GetComponent <BoxCollider2D>();

        Collider2D[]    rooms         = new Collider2D[1];
        ContactFilter2D contactFilter = new ContactFilter2D();

        contactFilter.SetLayerMask(LayerMask.GetMask("RoomLogic"));
        col2d.OverlapCollider(contactFilter, rooms);

        Collider2D roomCollider = rooms[0];

        return(roomCollider.gameObject.GetComponent <Room>());
    }
Beispiel #22
0
    protected virtual void Update()
    {
        boxCollider.OverlapCollider(filter, hits);
        for (int i = 0; i < hits.Length; i++)
        {
            if (hits[i] == null)
            {
                continue;
            }

            OnCollide(hits[i]);
            hits[i] = null;
        }
    }
        public void Hit()
        {
            if (Time.time < lastHitTime + cooldown)
            {
                return;
            }
            if (character.GetType() == typeof(PlayerCharacter))
            {
                ((PlayerCharacter)character).anim.SetTrigger("Attack");
            }
            this.Send(new MeleeAttackEvent(this));
            lastHitTime = Time.time;
            ContactFilter2D filter = new ContactFilter2D();

            if (layer == LayerUtils.Enemy)
            {
                filter.SetLayerMask(1 << LayerUtils.Player | 1 << LayerUtils.Terminal);
            }
            else if (layer == LayerUtils.Player)
            {
                filter.SetLayerMask(1 << LayerUtils.Enemy | 1 << LayerUtils.Terminal);
            }
            Collider2D[] overlapping  = new Collider2D[10];
            int          touchedCount = col.OverlapCollider(filter, overlapping);

            for (int i = 0; i < touchedCount; i++)
            {
                if (overlapping[i].gameObject.layer == LayerUtils.Terminal)
                {
                    if (layer == LayerUtils.Player)
                    {
                        Terminal terminal = overlapping[i].GetComponent <Terminal>();
                        terminal.EnableTerminal();
                    }
                    else if (layer == LayerUtils.Enemy)
                    {
                        Terminal terminal = overlapping[i].GetComponent <Terminal>();
                        terminal.DisableTerminal();
                    }
                }
                else
                {
                    Character ch = overlapping[i].GetComponent <Character>();
                    if (ch != null)
                    {
                        ch.ReceiveDamage(character, damage);
                    }
                }
            }
        }
Beispiel #24
0
    private void CheckForGameOver()
    {
        Collider2D[]    overlappingColliders = new Collider2D[kNumColumns];
        ContactFilter2D filter2D             = new ContactFilter2D();

        filter2D.SetLayerMask(m_BlocksLayerMask);
        filter2D.useLayerMask = true;
        int numCollisions = m_GameOverChecker.OverlapCollider(filter2D, overlappingColliders);

        if (numCollisions > 4)
        {
            ScoreController.Instance.GameOver = true;
        }
    }
    public int CheckCollided()
    {
        int di_number = 0;
        int number    = Colloder.OverlapCollider(GameManerge.Get_obj.canshu, collider2Ds);

        for (int i = 0; i < number; i++)
        {
            if (collider2Ds[i].tag == "DiXing")
            {
                di_number++;
            }
        }
        return(di_number);
    }
Beispiel #26
0
    // Update is called once per frame
    void Update()
    {
        if (freezeController.getFreeze())
        {
            return;
        }

        Collider2D[]    results = new Collider2D[5];
        ContactFilter2D filter  = new ContactFilter2D();

        filter.SetLayerMask(collisionMask);
        ctrlCollider.OverlapCollider(filter, results);

        for (int i = 0; i < results.Length; i++)
        {
            Collider2D hit = results[i];
            if (!hit)
            {
                continue;
            }

            Hitbox hitbox = hit.GetComponent <Hitbox>();
            if (!hitbox)
            {
                continue;
            }

            if (instakill)
            {
                hitbox.Die();
            }
            else
            {
                Controller2D ctrl      = hitbox.GetComponent <Controller2D>();
                Vector2      direction = ctrl == null ? Vector2.right : -ctrl.collisions.faceDir * Vector2.right;

                if (ctrl.transform.position.y < transform.position.y)
                {
                    direction.y = -1;
                }

                hitbox.Damage(damage.GetValue(), new Hitbox.Knockback
                {
                    duration  = knockbackDuration.GetValue(),
                    direction = direction,
                    speed     = knockbackSpeed.GetValue()
                });
            }
        }
    }
Beispiel #27
0
 public void FreezingZone(BoxCollider2D abilityCollider)
 {
     Debug.Log("进入了冰冻函数");
     Collider2D[] waterColliders = new Collider2D[8];
     if (abilityCollider.OverlapCollider(filter, waterColliders) != 0)
     {
         Debug.Log(string.Format("冻到了水域{0}", waterColliders[0].name));
         BoxCollider2D waterCollider    = (BoxCollider2D)waterColliders[0];
         BoxCollider2D freezingCollider = poolManager.GetGameObject(IceFreezingZoneName).GetComponent <BoxCollider2D>();
         freezingCollider.gameObject.SetActive(true);
         freezingCollider.GetComponent <IceDisappear>().SetPoolManger(poolManager);
         ColliderBoundsCalculator.SetColliderSizeAndPositionByRect(freezingCollider, ColliderBoundsCalculator.GetColliderIntersection(abilityCollider, waterCollider));
     }
 }
Beispiel #28
0
    // Update is called once per frame
    void Update()
    {
        if (Time.time > lastFire + shootCooldown)
        {
            switch (shootType)
            {
            case towerShootType.CONTINUOUS:
                Instantiate(missilePreFab, firePoint.position, firePoint.rotation);
                lastFire = Time.time;
                break;

            case towerShootType.ON_SIGHT:
                if (detectionArea.OverlapCollider(playerFilter, playerDetected) == 1)
                {
                    Instantiate(missilePreFab, firePoint.position, firePoint.rotation);
                    lastFire = Time.time;
                }
                break;

            case towerShootType.ACTIVATE:
                if (isActive)
                {
                    Instantiate(missilePreFab, firePoint.position, firePoint.rotation);
                    lastFire = Time.time;
                }
                else if (detectionArea.OverlapCollider(playerFilter, playerDetected) == 1)
                {
                    isActive = true;
                }
                break;

            default:
                Debug.LogError("Error on Tower Shoot Type");
                break;
            }
        }
    }
    //Denna metoden kollar om spelaren är död. Spelaren är död om man trillar under kartan eller nuddar några spikar.
    public bool IsDead()
    {
        if (transform.position.y < -10) //Du trillar under kartan om spelarens y-värde är under -10.
        {
            return(true);
        }

        Collider2D[]    colliders = new Collider2D[10];
        ContactFilter2D filter    = new ContactFilter2D();

        filter.SetLayerMask(LayerMask.GetMask("CanKillPlayer")); //Spikar har kollision på layern "CanKillPlayer".

        int length = deathCheck.OverlapCollider(filter, colliders);

        for (int i = 0; i < length; i++)
        {
            if (colliders[i].gameObject != gameObject) //Säkerhetscheck så att vi inte råkar kollidera med oss själva, behövs egentligen inte.
            {
                return(true);
            }
        }

        return(false);
    }
Beispiel #30
0
    private Flower CheckIfFlowerOverlap()
    {
        // set up contact filter and results array
        ContactFilter2D colliderFilter = new ContactFilter2D();

        colliderFilter.SetLayerMask(LayerMask.GetMask("Flower"));
        colliderFilter.useTriggers = true;

        Collider2D[] results = new Collider2D[1];

        // find collider overlap
        int numberOfContacts = _collider.OverlapCollider(colliderFilter, results);

        return(numberOfContacts == 0 ? null : results[0].GetComponent <Flower>());
    }