- Joined
- Apr 2, 2014
- Messages
- 213
TypeScript:
export interface Deck<T = any> {
cards: Card<T>[];
}
export interface Card<T> {
name: string,
giveBonus: (param: T) => void;
param: T;
}
function giveHealth({ amount, heal }: { amount: number, heal: boolean }) {
const unit = Unit.fromHandle(GetTriggerUnit());
if(!unit) return;
unit.maxLife += amount;
if(heal) unit.life += amount;
}
function increaseSpellPower(by: number){
Players[0].setTechResearched(FourCC("SSSS"), by);
}
const exampleDeck:Deck = {
cards: [
{
name: "Big Flesh",
giveBonus: giveHealth,
param: {amount: 50, heal: true}
},
{
name: "Huge Spell Powerer",
giveBonus: increaseSpellPower,
param: 3
}
]
}
function PickFromDeck(){
const card = exampleDeck.cards[0];
card.giveBonus(card.param);
}
I will have many cards that will do certain stuff (hp mana heal unit aoe stun). With this logic I don't need any if statements when picking the card like
TypeScript:
if(type === "life") do stuff
else if(type === "unit") do stuff
- I want params type to be defined by the giveBonus callback's parameter type. Is it possible?
- Does this code make sense?
- Rate this logic on 1-10
Last edited: