1
Fork 0
slime-blood-and-pain/Assets/Scripts/EntityMonsterSlime.cs

58 lines
2.1 KiB
C#
Raw Normal View History

2019-04-27 20:19:35 +00:00
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
2019-04-28 14:34:50 +00:00
public class EntityMonsterSlime : EntityMonster
2019-04-27 20:19:35 +00:00
{
2019-04-27 23:34:30 +00:00
public float moveChance = 0.5f;
2019-04-27 20:19:35 +00:00
public float visionRange = 4;
2019-04-28 14:34:50 +00:00
protected EntityPlayer player;
2019-04-27 20:19:35 +00:00
protected new void Start() {
base.Start();
2019-04-28 14:34:50 +00:00
player = GameObject.FindGameObjectWithTag("Player").GetComponent<EntityPlayer>();
2019-04-27 20:19:35 +00:00
}
public override void OnTurn(){
2019-04-27 23:34:30 +00:00
if(Random.Range(0f, 1f) < moveChance) return;
2019-04-27 20:19:35 +00:00
if (CanSeePlayer()){
2019-04-27 22:29:23 +00:00
Vector2Int distance = player.MapPosition - MapPosition;
if (distance.x < 0 && map.CanMoveTo(MapPosition + Vector2Int.left)){
transform.Translate(Vector3.left);
}
else if (distance.x > 0 && map.CanMoveTo(MapPosition + Vector2Int.right)){
transform.Translate(Vector3.right);
}
else if (distance.y > 0 && map.CanMoveTo(MapPosition + Vector2Int.up)){
transform.Translate(Vector3.up);
}
else if (distance.y < 0 && map.CanMoveTo(MapPosition + Vector2Int.down)){
transform.Translate(Vector3.down);
}
2019-04-27 20:19:35 +00:00
}
else {
int direction = Random.Range(0, 4);
if (direction == 0 && map.CanMoveTo(MapPosition + Vector2Int.left)){
transform.Translate(Vector3.left);
}
else if (direction == 1 && map.CanMoveTo(MapPosition + Vector2Int.right)){
transform.Translate(Vector3.right);
}
else if (direction == 2 && map.CanMoveTo(MapPosition + Vector2Int.up)){
transform.Translate(Vector3.up);
}
else if (direction == 3 && map.CanMoveTo(MapPosition + Vector2Int.down)){
transform.Translate(Vector3.down);
}
}
}
public bool CanSeePlayer(){
return Vector3.Distance(player.transform.position, transform.position) < visionRange;
}
private void OnDrawGizmosSelected() {
Gizmos.color = Color.cyan;
Gizmos.DrawWireSphere(transform.position, visionRange);
}
}