gjaros
Member
- Joined
- Aug 27, 2022
- Messages
- 5
- Programming Experience
- 3-5
This is something I had to write (to make something work, and it did) and it made me cringe.
What I would like to be able to do is something simple like:
Player and Enemy are separate classes. The config member is a class instance of EnemyConfig and PlayerConfig (both named "config"), respectively. Both EnemyConfig and PlayerConfig inherit from the class Config.
The thoughts behind my design so far and background:
How should I approach this using anything C# has? I've looked around at abstract classes, inheritance, interfaces but I don't know how it should be done.
C#:
string advancement = obj.name == "Player" ? obj.GetComponent<Player>().config.Advancement : obj.GetComponent<Enemy>().config.Advancement;
What I would like to be able to do is something simple like:
C#:
string advancement = obj.config.advancement.
Player and Enemy are separate classes. The config member is a class instance of EnemyConfig and PlayerConfig (both named "config"), respectively. Both EnemyConfig and PlayerConfig inherit from the class Config.
The thoughts behind my design so far and background:
- This is a game I'm working on in Unity.
- Player and Enemy have a lot in common as far as state (config) but not much else. Their behavior is completely different. I would like them to inherit from a common class too but I don't know how I would implement that with this config issue I'm having.
- Config has two purposes.
- Hold state and remove clutter from the classes that instance it.
- Is assigned by using NewtonSoft's Json Convert to make objects easily configurable.
- With those two points in mind I would like to keep Config even though the Player/Enemy class could just hold all those members themselves.
Configs:
public class Config
{
public string Name { get; set; }
public State Stats { get; set; }
public string Advancement { get; set; }
}
public class EnemyConfig : Config
{
public List<string> Skills { get; set; }
public Characteristic Characteristics { get; set; }
public Movement Moves { get; set; }
public Behavior Behaviors { get; set; }
}
public class PlayerConfig : Config
{
public List<Hotkey> Hotkeys { get; set; }
}
Enemy Class:
public class Enemy : MonoBehaviour
{
public EnemyConfig config;
private EnemyMovement movement;
private EnemyAttack attack;
//Stuff
}
Player Class:
public class Player : MonoBehaviour
{
[SerializeField] public Camera playerCamera;
public PlayerConfig config;
private PlayerMovement movement;
private PlayerAttack attack;
public Health health;
public Madra madra;
private Inventory inventory;
public readonly Vector2 castPoint = new Vector2(1.0f, 1.0f);
public List<SkillCast> skillCasts;
public List<Buff> buffs;
private readonly float sightCooldown = 0.5f;
private float sightCooldownTimer = 0f;
//Stuff
}
How should I approach this using anything C# has? I've looked around at abstract classes, inheritance, interfaces but I don't know how it should be done.