Created
April 26, 2020 13:54
-
-
Save endlesstravel/516767dad62a427a933cd03b41daa3e3 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using Love; | |
using System; | |
using System.Collections.Generic; | |
using System.Linq; | |
using System.Text; | |
using System.Threading.Tasks; | |
namespace SceneManager | |
{ | |
public class SceneWatchdog : Scene | |
{ | |
readonly Dictionary<string, GameScene> gameSceneDict = new Dictionary<string, GameScene>(); | |
public void Add(string uniqueName, GameScene gameScene) => gameSceneDict.Add(uniqueName, gameScene); | |
public void Change(string name) | |
{ | |
if (!gameSceneDict.ContainsKey(name)) | |
throw new Exception("have no scene name: " + name); | |
foreach (var gs in gameSceneDict.Where(kv => kv.Key != name).ToArray()) | |
{ | |
gs.Value.Pause(); | |
} | |
currentGameScene = gameSceneDict[name]; | |
currentGameSceneName = name; | |
} | |
GameScene currentGameScene; | |
string currentGameSceneName; | |
public GameScene CurrentScene => currentGameScene; | |
public string CurrentSceneName => currentGameSceneName; | |
public override void KeyPressed(KeyConstant key, Scancode scancode, bool isRepeat) => currentGameScene?.KeyPressed(key, scancode, isRepeat); | |
public override void KeyReleased(KeyConstant key, Scancode scancode) => currentGameScene?.KeyReleased(key, scancode); | |
public override void MouseMoved(float x, float y, float dx, float dy, bool isTouch) => currentGameScene?.MouseMoved(x, y, dx, dy, isTouch); | |
public override void MousePressed(float x, float y, int button, bool isTouch) => currentGameScene?.MousePressed(x, y, button, isTouch); | |
public override void MouseReleased(float x, float y, int button, bool isTouch) => currentGameScene?.MouseReleased(x, y, button, isTouch); | |
public override void MouseFocus(bool focus) => currentGameScene?.MouseFocus(focus); | |
public override void WheelMoved(int x, int y) => currentGameScene?.WheelMoved(x, y); | |
public override void JoystickPressed(Joystick joystick, int button) => currentGameScene?.JoystickPressed(joystick, button); | |
public override void JoystickReleased(Joystick joystick, int button) => currentGameScene?.JoystickReleased(joystick, button); | |
public override void JoystickAxis(Joystick joystick, float axis, float value) => currentGameScene?.JoystickAxis(joystick, axis, value); | |
public override void JoystickHat(Joystick joystick, int hat, JoystickHat direction) => currentGameScene?.JoystickHat(joystick, hat, direction); | |
public override void JoystickGamepadPressed(Joystick joystick, GamepadButton button) => currentGameScene?.JoystickGamepadPressed(joystick, button); | |
public override void JoystickGamepadReleased(Joystick joystick, GamepadButton button) => currentGameScene?.JoystickGamepadReleased(joystick, button); | |
public override void JoystickGamepadAxis(Joystick joystick, GamepadAxis axis, float value) => currentGameScene?.JoystickGamepadAxis(joystick, axis, value); | |
public override void JoystickAdded(Joystick joystick) => currentGameScene?.JoystickAdded(joystick); | |
public override void JoystickRemoved(Joystick joystick) => currentGameScene?.JoystickRemoved(joystick); | |
public override void TouchMoved(long id, float x, float y, float dx, float dy, float pressure) => currentGameScene?.TouchMoved(id, x, y, dx, dy, pressure); | |
public override void TouchPressed(long id, float x, float y, float dx, float dy, float pressure) => currentGameScene?.TouchPressed(id, x, y, dx, dy, pressure); | |
public override void TouchReleased(long id, float x, float y, float dx, float dy, float pressure) => currentGameScene?.TouchReleased(id, x, y, dx, dy, pressure); | |
public override void TextEditing(string text, int start, int end) => currentGameScene?.TextEditing(text, start, end); | |
public override void TextInput(string text) => currentGameScene?.TextInput(text); | |
public override void WindowFocus(bool focus) => currentGameScene?.WindowFocus(focus); | |
public override void WindowVisible(bool visible) => currentGameScene?.WindowVisible(visible); | |
public override void WindowResize(int w, int h) => currentGameScene?.WindowResize(w, h); | |
public override void DirectoryDropped(string path) => currentGameScene?.DirectoryDropped(path); | |
public override void FileDropped(string fileFilePath) => currentGameScene?.FileDropped(fileFilePath); | |
public override bool Quit() => currentGameScene.Quit(); | |
public override void LowMemory() => currentGameScene?.LowMemory(); | |
public override void Load() => currentGameScene?.Load(); | |
public override void Update(float dt) => currentGameScene?.Ticks(dt); | |
public override void Draw() => currentGameScene?.Draw(); | |
} | |
public class GameScene | |
{ | |
public virtual void KeyPressed(KeyConstant key, Scancode scancode, bool isRepeat) { } | |
public virtual void KeyReleased(KeyConstant key, Scancode scancode) { } | |
public virtual void MouseMoved(float x, float y, float dx, float dy, bool isTouch) { } | |
public virtual void MousePressed(float x, float y, int button, bool isTouch) { } | |
public virtual void MouseReleased(float x, float y, int button, bool isTouch) { } | |
public virtual void MouseFocus(bool focus) { } | |
public virtual void WheelMoved(int x, int y) { } | |
public virtual void JoystickPressed(Joystick joystick, int button) { } | |
public virtual void JoystickReleased(Joystick joystick, int button) { } | |
public virtual void JoystickAxis(Joystick joystick, float axis, float value) { } | |
public virtual void JoystickHat(Joystick joystick, int hat, JoystickHat direction) { } | |
public virtual void JoystickGamepadPressed(Joystick joystick, GamepadButton button) { } | |
public virtual void JoystickGamepadReleased(Joystick joystick, GamepadButton button) { } | |
public virtual void JoystickGamepadAxis(Joystick joystick, GamepadAxis axis, float value) { } | |
public virtual void JoystickAdded(Joystick joystick) { } | |
public virtual void JoystickRemoved(Joystick joystick) { } | |
public virtual void TouchMoved(long id, float x, float y, float dx, float dy, float pressure) { } | |
public virtual void TouchPressed(long id, float x, float y, float dx, float dy, float pressure) { } | |
public virtual void TouchReleased(long id, float x, float y, float dx, float dy, float pressure) { } | |
public virtual void TextEditing(string text, int start, int end) { } | |
public virtual void TextInput(string text) { } | |
public virtual void WindowFocus(bool focus) { } | |
public virtual void WindowVisible(bool visible) { } | |
public virtual void WindowResize(int w, int h) { } | |
public virtual void DirectoryDropped(string path) { } | |
public virtual void FileDropped(string fileFilePath) { } | |
public virtual bool Quit() { return true; } | |
public virtual void LowMemory() { } | |
public virtual void Load() { } | |
public virtual void Update(float dt) { } | |
public virtual void Draw() { } | |
protected virtual void OnExit() | |
{ | |
} | |
protected virtual void OnResume() | |
{ | |
} | |
protected virtual void OnPause() | |
{ | |
} | |
public RunState CurrentState { get; private set; } = RunState.NotStart; | |
public enum RunState | |
{ | |
NotStart, | |
Runing, | |
Paused, | |
End, | |
} | |
public void Pause() | |
{ | |
if (CurrentState == RunState.Runing) | |
{ | |
CurrentState = RunState.Paused; | |
OnPause(); | |
} | |
} | |
public void Exit() | |
{ | |
if (CurrentState != RunState.End) | |
{ | |
CurrentState = RunState.End; | |
OnExit(); | |
} | |
} | |
public void Ticks(float dt) | |
{ | |
if (CurrentState == RunState.NotStart) | |
{ | |
Load(); | |
CurrentState = RunState.Runing; | |
Update(dt); | |
} | |
else if (CurrentState == RunState.Runing) | |
{ | |
Update(dt); | |
} | |
else if (CurrentState == RunState.Paused) | |
{ | |
CurrentState = RunState.Runing; | |
OnResume(); | |
Update(dt); | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment