Untitled
unknown
plain_text
2 years ago
4.5 kB
10
Indexable
public class ShipeController : MonoBehaviour
{
#region public
public float HpMax = 100;
public StateShip state;
public float moveSpeed = 5f;
public float rotateSpeed;
public Transform mapFrame;
public GameObject particleFire;
public float damage = 4;
public Image hpBar;
#endregion
#region private
private Vector2 targetPosition;
private EnemyBase targetAttack;
bool isMove => (Vector2)transform.position != targetPosition;
private float _hp;
private float HP
{
get => _hp;
set
{
_hp = value;
hpBar.fillAmount = _hp / HpMax;
}
}
#endregion
private void Start()
{
HP = HpMax;
}
void Update()
{
if(targetAttack == null)
state = StateShip.Idle;
if (Input.GetMouseButton(0)) // Левый клик
{
SetTargetPosition();
}
if (Input.GetKeyDown(KeyCode.LeftControl))
{
if (state == StateShip.Attack)
state = StateShip.Idle;
else if(targetAttack != null)
state = StateShip.Attack;
}
MoveToTarget();
RotateTowardsTarget();
particleFire.SetActive(isMove);
if (targetAttack != null && this.state == StateShip.Attack && Vector2.Distance(targetAttack.transform.position, this.transform.position) < 4)
this.Attack(targetAttack);
}
void SetTargetPosition()
{
if (Input.GetMouseButton(0))
{
Vector2 mousePosition = Camera.main.ScreenToWorldPoint(Input.mousePosition);
RaycastHit2D[] hits = new RaycastHit2D[10];
int hitCount = Physics2D.RaycastNonAlloc(mousePosition, Vector2.zero, hits);
Dictionary<string, GameObject> name_clicks = new();
for (int i = 0; i < hitCount; i++)
{
GameObject hitObject = hits[i].collider.gameObject;
//Debug.Log("Hit object: " + hitObject.tag);
name_clicks.Add(hitObject.tag, hitObject);
}
if(name_clicks.ContainsKey("Enemy") && state != StateShip.Attack)
{
if (targetAttack != null)
targetAttack.DeSelect();
targetAttack = name_clicks["Enemy"].GetComponent<EnemyBase>();
targetAttack.Select();
Debug.Log("attack");
}else if (name_clicks.ContainsKey("Map"))
{
Debug.Log("move");
targetPosition = Camera.main.ScreenToWorldPoint(Input.mousePosition);
}
else
{
}
}
}
void RotateTowardsTarget()
{
Vector2 direction = (state == StateShip.Idle ?targetPosition: targetAttack.transform.position) - (Vector2)transform.position;
float angle = Mathf.Atan2(direction.y, direction.x) * Mathf.Rad2Deg;
Quaternion rotation;
switch (state)
{
case StateShip.Idle:
if (isMove)
{
rotation = Quaternion.AngleAxis(angle, Vector3.forward);
transform.rotation = Quaternion.Slerp(transform.rotation, rotation, Time.deltaTime * rotateSpeed);
}
break;
case StateShip.Attack:
rotation = Quaternion.AngleAxis(angle, Vector3.forward);
transform.rotation = Quaternion.Slerp(transform.rotation, rotation, Time.deltaTime * rotateSpeed);
break;
}
}
void MoveToTarget()
{
transform.position = Vector2.MoveTowards(transform.position, targetPosition, moveSpeed * Time.deltaTime);
}
public void TakeDamage(float amount)
{
HP -= amount;
Debug.Log("Player takes damage. Current health: " + HP);
if (HP <= 0)
{
Die();
}
}
public void Attack(EnemyBase enemy)
{
// Вызываем метод получения урона у противника
enemy.GetHit(damage);
Debug.Log("Player attacks enemy!");
}
private void Die()
{
Debug.Log("Player has died.");
// Дополнительные действия при смерти игрока
}
}
Editor is loading...
Leave a Comment