Tuesday, June 21, 2011

Foray into Photon - Part 12 - Seperating the Engine from the GUI

So today I hope to walk through the code necessary to log in. We are going to start with the client code as that is probably the easiest to work with. The first thing I want to do is separate the the IGameListener from the GUI login code. This is because we don't want to keep creating listeners that contain the same code. So lets go ahead and create a new script called MMOEngine and keep the Login script. Here is the MMOEngine code - it is basically all the code from login except the OnGUI code, the Start function is now Awake and the Game variable is now static:


using System;
using ExitGames.Client.Photon;
using UnityEngine;


public class MMOEngine : MonoBehaviour, IGameListener
{

    private static Game _engine;


    public static Game Engine
    {
        get { return _engine; }   
    }


    // Use this for initialization
    void Awake()
    {
        Application.runInBackground = true;
        _engine = new Game(this);
    }


    // Update is called once per frame
    void Update()
    {
        try
        {
            _engine.Update();
        }
        catch (Exception e)
        {
            Debug.Log(e);
        }
    }


    ///


    /// The on application quit.
    ///

    public void OnApplicationQuit()
    {
        try
        {
            _engine.Disconnect();
        }
        catch (Exception e)
        {
            Debug.Log(e);
        }
    }


    #region Inherited Interfaces


    #region IGameListener
    public bool IsDebugLogEnabled
    {
        get { return true; }
    }


    public void LogDebug(Game game, string message)
    {
        Debug.Log(message);
    }


    public void LogError(Game game, string message)
    {
        Debug.Log(message);
    }


    public void LogError(Game game, Exception exception)
    {
        Debug.Log(exception.ToString());
    }


    public void LogInfo(Game game, string message)
    {
        Debug.Log(message);
    }


    public void OnConnect(Game game)
    {
        Debug.Log("connected");
    }


    public void OnDisconnect(Game game, StatusCode returnCode)
    {
        Debug.Log("disconnected");
    }


    #endregion


    #endregion
}

The new code for Login.cs is this:


using System.Collections;
using UnityEngine;
using ExitGames.Client.Photon;
using AegisBornCommon;


public class Login : MonoBehaviour
{

    private Game _engine;


    public void Start()
    {
        _engine = MMOEngine.Engine;
    }


    public void OnGUI()
    {
        if (GUI.Button(new Rect(100, 60, 100, 30), "Connect"))
        {
            var peer = new PhotonPeer(_engine, false);


            _engine.Initialize(peer, "localhost:5055", "AegisBorn");
        }
        if (GUI.Button(new Rect(200, 60, 100, 30), "Send Operation"))
        {
            _engine.SendOp((OperationCode)100, new Hashtable(), false, 0);
        }
        GUI.Label(new Rect(100, 100, 300, 300), _engine.State.ToString());
    }
}

Now that we have them separated, we want to create a new game object called MMOEngine and attach our script to it. This now makes it like our SmartFoxServer code where we had the connection handler get a connection to the SmartFoxConnection class.

Nothing too dramatic this time. Next time we will be pulling over our GUI code from our other project and sending an operation that asks to log in. Enjoy!

No comments: