Post by code1 on Jul 20, 2018 16:15:00 GMT
Hello
I am going through this tutorial and tried to implement multiple abilities and found this
2. You could create a new aspect, perhaps one called "ability container" which holds an array of ability. Then the ability wouldn't need to be an aspect itself. Just make sure all cards have this aspect, and when adding the ability to the card, get a reference to the container and append it to the array.
this is what i have so far - i chose to use a list instead of an array not sure if that matters - i know there are differences but i dont know all of them so i may not know that i CANT use a list in this situation.
bit confused on assigning the card to the ability and how to proceed with this
democards is read
multiple abilities array json is converted to abilities list with object Ability
a card has a list of abilities
check for targets and add the targets (not sure how multple abilities with targets will work - would have to modify the clicktoplaycontroller alot i think for that but for now carry on)
the abilities are carried out in sequence
game continues
its complaining about reference errors - i am pretty sure it is because the ability class card is being referenced and it doesnt have anything assigned to it.... im not sure how it gets assigned
any help would be greatly appreciated.
code i changed:
{
"id": "Card4",
"type": "Minion",
"name": "Grunt 2",
"text": "",
"cost": 2,
"attack": 3,
"hit points": 2
},
{
"id": "Card5",
"type": "Minion",
"name": "Rich Grunt",
"text": "Draw a card when summoned.",
"cost": 2,
"attack": 1,
"hit points": 1,
"abilities": [{
"action": "DrawCardsAction",
"info": 1
}, {
"action": "DamageAction",
"info": 1,
"targetSelector": {
"type": "RandomTarget",
"mark": {
"alliance": "Enemy",
"zone": "Battlefield"
},
"count": 3
}
}
]
}
private static void AddAbilities (Card card, Dictionary<string, object> data) {
if (data.ContainsKey ("abilities") == false)
return;
var cardabilities = (List<object>)data ["abilities"];
var abiltiies = card.AddAspect<Abilities>();
abiltiies.abilities = new List<Ability>();
foreach (object entry in cardabilities)
{
var abilityData = (Dictionary<string, object>)entry;
var ability = AddAbility(card, abilityData);
abiltiies.abilities.Add(ability);
AddSelector(ability, abilityData);
}
}
private static Ability AddAbility(Card card, Dictionary<string, object> data) {
var ability = new Ability
{
actionName = (string)data["action"],
userInfo = data["info"]
}
return ability;
}
private static void AddSelector (Ability ability, Dictionary<string, object> data) {
if (data.ContainsKey ("targetSelector") == false)
return;
var selectorData = (Dictionary<string, object>)data["targetSelector"];
var typeName = (string)selectorData["type"];
var type = Type.GetType (typeName);
var instance = Activator.CreateInstance (type) as ITargetSelector;
instance.Load (selectorData);
ability.AddAspect<ITargetSelector> (instance);
}
minionsystem
void PostSummonAbility (SummonMinionAction action) {
var card = action.minion;
var abilities = card.GetAspect<Abilities> ();
if (abilities != null)
foreach(var ability in abilities.abilities)
container.AddReaction (new AbilityAction (ability));
}
spellsystem
void OnPrepareCastSpell (object sender, object args) {
var action = args as CastSpellAction;
var abiltiies = action.spell.GetAspect<Abilities> ();
foreach(var ability in abiltiies.abilities) {
var reaction = new AbilityAction (ability);
container.AddReaction (reaction);
}
}
abilities
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using TheLiquidFire.AspectContainer;
public class Abilities : IAspect
{
public IContainer container { get; set; }
public List<Ability> abilities;
public Card card { get { return container as Card; } }
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using TheLiquidFire.AspectContainer;
public class Ability : Container {
public IContainer container { get; set; }
public string actionName { get; set; }
public object userInfo { get; set; }
public Card card { get { return container as Card; } }
}
I am going through this tutorial and tried to implement multiple abilities and found this
2. You could create a new aspect, perhaps one called "ability container" which holds an array of ability. Then the ability wouldn't need to be an aspect itself. Just make sure all cards have this aspect, and when adding the ability to the card, get a reference to the container and append it to the array.
this is what i have so far - i chose to use a list instead of an array not sure if that matters - i know there are differences but i dont know all of them so i may not know that i CANT use a list in this situation.
bit confused on assigning the card to the ability and how to proceed with this
democards is read
multiple abilities array json is converted to abilities list with object Ability
a card has a list of abilities
check for targets and add the targets (not sure how multple abilities with targets will work - would have to modify the clicktoplaycontroller alot i think for that but for now carry on)
the abilities are carried out in sequence
game continues
its complaining about reference errors - i am pretty sure it is because the ability class card is being referenced and it doesnt have anything assigned to it.... im not sure how it gets assigned
any help would be greatly appreciated.
code i changed:
{
"id": "Card4",
"type": "Minion",
"name": "Grunt 2",
"text": "",
"cost": 2,
"attack": 3,
"hit points": 2
},
{
"id": "Card5",
"type": "Minion",
"name": "Rich Grunt",
"text": "Draw a card when summoned.",
"cost": 2,
"attack": 1,
"hit points": 1,
"abilities": [{
"action": "DrawCardsAction",
"info": 1
}, {
"action": "DamageAction",
"info": 1,
"targetSelector": {
"type": "RandomTarget",
"mark": {
"alliance": "Enemy",
"zone": "Battlefield"
},
"count": 3
}
}
]
}
private static void AddAbilities (Card card, Dictionary<string, object> data) {
if (data.ContainsKey ("abilities") == false)
return;
var cardabilities = (List<object>)data ["abilities"];
var abiltiies = card.AddAspect<Abilities>();
abiltiies.abilities = new List<Ability>();
foreach (object entry in cardabilities)
{
var abilityData = (Dictionary<string, object>)entry;
var ability = AddAbility(card, abilityData);
abiltiies.abilities.Add(ability);
AddSelector(ability, abilityData);
}
}
private static Ability AddAbility(Card card, Dictionary<string, object> data) {
var ability = new Ability
{
actionName = (string)data["action"],
userInfo = data["info"]
}
return ability;
}
private static void AddSelector (Ability ability, Dictionary<string, object> data) {
if (data.ContainsKey ("targetSelector") == false)
return;
var selectorData = (Dictionary<string, object>)data["targetSelector"];
var typeName = (string)selectorData["type"];
var type = Type.GetType (typeName);
var instance = Activator.CreateInstance (type) as ITargetSelector;
instance.Load (selectorData);
ability.AddAspect<ITargetSelector> (instance);
}
minionsystem
void PostSummonAbility (SummonMinionAction action) {
var card = action.minion;
var abilities = card.GetAspect<Abilities> ();
if (abilities != null)
foreach(var ability in abilities.abilities)
container.AddReaction (new AbilityAction (ability));
}
spellsystem
void OnPrepareCastSpell (object sender, object args) {
var action = args as CastSpellAction;
var abiltiies = action.spell.GetAspect<Abilities> ();
foreach(var ability in abiltiies.abilities) {
var reaction = new AbilityAction (ability);
container.AddReaction (reaction);
}
}
abilities
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using TheLiquidFire.AspectContainer;
public class Abilities : IAspect
{
public IContainer container { get; set; }
public List<Ability> abilities;
public Card card { get { return container as Card; } }
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using TheLiquidFire.AspectContainer;
public class Ability : Container {
public IContainer container { get; set; }
public string actionName { get; set; }
public object userInfo { get; set; }
public Card card { get { return container as Card; } }
}