using UnityEngine;
using System.Collections;

public class Movable : MonoBehaviour {
	
	public float speed = 6f;

	protected float otherSpeed = 1f;

	protected Rigidbody2D rb;
	protected SpriteRenderer sr;
	protected Animator anim;
	protected bool walking;

	public Icon icon;

	protected virtual void Start () {
		rb = GetComponent<Rigidbody2D>();
		sr = GetComponent<SpriteRenderer>();
		anim = GetComponent<Animator>();
		icon = GetComponent<Icon>();
	}

	protected void LateUpdate () {
		sr.sortingOrder = -Mathf.FloorToInt(transform.position.y * 25);
		bool walkingNow = (rb.velocity != Vector2.zero);
		if (walkingNow != walking) {
			walking = walkingNow;
			anim.SetBool("Walking", walking);
		}
	}

	public void Move (Vector2 move) {
		rb.velocity = speed * move * otherSpeed;
		if (Mathf.Abs(move.x) > 0.1f)
			transform.localScale = new Vector3(move.x > 0 ? 1 : -1, 1, 1);
	}

	public virtual void OnEnterArea (Area area) { }
	public virtual void OnExitArea (Area area) { }
}
