72 lines
2.2 KiB
C#
72 lines
2.2 KiB
C#
using System.Collections;
|
|
using System.Collections.Generic;
|
|
using UnityEngine;
|
|
|
|
public class BattleParticipantSprite : MonoBehaviour
|
|
{
|
|
public delegate void SpriteClickedAction(BattleParticipant participant);
|
|
public event SpriteClickedAction OnSpriteClicked;
|
|
|
|
private BattlePlayerControls playerControls;
|
|
private BattleParticipant participant;
|
|
private bool removedFromBattle;
|
|
|
|
public void Init(BattlePlayerControls playerControls, BattleParticipant participant) {
|
|
this.playerControls = playerControls;
|
|
this.participant = participant;
|
|
this.removedFromBattle = false;
|
|
}
|
|
|
|
// Start is called before the first frame update
|
|
void Start() {
|
|
gameObject.AddComponent<BoxCollider2D>();
|
|
}
|
|
|
|
// Update is called once per frame
|
|
void Update() {
|
|
if (this.colorChanged && !this.playerControls.CouldTarget(participant)) {
|
|
OnMouseExit();
|
|
}
|
|
}
|
|
|
|
void OnMouseDown() {
|
|
if (!this.removedFromBattle) {
|
|
OnSpriteClicked(participant);
|
|
}
|
|
}
|
|
|
|
private Color savedColor;
|
|
private bool colorChanged;
|
|
|
|
void OnMouseEnter() {
|
|
if (!this.removedFromBattle && this.playerControls.CouldTarget(participant)) {
|
|
SpriteRenderer sprite = GetComponent<SpriteRenderer>();
|
|
this.savedColor = sprite.color;
|
|
sprite.color = Color.Lerp(this.savedColor, Color.white, .5f);
|
|
this.colorChanged = true;
|
|
}
|
|
}
|
|
|
|
void OnMouseExit() {
|
|
if (this.colorChanged) {
|
|
SpriteRenderer sprite = GetComponent<SpriteRenderer>();
|
|
sprite.color = this.savedColor;
|
|
this.colorChanged = false;
|
|
}
|
|
}
|
|
|
|
public void NotifyDeath(BattleParticipant deadParticipant) {
|
|
if (deadParticipant != this.participant) {
|
|
throw new System.InvalidOperationException(
|
|
"BattleParticipantSprite received NotifyDeath from another battle participant.");
|
|
}
|
|
|
|
if (!this.removedFromBattle) {
|
|
SpriteRenderer sprite = GetComponent<SpriteRenderer>();
|
|
sprite.enabled = false;
|
|
BoxCollider2D collider = GetComponent<BoxCollider2D>();
|
|
collider.enabled = false;
|
|
this.removedFromBattle = true;
|
|
}
|
|
}
|
|
}
|