using System.Collections;

using System.Collections.Generic;

using UnityEngine;

using UnityEngine.SceneManagement;


public class CharacterMovement : MonoBehaviour {


public GameObject effect;//์‚ฌ๋ง ํšจ๊ณผ


public int health = 3; //์บ๋ฆญํ„ฐ ์ฒด๋ ฅ


[SerializeField] private float speed;

[Range(0, .3f)] [SerializeField] private float m_MovementSmoothing = .05f; //์ด๋™ ์‹œ ๋ถ€๋“œ๋Ÿฌ์›€ ์กฐ์ ˆ์— ์‚ฌ์šฉ

//[SerializeField] private Animator animator;


private float moveInputHorizontal;

private float moveInputVertical;

private Rigidbody2D rb;

private bool facingRight = true; //์บ๋ฆญํ„ฐ ์Šคํ”„๋ผ์ดํŠธ ๋’ค์ง‘๋Š” ๋ฐ์— ์‚ฌ์šฉ

private Vector3 refVelocity = Vector3.zero; //smoothDamp์— ์‚ฌ์šฉ

private SpriteRenderer spriteRenderer; //์บ๋ฆญํ„ฐ sprite renderer


[SerializeField]

private bool death = false;


void Start () {

rb = GetComponent();

spriteRenderer = GetComponent();

death = false;

}


void FixedUpdate()

{

//์บ๋ฆญํ„ฐ ์ด๋™

moveInputHorizontal = Input.GetAxisRaw("Horizontal");

moveInputVertical = Input.GetAxisRaw("Vertical");

Vector3 targetVelocity = new Vector3(moveInputHorizontal * Time.fixedDeltaTime, moveInputVertical * Time.fixedDeltaTime).normalized * speed;

rb.velocity = Vector3.SmoothDamp(rb.velocity, targetVelocity, ref refVelocity, m_MovementSmoothing);

//animator.SetFloat("Speed", Mathf.Abs(moveInputHorizontal) * speed); // ์ขŒ์šฐ ์• ๋‹ˆ๋ฉ”์ด์…˜


if (facingRight == false && moveInputHorizontal > 0)

{

Flip();

}

else if (facingRight == true && moveInputHorizontal

{

Flip();

}

}


void Update () {

if (health <= 0)

{

if (death == false)

{

Instantiate(effect, transform.position, Quaternion.identity);

//rb.isKinematic = true;

rb.constraints = RigidbodyConstraints2D.FreezePosition;

rb.constraints = RigidbodyConstraints2D.FreezeRotation;

rb.velocity = new Vector2(0, 0);

death = true;

Invoke("RestartGame", 2f);

}

}

}


void RestartGame()

{

SceneManager.LoadScene(SceneManager.GetActiveScene().buildIndex);

}


//์ขŒ์šฐ ๋ฐฉํ–ฅํ‚ค์— ๋”ฐ๋ผ ์บ๋ฆญํ„ฐ ๋’ค์ง‘๊ธฐ

void Flip()

{

facingRight = !facingRight;

Vector2 theScale = transform.localScale;

theScale.x *= -1;

transform.localScale = theScale;

}


์•„๊นŒ ์งˆ๋ฌธ์„ ์˜ฌ๋ ธ๋Š”๋ฐ, ์ฝ”๋“œ ์ „์ฒด๋ฅผ ์˜ฌ๋ ค์•ผ ํ™•์‹คํ•œ ๋‹ต๋ณ€์ด ๋‹ฌ๋ฆด ๊ฒƒ ๊ฐ™์•„์„œ ์ „์ฒด ์˜ฌ๋ ค์—ผ!

์–ด๋–ค ๋ถ„์ด ์—„์ฒญ ์นœ์ ˆํ•˜๊ฒŒ ์„ค๋ช…ํ•ด์ฃผ์‹œ๋ฉด์„œ ํ”„๋กœํผํ‹ฐ ์“ฐ๋ผ๊ณ  ๋ง์”€ํ•ด์ฃผ์…จ๋Š”๋ฐ, ํ”„๋กœํผํ‹ฐ ์จ๋„ ์‹คํ–‰์ด ์•ˆ๋˜๊ณ  ์—…๋ฐ์ดํŠธ ์•ˆ์— ๋„ฃ์–ด๋„ ์‹คํ–‰์ด ์•ˆ๋˜์„œ,

์ผ๋‹จ์€ ์ €์—๊ฒŒ ์ต์ˆ™ํ•œ ์—…๋ฐ์ดํŠธ๋ฌธ ์•ˆ์— ๋„ฃ์–ด์„œ ํ…Œ์ŠคํŠธ๋ฅผ ํ•ด๋ณด๊ณ  ์žˆ๊ธด ํ•œ๋ฐ, ๋‚˜์ค‘์— ์‹คํ–‰์ด ๋˜‘๋ฐ”๋กœ ๋˜๋ฉด ํ”„๋กœํผํ‹ฐ๋กœ ์˜ฎ๊ฒจ๋ณด๋ ค๊ตฌ์š”.


์ผ๋‹จ, ๋ฌธ์ œ๋Š” ์ €๊ธฐ ์—…๋ฐ์ดํŠธ๋ฌธ ์•ˆ์— ์žˆ๋Š” ๋‚ด์šฉ์ž…๋‹ˆ๋‹ค.

์ ์ด๋ž‘ ๋ถ€๋”ซํž ๋•Œ๋งˆ๋‹ค health๋Š” ์ •์ƒ์ ์œผ๋กœ ๊นŽ์ด๊ณ , health๊ฐ€ 0์ด ๋˜๋ฉด death๋„ ์ •์ƒ์ ์œผ๋กœ true๊ฐ€ ๋ฉ๋‹ˆ๋‹ค.

๊ทผ๋ฐ ์บ๋ฆญํ„ฐ ๋ฉˆ์ถ”๊ฒŒ ํ• ๋ ค๋ฉด

rb.constraints = RigidbodyConstraints2D.FreezePosition;

rb.constraints = RigidbodyConstraints2D.FreezeRotation;

rb.velocity = new Vector2(0, 0);

์š”๋ ‡๊ฒŒ ํ•˜๋Š”๊ฑฐ ์•„๋‹Œ๊ฐ€์š”? ใ…Ž;

invoke๋Š” ๋˜ ์ •์ƒ์ ์œผ๋กœ ์‹คํ–‰๋˜์„œ, health๊ฐ€ 0์ด ๋˜๊ณ  2์ดˆ ๊ธฐ๋‹ค๋ฆฌ๋ฉด ์”ฌ์ด ์ดˆ๊ธฐํ™”๋˜๋Š”๋ฐ, ์ €๋ถ€๋ถ„๋งŒ ์ ์šฉ์ด ์•ˆ๋˜๋„ค์šฉ

์ •๋ฆฌํ•˜์ž๋ฉด health๊ฐ€ 0์ด ๋˜๊ณ  ๋‚˜์„œ๋„ ์บ๋ฆญํ„ฐ๋Š” ๊ณ„์† ์›€์ง์ผ ์ˆ˜ ์žˆ๊ณ , ๊ทธ๋ ‡๊ฒŒ ๋ง‰ ์›€์ง์ด๋‹ค๊ฐ€ 2์ดˆ ์ง€๋‚˜๋ฉด ์”ฌ์ด ์žฌ์‹œ์ž‘๋ผ์š”

์”นํ—ˆ์ ‘์ด๋ผ ์•„์ง ์œ„ ์ฝ”๋“œ์˜ ๋ฌธ์ œ์ ์ด ๋ญ”์ง€ ๋ชจ๋ฅด๊ฒ ๋„ค์—ผ

์•Œ๋ ค์ฃผ์Ž„์—ผ