Example #1
0
 void OnTriggerEnter2D(Collider2D collision)
 {
     if (boxcollider.enabled == false)// 防止夹取多个食物
     {
         return;
     }
     pickFood = collision.gameObject.GetComponent <Food>();
     if (pickFood != null)
     {
         pickFood.transform.SetParent(this.transform);
         fetchFoodSeq.Kill();
         // Tweener back = transform.DOScaleY(1.48f, timer).SetEase(Ease.Linear);
         Tweener back = DOTween.To(() => spriteRenderer.size, x => spriteRenderer.size = x, new Vector2(2f, spriteRenderer.size.y), 1.1f).SetEase(Ease.Linear);
         back.OnUpdate(() =>
         {
             pickFood.Follow(spriteRenderer.size.x);
         });
         boxcollider.enabled = false; // 防止夹取多个食物
         back.OnComplete(() =>
         {
             isIdle = true;
             boxcollider.enabled = true;
             if (transform.childCount > 0)
             {
                 if (OnEat != null)
                 {
                     OnEat.Invoke(pickFood.foodType);
                 }
                 pickFood.BeEat();
                 //  Destroy(food.gameObject); // 或添加食用动作?
                 //Debug.Log("饱食度++");
             }
         });
     }
 }
Example #2
0
    protected override void OnCollisionEnter(Collision collision)
    {
        if (collision.gameObject.tag == "Barrier")
        {
            OnHitBarrier();
        }

        if (collision.gameObject.tag == "Border")
        {
            SceneManager.LoadScene(0);
        }

        if (collision.gameObject.tag == "Food")
        {
            Color32 objColor;
            objColor = collision.gameObject.GetComponent <MeshRenderer>().material.color;
            Destroy(collision.gameObject);
            var bone = Instantiate(BonePrefab);
            bone.GetComponent <Renderer>().material.color = objColor;
            Tails.Add(bone.transform);
            Speed *= 1.1f;
            if (OnEat != null)
            {
                OnEat.Invoke();
            }
            if (Tails.Count >= 3)
            {
                RemoveBone();
            }
        }
    }
Example #3
0
 public void IncreaseFat()
 {
     if (fat < 5)
     {
         fat++;
     }
     OnEat?.Invoke();
     SetFatProperties(fat);
 }
Example #4
0
 private void Eat(Food obj)
 {
     for (int i = 0; i < obj.Mass; i++)
     {
         var pos   = obj.Position;
         var peace = new SnakePeace(pos);
         snakePeaces.Insert(0, peace);
         OnEat?.Invoke(this, obj, peace);
     }
 }
Example #5
0
 public void Eat(Plant p)
 {
     if (p.Size == 3)
     {
         OnEat?.Invoke(this,
                       new HerbivoreOnEatArgs($"{this.Name} ate plant {p.Name}"));
     }
     else
     {
         OnEat?.Invoke(this,
                       new HerbivoreOnEatArgs($"{this.Name} can't eat plant {p.Name}, because it's too small"));
     }
 }
Example #6
0
        /// <summary>
        /// Grows up the snake.
        /// </summary>
        /// <param name="food">The food.</param>
        public void Eat(IFoodComponent food)
        {
            //put the new body part on the tail's place
            var newBodyPart = _bodyFactory.Create(null, GameComponentType.Body, Direction, Step, Step);

            newBodyPart.CurPosition  = Tail.CurPosition;
            newBodyPart.PrevPosition = Tail.PrevPosition;

            //step back tail
            Tail.CurPosition = Tail.PrevPosition;
            Tail.Direction   = GetDirestion(newBodyPart.CurPosition, Tail.CurPosition);

            //set direction for new food
            newBodyPart.Direction = Direction;

            //grow the snake
            SnakeBodyWithHead.Insert(SnakeBodyWithHead.Count - 1, newBodyPart);
            _body = null; // it will cause to recreate

            Field.PutOn(newBodyPart);
            OnEat?.Invoke(food);
        }
Example #7
0
 public void Eat(Herbivore h)
 {
     OnEat?.Invoke(this, new CarnivoreOnEatArgs($"{this.Name} ate {h.Name}"));
 }