Post

Unity Project - Arkanoid24, Ball System

๐ŸBall System

  • ๊ธฐ์กด ๊ณต ์‹œ์Šคํ…œ์€ Ball.cs์—์„œ ์ถฉ๋Œ, ์ด๋™, ์„ค์ • ๋“ฑ ๋ชจ๋“  ๊ฒƒ์ด ์ด๋ฃจ์–ด์กŒ๋‹ค.
  • ์ด๋Š” ๋‚˜์ค‘์— ๊ณต ๊ด€๋ จ ์‚ฌํ•ญ์„ ์ˆ˜์ •ํ•  ๋•Œ ์•„์ฃผ ๋ถˆํŽธํ•  ์ˆ˜ ์žˆ๋‹ค๊ณ  ์ƒ๊ฐํ•ด ๋ชจ๋“ˆํ™”๋ฅผ ์ง„ํ–‰ํ–ˆ๋‹ค.

Ball Modularzation

ScriptNameDescription
BallPreference.csBall.cs Base - ๋ณผ ์„ค์ • ๊ด€๋ จ (์ด๋™์†๋„, ์†Œ์œ ์ž ๋“ฑ)
Ball.csPreference๋ฅผ ์ƒ์† ๋ฐ›์Œ ์‹ค์งˆ์ ์ด ๋ณผ ์ƒํƒœ(Ball State) ์ด๋™ ๋กœ์ง ๋ฐ ์ค€๋น„ ๋กœ์ง
BallCollision.cs๋ณผ์˜ ์ถฉ๋Œ ๋ถ€๋ถ„ (๋ฒฝ์ด๋‚˜ ํ”Œ๋ ˆ์ด์–ด ๋˜๋Š” ์™ธ๋ถ€ ๊ฒฝ๊ณ„์„ )
  • BallPreference.cs

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    
    	
      using UnityEngine;
    	
      public class BallPreference : MonoBehaviour
      {
          public enum BALL_STATE
          {
              READY,
              LAUNCH
          }
    	
          #region Member Variables
    	
          [Header("Ball Information")]
          [Range(4f, 6f)] public float MinSpeed = 4f;
          [Range(14f, 16f)] public float MaxSpeed = 16f;
          // Ball Start Default Speed
          [Range(7f, 12f)] public float defaultSpeed = 7f;
    	
          [SerializeField] protected float _currentSpeed;
    	
          protected Rigidbody2D _paddleRbody;
          protected Rigidbody2D _ballRbody;
    	
          // Game Pause
          protected Vector2 _prevVelocity;
          protected bool _isStarted = true;
    	
          public float ballIncreaseSpeedScope = 0.5f;
    	
          // Player Depend, Reference
          protected GameObject _playerObject;
    	
          public BALL_STATE BallState { get; set; } = BALL_STATE.READY;
    	    
          // Getter
          public GameObject BallOwner { get { return _playerObject; } }
    	
          public BallSkillState BallSkill;
    	
          #endregion
    	
    	
          #region Unity Flow
          protected virtual void Awake()
          {
              _ballRbody = GetComponent<Rigidbody2D>();
          }
    	
          protected virtual void Start()
          {
              _currentSpeed = defaultSpeed;
              BallSkill = BallOwner.GetComponent<BallSkillState>();
          }
    	
          protected virtual void FixedUpdate()
          {
              if(Managers.Game.State != GameState.Play)
              {
                  if (_isStarted)
                  {
                      _isStarted = false;
                      _prevVelocity = _ballRbody.velocity;
                  }
    	
                  _ballRbody.velocity = Vector2.zero;
                  return;
              }
    	
              if (!_isStarted)
              {
                  _ballRbody.velocity = _prevVelocity;
                  _isStarted = true;
              }
          }
          #endregion
    	
    	
          #region Utility
          public void AssignPlayer(GameObject player)
          {
              _playerObject = player;
    	
              GetPlayerComponent();
          }
    	
          private void GetPlayerComponent()
          {
              _paddleRbody = _playerObject.GetComponent<Rigidbody2D>();
          }
    	
          public void SetAdditionalCurrentSpeed(float additionalSpeed)
          {
              _currentSpeed = defaultSpeed + additionalSpeed;
    	
              if (_currentSpeed > MaxSpeed)
                  _currentSpeed = MaxSpeed;
              else if(_currentSpeed < MinSpeed)
                  _currentSpeed = MinSpeed;
          }
    	
          public void BallHitBrick()
          {
              BallSkill.SetSpeed(BallOwner);
          }
          #endregion
      }
    

  • Ball.cs

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    144
    145
    
      using System;
      using UnityEngine;
    	
      public class Ball : BallPreference
      {
          #region Member Variables
    	
          public bool isCatchLaunch = false;
    	
          public int _defaultPower = 1;
          public int _maxPower = 1;
    	
          public float _posX;
    	
          // Event
          private event Action OnBallLaunch;
    	
          #endregion
    	
    	
          #region Unity Flow
          protected override void Awake()
          {
              // Get Component
              base.Awake();
          }
    	
          protected override void Start()
          {
              base.Start();
    	        
              OnBallLaunch += BallToStart;
    	
              SetAdditionalCurrentSpeed(BallOwner.GetComponent<BallSkillState>().BallExtraSpeed);
              SetPower(BallSkill.BallExtraPower);
          }
    	
          protected override void FixedUpdate()
          {
              base.FixedUpdate();
    	
              BallStateUpdateMethod();
          }
          #endregion
    	
    	
          #region Ball State
    	
          private void BallStateUpdateMethod()
          {
              Action ballAction = BallState switch
              {
                  BALL_STATE.READY => () => BallToReady(),
                  BALL_STATE.LAUNCH => () => BallToLaunch(),
                  _ => () => { }
              };
    	
              ballAction();
          }
    	
          private void BallToStart()
          {
              if (BallState != BALL_STATE.READY) return;
              if(BallSkill.CurrentSkill == Items.Catch) isCatchLaunch = true;
              BallState = BALL_STATE.LAUNCH;
    	
              CalculateBallPosToPaddle();
          }
    	
          private void CalculateBallPosToPaddle()
          {
              var paddleWidth = _playerObject.GetComponent<BoxCollider2D>().bounds.size.x;
              var posX = _ballRbody.position.x - _paddleRbody.position.x;
    	
              posX = posX / paddleWidth;
    	        
              var direction = new Vector2(posX, 1).normalized;
    	
              if (posX != 0)
                  _ballRbody.velocity = direction * _currentSpeed;
              else
                  _ballRbody.velocity = Vector2.up * defaultSpeed;
              isCatchLaunch = false;
          }
    	
          private void BallToReady()
          {
              FollowThePaddle(_posX);
          }
    	
          private void FollowThePaddle(float posX = 0f)
          {
              Vector2 paddlePos = _paddleRbody.position;
              Vector2 newBallPos = new Vector2(paddlePos.x, paddlePos.y + 0.5f);
    	
              _ballRbody.position = newBallPos + new Vector2(posX, 0f);
              _ballRbody.velocity = Vector2.zero;
          }
    	
          private void BallToLaunch()
          {
              var ballVelocitySpeed = _ballRbody.velocity.magnitude;
    	
              if(ballVelocitySpeed > _currentSpeed || ballVelocitySpeed < _currentSpeed)
              {
                  _ballRbody.velocity = _ballRbody.velocity.normalized * _currentSpeed;
              }
          }
          #endregion
    	
    	
          #region Item SKill
    	
          public void CheckCatchActivation()
          {
              if (BallSkill.CurrentSkill == Items.Catch)
              {
                  BallState = BALL_STATE.READY;
                  _posX = transform.position.x - _paddleRbody.transform.position.x;
              }
          }
    	
    	    
          public void SetPower(int extraPower)
          {
              _maxPower = _defaultPower + extraPower;
              if(BallSkill.CurrentSkill == Items.Power)
              {
                  transform.localScale = transform.localScale * 2f;
              }
          }
    	
          #endregion
    	
    	
          public void CallBallLaunch()
          {
              OnBallLaunch?.Invoke();
          }
    	
          private void OnDestroy()
          {
              OnBallLaunch -= BallToStart;
          }
      }
    
    • Ball.cs๋Š” BallPreference๋ฅผ ์ƒ์† ๋ฐ›์•„์„œ ๋ณผ์˜ ์„ค์ •์„ ๋“ค๊ณ ์™€ ๋กœ์ง๋งŒ ์ง„ํ–‰ํ•˜๊ฒŒ ๋ณ€๊ฒฝํ–ˆ๋‹ค.
    • ๊ธฐ๋ณธ์ ์ธ ๋ณผ ์ƒํƒœ(State)์™€ ์„ค์ •(Speed, Owner)๋“ฑ์€ ์ „๋ถ€ Preference์—์„œ ์ฒ˜๋ฆฌํ•œ๋‹ค.
    • ์ด๋ ‡๊ฒŒ ํ•จ์œผ๋กœ์จ Ball.cs๋Š” ์Šคํ‚ฌ๊ณผ ์‹ค์งˆ์ ์ธ ๋™์ž‘(velocity) ๋ถ€๋ถ„๋งŒ ์ดํ–‰ํ•˜๋ฉด ๋œ๋‹ค.

  • BallCollision.cs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
using UnityEngine;
using static BallPreference;

public class BallCollision : MonoBehaviour
{
    private Ball _ball;
    private Rigidbody2D _ballRbody;

    private void Awake()
    {
        _ballRbody = GetComponent<Rigidbody2D>();
        _ball = GetComponent<Ball>();
    }


    #region Collision

    private void OnCollisionEnter2D(Collision2D col)
    {
        var activePlayer = Managers.Player.GetActivePlayers();

        foreach (var player in activePlayer)
        {
            if (col.gameObject == player && _ball.BallState != BALL_STATE.READY)
            {
                SFX.Instance.PlayOneShot(SFX.Instance.paddleHit);
                float paddleWidth = col.collider.bounds.size.x;

                _ball.CheckCatchActivation();
                if (player.GetComponent<BallSkillState>().CurrentSkill != Items.Catch)
                {
                    var posX = HitFactor(col.transform.position, paddleWidth);
                    var direction = new Vector2(posX, 1).normalized;

                    _ballRbody.velocity = _ballRbody.velocity.magnitude * direction;
                }
            }
            else if (col.gameObject.CompareTag("Brick"))
            {
                _ball.BallHitBrick();
            }
        }
    }

    #endregion


    #region Utility
    private float HitFactor(Vector2 paddlePos, float paddleWidth)
    {
        return (_ballRbody.position.x - paddlePos.x) / paddleWidth * 2f;
    }
#endregion
}
  • ๊ณต์˜ ์ถฉ๋Œ ๋ถ€๋ถ„์—์„œ๋Š” ์‹ค์ œ ์ถฉ๋Œ์ด ์ผ์–ด๋‚ฌ์„ ๋•Œ ์‹คํ–‰ํ•ด์•ผํ•  ๋ถ€๋ถ„๋งŒ ๋ชจ๋“ˆํ™” ํ•ด๋†จ๋‹ค.
  • ํ”Œ๋ ˆ์ด์–ด (๊ฐ๋„๋ฅผ ์œ„ํ•จ)์™€ ๋ฒฝ๋Œ๋ถ€๋ถ„์œผ๋กœ ๋‚˜๋ˆ„์–ด ์ฒ˜๋ฆฌํ–ˆ๋‹ค.

๐Ÿ›Ž๏ธServiceLocator

  • ์„œ๋น„์Šค ๋กœ์ผ€์ดํ„ฐ๋Š” ์˜์กด์„ฑ ์ฃผ์ž…(Dependency Injection)๋ฅผ ์ค‘์•™์—์„œ ์ฒ˜๋ฆฌํ•ด์ฃผ๋Š” ์‹œ์Šคํ…œ์ด๋‹ค.
  • ์„œ๋น„์Šค๋ฅผ ๋“ฑ๋ก ํ•ด๋†“๊ณ  ์–ด๋””์„œ๋“  ์ ‘๊ทผํ•˜์—ฌ ํ•ด๋‹น ์„œ๋น„์Šค๋ฅผ ์ด์šฉํ•  ์ˆ˜ ์žˆ๋Š” ๋งค๋‹ˆ์ € ๊ฐ™์€ ์‹œ์Šคํ…œ์ด๋‹ค.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
using System.Collections.Generic;
using UnityEngine;

public static class ServiceLocator
{
    private static readonly Dictionary<System.Type, Object> _services =
        new Dictionary<System.Type, Object>();

    public static void RegisterService<T>(T service) where T : Object
    {
        _services[service.GetType()] = service;
    }

    public static T GetService<T>() where T : Object
    {
        if (_services.TryGetValue(typeof(T), out Object service))
        {
            return service as T;
        }
        return null;
    }
}

์‹ฑ๊ธ€ํ†ค ํŒจํ„ด๊ณผ ์„œ๋น„์Šค ๋กœ์ผ€์ดํ„ฐ

  • Singleton
    • ์‹ฑ๊ธ€ํ†ค ํŒจํ„ด์€ ํด๋ž˜์Šค์— ์ธ์Šคํ„ด์Šค๊ฐ€ ํ•˜๋‚˜๋งŒ ์žˆ์Œ์„ ๋ณด์žฅํ•˜๋Š” ํŒจํ„ด์ด๋‹ค.
    • static์†์„ฑ์ด๋‚˜ method๋ฅผ ํ†ตํ•ด์„œ ํด๋ž˜์Šค ์ „์—ญ์— ์—‘์„ธ์Šค ์ง€์ ์„ ์ œ๊ณตํ•œ๋‹ค.
    • ์ „์—ญ์—์„œ ์—‘์„ธ์Šค๊ฐ€ ๊ฐ€๋Šฅํ•œ์ ์€ ๊ฐ„๋‹จํ•œ ์‚ฌ์šฉ์„ ์ œ๊ณตํ•˜์ง€๋งŒ ์ปคํ”Œ๋ง์œผ๋กœ ์ด๋ฃจ์–ด์งˆ ์ˆ˜ ์žˆ๋‹ค.
    • ์ „์—ญ ์—‘์„ธ์Šค๋ฅผ ํ•  ์ˆ˜ ์žˆ๋Š” ์ด์ ์ด ๋‹จ์ ์œผ๋กœ๋„ ์ž‘์šฉํ•˜๋Š”๋ฐ, ๋‹จ์œ„ ํ…Œ์ŠคํŠธ๋ฅผ ์–ด๋ ต๊ฒŒ ๋งŒ๋“ ๋‹ค.

  • Service Locator
    • ์„œ๋น„์Šค ๋กœ์ผ€์ดํ„ฐ๋Š” ์„œ๋น„์Šค์™€ ๊ตฌ์„ฑ์š”์†Œ๋ฅผ ๋“ฑ๋กํ•˜๊ณ  ํ•„์š”ํ•  ๋•Œ ๊ฒ€์ƒ‰ํ•ด์„œ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค.
    • ์‹ฑ๊ธ€ํ†ค ํŒจํ„ด๊ณผ ๋งค์šฐ ์œ ์‚ฌํ•˜์ง€๋งŒ ๊ฒฐํ•ฉ๋„๊ฐ€ ์‹ฑ๊ธ€ํ†ค๊ณผ๋Š” ๋‹ค๋ฅด๊ฒŒ ์ ์šฉ๋œ๋‹ค.
    • ํด๋ž˜์Šค๊ฐ€ ์„œ๋กœ์— ๋Œ€ํ•œ ์ฐธ์กฐ๋ฅผ ์ง์ ‘ ์ธ์Šคํ„ด์Šคํ™”ํ•˜๊ฑฐ๋‚˜ ๋ณด์œ ํ•˜์ง€ ์•Š๊ธฐ ๋•Œ๋ฌธ์—,
      ํด๋ž˜์Šค๊ฐ„์˜ ๊ฒฐํ•ฉ์„ ์ค„์ธ๋‹ค. ํ•˜์ง€๋งŒ ํด๋ž˜์Šค ์ข…์†์„ฑ์„ ์ˆจ๊ธธ ์ˆ˜ ์žˆ๋‹ค.

์ •๋ฆฌํ•˜๋ฉฐโ€ฆ

๊ณต ๋ชจ๋“ˆํ™”

  • ๋ชจ๋“ˆํ™”๋ฅผ ์ง„ํ–‰ํ•˜๋ฉด์„œ ํฌ๊ฒŒ ์–ด๋ ค์› ๋˜ ๋ถ€๋ถ„์€ ์กด์žฌํ•˜์ง€ ์•Š์•˜๋‹ค.
  • ๊ทธ๋„ ๊ทธ๋Ÿด ๊ฒƒ์ด ๊ธฐ์กด ๋‚ด์šฉ์„ ์„ธ๋ถ„ํ™” ์‹œํ‚ค๋Š” ์ž‘์—…์ด์—ˆ๊ณ  ์„ธ๋ถ„ํ™” ์‹œํ‚จ ๋‚ด์šฉ์„ ์—ฐ๊ฒฐํ•˜๋Š” ๊ฒƒ์ด ์•„๋‹Œ
    ์ƒ์†์˜ ํ˜•ํƒœ๊ฐ€ ๋งž๋‹ค๊ณ  ํŒ๋‹จํ•˜์—ฌ ์„ค์ • ๊ฐ’์„ ๊ทธ๋Œ€๋กœ ์ƒ์†๋ฐ›๋Š” ํ˜•ํƒœ๋กœ ์‚ฌ์šฉ ํ–ˆ๊ธฐ ๋•Œ๋ฌธ์ด๋‹ค.
  • ๊ทธ๋ž˜๋„ ์„ค์ •, ๋ณผ ์‹ค์ œ ์•ก์…˜, ์ถฉ๋Œ ๋ถ€๋ถ„์„ ๋”ฐ๋กœ ๋‚˜๋ˆ  ๊ด€๋ฆฌํ•˜๋‹ˆ ๋‹ค๋ฅธ ํŒ€์›์ด ๋‚ด ์ฝ”๋“œ์— ์ ‘๊ทผํ•  ๋•Œ๋„ ์šฉ์ดํ–ˆ๋‹ค.

์„œ๋น„์Šค ๋กœ์ผ€์ดํ„ฐ

  • ์„œ๋น„์Šค ๋กœ์ผ€์ดํ„ฐ ํŒจํ„ด์€ ๊ธฐ์กด ์‹ฑ๊ธ€ํ†ค ํ˜•ํƒœ์˜ ๋งค๋‹ˆ์ €๋“ค์„ ์–ด๋Š์ •๋„ ๋Œ€์ฒดํ•  ์ˆ˜ ์žˆ๊ฒŒ ํ•ด์ฃผ์—ˆ๋‹ค.
  • ๋งค๋‹ˆ์ €๋“ค๋„ ์ „์—ญ์—์„œ ์—‘์„ธ์Šคํ•  ์ˆ˜ ์žˆ๋‹ค๋Š” ์ ์„ ์ œ์™ธํ•˜๋ฉด SRP(๋‹จ์ผ ์ฑ…์ž„ ์›์น™)์„ ์ค€์ˆ˜ํ•ด์•ผํ•œ๋‹ค.
  • ์„œ๋น„์Šค ๋กœ์ผ€์ดํ„ฐ๋ฅผ ์‚ฌ์šฉํ•ด ์–ด๋””์„œ๋“  ์˜์กด์„ฑ์„ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๊ฒŒ ํ–ˆ๋‹ค.
  • ์ด๋Š” GameManager์™€ ๊ฐ™์€ ๋งค๋‹ˆ์ €์—์„œ Player ๊ฐ™์€ ์ „์—ญ ์ •๋ณด๋ฅผ ๋‹ค๋ฃฐ ํ•„์š”๊ฐ€ ์—†์–ด์กŒ๋‹ค.
This post is licensed under CC BY 4.0 by the author.