207 lines
5.4 KiB
C#
Executable file
207 lines
5.4 KiB
C#
Executable file
using System.Collections;
|
|
using System.Collections.Generic;
|
|
using UnityEngine;
|
|
using UnityEngine.SceneManagement;
|
|
using Unity.Mathematics;
|
|
using UnityEngine.UIElements;
|
|
using System;
|
|
|
|
public class EnityControl : MonoBehaviour
|
|
{
|
|
public int EID;
|
|
public int MinDamage;
|
|
public int MaxDamage;
|
|
public float Speed;
|
|
float SpeedNow;
|
|
public float YSpeed;
|
|
float YSpeedNow;
|
|
public int OnAttackSpeed;
|
|
public Transform PatrolPoint;
|
|
Transform Player;
|
|
public float PatrolDistance;
|
|
public float ViewDistane;
|
|
private SpriteRenderer Sp;
|
|
public int Lives;
|
|
public int kick;
|
|
public ParticleSystem DamagePart;
|
|
public Slider Healt;
|
|
|
|
Rigidbody2D rb;
|
|
|
|
public Animator Animator;
|
|
public bool flip;
|
|
|
|
bool MoveRight = true;
|
|
public int Damage;
|
|
public int FinDamage;
|
|
|
|
private bool chill = false;
|
|
private bool angry = false;
|
|
private bool back = false;
|
|
|
|
|
|
public Move PL;
|
|
public GameObject Money;
|
|
public ArmorOnPlayer Armor;
|
|
int ThisScene;
|
|
bool started;
|
|
|
|
private Vector3 it;
|
|
|
|
private void Start()
|
|
{
|
|
started = true;
|
|
ThisScene = SceneManager.loadedSceneCount;
|
|
Sp = GetComponent<SpriteRenderer>();
|
|
rb = GetComponent<Rigidbody2D>();
|
|
Player = GameObject.Find("player").transform;
|
|
Animator = GetComponent<Animator>();
|
|
it = this.gameObject.transform.position;
|
|
ThisScene = SceneManager.loadedSceneCount;
|
|
try
|
|
{
|
|
Armor = GameObject.Find("BodyPlayer").GetComponent<ArmorOnPlayer>();
|
|
}
|
|
catch { Debug.Log("Old"); }
|
|
if (Armor == null)
|
|
{
|
|
|
|
}
|
|
}
|
|
|
|
public void Atack()
|
|
{
|
|
Animator.SetTrigger("Atack");
|
|
}
|
|
public void OnTriggerEnter2D(Collider2D collision)
|
|
{
|
|
if (collision.tag == "Player")
|
|
{
|
|
Atack();
|
|
int damage = UnityEngine.Random.Range(MinDamage, MaxDamage);
|
|
|
|
if (Armor != null)
|
|
{
|
|
FinDamage = (damage - GameObject.Find("BodyPlayer").GetComponent<ArmorOnPlayer>().HeadArmorSafe);
|
|
if(FinDamage <0){FinDamage = 0;}
|
|
collision.gameObject.GetComponent<Move>().Lives -=FinDamage;
|
|
}
|
|
else { Player.gameObject.GetComponent<Move>().Lives -= damage; }
|
|
|
|
}
|
|
if(collision.tag == "Sword" && Armor.Attack >0)
|
|
{
|
|
Lives -= Armor.Attack;
|
|
DamagePart.Play();
|
|
}
|
|
}
|
|
|
|
public void Update()
|
|
{
|
|
if(Player != null){
|
|
rb.linearVelocity = new Vector2(SpeedNow, rb.linearVelocity.y);
|
|
if (Vector3.Distance(Player.position, transform.position) < ViewDistane)
|
|
{
|
|
angry = true;
|
|
if (Player.position.x > transform.position.x)
|
|
{
|
|
OnRightButtonDown();
|
|
}
|
|
else
|
|
{
|
|
OnLeftButtonDown();
|
|
}
|
|
|
|
print("amgry");
|
|
}
|
|
else
|
|
{
|
|
if (Vector3.Distance(transform.position, PatrolPoint.position) > PatrolDistance && angry)
|
|
{
|
|
if (Vector3.Distance(transform.position, PatrolPoint.position) > PatrolDistance)
|
|
{
|
|
if (PatrolPoint.position.x > transform.position.x)
|
|
{
|
|
OnRightButtonDown();
|
|
}
|
|
else
|
|
{
|
|
OnLeftButtonDown();
|
|
}
|
|
}
|
|
print("Back");
|
|
}
|
|
else
|
|
{
|
|
if (transform.position.x - PatrolPoint.position.x > PatrolDistance / 2 && !angry)
|
|
{
|
|
MoveRight = false;
|
|
}
|
|
else if (transform.position.x - PatrolPoint.position.x < -PatrolDistance / 2) { MoveRight = true; }
|
|
if (!MoveRight)
|
|
{
|
|
OnLeftButtonDown();
|
|
}
|
|
else { OnRightButtonDown(); }
|
|
print("chill");
|
|
}
|
|
angry = false;
|
|
}
|
|
if (Lives <=0 )
|
|
{
|
|
Animator.SetTrigger("die");
|
|
Invoke("Dest", 1f);
|
|
}
|
|
}
|
|
}
|
|
public void OnLeftButtonDown()
|
|
{
|
|
Sp.flipX = false;
|
|
if (SpeedNow >= -Speed)
|
|
{
|
|
SpeedNow = -Speed;
|
|
}
|
|
Animator.SetTrigger("walk");
|
|
}
|
|
|
|
public void OnRightButtonDown()
|
|
{
|
|
Sp.flipX = true;
|
|
if (SpeedNow <= Speed)
|
|
{
|
|
SpeedNow = Speed;
|
|
}
|
|
Animator.SetTrigger("walk");
|
|
}
|
|
|
|
public void OnButtonUp()
|
|
{
|
|
SpeedNow = 0;
|
|
Animator.SetTrigger("stand");
|
|
}
|
|
public void BJump()
|
|
{
|
|
rb.linearVelocity = Vector2.up * kick;
|
|
}
|
|
|
|
public void OnFlyButtonDown()
|
|
{
|
|
rb.linearVelocity = new Vector2( rb.linearVelocity.x, YSpeedNow);
|
|
}
|
|
|
|
void Dest()
|
|
{
|
|
Destroy(this.gameObject);
|
|
}
|
|
void OnDestroy()
|
|
{
|
|
if(ThisScene == SceneManager.loadedSceneCount && started)
|
|
{
|
|
Instantiate(Money, transform.position, new Quaternion(0,0,0,0));
|
|
List<int> KE = Currencies.Load("KE");
|
|
if(KE.Count < 10 ){while (KE.Count <10) KE.Add(0);}
|
|
KE[EID]++;
|
|
Currencies.Save("KE", KE);
|
|
}
|
|
}
|
|
}
|