Stats 05: Linkers

In this section, we’ll be extending the RPGAttribute class with the IStatLinkable interface which will define the methods used to add RPGStatLinkers to the attribute. The stat linkers will connect two stat’s values together, allowing one stat’s value to influence the other stat’s value. 

This post is an old implementation and should be only used as a reference. This post maybe updated or replaced in the future.

Stat Linker

The Base stat linker is an abstract class since we intend to inherit from the base linker to allow use to create as many linker types as possible later on. First we’ll create the base Stat Linker class then create an example linker that inherits from the base linker.

using UnityEngine;
using System.Collections;
public abstract class RPGStatLinker {
    private RPGStat _stat;
 
    public</span> RPGStatLinker(RPGStat stat) {
        _stat = stat;
    }
 
    public RPGStat Stat {
        get</span> { return _stat; }
    }
 
    public abstract int Value { get; }
}

The Value property will allow future linkers to determine the value of the linker based on the linked Stat’s value. Future linkers change their constructers to allow for additional parameters that can influence the Value calculations. Next is an example of a linker that takes a ratio of the linked stat as the Value returned by it.

using UnityEngine;
using System.Collections;
public class RPGStatLinkerBasic : RPGStatLinker {
    private float _ratio;
 
    public override int Value {
        get { return (int)(Stat.StatValue * _ratio); }
    }
 
    public RPGStatLinkerBasic(RPGStat stat, float ratio) : base(stat) {
        _ratio = ratio;
    }
}

Linkable Interface

The IStatLinkable interface defines the StatLinkerValue, property which is the value the combined total of all the linkers attached to the stat, and methods which will add remove and update the attached linkers.

using UnityEngine;
using System.Collections;
public interface IStatLinkable {
    int StatLinkerValue { get; }
 
    void AddLinker(RPGStatLinker linker);
    void ClearLinkers();
    void UpdateLinkers();
}

Implementing IStatLinkable

We’re going to implement the IStatLinkable into our RPGAttribute stat class. The following code is the complete version of the RPGAttribute class with the IStatScalable implementation.

using UnityEngine;
using System.Collections;
using System.Collections.Generic;
public class RPGAttribute : RPGStatModifiable, IStatScalable, IStatLinkable {
    private int _statLevelValue;
    private int _statLinkerValue;
    private List<RPGStatLinker> _statLinkers;
 
    public int StatLevelValue {
        get { return _statLevelValue; }
    }
 
    public int StatLinkerValue {
        get {
            UpdateLinkers();
            return _statLinkerValue; 
        }
    }
 
    public override int StatBaseValue {
        get { return base.StatBaseValue + StatLevelValue + StatLinkerValue; }
    }
 
    public virtual void ScaleStat(int level) {
        _statLevelValue = level;
    }    
 
    public void AddLinker(RPGStatLinker linker) {
        _statLinkers.Add(linker);
    }
 
    public void ClearLinkers() {
        _statLinkers.Clear();
    }
 
    public void UpdateLinkers() {
        _statLinkerValue = 0;
        foreach(RPGStatLinker link in _statLinkers) {
            _statLinkerValue += link.Value;
        }
    }
 
    public RPGAttribute() {
        _statLinkers = new List<RPGStatLinker>();
    }
}

In the class we create a new list that will contain all our linkers and in the corresponding methods from the interface we tie in adding or removing the linkers. The UpdateLinkers method simple loops through all the linkers and sums their values and then sets the StatLinker Value to their sum. The last change is we add the StatLinkerValue property into the return value of the StatBaseValue property, since linkers in this setup will affect the base value and will be used in the calculations of stat modifiers.

Note* In this version of the attribute class we updated the linker’s value every time we retrieve the Stat’s value. This could cause the linker value to be updated multiple times per update in the game. Later on we will come back to this code and update the stats to only update the linkers when the linked stat’s value changes.

Updating the Default Stats

In this updated version of the default stat collection, the health and mana stats will be changed into RPGAttributes allowing them to link to the Stamina and Wisdom stats. In this case the Health will increase based off the Stamina value and the Mana will increase based off the Wisdom value.

using UnityEngine;
using System.Collections;
public class RPGDefaultStats : RPGStatCollection {
    protected override void ConfigureStats() {
        var stamina = CreateOrGetStat<RPGAttribute>(RPGStatType.Stamina);
        stamina.StatName = "Stamina";
        stamina.StatBaseValue = 10;
 
        var wisdom = CreateOrGetStat<RPGAttribute>(RPGStatType.Wisdom);
        wisdom.StatName = "Wisdom";
        wisdom.StatBaseValue = 5;
 
        var health = CreateOrGetStat<RPGAttribute>(RPGStatType.Health);
        health.StatName = "Health";
        health.StatBaseValue = 100;
        health.AddLinker(new RPGStatLinkerBasic(CreateOrGetStat<RPGAttribute>(RPGStatType.Stamina), 13f));
        health.UpdateLinkers();
 
        var mana = CreateOrGetStat<RPGAttribute>(RPGStatType.Mana);
        mana.StatName = "Mana";
        mana.StatBaseValue = 2000;
        mana.AddLinker(new RPGStatLinkerBasic(CreateOrGetStat<RPGAttribute>(RPGStatType.Wisdom), 68f));
        health.UpdateLinkers();
    }
}

Finally we update the test script once again to check if the linkable stats work. All we are checking is if we scale the Stamina and Wisdom stat that the Health and Mana stat’s values correctly change.

Leave a Reply

Your email address will not be published. Required fields are marked *