State Pattern
νκ·Έ: C#, CSharp, Interview, StatePattern, Study, Unity
μΉ΄ν κ³ λ¦¬: UnityStudy
π State Pattern
βοΈ μν ν¨ν΄μ΄λ?
μνν¨ν΄μ΄λ κ° μνλ³λ‘ μ€ν¬λ¦½νΈλ₯Ό λΆλ¦¬
μ¦, μμ€ν μ κ° μνλ₯Ό ν΄λμ€λ‘ λΆλ¦¬ν΄ νννλ κ²μ΄λ€.
ν μ€ν¬λ¦½νΈμ λͺ¨λ λ£μ μ μλ λ΄μ©μ μ λλλ κ²μΌκΉ?
μ²«μ¨°λ‘ ν κ°μ²΄μ μνλ₯Ό μ¬λ¬ μ€ν¬λ¦½νΈλ‘ λλλ κ²μ΄κΈ°μ μ½λμ κ°λ μ±μ΄ μ’μμ§λ€.
λμ§Έλ‘ κ° μνλ³λ‘ μ½λλ₯Ό κ΄λ¦¬νκΈ°μ κ°λ°νλ μ μ₯μμ κ΄λ¦¬κ° μ½λ€. (μν μΆκ°κ° μ½λ€.)
νμ§λ§ λ¨μ μ μ‘΄μ¬νλ€.
μνκ° λ무 λ§μμ§κ² λλ©΄ κ·Έλ§νΌ μ€ν¬λ¦½νΈκ° μ¦κ°νκΈ°μ κ΄λ¦¬κ° μ΄λ ΅λ€.
μ΄ νλκ° κ΅μ₯ν ν° λ¨μ μ΄ λ μ μλ€κ³ μκ°νλ€.
μ΄μ μ€μ κ²μμμ μ΄λ»κ² μ¬μ©λ μ μλμ§ ν λ² μκ°ν΄λ³΄μ.
λ§μ½ κ²μμ μ‘΄μ¬νλ μμμ μνλ₯Ό μ μ©νλ€λ©΄ μ΄λ»κ² λ κΉ?
μμκ° κΈ°λ³Έ μν / λ€λ €μ§ μν / λμμ§ μν / λμ μ§ μν λ₯Ό κ°μ§λ€κ³ μκ°ν΄λ³΄μ.
κ·Έλ¦¬κ³ μμ μ€λΈμ νΈλ μ΄ λͺ¨λ μ€ν¬λ¦½νΈλ₯Ό κ°μ§λ κ²μ΄ μλ,
μ΄λ€μ κ΄λ¦¬νλ 맀λμ μ€ν¬λ¦½νΈλ₯Ό κ°μ§λ€.
κ·ΈλΌ κ° μνλ³λ‘ μ½λλ₯Ό μ§μ€λ€.
(μ°μ λͺ¨λ μνλ BaseState μ΄λΌλ μΆμ ν΄λμ€λ₯Ό μμλ°λλ€.)
π μ½λ
π BaseState
1
2
3
4
5
6
7
8
using UnityEngine;
public abstract class BoxBaseState
{
public abstract void EnterState(BoxStateManager box);
public abstract void UpdateState(BoxStateManager box);
public abstract void OnCollisionEnter(BoxStateManager box, Collision collision);
}
π IdleState
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
using UnityEngine;
public class BoxIdle : BoxBaseState
{
public override void EnterState(BoxStateManager box) { }
public override void UpdateState(BoxStateManager box)
{
if(box.player != null && box.player._yes)
{
box.player._yes = false;
box.SwitchState(box._pickState);
}
}
public override void OnCollisionEnter(BoxStateManager box, Collision collision)
{
GameObject other = collision.gameObject;
if(other.CompareTag("Player"))
{
if(box.player == null) box.player = other.GetComponent<PlayerController>();
box.player.DrawUI();
}
}
}
π PickedupState
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
using UnityEngine;
public class BoxPickedUp : BoxBaseState
{
public override void EnterState(BoxStateManager box)
{
box.transform.position = box.player.transform.position + new Vector3(0, 2f, 0);
box.transform.SetParent(box.player.transform);
box.GetComponent<Rigidbody>().isKinematic = true;
}
public override void UpdateState(BoxStateManager box)
{
if(box.player._putDown)
{
box.player._putDown = false;
box.SwitchState(box._putDownState);
}
else if(box.player._throwAway)
{
box.player._throwAway = false;
box.SwitchState(box._throwAwayState);
}
}
public override void OnCollisionEnter(BoxStateManager box, Collision collision) { }
}
π PutDownState
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
using UnityEngine;
public class BoxPutdown : BoxBaseState
{
float _countdown = 1.0f;
public override void EnterState(BoxStateManager box)
{
box.transform.SetParent(null);
box.transform.position = box.player.transform.position + new Vector3(2, 0, 0);
box.GetComponent<Rigidbody>().isKinematic = false;
}
public override void UpdateState(BoxStateManager box)
{
if(_countdown > 0)
_countdown -= Time.deltaTime;
else
box.SwitchState(box._idleState);
}
public override void OnCollisionEnter(BoxStateManager box, Collision collision) { }
}
π ThrowedAwayState
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
using UnityEngine;
public class BoxThrowedAway : BoxBaseState
{
float _countdown = 2.0f;
public override void EnterState(BoxStateManager box)
{
box.transform.SetParent(null);
box.GetComponent<Rigidbody>().isKinematic = false;
box.GetComponent<Rigidbody>().AddForce(Vector3.right * 10, ForceMode.Impulse);
}
public override void UpdateState(BoxStateManager box)
{
if(_countdown > 0)
_countdown -= Time.deltaTime;
else
box.SwitchState(box._idleState);
}
public override void OnCollisionEnter(BoxStateManager box, Collision collision) { }
}
π StateManager
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
using UnityEngine;
public class BoxStateManager : MonoBehaviour
{
BoxBaseState _currentState;
public PlayerController player;
public BoxIdle _idleState = new BoxIdle();
public BoxPickedUp _pickState = new BoxPickedUp();
public BoxPutdown _putDownState = new BoxPutdown();
public BoxThrowedAway _throwAwayState = new BoxThrowedAway();
private void Start()
{
_currentState = _idleState;
_currentState.EnterState(this);
}
protected void Update()
{
_currentState.UpdateState(this);
}
public void SwitchState(BoxBaseState state)
{
_currentState = state;
_currentState.EnterState(this);
}
void OnCollisionEnter(Collision collision)
{
_currentState.OnCollisionEnter(this, collision);
}
}
λκΈλ¨κΈ°κΈ°