FIZIXAgency

Unity 3D State Manager: Game State Manager C# Script

Aug 29th, 2012
5,821
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /*
  2.     Unity 3D: Game State Manager Script Source for State Manager
  3.        
  4.     Copyright 2012 FIZIX Digital Agency
  5.     http://www.fizixstudios.com
  6.        
  7.     For more information see the tutorial at:
  8.     http://www.fizixstudios.com/labs/do/view/id/unity-game-state-manager
  9.        
  10.        
  11.     Notes:
  12.         This script is a C# game state manager for Unity 3D; you should review the gamestart.cs
  13.         script to help understand how to implement game states.
  14. */
  15.  
  16.  
  17.  
  18. using UnityEngine;
  19. using System.Collections;
  20.  
  21. public class gamestate : MonoBehaviour {
  22.    
  23.     // Declare properties
  24.     private static gamestate instance;
  25.     private string activeLevel;         // Active level
  26.     private string name;                // Characters name
  27.     private int maxHP;                  // Max HP
  28.     private int maxMP;                  // Map MP
  29.     private int hp;                     // Current HP
  30.     private int mp;                     // Current MP
  31.     private int str;                    // Characters Strength
  32.     private int vit;                    // Characters Vitality
  33.     private int dex;                    // Characters Dexterity
  34.     private int exp;                    // Characters Experience Points
  35.    
  36.    
  37.    
  38.     // ---------------------------------------------------------------------------------------------------
  39.     // gamestate()
  40.     // ---------------------------------------------------------------------------------------------------
  41.     // Creates an instance of gamestate as a gameobject if an instance does not exist
  42.     // ---------------------------------------------------------------------------------------------------
  43.     public static gamestate Instance
  44.     {
  45.         get
  46.         {
  47.             if(instance == null)
  48.             {
  49.                 instance = new GameObject("gamestate").AddComponent<gamestate> ();
  50.             }
  51.  
  52.             return instance;
  53.         }
  54.     }  
  55.    
  56.     // Sets the instance to null when the application quits
  57.     public void OnApplicationQuit()
  58.     {
  59.         instance = null;
  60.     }
  61.     // ---------------------------------------------------------------------------------------------------
  62.    
  63.    
  64.     // ---------------------------------------------------------------------------------------------------
  65.     // startState()
  66.     // ---------------------------------------------------------------------------------------------------
  67.     // Creates a new game state
  68.     // ---------------------------------------------------------------------------------------------------
  69.     public void startState()
  70.     {
  71.         print ("Creating a new game state");
  72.        
  73.         // Set default properties:
  74.         activeLevel = "Level 1";
  75.         name = "My Character";
  76.         maxHP = 250;
  77.         maxMP = 60;
  78.         hp = maxHP;
  79.         mp = maxMP;
  80.         str = 6;
  81.         vit = 5;
  82.         dex = 7;
  83.         exp = 0;
  84.                
  85.         // Load level 1
  86.         Application.LoadLevel("level1");
  87.     }
  88.    
  89.    
  90.    
  91.     // ---------------------------------------------------------------------------------------------------
  92.     // getLevel()
  93.     // ---------------------------------------------------------------------------------------------------
  94.     // Returns the currently active level
  95.     // ---------------------------------------------------------------------------------------------------
  96.     public string getLevel()
  97.     {
  98.         return activeLevel;
  99.     }
  100.    
  101.    
  102.     // ---------------------------------------------------------------------------------------------------
  103.     // setLevel()
  104.     // ---------------------------------------------------------------------------------------------------
  105.     // Sets the currently active level to a new value
  106.     // ---------------------------------------------------------------------------------------------------
  107.     public void setLevel(string newLevel)
  108.     {
  109.         // Set activeLevel to newLevel
  110.         activeLevel = newLevel;
  111.     }
  112.    
  113.    
  114.     // ---------------------------------------------------------------------------------------------------
  115.     // getName()
  116.     // ---------------------------------------------------------------------------------------------------
  117.     // Returns the characters name
  118.     // ---------------------------------------------------------------------------------------------------
  119.     public string getName()
  120.     {
  121.         return name;
  122.     }
  123.    
  124.    
  125.     // ---------------------------------------------------------------------------------------------------
  126.     // getHP()
  127.     // ---------------------------------------------------------------------------------------------------
  128.     // Returns the characters hp
  129.     // ---------------------------------------------------------------------------------------------------
  130.     public int getHP()
  131.     {
  132.         return hp;
  133.     }
  134.    
  135.     // ---------------------------------------------------------------------------------------------------
  136.     // getMP()
  137.     // ---------------------------------------------------------------------------------------------------
  138.     // Returns the characters mp
  139.     // ---------------------------------------------------------------------------------------------------
  140.     public int getMP()
  141.     {
  142.         return mp;
  143.     }
  144. }
RAW Paste Data