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

81 lines
3 KiB
C#
Raw Permalink 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-28 16:00:11 +00:00
public float visionRange = 4f;
2019-04-29 13:27:26 +00:00
public float attackRange = 1f;
public float damage = 1f;
2019-04-29 08:39:48 +00:00
public GameObject attackAnimation;
2019-04-28 14:34:50 +00:00
protected EntityPlayer player;
2019-04-27 20:19:35 +00:00
2019-04-29 17:53:45 +00:00
[BeforeStartAttribute]
public Sprite upSprite;
[BeforeStartAttribute]
public Sprite downSprite;
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-29 17:53:45 +00:00
if(Random.value < 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;
2019-04-29 13:27:26 +00:00
if(distance.magnitude <= attackRange) {
float actualDamage = Random.value * damage;
player.hp -= actualDamage;
Instantiate(attackAnimation, player.transform);
2019-04-29 17:53:45 +00:00
messageBar.Write("Took " + actualDamage.ToString("0.0") + " damage!", Color.red);
2019-04-29 13:27:26 +00:00
}
else if (distance.x < 0 && map.CanMoveTo(MapPosition + Vector2Int.left)){
2019-04-27 22:29:23 +00:00
transform.Translate(Vector3.left);
2019-04-29 17:53:45 +00:00
spriteRenderer.flipX = false;
2019-04-27 22:29:23 +00:00
}
else if (distance.x > 0 && map.CanMoveTo(MapPosition + Vector2Int.right)){
transform.Translate(Vector3.right);
2019-04-29 17:53:45 +00:00
spriteRenderer.flipX = true;
2019-04-27 22:29:23 +00:00
}
else if (distance.y > 0 && map.CanMoveTo(MapPosition + Vector2Int.up)){
transform.Translate(Vector3.up);
2019-04-29 17:53:45 +00:00
spriteRenderer.sprite = upSprite;
2019-04-27 22:29:23 +00:00
}
else if (distance.y < 0 && map.CanMoveTo(MapPosition + Vector2Int.down)){
transform.Translate(Vector3.down);
2019-04-29 17:53:45 +00:00
spriteRenderer.sprite = downSprite;
2019-04-27 22:29:23 +00:00
}
2019-04-27 20:19:35 +00:00
}
else {
2019-04-29 13:27:26 +00:00
int direction = Random.Range(0, 4);
if (direction == 0 && map.CanMoveTo(MapPosition + Vector2Int.left)){
transform.Translate(Vector3.left);
2019-04-29 17:53:45 +00:00
spriteRenderer.flipX = false;
2019-04-29 13:27:26 +00:00
}
else if (direction == 1 && map.CanMoveTo(MapPosition + Vector2Int.right)){
transform.Translate(Vector3.right);
2019-04-29 17:53:45 +00:00
spriteRenderer.flipX = true;
2019-04-27 20:19:35 +00:00
}
2019-04-29 13:27:26 +00:00
else if (direction == 2 && map.CanMoveTo(MapPosition + Vector2Int.up)){
transform.Translate(Vector3.up);
2019-04-29 17:53:45 +00:00
spriteRenderer.sprite = upSprite;
2019-04-29 13:27:26 +00:00
}
else if (direction == 3 && map.CanMoveTo(MapPosition + Vector2Int.down)){
transform.Translate(Vector3.down);
2019-04-29 17:53:45 +00:00
spriteRenderer.sprite = downSprite;
2019-04-27 20:19:35 +00:00
}
}
}
public bool CanSeePlayer(){
return Vector3.Distance(player.transform.position, transform.position) < visionRange;
}
private void OnDrawGizmosSelected() {
Gizmos.color = Color.cyan;
Gizmos.DrawWireSphere(transform.position, visionRange);
}
}