Coder Social home page Coder Social logo

openframework's Introduction

OpenFramework

openframeworkchart

About:

The idea behind this framework is to simply have a main context and some services.

  • Context: is a place to register and get services.
  • Service: anything in the game/app that offers a service. like uiService, audioService, gameService, backendService etc.

Usage:

  • clone OpenFramework: git clone https://github.com/omid3098/OpenFramework.git
  • Make your own context derived from GameContext.
  • Implement your services derived from IService interface or use predefined helper services.
  • In your gamecontext, register your services inside SetupGameContext method.
  • call Init(this); after registering all services. this will initialize all services one by one and pass your context as gamecontext of the services.
  • [Optional] Check sample folder. MyGameContext.cs

So this is the basics you need to do and you are good to go!

Sample Context:

    public class MyContext : GameContext
    {
        // if you need, subscribe to OnReady in your services.
        public override event OnReadyHandler OnReady;

        public override void SetupGameContext()
        {
            // Register what ever service you want and/or add/remove them as you need ๐Ÿ˜„
            Register<GameService>();
            Register<InputService>();
            Register<AsyncService>();
            Init(this);
        }

        // after all services registered and initialized successfully, OnReadyCallback will execute.
        protected override void OnReadyCallback()
        {
            // do your stuffs here or subscribe to OnReady in your services.
            if (OnReady != null)
                OnReady.Invoke();
        }
    }

Sample Service Surgery: ๐Ÿ’‰

Here is how you write a simple InputService:

Interface simply inherits IService. you can add any method/property as you like.

    public interface IInputService : IService
    {

    }

and your concrete service inherits from your own interface

public class InputService : IInputService, IUpdatable
    {

these are sample events used for input service

        public delegate void TouchDelegate(Vector2 pos);
        public event TouchDelegate OnMouseDown;
        public event TouchDelegate OnMouseUp;
        public event TouchDelegate OnMouse;

Each IService has context so you can have access to other services and ready to check if your service is ready to use yet.

        public GameContext context { get; set; }
        public bool ready { get; set; }

Init will execute when you register services in your contex and call Init(this);

        public IEnumerator Init()
        {
            // do anythign sync/async here and when your service is ready set that to true.
            ready = true;
            yield return null;
        }

You can start and stop your services as you like. set initial valuse or dispose them on StopService. or don't use them!

        public void StartService()
        {
        }

        public void StopService()
        {
        }

Each service that inherit from IUpdatable will have IUpdate method so we can have update cycle in our services.

        public void IUpdate()
        {
            if (Input.GetMouseButtonDown(0)) if (OnMouseDown != null) OnMouseDown.Invoke(Input.mousePosition);
            if (Input.GetMouseButtonUp(0)) if (OnMouseUp != null) OnMouseUp.Invoke(Input.mousePosition);
            if (Input.GetMouseButton(0)) if (OnMouse != null) OnMouse.Invoke(Input.mousePosition);
        }

Consider checking this benchmark test between Monobehaviour Update and IUpdate:

Task was to add two numbers in update.

Iterations: 1000
OpenFramework IUpdate => 0.16ms
MonoBehaviour Update => 0.54ms

Iterations: 5000
OpenFramework IUpdate => 0.76ms
MonoBehaviour Update => 2.34ms

How far you can go with this?

  • Write custom service and send a pull request.
  • You can port any asset/lib as a service and share them as gist. ie. port LeanTouch into InputService! or Tween libs into TweenService.
  • after ~year we will have a framework that we only write game logic instead of doing same things for every project!

openframework's People

Contributors

omid3098 avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.