Skip to content

Instantly share code, notes, and snippets.

@SenpaiRar
Last active May 2, 2019 02:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save SenpaiRar/74bb576661c2adbfb20c9942d657718f to your computer and use it in GitHub Desktop.
Save SenpaiRar/74bb576661c2adbfb20c9942d657718f to your computer and use it in GitHub Desktop.
This is the parent class of all modifiers. Every modifier we make will inherit from this class. The second file is an example of a completed modifier which slows the player down.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
//Notice how this class doesnn't inherit from Monobehaviour. This means we can't add this to gameobjects
public abstract class Modifier_Template
{
//By making all of these abstract, we force every modifier to have these functions
public abstract float Attribute { get; set; } //This is a property. We're essentially force every child to have a float number variabled named Attribute
//This attribute property usage depends on the modifier, but generally, it tells the modifier by how much to change a stat in Player_Manager
public abstract void AttributeChange(Player_Manager Player); //Attribute Change is the function that changes a stat in Player_Manager;
public abstract void EndChange(Player_Manager Player); //EndChange is the function that resets the Player's stats once the modifier is over
public abstract bool ConditionMet(Player_Manager Player); //A bool is a data type with only true false. In this case, this function returns whether the
//Should be run or not depending on the hunger/water
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Sluggishness : Modifier_Template //We inherit from the Template above using the colon
{
public Sluggishness(float Value){ //This something new! Here it what is known as a constructor. A constructor can be thought
Attribute = Value; //of as a sort of function that is called when a new object is created.
} //In this case, we're setting the Attribute property below to the number we pass in when we create the object
private float attribute; //Here we define a private float variable which holds our attribute number.
public override float Attribute //Because Attribute is abstract, we implement using the override keyword. In this case, we
{ //we define it as a standard property
get
{
return attribute;
}
set
{
attribute = value;
}
}
public override void AttributeChange(Player_Manager Player) //Attribute change is called by the modifier manager to actually
{ //Change the stat in Player_Manager. By having it in its own
Player.current_Speed = Player.Max_Speed*attribute; //Function, we can easily change what the modifier does without
} //changing the rest of the modifier
public override void EndChange(Player_Manager Player) //For some modifiers like this, if we didn't have a function to revert
{ //The Player_Manager stat, the stat would stay the same even if it the
Player.current_Speed = Player.Max_Speed; //Modifier stopped working. In this case, it reverts the player's speed
} //Back to their normal speed.
public override bool ConditionMet(Player_Manager Player) //This function determines whether the modifier should be run. It takes in
{ //The Player_Manager and checks a certain stat, returning (True) if
if(Player.current_Hunger_Level < 60) //the modifier should be ran or (false) if it shouldn't.
{
return (true);
}
else
{
return (false);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment