Jump to content
  • Advertisement
harugawa

C# Potions usage and how to implement it

Recommended Posts

Alright, I'm not even sure if I write my first post in the right sub category. 

Basically I'm creating some browser game based on .net and you got there an inventory with a bunch of potions. I'm just not really sure how to handle different potions and its usage on the backend to not make it into a spagetti. So, whenever user clicks on the potion, it sends an id of the potion to the api endpoint, I'm checking there the type and just getting the info about potion. I got an enum which describes the type, like mana, hp, energy, exp or you name it. 

So what now. Do I make like a huge switch statment and just pass it to a different methods or something and do there what has to be done with the potion? Sorry for not showcasing any code, but I do not have any for this, just trying to brainstorm how to implement some potion usage mechanics. Am I going the right direction or I just missed the path and heading to the dead end?

 

Edited by harugawa

Share this post


Link to post
Share on other sites
Advertisement

It depends on the design of your game.

First of all, you say a switch statement would be "huge": does it mean that there are going to be many potion "SKUs", many essentially different potion effects requiring separate specific code, or both? In general, switch statements are appropriate for a small and stable set of alternatives: probably not potion types.

If potion effects are shared between many potions (e.g. one potion gives a lot of mana and heals a few hit points, one gives a little mana and heals many hit points, one heals hit points only) or with other sources (e.g. a bath in an enchanted fountain restores mana or heals hit points like drinking a potion) you can reduce duplication by focusing on the effects (e.g. a routines to heal N hit points , add N mana, etc.) and describing potions with a data structure listing their effects and other properties (e.g. "this potion is called potion of invigorating healing, it is pale blue, it heals 45 hit points, it gives 3 mana, 3 energy, 0 experience"), even without an ID. Then when a potion is consumed you would go through its description, processing general drinking rules (e.g. choking because of bad taste) and effects one by one in a standard order, without low value potion-specific code,

This data-driven approach would facilitate potion variations by allowing them to be anonymous, whereas potions with the same ID would have to be identical. Potions could be generated randomly (e.g. average strength of healing potions increases proportionally to character hit points, and they are all a "potion of healing"), altered (e.g. by mixing them), deduced from other data (e.g. distilling a corpse produces a potion with effects that depend on the creature).

 

Share this post


Link to post
Share on other sites

What I would do is create some form of consumable function, create a switch statement around the adjustable stat

ConsumePotion(StatType.Health, 25); = SmallHealthPotion

ConsumePotion(StatType.Armor, 200); = Greater Armor Potion

If you do your switch based on a specific stat, you have an unlimited potion arsenal, with minor changes instead of tons of changes.

Share this post


Link to post
Share on other sites

Decouple the data from the implementation details so that you don't need a massive switch statement. Imagine you had 500 different types of potions. Do you really want to make a switch statement with 500 case-statements? I certainly wouldn't.

Here is an example:

class Potion
{
	public int mana;
	public int health;
	public int energy;
	public int experience;
}

Creating a potion is simple:

Potion superAwesomePotion = new Potion();
superAwesomePotion.mana			= 10;		// Indicates that the potion should add 10 mana
superAwesomePotion.health		= 6;		// Indicates that the potion should add 6 health
superAwesomePotion.energy		= 2;		// Indicates that the potion should add 2 energy
superAwesomePotion.experience	= 25;		// Indicates that the potion should add 25 XP

You can store this data in XML files, JSON, or binary data that can be loaded at runtime or stored in the game in some way. You could create an array of potions that could be indexed or named (dictionary).

To apply the potion to a character (or anything really), you could have a UsePotion method:

private void UsePotion(Potion potion)
{
	this.mana		+= potion.mana;
	this.health		+= potion.health;
	this.energy		+= potion.energy;
	this.experience	+= potion.experience;
}

[Edit] Fixed potion creation sample as mana was duplicated

Edited by Jamison

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!