при клике по карте
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class ClickFeature : MonoBehaviour
{
public GameObject hero;
SpriteRenderer spriteRenderer;
private void Awake()
{
spriteRenderer = GetComponent<SpriteRenderer>();
if (GetComponentInChildren<HPCalc>() != null)
{
HPCalc = GetComponentInChildren<HPCalc>();
}
}
HPCalc HPCalc;
private void OnMouseDown()
{
if (HPCalc != null)
{
HPCalc.EnemyCalc();
}
else
{
hero.transform.position = this.transform.position;
}
transform.localScale = new Vector2(0.50f, 0.60f);
spriteRenderer.color = new Color(0.93f, 0.93f, 0.93f);
}
private void OnMouseUp()
{
transform.localScale = new Vector2(0.46f, 0.56f);
spriteRenderer.color = new Color(1, 1, 1);
}
private void OnMouseOver()
{
spriteRenderer.color = new Color(0.97f, 0.97f, 0.97f);
}
private void OnMouseExit()
{
spriteRenderer.color = new Color(1, 1, 1);
}
}
функция HPCalc
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class HPCalc : MonoBehaviour
{
[SerializeField] public TextMesh hpTextMesh;
[SerializeField] public TextMesh dmgTextMesh;
int EnemyHP;
int EnemyDMG;
private void Start()
{
EnemyHP = int.Parse(hpTextMesh.text);
EnemyDMG = int.Parse(dmgTextMesh.text);
}
public HPnDMGHero HPnDMGHero;
public void EnemyCalc()
{
HPnDMGHero.HeroCalc(ref EnemyHP, ref EnemyDMG);
hpTextMesh.text = EnemyHP.ToString();
dmgTextMesh.text = EnemyDMG.ToString();
}
}
и ещё одна функция HeroCalc, которую может быть не стоило делать?
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class HPnDMGHero : MonoBehaviour
{
[SerializeField] public TextMesh hpTextMesh;
[SerializeField] public TextMesh dmgTextMesh;
int HeroHP;
int HeroDMG;
private void Start()
{
HeroHP = int.Parse(hpTextMesh.text);
HeroDMG = int.Parse(dmgTextMesh.text);
}
public void HeroCalc(ref int HPEnemy, ref int DMGEnemy)
{
if (HeroHP > DMGEnemy)
{
HeroHP -= DMGEnemy;
}
if (HPEnemy > HeroDMG)
{
HPEnemy -= HeroDMG;
}
hpTextMesh.text = HeroHP.ToString();
dmgTextMesh.text = HeroDMG.ToString();
}
}