Guest User

Unity

a guest
Sep 16th, 2015
112
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 14.57 KB | None | 0 0
  1. //    ##########################################     Attributes file         ########################################## //
  2.  
  3. public class Attribute : BaseStat {
  4.     public Attribute() {
  5.         // Henter ExpToLevel og LevelModifier from Basic Setters And Getters @ Basestat
  6.         ExpToLevel = 50;
  7.         LevelModifier = 1.05f;
  8.     }
  9. }
  10.  
  11. // List of all the attributes in the game
  12. public enum AttributeName {
  13.     Might,
  14.     Constitution,
  15.     Nimbleness,
  16.     Speed,
  17.     Consentration,
  18.     Willpower,
  19.     Charisma
  20. }
  21.  
  22.  
  23. //    ##########################################     BaseCharacter file          ########################################## //
  24.  
  25. using UnityEngine;
  26. using System.Collections;
  27. using System; //Added to access the enum
  28.  
  29. public class BaseCharacter : MonoBehaviour {
  30.    
  31.     private string _name;
  32.     private int _level;
  33.     private uint _freeExp;
  34.  
  35.     private Attribute[] _primaryAttribute;
  36.     private Vital[] _vital;
  37.     private Skill[] _skill;
  38.  
  39.     public void Awake() {
  40.         _name = string.Empty;
  41.         _level = 0;
  42.         _freeExp = 0;
  43.  
  44.         _primaryAttribute = new Attribute[Enum.GetValues(typeof(AttributeName)).Length];
  45.         _vital = new Vital[Enum.GetValues(typeof(VitalName)).Length];
  46.         _skill = new Skill[Enum.GetValues(typeof(SkillName)).Length];
  47.  
  48.         SetupPrimaryAttributes();
  49.         SetupVitals();
  50.         SetupSkills();
  51.     }
  52.     public string Name {
  53.         get{ return _name; }
  54.         set{ _name = value;}
  55.     }
  56.     public int Level {
  57.         get{ return _level; }
  58.         set { _level = value; }
  59.     }
  60.     public uint FreeExp {
  61.         get{ return _freeExp; }
  62.         set{ _freeExp = value; }
  63.     }
  64.     public void AddExp(uint exp) {
  65.         _freeExp += exp;
  66.         CalculateLevel();
  67.     }
  68.  
  69.  
  70.     //take average of all of the players skills and assign that as the players level
  71.    
  72.     public void CalculateLevel() {
  73.     }
  74.    
  75.     private void SetupPrimaryAttributes() {
  76.         for(int cnt = 0; cnt < _primaryAttribute.Length; cnt++)  
  77.             _primaryAttribute[cnt] = new Attribute();
  78.             // _primaryAttribute[cnt].Name = ((AttributeName)cnt).ToString();
  79.  
  80.     }
  81.    
  82.     private void SetupVitals() {
  83.         for(int cnt = 0; cnt < _vital.Length; cnt++)
  84.             _vital[cnt] = new Vital();
  85.  
  86.         SetupVitalModifiers ();
  87.     }
  88.  
  89.     private void SetupSkills() {
  90.         for(int cnt = 0; cnt < _skill.Length; cnt++)
  91.             _skill[cnt] = new Skill();
  92.  
  93.         SetupSkillModifiers ();
  94.     }
  95.     public Attribute GetPrimaryAttribute(int index) {
  96.         return _primaryAttribute[index];
  97.     }
  98.     public Vital GetVital(int index) {
  99.         return _vital[index];
  100.     }
  101.     public Skill GetSkill(int index) {
  102.         return _skill[index];
  103.     }
  104.     private void SetupVitalModifiers() {
  105.         //Health
  106.         //GetVital((int)VitalName.Health).AddModifier(new ModifyingAttribute(GetPrimaryAttribute((int)AttributeName.Constitution), .5f));
  107.         ModifyingAttribute health = new ModifyingAttribute();
  108.         health.attribute = GetPrimaryAttribute((int)AttributeName.Constitution);
  109.         health.ratio = .5f;
  110.         //Energy
  111.         //GetVital((int)VitalName.Energy).AddModifier(new ModifyingAttribute(GetPrimaryAttribute((int)AttributeName.Constitution), 1));
  112.         ModifyingAttribute energy = new ModifyingAttribute();
  113.         energy.attribute = GetPrimaryAttribute((int)AttributeName.Constitution);
  114.         energy.ratio = 1;
  115.         //Mana
  116.         //GetVital((int)VitalName.Mana).AddModifier(new ModifyingAttribute(GetPrimaryAttribute((int)AttributeName.Willpower), 1));
  117.         ModifyingAttribute mana = new ModifyingAttribute();
  118.         mana.attribute = GetPrimaryAttribute((int)AttributeName.Willpower);
  119.         mana.ratio = 1;
  120.     }
  121.     private void SetupSkillModifiers() {
  122.         //Melee Offence
  123.         //GetSkill((int)SkillName.Melee_Offence).AddModifier(new ModifyingAttribute(GetPrimaryAttribute((int)AttributeName.Might), .33f));
  124.         //GetSkill((int)SkillName.Melee_Offence).AddModifier(new ModifyingAttribute(GetPrimaryAttribute((int)AttributeName.Nimbleness), .33f));
  125.         ModifyingAttribute MeleeOffenceModifier1 = new ModifyingAttribute();
  126.         ModifyingAttribute MeleeOffenceModifier2 = new ModifyingAttribute();
  127.         MeleeOffenceModifier1.attribute = GetPrimaryAttribute((int)AttributeName.Might);
  128.         MeleeOffenceModifier2.attribute = GetPrimaryAttribute((int)AttributeName.Nimbleness);
  129.         MeleeOffenceModifier1.ratio = .33f;
  130.         MeleeOffenceModifier2.ratio = .33f;
  131.         //Melee Defence
  132.         //GetSkill((int)SkillName.Melee_defence).AddModifier(new ModifyingAttribute(GetPrimaryAttribute((int)AttributeName.Speed), .33f));
  133.         //GetSkill((int)SkillName.Melee_defence).AddModifier(new ModifyingAttribute(GetPrimaryAttribute((int)AttributeName.Constitution), .33f));
  134.         ModifyingAttribute MeleeDefence1 = new ModifyingAttribute();
  135.         ModifyingAttribute MeleeDefence2 = new ModifyingAttribute();
  136.         MeleeDefence1.attribute = GetPrimaryAttribute((int)AttributeName.Speed);
  137.         MeleeDefence2.attribute = GetPrimaryAttribute((int)AttributeName.Constitution);
  138.         MeleeDefence1.ratio = .33f;
  139.         MeleeDefence2.ratio = .33f;
  140.         //Magic Offence
  141.         //GetSkill((int)SkillName.Magic_Offence).AddModifier(new ModifyingAttribute(GetPrimaryAttribute((int)AttributeName.Consentration), .33f));
  142.         //GetSkill((int)SkillName.Magic_Offence).AddModifier(new ModifyingAttribute(GetPrimaryAttribute((int)AttributeName.Willpower), .33f));
  143.         ModifyingAttribute MagicOffence1 = new ModifyingAttribute();
  144.         ModifyingAttribute MagicOffence2 = new ModifyingAttribute();
  145.         MagicOffence1.attribute = GetPrimaryAttribute((int)AttributeName.Consentration);
  146.         MagicOffence2.attribute = GetPrimaryAttribute((int)AttributeName.Willpower);
  147.         MagicOffence1.ratio = .33f;
  148.         MagicOffence2.ratio = .33f;
  149.         //Magic Defence
  150.         //GetSkill((int)SkillName.Magic_Defence).AddModifier(new ModifyingAttribute(GetPrimaryAttribute((int)AttributeName.Consentration), .33f));
  151.         //GetSkill((int)SkillName.Magic_Defence).AddModifier(new ModifyingAttribute(GetPrimaryAttribute((int)AttributeName.Willpower), .33f));
  152.         ModifyingAttribute MagicDefence1 = new ModifyingAttribute();
  153.         ModifyingAttribute MagicDefence2 = new ModifyingAttribute();
  154.         MagicDefence1.attribute = GetPrimaryAttribute((int)AttributeName.Consentration);
  155.         MagicDefence2.attribute = GetPrimaryAttribute((int)AttributeName.Willpower);
  156.         MagicDefence1.ratio = .33f;
  157.         MagicDefence2.ratio = .33f;
  158.         //Ranged Offence
  159.         //GetSkill((int)SkillName.Ranged_Offence).AddModifier(new ModifyingAttribute(GetPrimaryAttribute((int)AttributeName.Consentration), .33f));
  160.         //GetSkill((int)SkillName.Ranged_Offence).AddModifier(new ModifyingAttribute(GetPrimaryAttribute((int)AttributeName.Speed), .33f));
  161.         ModifyingAttribute RangedOffence1 = new ModifyingAttribute();
  162.         ModifyingAttribute RangedOffence2 = new ModifyingAttribute();
  163.         RangedOffence1.attribute = GetPrimaryAttribute((int)AttributeName.Consentration);
  164.         RangedOffence2.attribute = GetPrimaryAttribute((int)AttributeName.Speed);
  165.         RangedOffence1.ratio = .33f;
  166.         RangedOffence2.ratio = .33f;
  167.         //Ranged Defence
  168.         //GetSkill((int)SkillName.Ranged_Defence).AddModifier(new ModifyingAttribute(GetPrimaryAttribute((int)AttributeName.Speed), .33f));
  169.         //GetSkill((int)SkillName.Ranged_Defence ).AddModifier(new ModifyingAttribute(GetPrimaryAttribute((int)AttributeName.Nimbleness), .33f));
  170.         ModifyingAttribute RangedDefence1 = new ModifyingAttribute();
  171.         ModifyingAttribute RangedDefence2 = new ModifyingAttribute();
  172.         RangedDefence1.attribute = GetPrimaryAttribute((int)AttributeName.Speed);
  173.         RangedDefence2.attribute = GetPrimaryAttribute((int)AttributeName.Nimbleness);
  174.         RangedDefence1.ratio = .33f;
  175.         RangedDefence2.ratio = .33f;
  176.     }
  177.     public void StatUpdate() {
  178.  
  179.         for(int cnt = 0; cnt < _vital.Length; cnt++)
  180.             _vital[cnt].Update();
  181.  
  182.         for(int cnt = 0; cnt < _skill.Length; cnt++)
  183.             _skill[cnt].Update();
  184.    
  185.     }
  186. }
  187.  
  188.  
  189. //    ##########################################     BaseStats file          ########################################## //
  190.  
  191. public class BaseStat {
  192.  
  193.     private int _baseValue;             // the base value of this stats
  194.     private int _buffValue;             // the ammount of the buff to this stats
  195.     private int _expToLevel;            // the total amount of XP needed to raise this skill
  196.     private float _levelModifier;       // the modifier applied to the exp needed to raise the skills
  197.  
  198.     public BaseStat() {
  199.         _baseValue = 0;
  200.         _buffValue = 0;
  201.         _levelModifier = 1.1f;
  202.         _expToLevel = 100;
  203.     }
  204.  
  205. #region Basic Setters And Getters
  206.     // Basic setters and getters
  207.  
  208.     public int BaseValue {
  209.         get { return _baseValue; }
  210.         set { _baseValue = value; }
  211.     }
  212.  
  213.     public int BuffValue {
  214.         get { return _buffValue; }
  215.         set { _buffValue = value; }
  216.     }
  217.  
  218.     public int ExpToLevel {
  219.         get { return _expToLevel; }
  220.         set { _expToLevel = value; }
  221.     }
  222.  
  223.     public float LevelModifier {
  224.         get { return _levelModifier; }
  225.         set { _levelModifier = value; }
  226.     }
  227. #endregion
  228.  
  229.  
  230.     // method used to calculate how much xp needed to gain next level
  231.     private int CalculateExpToLevel(){
  232.         return (int)(_expToLevel * _levelModifier);
  233.     }
  234.  
  235.     // method used to level up and increase the basevalue.
  236.     public void LevelUp(){
  237.         _expToLevel = CalculateExpToLevel();
  238.         _baseValue++;
  239.     }
  240.  
  241.     // method used to return the basevalue + the buffed value
  242.     public int AdjustedBaseValue {
  243.         get{ return _baseValue + _buffValue; }
  244.     }
  245.  
  246. }
  247.  
  248. //    ##########################################     CharacterGenerator file         ########################################## //
  249.  
  250.  
  251. using UnityEngine;
  252. using System.Collections;
  253. using System; // used for the enum class
  254.  
  255. public class CharacterGenerator : MonoBehaviour {
  256.  
  257.     private PlayerCharacter _toon;                          // the custom character
  258.     private const int START_POINTS = 350;                   // the total ammount of start points the user can spend
  259.     private const int MIN_START_ATTRIBUTE_VALUE = 10;       // The minimum value of each startpoint.
  260.     private int pointsLeft;
  261.  
  262.  
  263.     // Use this for initialization
  264.     void Start () {
  265.         _toon = new PlayerCharacter();
  266.         _toon.Awake();
  267.         pointsLeft = START_POINTS;
  268.  
  269.         for(int cnt = 0; cnt < Enum.GetValues(typeof(AttributeName)).Length; cnt++){
  270.             _toon.GetPrimaryAttribute(cnt).BaseValue = MIN_START_ATTRIBUTE_VALUE;
  271.         }
  272.  
  273.     }
  274.    
  275.     // Update is called once per frame
  276.     void Update () {
  277.  
  278.     }
  279.  
  280.     void OnGUI(){
  281.  
  282.  
  283.         DisplayAttributes();
  284.         DisplayPointsLeft();
  285.         DisplayVitals();
  286.         DisplaySkills();
  287.         DisplayName();
  288.  
  289.     }
  290.  
  291.     private void DisplayName() {
  292.         GUI.Label(new Rect(Screen.width / 2 + 60,445,45,25), "Name:");
  293.         _toon.Name = GUI.TextArea(new Rect(Screen.width / 2,465,150,25),_toon.Name);
  294.     }
  295.  
  296.     //private int numberOfAttributes = 0; // variable to count how many attributes and make adjustment in GUI size
  297.  
  298.     private void DisplayAttributes(){
  299.         for(int cnt = 0; cnt < Enum.GetValues(typeof(AttributeName)).Length; cnt++){
  300.             GUI.Label(new Rect(10,20 + (cnt * 20 ),100,25),((AttributeName)cnt).ToString() );                               // name
  301.             GUI.Label(new Rect(110,20 + (cnt * 20 ),30,25),_toon.GetPrimaryAttribute(cnt).AdjustedBaseValue.ToString());    // value
  302.  
  303.             if(GUI.Button(new Rect(150,20 + (cnt * 20 ),20,20), "+")){          //if " + " button is clicked                // add
  304.                 if(pointsLeft > 0){
  305.                     _toon.GetPrimaryAttribute(cnt).BaseValue++;
  306.                     pointsLeft--;
  307.                     _toon.StatUpdate();
  308.                 }
  309.             }
  310.             //if(_toon.GetPrimaryAttribute(cnt).BaseValue < MIN_START_ATTRIBUTE_VALUE){
  311.        
  312.             //}
  313.  
  314.             if(GUI.Button(new Rect(175,20 + (cnt * 20 ),20,20), "-")){          //if " - " button is clicked                // subtract
  315.                 if(_toon.GetPrimaryAttribute(cnt).BaseValue > MIN_START_ATTRIBUTE_VALUE){
  316.                     _toon.GetPrimaryAttribute(cnt).BaseValue--;
  317.                     pointsLeft++;
  318.                     _toon.StatUpdate();
  319.                 }
  320.             }
  321.  
  322.         }
  323.     }
  324.     //private int numberOfVitals = 0; // variable to count how many vitals and make adjustment in GUI size
  325.  
  326.     private void DisplayVitals(){
  327.         for(int cnt = 0; cnt < Enum.GetValues(typeof(VitalName)).Length; cnt++){
  328.             GUI.Label(new Rect(10,20 + ((cnt + 7) * 20 ),100,25),((VitalName)cnt).ToString() );
  329.             GUI.Label(new Rect(115,20 + ((cnt + 7) * 20 ),30,25),_toon.GetVital(cnt).AdjustedBaseValue.ToString());
  330.             //numberOfVitals++;
  331.         }
  332.     }
  333.  
  334.  
  335.  
  336.     private void DisplaySkills(){
  337.         for(int cnt = 0; cnt < Enum.GetValues(typeof(SkillName)).Length; cnt++){
  338.             GUI.Label(new Rect(Screen.width - 110,20 + (cnt * 20 ),100,25),((SkillName)cnt).ToString() );
  339.             GUI.Label(new Rect(Screen.width - 125,20 + (cnt * 20 ),30,25),_toon.GetSkill(cnt).AdjustedBaseValue.ToString());
  340.         }
  341.     }
  342.  
  343.     private void DisplayPointsLeft(){
  344.         GUI.Label(new Rect(10,Screen.height / 2,150,20), "Points Left: " + pointsLeft);
  345.     }
  346.  
  347. }
  348.  
  349. //    ##########################################     ModifiedStat file       ########################################## //
  350.  
  351. using System.Collections.Generic;
  352.  
  353. public class ModifiedStat : BaseStat {
  354.  
  355.     private List<ModifyingAttribute> _mods;         // a list of attributes that modify this stat
  356.     private int _modValue;                          // stores the ammount added to the baseValue from the modifyers
  357.  
  358.     // constructor
  359.     public ModifiedStat(){
  360.         _mods = new List<ModifyingAttribute>();
  361.         _modValue = 0;
  362.     }
  363.  
  364.     // function to add the mod. attribute to the list
  365.     public void AddModifyer( ModifyingAttribute mod) {
  366.         _mods.Add(mod);
  367.     }
  368.  
  369.     // calculate the ammout we are going to modify our base value with
  370.     private void CalculateModValue() {
  371.         _modValue = 0;
  372.  
  373.         if(_mods.Count > 0)
  374.             foreach(ModifyingAttribute att in _mods)
  375.                 _modValue += (int)(att.attribute.AdjustedBaseValue * att.ratio);
  376.     }
  377.  
  378.     // Using the BaseValue and the BuffValue from Basestat's getters and setters
  379.     public new int AdjustedBaseValue {
  380.         get{ return BaseValue + BuffValue + _modValue; }
  381.     }
  382.  
  383.     public void Update(){
  384.         CalculateModValue();
  385.     }
  386.  
  387. }
  388.  
  389. public struct ModifyingAttribute {
  390.     public Attribute attribute;
  391.     public float ratio;
  392.  
  393.     public ModifyingAttribute(Attribute att, float rat){
  394.         attribute = att;
  395.         ratio = rat;
  396.     }
  397.  
  398. }
  399.  
  400.  
  401.  
  402. //    ##########################################     PlayerCharacter file        ########################################## //
  403. public class PlayerCharacter : BaseCharacter {
  404. }
  405.  
  406. //    ##########################################     Skill file          ########################################## //
  407.  
  408. public class Skill : ModifiedStat {
  409.  
  410.     private bool _known;
  411.  
  412.     public Skill(){
  413.         _known = false;
  414.         ExpToLevel = 25;
  415.         LevelModifier = 1.1f;
  416.     }
  417.  
  418.     public bool Known {
  419.         get{ return _known; }
  420.         set{ _known = value; }
  421.  
  422.     }
  423.  
  424. }
  425.  
  426. public enum SkillName {
  427.     Melee_Offence,
  428.     Melee_defence,
  429.     Ranged_Offence,
  430.     Ranged_Defence,
  431.     Magic_Offence,
  432.     Magic_Defence
  433. }
  434. //    ##########################################     Vital file          ########################################## //
  435.  
  436. public class Vital : ModifiedStat {
  437.  
  438.     private int _curValue;
  439.  
  440.     public Vital() {
  441.         _curValue = 0;
  442.         ExpToLevel = 50;
  443.         LevelModifier = 1.1f;
  444.     }
  445.  
  446.     public int CurValue {
  447.         get{
  448.             if(_curValue > AdjustedBaseValue)
  449.                 _curValue = AdjustedBaseValue;
  450.  
  451.             return _curValue;
  452.         }
  453.         set{ _curValue = value; }
  454.     }
  455.  
  456. }
  457.  
  458. public enum VitalName {
  459.     Health,
  460.     Energy,
  461.     Mana
  462. }
Advertisement
Add Comment
Please, Sign In to add comment