using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Villager : MonoBehaviour
{
[SerializeField] private float speed = 1f; //villager speed
[SerializeField] private int lives = 3; // villager lives
private Animator anim;
private SpriteRenderer sprite;
private States State
{
get { return (States)anim.GetInteger("State"); }
set { anim.SetInteger("State", (int)value); }
}
private void Awake()
{
anim = GetComponent<Animator>();
sprite = GetComponentInChildren<SpriteRenderer>();
}
private void Start()
{
State = States.idle;
StartCoroutine(Walk());
}
private void Update()
{
}
IEnumerator Walk()
{
State = States.walk;
StartCoroutine(WalkRight());
yield return new WaitForSeconds(5f);
StartCoroutine(WalkLeft());
}
IEnumerator WalkRight()
{
Vector3 direction = transform.right * 3f;
transform.position = Vector3.MoveTowards(transform.position, transform.position + direction, speed * Time.deltaTime);
sprite.flipX = transform.position.x > 0.0f;
yield return new WaitForSeconds(4f);
}
IEnumerator WalkLeft()
{
Vector3 direction = transform.right * -3f;
transform.position = Vector3.MoveTowards(transform.position, transform.position + direction, speed * Time.deltaTime);
sprite.flipX = transform.position.x < 0.0f;
yield return new WaitForSeconds(5f);
}
public void GetDamage()
{
lives -= 1;
Debug.Log("Villager" + lives);
}
public enum States
{
idle,
walk
}
}