using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class PlayerMove : MonoBehaviour {

CapsuleCollider2D capsule_pl;
public GameManager gameManager;
public float maxSpeed;
public float jumpPower;
Rigidbody2D rigid;
SpriteRenderer spriteRenderer;
Animator anim;
int up_Value;
int left_Value;
int right_Value;
bool up_Down;
bool left_Down;
bool right_Down;
bool up_Up;
bool left_Up;
bool right_Up;
void Awake () {
rigid = GetComponent ();
spriteRenderer = GetComponent ();
anim = GetComponent ();
capsule_pl = GetComponent ();
}

void Update () {
//์ ํ”„
if (Input.GetButtonDown ("Jump") && !anim.GetBool ("isjumping") || up_Down && !anim.GetBool ("isjumping")) {
rigid.AddForce (Vector2.up * jumpPower, ForceMode2D.Impulse);
anim.SetBool ("isjumping", true);
}

//๋ฉˆ์ถ”๋Š” ํž˜

if (Input.GetButtonUp ("Horizontal")) {
rigid.velocity = new Vector2 (rigid.velocity.normalized.x * 0.5f, rigid.velocity.y);
}

//๋ฐฉํ–ฅ ์ „ํ™˜

if (Input.GetButton ("Horizontal") || left_Down || right_Down) {
spriteRenderer.flipX = Input.GetAxisRaw ("Horizontal") == -1;
}

if (Mathf.Abs (rigid.velocity.x))
anim.SetBool ("iswalking", false);
else
anim.SetBool ("iswalking", true);
up_Down = false;
up_Up = false;
left_Down = false;
left_Up = false;
right_Down = false;
right_Up = false;

}

void FixedUpdate () {
//๊ฐ€์†๋„
float h = Input.GetAxisRaw ("Horizontal") + right_Value + left_Value;
rigid.AddForce (Vector2.right * h * 2, ForceMode2D.Impulse);
if (rigid.velocity.x > maxSpeed) //Right Max Speed
rigid.velocity = new Vector2 (maxSpeed, rigid.velocity.y);
else if (rigid.velocity.x) rigid.velocity = new Vector2 (maxSpeed * (-1), rigid.velocity.y);
//์ฐฉ์ง€
if (rigid.velocity.y) {
Debug.DrawRay (rigid.remove_style_tag);
rigid.AddForce (new Vector2 (dirc, 2) * 7, ForceMode2D.Impulse);
//์• ๋‹ˆ๋ฉ”์ด์…˜
anim.SetTrigger ("Damaged");
Invoke ("offDamaged", 3);
}
}

void offDamaged () {
gameObject.layer = 10;
spriteRenderer.color = new Color (1, 1, 1, 1);
}

public void OnDie () {
spriteRenderer.color = new Color (1, 1, 1, 0.4f);
spriteRenderer.flipY = true;
capsule_pl.enabled = false;
rigid.AddForce (Vector2.up * 30, ForceMode2D.Impulse);
}

public void VelocityZero () {
rigid.velocity = Vector2.zero;
}
public void ButtonDown (string type) {
switch (type) {
case "U":
up_Value = 1;
up_Down = true;
break;
case "R":
right_Value = 1;
right_Down = true;
break;
case "L":
left_Value = -1;
left_Down = true;
break;
}
}

public void ButtonUp (string type) {
switch (type) {
case "U":
up_Value = 0;
up_Up = true;
break;
case "R":
right_Value = 0;
right_Up = true;
break;
case "L":
left_Value = 0;
left_Up = true;
break;
}
}
}