<file_sep>﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class EnemyAttack : MonoBehaviour
{
    // Start is called before the first frame update
    Animator animator;
    public Transform attackPoint;
    public float attackRange;
    public LayerMask playerLayer;
    new Rigidbody rigidbody;
    public AnimationClip attackAnimation;

    float nextAttackTime = 0f;
    void Start()
    {
        rigidbody = GetComponent<Rigidbody>();
        animator = GetComponent<Animator>();
    }

    // Update is called once per frame
    public void Attack(){
        rigidbody.velocity = Vector3.zero;//stop enemy moving
        Collider[] players = Physics.OverlapSphere(attackPoint.position, attackRange, playerLayer);
         if (Time.time >= nextAttackTime)
        {    
            // call attack animation
            animator.SetTrigger("Attack");
            Debug.Log("enemy attack");

            foreach (var player in players)
            {
                player.GetComponentInParent<Player>().TakeDamaged(1);
            }     
            nextAttackTime = Time.time + attackAnimation.length;
        }
    }

    private void OnDrawGizmosSelected() {
        Gizmos.DrawSphere(attackPoint.position, attackRange);
    }
}
<file_sep>﻿using UnityEngine;
using UnityEngine.SceneManagement;
public class GameManager : MonoBehaviour
{
    public bool isGameEnded = false;
    GameObject[] enemies;

    Animator gameOverAnim;
    private void Start() {
        
        gameOverAnim = GameObject.Find("TransitionScreen").transform.GetComponent<Animator>();
    }
    public void GameOver(){
        gameOverAnim.SetTrigger("GameOver");//trigger fade out animation)
    }
    public void Winning(){
        gameOverAnim.SetTrigger("Winning");//trigger fade out animation)
    }

    public void LoadGameOverScene(){//use when gameoverAnim is ended
        SceneManager.LoadScene("GameOver");
    } 
    public void LoadWinningScene(){//use when gameoverAnim is ended
        SceneManager.LoadScene("Winning");
    } 



    private void Update() {
        enemies = GameObject.FindGameObjectsWithTag("Enemy");
        if(enemies.Length == 0){
            Winning();//if enemies are 0 game over 
        }
    }
}<file_sep>﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class HealthBarBehaviour : MonoBehaviour
{

    public Color low;
    public Color high;
    public Vector3 offset;
    public bool isEnemyHealthBar = false;
    public Slider slider;

    public void SetMaxHealth(float maxhealth){
        slider.maxValue = maxhealth;
        slider.value = maxhealth;
    }
    public void SetHealth(float health){
        
        // slider.gameObject.SetActive(health < maxhealth);//show healthbar when enemy healthbar is not full
        slider.value = health;
    }
    // Update is called once per frame
    void Update()
    {
        if (isEnemyHealthBar)
        {//if this health used by enemy object
            Vector3 position = Camera.main.WorldToScreenPoint(transform.parent.position + offset);
            slider.transform.position = position;
        }
    }

    public void SetHealthBarActivated(bool condition){//display health bar depend on some condition
        slider.gameObject.SetActive(condition);
    }
}   
<file_sep>﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class PlayerCombat : MonoBehaviour
{
    // Start is called before the first frame update
    public Transform Sword;
    public Animator swordAnimator;
    public Transform trailRenderer;
    public LayerMask enemyLayer;

    public Transform attackPoint;
    public float attackRange;
    void Start()
    {
        // Sword = gameObject.transform.GetChild(0).gameObject.transform.GetChild(0).gameObject;
        Sword.GetComponent<SpriteRenderer>().enabled = false;
    }

    // Update is called once per frame
    void Update()
    {
        if (Input.GetAxis("Fire1") > 0)
        {
            Sword.GetComponent<SpriteRenderer>().enabled = true;
            trailRenderer.GetComponent<TrailRenderer>().enabled = true;

            Attack();
        }   
    }

    public void EnableRenderer(){
        Sword.GetComponent<SpriteRenderer>().enabled = false;
        trailRenderer.GetComponent<TrailRenderer>().enabled = false;
    }

    void Attack(){
        swordAnimator.SetTrigger("Attack");
        Collider[] enemies = Physics.OverlapSphere(attackPoint.position, attackRange, enemyLayer);

        foreach (var enemy in enemies)
        {
            Debug.Log(enemy.name);
            enemy.GetComponent<Enemy>().TakeDamaged(5);
        }
    }

    private void OnDrawGizmosSelected() {
        Gizmos.DrawSphere(attackPoint.position, attackRange);
    }
}
<file_sep>﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class PlayerMovement : MonoBehaviour
{
    public float speed;
    public Rigidbody playerRb;
    public Vector3 movement;

    public GameObject playerSprite;

    GameObject playerpointLight;
    bool isFlipped = false; 

    bool IsGrounded = false;//boolean when player collide with ground
    Vector3 jumpVector = new Vector3(0,1,0);
    Vector3 newPlayerLocalScale;//reference for new player localscale
    public float jumpForce;
    Animator animator;//animator for player
    public Animator camAnim;

    public ParticleSystem landingParticle;
    public ParticleSystem runParticle;
    // Start is called before the first frame update
    void Start()
    {
        animator = transform.GetChild(0).GetComponent<Animator>();
        playerRb = GetComponent<Rigidbody>();
        newPlayerLocalScale = new Vector3(1,1,1);
    }

    // Update is called once per frame
    void Update()
    {
        movement.x = Input.GetAxis("Horizontal");
        movement.z = Input.GetAxis("Vertical");
        

        RotateDirection(movement.x);
        movement*= speed;
        //intantiate run animation
        RunAnimate(movement.x, movement.z);

        if (Input.GetKeyDown(KeyCode.Space) && IsGrounded)
        {
            IsGrounded = false;//change isGrounded value to false then jump
            Jump();
        }
    }

    void RunAnimate(float movementX, float movementZ){
        int speed = 0;//speed variable to instantiate run animation
        var movementXAbs = Mathf.Abs(movementX);//get absolute value from x axis
        var movementZAbs = Mathf.Abs(movementZ);//get absolute value from z axis
        speed = movementXAbs > 0 || movementZAbs > 0? 1: -1; // if movement X or Z abs greeater than 0, then assigned 1 to speed variable, and otherwise assign -
        animator.SetFloat("Run",speed);
    }

    private void FixedUpdate() {
        playerRb.velocity = new Vector3(movement.x, playerRb.velocity.y, movement.z);
    }
    private void RotateDirection(float xAxis) {
        if (xAxis < 0 && !isFlipped)
        {//change variable reference value base on player x axis value(right or left move)
            isFlipped = true;
            newPlayerLocalScale = new Vector3(-1,1,1); 
        }
        if(xAxis > 0 && isFlipped){
            isFlipped = false;
            newPlayerLocalScale = new Vector3(1,1,1); 
        }
        FlipPlayerObject(newPlayerLocalScale);
    }


    private void FlipPlayerObject(Vector3 newLocalScale){
        playerSprite.transform.localScale = newLocalScale;
    }


    private void OnCollisionStay(Collision collider) {
        if (collider.gameObject.tag == "Ground")
        {
            if(!runParticle.isEmitting){//if run particle is not emmiting play particle
                runParticle.Play();
            }
            IsGrounded = true;
        }
    }

    private void OnCollisionExit(Collision collider) {
        if (collider.gameObject.tag == "Ground")
        {//stop particle when player jump on ground
            if(runParticle.isEmitting){//if run particle is emmiting stop particle
                runParticle.Stop();
            }
            IsGrounded = false;
        }
    }

    private void OnCollisionEnter(Collision collider) {
        if (collider.gameObject.tag == "Ground")
        {
            if (!landingParticle.isEmitting)//check wheter landing particle is not emiting 
            {
                //camera shake effect
                Camera.main.GetComponent<FollowPlayer>().Shake(1f, 10);
                landingParticle.Play();//play landing particle
            }
        }
    }



    private void Jump(){
        playerRb.AddForce(Vector3.up*jumpForce, ForceMode.Impulse);
    }
    


}
<file_sep>﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class EnemyDetection : MonoBehaviour
{
    Transform PlayerObject;
    float distanceToPlayer;
    new Rigidbody rigidbody;
    Animator animator;

    float enemyAttackRange;
    public float enemyChaseRange = 2.4f;
    public float moveSpeed = 1f;
    bool isFlipped = false;
    Vector3 newLocalScale;


    // Start is called before the first frame update
    void Start()
    {
        newLocalScale = GetComponent<Transform>().localScale;
        enemyAttackRange = GetComponent<EnemyAttack>().attackRange;
        animator = GetComponent<Animator>();
        rigidbody = GetComponent<Rigidbody>();
        PlayerObject = GameObject.Find("PlayerObject").transform.GetChild(0);

    }

    // Update is called once per frame
    void Update()
    {
        distanceToPlayer = Vector3.Distance(PlayerObject.transform.position, transform.position);//get distance from player to enemy
     
        // Debug.Log("distance between "+transform.name+" and player is"+distanceToPlayer);
        if(distanceToPlayer < enemyChaseRange){

            ChasePlayer();
            if(distanceToPlayer < enemyAttackRange){
                StopChase();
                GetComponent<EnemyAttack>().Attack();
            }        
        }else{
            StopChase();
        }
    }
    private void FlipPlayerObject(Vector3 newLocalScale){
       transform.localScale = newLocalScale;
    }

    void ChasePlayer(){
        // Debug.Log("im chasing you");
        var newpos = Vector3.MoveTowards(rigidbody.position, PlayerObject.position, moveSpeed*Time.deltaTime);
        
        // change enemy faced
        if (PlayerObject.transform.position.x > transform.position.x && !isFlipped){
            isFlipped = true;
            newLocalScale = new Vector3(-newLocalScale.x,newLocalScale.y,newLocalScale.z); 
        }
        if(PlayerObject.transform.position.x < transform.position.x && isFlipped){
            isFlipped = false;
            newLocalScale = new Vector3(Mathf.Abs(newLocalScale.x),newLocalScale.y,newLocalScale.z); 
        }
        FlipPlayerObject(newLocalScale);//asign new local scale

        //start running animation
        animator.SetFloat("Running", 1);
        rigidbody.MovePosition(newpos);
    }

    void StopChase(){
        rigidbody.velocity = Vector3.zero;
        //stop running animation
        animator.SetFloat("Running", -1);
    }
}
<file_sep>using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class Enemy : MonoBehaviour
{
    // Start is called before the first frame update
    public float maxhealth;
    float currentHealth;
    new Rigidbody rigidbody;
    public ParticleSystem walkParticle;
    public ParticleSystem landingParticle;

    public ParticleSystem dieParticle;

    public Animator animator;

    public HealthBarBehaviour healthBar;
    public AudioSource bloodSfx;
    void Start()
    {
        rigidbody = GetComponent<Rigidbody>();
        currentHealth = maxhealth;
        //set halthbar to enemy
        healthBar.SetMaxHealth(maxhealth);

    }

    private void Update() {
        healthBar.SetHealthBarActivated(currentHealth < maxhealth);
    }

    private void FixedUpdate() {
        if (transform.position.y < -3)
        {
            Die();
        }
    }

    public void TakeDamaged(int damage){
        animator.SetTrigger("Attacked");
        //Decrease enemy health
        currentHealth -= damage;
        //set healthbar value
        healthBar.SetHealth(currentHealth);
        //knock back enemy
        rigidbody.AddForce(Vector3.up*1f, ForceMode.Impulse);
        
        
        if(currentHealth <= 0){
            if(!dieParticle.isEmitting){
                dieParticle.Play();//start blood particle
                bloodSfx.Play();//play sfx
            }
            Die();
        }
    }

    void Die(){
        Debug.Log("die");
        Destroy(gameObject, dieParticle.main.duration);
    }
    

    private void OnCollisionStay(Collision other) {
        if(other.gameObject.tag == "Ground"){
            if(!walkParticle.isEmitting){
                //if particle not emmiting play particle
                walkParticle.Play();
            }
        }
    }
    private void OnCollisionExit(Collision other) {
        if(other.gameObject.tag == "Ground"){
            if(walkParticle.isEmitting){
                walkParticle.Stop();//stop walk particle
            }
            if(!landingParticle.isEmitting){
                landingParticle.Play();//play landing particle
            }
        }
    }

}
<file_sep>﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Player : MonoBehaviour
{
    // Start is called before the first frame update
    public float maxhealth = 100;
    float currentHealth;
    new Rigidbody rigidbody;
    public HealthBarBehaviour healthBar;

    public Animator animator;

    public AudioSource bgMusic;
    void Start()
    {
        rigidbody = GetComponent<Rigidbody>();
        currentHealth = maxhealth;
        healthBar.SetMaxHealth(maxhealth);
    }

    private void FixedUpdate() {
        if (transform.position.y < -3)
        {
            Die();
        }
    }
    public void tes(){
        Debug.Log("kasjdlsad");
    }
    public void TakeDamaged(int damage){
        animator.SetTrigger("Damaged");
        //Decrease player health
        currentHealth -= damage;
        //set healthbar value
        healthBar.SetHealth(currentHealth);
        //knock back player
        // rigidbody.AddForce(new Vector3(rigidbody.position.x, 0, rigidbody.position.z));
        rigidbody.AddForce(Vector3.up*2f, ForceMode.Impulse);
        
        if(currentHealth <= 0){
            Die();
        }
    }

    void Die(){
        Debug.Log("die");
        bgMusic.Stop();
        Destroy(gameObject);
        GameObject.Find("TransitionScreen").transform.GetComponent<GameManager>().GameOver();//call gameover fucntion from gamemanager script
    }

}
<file_sep>﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class GrassMoving : MonoBehaviour
{
    // Start is called before the first frame update

    Animator animator;
    void Start()
    {
        animator = GetComponent<Animator>();
    }

    // Update is called once per frame
    // private void OnTriggerEnter(Collider collider) {
    //     if (collider.gameObject.tag == "Player"){
    //         //when player collide with grass trigger "forced" grass animation
    //         animator.SetTrigger("TouchedPlayer"); 
    //     }
    // }
}
<file_sep>﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class FollowPlayer : MonoBehaviour
{
    public Transform playerTransform;
    new Camera camera;
    public Vector3 offSet; 
    // Start is called before the first frame update
    void Start()
    {
        camera = Camera.main;
    }

    

    // Update is called once per frame
    void Update()
    {
        camera.transform.position = playerTransform.position + offSet;
    }

    public IEnumerator Shake(float duration, float magnitude){
        Vector3 originalPos = transform.localPosition;
        float elapse = 0.5f;

        while (elapse <duration)
        {
            float x = Random.Range(-1,1) * magnitude;
            float y = Random.Range(-1,1) * magnitude;

            transform.localPosition = new Vector3(x,y, originalPos.z);
            elapse += Time.deltaTime;//increment sec time
            yield return null;
        }

        transform.localPosition = originalPos;
    }
}
<file_sep>﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class DecorationBehavior : MonoBehaviour
{
    // Start is called before the first frame update
    Color transparent;
    Color normal;
    void Start()
    {
        transparent = GetComponent<SpriteRenderer>().color;
        normal = GetComponent<SpriteRenderer>().color;
        transparent.a = 0.5f;
        normal.a = 1f;
    }

    private void OnTriggerStay(Collider collider) {
        if (collider.gameObject.tag == "Player")
        {
            GetComponent<SpriteRenderer>().color = transparent;
        }
    }
    private void OnTriggerExit(Collider collider) {
        if (collider.gameObject.tag == "Player")
        {
            GetComponent<SpriteRenderer>().color = normal;
        }
        
    }

    // Update is called once per frame
    void Update()
    {
        
    }
}
