182 lines
5.3 KiB
C#
182 lines
5.3 KiB
C#
using Assets.Scripts;
|
|
using Assets.Scripts.Player;
|
|
using Assets.Scripts.Slimes;
|
|
using Newtonsoft.Json.Linq;
|
|
using System;
|
|
using System.Collections;
|
|
using System.Collections.Generic;
|
|
using UnityEngine;
|
|
using System.Diagnostics;
|
|
|
|
namespace Assets.Scripts.InteractableObjects
|
|
{
|
|
public class Enemy : InteractableObject
|
|
{
|
|
System.Random rand = new System.Random();
|
|
BasicSlime slime;
|
|
SlimeFactory factory = new SlimeFactory();
|
|
public SlimeType slimeType;
|
|
bool isJumping;
|
|
Stopwatch jumpTimer;
|
|
bool followsPlayer;
|
|
bool isAttacking;
|
|
Stopwatch attackTimer;
|
|
|
|
// Start is called before the first frame update
|
|
void Start()
|
|
{
|
|
isJumping = true;
|
|
jumpTimer = new Stopwatch();
|
|
jumpTimer.Start();
|
|
followsPlayer = false;
|
|
isAttacking = false;
|
|
attackTimer = new Stopwatch();
|
|
//Nothing
|
|
}
|
|
|
|
// Update is called once per frame
|
|
void Update()
|
|
{
|
|
if (gameObject.activeSelf)
|
|
{
|
|
if (!isJumping)
|
|
{
|
|
jumpTimer.Start();
|
|
isJumping = true;
|
|
Vector3 jumpVector;
|
|
if (followsPlayer)
|
|
{
|
|
jumpVector = GameObject.Find("Player").transform.position - gameObject.transform.position;
|
|
jumpVector *= 0.25f * Mathf.Log10(jumpVector.magnitude);
|
|
jumpVector.y = 5;
|
|
}
|
|
else
|
|
{
|
|
jumpVector = new Vector3(rand.Next(-5, 5), 10, rand.Next(-5, 5));
|
|
}
|
|
gameObject.GetComponent<Rigidbody>().AddForce(jumpVector, ForceMode.Impulse);
|
|
}
|
|
if (jumpTimer.ElapsedMilliseconds >= 5000)
|
|
{
|
|
jumpTimer.Reset();
|
|
isJumping = false;
|
|
}
|
|
if (gameObject.transform.position.x > 1000 || gameObject.transform.position.x > 5000 || gameObject.transform.position.x > 5000)
|
|
{
|
|
UnityEngine.Debug.Log("BUG!");
|
|
}
|
|
}
|
|
}
|
|
|
|
void OnTriggerExit(Collider col)
|
|
{
|
|
if (col.name.Contains("_") && col.gameObject == gameObject.transform.parent.gameObject)
|
|
{
|
|
gameObject.GetComponent<Rigidbody>().AddForce(gameObject.GetComponent<Rigidbody>().linearVelocity * -2, ForceMode.Impulse);
|
|
}
|
|
}
|
|
|
|
public void handleAttack()
|
|
{
|
|
isAttacking = true;
|
|
//Create attack logic here
|
|
}
|
|
|
|
public void handleFollow(bool isFollowing)
|
|
{
|
|
followsPlayer = isFollowing;
|
|
isAttacking = false;
|
|
//Stop attacks
|
|
}
|
|
|
|
public void handleDetection()
|
|
{
|
|
followsPlayer = true;
|
|
}
|
|
|
|
public override void handleInteraction(GameObject player)
|
|
{
|
|
keepAlive = true;
|
|
}
|
|
|
|
public void scaleEnemy(PlayerGameObject player)
|
|
{
|
|
if (slime == null)
|
|
{
|
|
switch (slimeType)
|
|
{
|
|
case SlimeType.MAGE:
|
|
slime = factory.generateMageSlime(player);
|
|
break;
|
|
case SlimeType.NORMAL:
|
|
slime = factory.generateNormalSlime(player);
|
|
break;
|
|
case SlimeType.METAL:
|
|
slime = factory.generateMetalSlime(player);
|
|
break;
|
|
case SlimeType.WARRIOR:
|
|
slime = factory.generateWarriorSlime(player);
|
|
break;
|
|
case SlimeType.FOREST:
|
|
slime = factory.generateForestSlime(player);
|
|
break;
|
|
case SlimeType.BOSS:
|
|
slime = factory.generateBossSlime(player);
|
|
break;
|
|
}
|
|
}
|
|
}
|
|
|
|
public int[] getStats()
|
|
{
|
|
return slime.getStats();
|
|
}
|
|
|
|
public string getEnemyName()
|
|
{
|
|
return slimeType.ToString().ToLower() + " slime";
|
|
}
|
|
|
|
public int calculateDamage()
|
|
{
|
|
return slime.calculateDamage(rand);
|
|
}
|
|
|
|
public int calculateHeavy()
|
|
{
|
|
return slime.calculateHeavy(rand);
|
|
}
|
|
|
|
public bool takeDamage(int amount)
|
|
{
|
|
return slime.takeDamage(amount, rand);
|
|
}
|
|
|
|
public int getExperience()
|
|
{
|
|
return slime.getExperience();
|
|
}
|
|
|
|
public Item getItem()
|
|
{
|
|
return slime.getItem();
|
|
}
|
|
|
|
public string saveEnemy()
|
|
{
|
|
string result = "";
|
|
result = result + FileHandler.generateJSON("enemytype", "\"" + slimeType.ToString().ToLower() + "\"");
|
|
if (slime != null)
|
|
{
|
|
result = result + ",\r\n" + slime.saveSlime();
|
|
}
|
|
return result;
|
|
}
|
|
|
|
public void loadEnemy(JToken json)
|
|
{
|
|
slime = new BasicSlime(json);
|
|
slimeType = (SlimeType)Enum.Parse(typeof(SlimeType), json["enemytype"].ToString());
|
|
}
|
|
}
|
|
} |