aboutsummaryrefslogtreecommitdiff
path: root/examples/state_machine/main.cpp
blob: 0b7e1910cd307b16b8c7362a026cf12ea1309f13 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
#include <iostream>

#include "libjin/jin.h"

using namespace std;
using namespace JinEngine::AI;
using namespace JinEngine::Game;
using namespace JinEngine::Input;
using namespace JinEngine::Graphics;
using namespace JinEngine::Graphics::Particles;
using namespace JinEngine::Filesystem;
using namespace JinEngine::Time;

ParticleSystem* p;
StateMachine sm;
Timer timer;
Timer::Handler* hnd;
void onLoad()
{
    sm.addState("Run");
    sm.addState("Idle");
    sm.addState("Sleep");
    sm.addState("Jump");
    sm.addParametert("run");
    sm.addParameterb("idle");
    /*sm.addTransitionb("Empty", "Idle", "run", StateMachine::BOOL_IS, true);
    sm.addTransitionb("Idle", "Run", "run", StateMachine::BOOL_IS, false);
    */
    sm.addTransitiont("Empty", "Idle", "run");
    sm.addTransitiont("Idle", "Run", "run");
    sm.setEnterListener([](const string& state, void* p) {
        cout << "Enter: " << state << endl;
    });
    sm.addEnterListener("Run", [](void* p) {
        cout << "A Run state enter callback" << endl;
    });
    sm.addTranslateListener("Idle", "Run", [](void* p) {
        cout << "From Idle to Run" << endl;
    });
    Window::get()->show();
    hnd = timer.every(500, [](void*) {
        cout << sm.getCurrentState() << endl;
    }, NULL);
    timer.after(2500, [](void*) {
        //sm.setParameterb("run", true);
        sm.setParametert("run");
        //timer.cancel(hnd);
    }, NULL);
    timer.after(5000, [](void*) {
        sm.setParametert("run");
        //sm.setParameterb("run", false);
    }, NULL);
}

void onEvent(Event* e)
{
    static Application* Application = Application::get();
    if (e->type == EventType::QUIT)
        Application->stop();
}

void onUpdate(int ms)
{
    timer.update(ms);
    sm.update();
}

void onDraw()
{
}

int main(int argc, char* argv[])
{
    Application* Application = Application::get();
    Application::Setting setting;
    setting.loader = onLoad;
    setting.eventHandler = onEvent;
    setting.updater = onUpdate;
    setting.drawer = onDraw;
    Application->init(&setting);

    AssetDatabase::get()->mount(".");

    Window* wnd = Window::get();
    Window::Setting wndSetting;
    wndSetting.width = 600;
    wndSetting.height = 512;
    wndSetting.title = "Jin v0.1.1";
    wndSetting.fps = 60;
    wndSetting.vsync = false;
    wndSetting.fullscreen = false;
    wndSetting.resizable = false;
    wndSetting.icon = ".";
    wnd->init(&wndSetting);

    Application->run();

    Application->quit();
    wnd->quit();

    return 0;
}