endlesstravel / Love2dCS

C# Wrapper for LÖVE, a 2d game engine
MIT License
166 stars 14 forks source link

make love objects creatable outside load event #82

Closed Shadowblitz16 closed 4 years ago

Shadowblitz16 commented 5 years ago

is it possible to make love objects creatable outside Scene.Load()?

for example this is what you have to do right now..

public class Game : Scene
{
    Image image;
    public override Load()
    {
        Image = Graphics.NewImage(*/something here /*);
    }
}

it would be nice to be able to do..

public class Game : Scene
{
    Image image = Graphics.NewImage(*/something here /*);
    public override Load()
    {

    }
}
endlesstravel commented 5 years ago

yes you can, check out this : https://endlesstravel.github.io/#/tutorial/03.boot-config?id=note

Shadowblitz16 commented 5 years ago

My issue is doing something like this..

using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using LiteNetLib;
using Love;

namespace BBEngine
{
    public class Game
    {

        private static GameManager Manager = new GameManager();
        private class  GameManager : Scene
        {
            bool Initialized = false;

            static private Game   Game;
            static private Canvas Canvas;

            private int   _w     = 256;
            private int   _h     = 240;
            private int   _speed = 60;
            private int   _scale = 1;
            private float _delta = 0;

            public int   W
            {
                get
                {
                    return _w;
                }
                set
                {
                    _w = value;
                    if (Initialized) Canvas = Graphics.NewCanvas(W / Scale, H /Scale);
                }
            }
            public int   H
            {
                get
                {
                    return _h;
                }
                set
                {
                    _h = value;
                    if (Initialized) Canvas = Graphics.NewCanvas(W / Scale, H / Scale);
                }
            }
            public int   Scale
            {
                get
                {
                    return _scale;
                }
                set
                {
                    _scale = value;
                    if (Initialized) Canvas = Graphics.NewCanvas(W / Scale, H / Scale);
                }
            }
            public int   Speed
            {
                get
                {
                    return _speed;
                }
                set
                {
                    _speed = value;
                    if (Initialized) Timer.EnableLimitMaxFPS(Speed);
                }
            }
            public float Delta
            {
                get
                {
                    return _delta;
                }
                set
                {
                    _delta = value;
                }
            }

            public GameManager()
            {

            }

            public void Run(Game game)
            {
                Game = game;
                Boot.Run(this, new BootConfig() { WindowWidth = W*Scale, WindowHeight = H*Scale });
            }

            public override void Load()
            {
                Gfx.Init();
                Timer.EnableLimitMaxFPS(Speed);
                Canvas = Graphics.NewCanvas(W / Scale, H / Scale);
                Game.OnInit();

            }
            public override void Update(float dt)
            {
                Btn.Update();
                Sys.Update();

                Delta = Timer.GetDelta();
                Game.OnTick();
                Obj.TickAll();
            }
            public override void Draw()
            {
                var sx = Graphics.GetWidth()  / W;
                var sy = Graphics.GetHeight() / H;
                Graphics.Scale(sx, sy);
                Game.OnDraw();
                Obj.TickAll();

            }
            public override bool Quit()
            {
                Game.OnQuit();
                return true;
            }
        }

        //Properties
        public int   W    
        {
            get
            {
                return Manager.W;
            }
            set
            {
                Manager.W = value;
            }
        }
        public int   H    
        {
            get
            {
                return Manager.H;
            }
            set
            {
                Manager.H = value;
            }
        }
        public int   Scale
        {
            get
            {
                return Manager.Scale;
            }
            set
            {
                Manager.Scale = value;
            }
        }
        public int   Speed
        {
            get
            {
                return Manager.Speed;
            }
            set
            {
                Manager.Speed = value;
            }
        }
        public float Delta
        {
            get
            {
                return Manager.Delta;
            }
            set
            {
                Manager.Delta = value;
            }
        }

        public static void Run(Game game)
        {
            Manager.Run(game);
        }

        internal static void Sync(NetPacket packet, NetPeer peer)
        {
            using (packet)
            {
               // OnSync(peer, packet);
            }
        }

        //Events
        protected virtual void OnInit() {  }
        protected virtual void OnTick() {  }
        protected virtual void OnDraw() {  }
        protected virtual void OnQuit() {  }
        protected virtual void OnSync(NetPeer peer, NetPacket packet) {  }
    }   
}
Shadowblitz16 commented 5 years ago

@endlesstravel I think this should be implemented in the default modules. Love.Resource is missing a ton of overloads and object types. I don't see any reason why you should keep the module creation methods reliant on Love initialization

endlesstravel commented 4 years ago

your are right, now i change the default behavior on FileSystem, you can read file everywhere you want from 11.0.44 https://www.nuget.org/packages/Love2dCS/11.0.44