Add CrashingPunch and making it work

This commit is contained in:
henriquenakashima 2020-09-18 23:33:57 -04:00
parent 1c3056778e
commit d7c19e2572
15 changed files with 204 additions and 27 deletions

43
Assets/Battle/BaseCard.cs Normal file
View file

@ -0,0 +1,43 @@
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public enum CardId {
CrashingPunch
}
public enum CardType {
Action
}
public abstract class BaseCard
{
private CardId id;
private string displayName;
private int cost;
private CardType type;
private string effectText;
public BaseCard(CardId id, string displayName, int cost, CardType type, string effectText) {
this.id = id;
this.displayName = displayName;
this.cost = cost;
this.type = type;
this.effectText = effectText;
}
public abstract IEnumerator Use(BattlePlayerControls playerControls);
public CardId GetId() {return id;}
public string GetDisplayName() {return displayName;}
public int GetCost() {return cost;}
public string GetDisplayType() {
switch (type) {
case CardType.Action:
return "";
default:
throw new System.ArgumentException("CardType unknown", "cardType");
}
}
public string GetEffectText() {return effectText;}
}

View file

@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: e40048c9e74d54948bcbc7d7cad13e95
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View file

@ -29,11 +29,14 @@ public class BattleController : MonoBehaviour
HeroId id = heroIds[i];
heroes[i] = new BattleHero(id);
heroes[i].init();
heroes[i].Init();
heroSprites[i] = HeroDatabase.createHeroSprite(id);
heroSprites[i].name = "Hero" + i + "_Sprite";
heroSprites[i].transform.position = new Vector3(-6 + 6 * i, 0, 0);
BattleParticipantSprite bps = heroSprites[i].AddComponent<BattleParticipantSprite>();
BattleParticipant participant = heroes[i];
bps.OnSpriteClicked += delegate() {battlePlayerControls.OnParticipantSpriteClicked(participant);};
heroUIs[i] = (HeroInfoUI) Instantiate(heroUIPrefab.gameObject, uiCanvas.transform).GetComponent(typeof(HeroInfoUI));
heroUIs[i].Init(heroSprites[i], heroes[i]);
@ -46,11 +49,14 @@ public class BattleController : MonoBehaviour
MonsterId id = monsterIds[i];
monsters[i] = MonsterDatabase.createMonsterScript(id);
monsters[i].init();
monsters[i].Init();
monsterSprites[i] = MonsterDatabase.createMonsterSprite(id);
monsterSprites[i].name = "Monster" + i + "_Sprite";
monsterSprites[i].transform.position = new Vector3(-6 + 6 * i, 3, 0);
BattleParticipantSprite bps = monsterSprites[i].AddComponent<BattleParticipantSprite>();
BattleParticipant participant = monsters[i];
bps.OnSpriteClicked += delegate() {battlePlayerControls.OnParticipantSpriteClicked(participant);};
monsterUIs[i] = (HeroInfoUI) Instantiate(heroUIPrefab.gameObject, uiCanvas.transform).GetComponent(typeof(HeroInfoUI));
monsterUIs[i].Init(monsterSprites[i], monsters[i]);
@ -68,14 +74,14 @@ public class BattleController : MonoBehaviour
public void HitRandomParticipant() {
if (Random.Range(0f, 1f) < 0.5f) {
heroes[Random.Range(0, heroes.Length)].dealDamage(1);
heroes[Random.Range(0, heroes.Length)].DealDamage(1);
} else {
monsters[Random.Range(0, monsters.Length)].dealDamage(2);
monsters[Random.Range(0, monsters.Length)].DealDamage(2);
}
}
public void OnCardUsed(int index) {
Debug.Log("OnCardClicked " + index);
HitRandomParticipant();
public void OnCardUsed(BaseCard card) {
Debug.Log("OnCardClicked " + card);
StartCoroutine(card.Use(battlePlayerControls));
}
}

View file

@ -8,23 +8,23 @@ public class BattleParticipant
protected int hp;
protected int maxHP;
public void init() {
public void Init() {
this.hp = this.maxHP;
}
public string getDisplayName() {
public string GetDisplayName() {
return this.displayName;
}
public int getHP() {
public int GetHP() {
return this.hp;
}
public int getMaxHP() {
public int GetMaxHP() {
return this.maxHP;
}
public void dealDamage(int damage) {
public void DealDamage(int damage) {
this.hp -= damage;
if (this.hp < 0) this.hp = 0;
}

View file

@ -0,0 +1,36 @@
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class BattleParticipantSprite : MonoBehaviour
{
public delegate void SpriteClickedAction();
public event SpriteClickedAction OnSpriteClicked;
// Start is called before the first frame update
void Start() {
gameObject.AddComponent<BoxCollider2D>();
}
// Update is called once per frame
void Update() {
}
void OnMouseDown() {
Debug.Log("Clicked " + this.name);
OnSpriteClicked();
}
private Color savedColor;
void OnMouseEnter() {
SpriteRenderer sprite = GetComponent<SpriteRenderer>();
this.savedColor = sprite.color;
sprite.color = Color.Lerp(this.savedColor, Color.white, .5f);
}
void OnMouseExit() {
SpriteRenderer sprite = GetComponent<SpriteRenderer>();
sprite.color = this.savedColor;
}
}

View file

@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: f53484a8e76654248a278d9cc4910f0e
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View file

@ -5,13 +5,15 @@ using UnityEngine.UI;
public class BattlePlayerControls : MonoBehaviour
{
public BattleController battleController;
public GameObject uiCanvas;
public GameObject cardPrefab;
public delegate void CardUsedAction(int i);
public delegate void CardUsedAction(BaseCard card);
public event CardUsedAction OnCardUsed;
private List<GameObject> cardsInHand;
private List<BaseCard> cardsInHand;
private List<GameObject> cardsInHandUIs;
// Start is called before the first frame update
void Start()
@ -26,14 +28,19 @@ public class BattlePlayerControls : MonoBehaviour
}
public void SetHand(int numberOfCards) {
this.cardsInHand = new List<GameObject>();
this.cardsInHand = new List<BaseCard>();
this.cardsInHandUIs = new List<GameObject>();
for (int i = 0; i < numberOfCards; i++) {
GameObject newCard = Instantiate(this.cardPrefab, this.uiCanvas.transform);
// TODO
BaseCard newCard = new CardCrashingPunch();
this.cardsInHand.Add(newCard);
newCard.transform.position = CalculateCardPosition(i, numberOfCards);
Button button = newCard.GetComponent<Button>();
GameObject newCardUI = Instantiate(this.cardPrefab, this.uiCanvas.transform);
this.cardsInHandUIs.Add(newCardUI);
newCardUI.transform.position = CalculateCardPosition(i, numberOfCards);
newCardUI.GetComponent<CardUI>().Init(newCard);
int cardIndex = i;
button.onClick.AddListener(delegate {OnCardClicked(cardIndex);});
newCardUI.GetComponent<Button>().onClick.AddListener(delegate {OnCardClicked(newCard);});
}
}
@ -45,7 +52,27 @@ public class BattlePlayerControls : MonoBehaviour
return new Vector3(Screen.width * 0.5f - DELTA / 2 * (count - 1) + DELTA * index, 160, 0);
}
private void OnCardClicked(int index) {
OnCardUsed(index);
private void OnCardClicked(BaseCard card) {
OnCardUsed(card);
}
private bool waitingForTarget = false;
private BattleParticipant participantClicked = null;
public IEnumerator ChooseTarget(System.Action<BattleParticipant> targetCallback) {
this.waitingForTarget = true;
while (this.participantClicked == null) {
yield return null;
}
this.waitingForTarget = false;
targetCallback(participantClicked);
this.participantClicked = null;
yield break;
}
public void OnParticipantSpriteClicked(BattleParticipant paticipant) {
if (this.waitingForTarget) {
this.participantClicked = paticipant;
}
}
}

View file

@ -11,6 +11,13 @@ public class CardUI : MonoBehaviour
public Image artImage;
public Text effectText;
public void Init(BaseCard card) {
this.nameText.text = card.GetDisplayName();
this.typeText.text = card.GetDisplayType();
this.costText.text = "" + card.GetCost();
this.effectText.text = card.GetEffectText();
}
// Start is called before the first frame update
void Start()
{

8
Assets/Battle/Cards.meta Normal file
View file

@ -0,0 +1,8 @@
fileFormatVersion: 2
guid: 4e4390544ec8a5d49a21725388e0b297
folderAsset: yes
DefaultImporter:
externalObjects: {}
userData:
assetBundleName:
assetBundleVariant:

View file

@ -0,0 +1,17 @@
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class CardCrashingPunch : BaseCard
{
public CardCrashingPunch() : base(CardId.CrashingPunch, "Crashing Punch", 3, CardType.Action, "Deals 3 damage to target.") {
}
public override IEnumerator Use(BattlePlayerControls playerControls) {
List<BattleParticipant> target = new List<BattleParticipant>();
yield return playerControls.StartCoroutine(playerControls.ChooseTarget((t) => {target.Add(t);}));
Debug.Log("Punching " + target[0]);
target[0].DealDamage(3);
}
}

View file

@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: ceb8168e4a1f0d24a91a86ec71f73a96
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View file

@ -29,7 +29,7 @@ public class HeroInfoUI : MonoBehaviour
void Start()
{
this.mainCamera = Camera.main;
this.nameText.text = this.battleParticipant.getDisplayName();
this.nameText.text = this.battleParticipant.GetDisplayName();
}
// Update is called once per frame
@ -38,8 +38,8 @@ public class HeroInfoUI : MonoBehaviour
Vector3 targetPos = this.mainCamera.WorldToScreenPoint(target.transform.position);
transform.position = targetPos;
int hp = this.battleParticipant.getHP();
int maxHP = this.battleParticipant.getMaxHP();
int hp = this.battleParticipant.GetHP();
int maxHP = this.battleParticipant.GetMaxHP();
if (this.renderedHP != hp || this.renderedMaxHP != maxHP) {
this.renderedHP = hp;