Exemplo n.º 1
0
        public override void Start()
        {
            // 시작할때 지정해준 목숨만큼 증가시킨다.
            IncreaseLife(StartLife);

            // 플레이어가 죽으면 실행할 이벤트를 추가
            // 죽으면 깜박이는 애니메이션등을 여기서 구현한다.
            // 플레이어가 죽으면 죽은 애니메이션 출력하고
            // 현재 라이프가 몇개 있는지 확인하여
            // 게임오버일지 재배치할지 결정한다.

            playerhealth.EventOnDead += () =>
            {
                // 죽는 애니메이션 추가와 실행
                AnimationSprite animation = player.GetComponent <AnimationSprite>();
                SpriteComponent sprite    = player.GetComponent <SpriteComponent>();
                animation.ImageCurrentIndex = 0;
                animation.ImageList.Clear();
                animation.ImageList.Add(Resources.player_die_01);
                animation.ImageList.Add(Resources.player_die_02);
                animation.ImageList.Add(Resources.player_die_03);
                animation.ImageList.Add(Resources.player_die_04);

                animation.Enabled = true;


                // 상호작용 컴포넌트들을 작동중시키기고 시작위치로 다시 움기기 위한 컴포넌트만 작동시킨뒤
                // 목적지에 도착하면 다시 모든 컴포넌트를 동작시킨다.
                player.collider.Enabled = false;
                BulletShooter bulletShooter = player.GetComponent <BulletShooter>();
                bulletShooter.Enabled = false;
                InputComponent input = player.GetComponent <InputComponent>();
                input.Enabled = false;

                Action handler = null;


                // 죽은 애니메이션 모두 완료후 다음 한번만 작동하는 이벤트를 추가시킨다.
                handler = () =>
                {
                    // 부활하는 동안 깜박이는 애니매이션 추가
                    animation.EventPrintedAllImage -= handler;
                    sprite.Image = Resources.player_1;
                    animation.ImageList.Clear();
                    animation.ImageList.Add(Resources.player_1);
                    animation.ImageList.Add(Resources.player_1_02);

                    // 남은 라이프 갯수 확인하여 게임오버할지 판별
                    if (CurrentLife > 0)
                    {
                        player.transform.position = (Vec2D)UIManager.Instance.GetLastShipLoaction;
                        ReduceLife(1);

                        TargetScrolling scrolling = player.GetComponent <TargetScrolling>();
                        if (scrolling != null)
                        {
                            scrolling.Enabled = true;

                            // 부활 하여 시작위치까지 이동하면 상호작용 컴포넌트 작동시작 및 애니메이션 중단
                            scrolling.OnReachDestination += async() =>
                            {
                                player.transform.Rotation = 0;
                                scrolling.Enabled         = false;
                                input.Enabled             = true;

                                await Task.Delay(1000);

                                player.collider.Enabled = true;
                                bulletShooter.Enabled   = true;
                                animation.Enabled       = false;
                                sprite.Image            = Resources.player_1;
                            };
                        }
                    }
                    // 남은 목숨이 0이라면
                    else
                    {
                        // 이벤트 있으면 실행시키고
                        EventNoMoreLife?.Invoke();
                        // 게임오버 메시지 출력하기
                        UIManager.Instance.EnableGameOver();
                        GameObject.Destroy(player);
                    }
                };
                animation.EventPrintedAllImage += handler;
            };
        }
        /// <summary>
        /// slot에 설정된 적 유닛의 로직을 설정한다.
        /// 각 컴포넌트에 이벤트를 추가한다.
        /// </summary>
        /// <param name="slot">설정할 슬롯</param>
        /// <param name="score">죽으면 추가할 점수</param>
        private void SetupEnemy(Slot slot, int score)
        {
            BezierCurveMove move         = slot.gameObject.GetComponent <BezierCurveMove>();
            HealthSystem    health       = slot.gameObject.GetComponent <HealthSystem>();
            DamageSystem    damageSystem = slot.gameObject.GetComponent <DamageSystem>();

            //damageSystem.EventGiveDamage += () => health.GetDamage(2);

            // 한번만 작동하게 하는 이벤트 삽입
            // 자기 자신에게 데미지 주기
            Action selfDamage = null;

            selfDamage = () =>
            {
                damageSystem.EventGiveDamage -= selfDamage;
                health.GetDamage(2);
            };
            damageSystem.EventGiveDamage += selfDamage;

            // 죽으면 애니메이션 재생하게 하기
            health.EventOnDead += () =>
            {
                slot.gameObject.collider.Enabled = false;
                move.Enabled = false;
                AnimationSprite animation = slot.gameObject.GetComponent <AnimationSprite>();
                animation.ImageCurrentIndex   = 0;
                animation.ImageChangeInterval = 0.1f;
                animation.ImageList.Clear();
                animation.ImageList.Add(Resources.enemy_die_01);
                animation.ImageList.Add(Resources.enemy_die_02);
                animation.ImageList.Add(Resources.enemy_die_03);
                animation.ImageList.Add(Resources.enemy_die_04);
                animation.ImageList.Add(Resources.enemy_die_05);
                // 재생 다하면 오브젝트 삭제
                animation.EventPrintedAllImage += () => GameObject.Destroy(slot.gameObject, 0);
                animation.EventPrintedAllImage += () => slot.gameObject = null;
            };
            // 점수 증가
            health.EventOnDead += () => GameManager.Instance.IncreaseScore(score);
            health.EventOnDead += () => aliveUnits.Remove(slot.gameObject);

            // 보스몬스터일경우 남은 HP가 1이면 애니메이션 스프라이트 변경
            if (slot.gameObject as Enemy03 != null)
            {
                health.EventGetDamage += (leftHP) =>
                {
                    if (leftHP == 1)
                    {
                        AnimationSprite animationSprite = slot.gameObject.GetComponent <AnimationSprite>();
                        if (animationSprite != null)
                        {
                            animationSprite.ImageList.Clear();
                            animationSprite.ImageList.Add(Resources.Enemy_03_03_01);
                            animationSprite.ImageList.Add(Resources.Enemy_03_03_01);
                        }
                    }
                    else if (leftHP == 2)
                    {
                        AnimationSprite animationSprite = slot.gameObject.GetComponent <AnimationSprite>();
                        if (animationSprite != null)
                        {
                            animationSprite.ImageList.Clear();
                            animationSprite.ImageList.Add(Resources.Enemy_03_02_01);
                            animationSprite.ImageList.Add(Resources.Enemy_03_02_01);
                        }
                    }
                };
            }
        }