aboutsummaryrefslogtreecommitdiff
path: root/screens/menu_screen.wren
blob: ea712d31e34e290c8cb85ba4c0b79a0676e67850 (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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
import "graphics" for Canvas, Color
import "./controls" for Controls
import "./config" for Config
import "audio" for AudioEngine
import "font" for Font
import "math" for Math
import "dome" for Platform

class MenuScreen {
    construct new(gameState) {
        __gameState = gameState
        var channel = AudioEngine.play("menu_music", 0.5, true)
        _selectedMenuIdx = 0
        _prevMenuIdx = 0
        _fadeFrame = 255
        _fadeSelector = 255
        _menuItems =  [
            {
                "name": "Start",
                "selected": true,
                "state": "level1",
                "font": Font.default,
                "padding": 20
            },
            {
                "name": "Credits",
                "selected": false,
                "state": "credit",
                "font": Font.default,
                "padding": 40
            }
        ]
        _time = 0
    }

    update() {
        if(Controls.justPressed(Config.KeyboardConstants["ATTACK"])) {
            __gameState.switch(_menuItems[_selectedMenuIdx]["state"])
        }

        if(Controls.justPressed(Config.KeyboardConstants["UP"])) {
            _prevMenuIdx = _selectedMenuIdx
            if((_selectedMenuIdx-1) < 0) {
                _selectedMenuIdx = (_menuItems.count - 1)
            } else {
                _selectedMenuIdx = _selectedMenuIdx - 1
            }
            AudioEngine.play("select")
        }

        if(Controls.justPressed(Config.KeyboardConstants["DOWN"])) {
            _prevMenuIdx = _selectedMenuIdx
            if((_selectedMenuIdx+1) > (_menuItems.count - 1)) {
                _selectedMenuIdx = 0
            } else {
                _selectedMenuIdx = _selectedMenuIdx + 1
            }
            AudioEngine.play("select")
        }
        
        if(_prevMenuIdx!=_selectedMenuIdx) {
            _menuItems[_prevMenuIdx]["selected"] = false
            _menuItems[_selectedMenuIdx]["selected"] = true
        }
        _fadeFrame = Math.floor(127 + (127 * Math.cos(System.clock)))
        _fadeSelector = inOutQuad(_time, 0, 255, 50)
        if (_time>51) {
            _time = 0
        } else {
            _time = _time + 1
        }
    }

    draw(dt) {
        
        drawTitle()
        
        for(menuItem in _menuItems) {
            drawMenu(menuItem)
        }

        drawBlinker()
    }

    drawTitle() {
        Canvas.print("REBIRTH",Config.W/2 - 40,Config.H/2 - 20, Color.white, "font_title")
    }

    drawMenu(menuItem) {
        var x = Config.W/2 - ((menuItem["name"].count * 8)/2)
        var y = Config.H/2 + menuItem["padding"]
        Canvas.print(menuItem["name"],x ,y , menuItem["selected"] ? Color.red : Color.white, menuItem["font"])
        if(menuItem["selected"]) {
            Canvas.print(">",x - 10 ,y , Color.new(255, 0, 77, _fadeSelector), menuItem["font"])
        }
    }

    drawBlinker() {
        Canvas.print("Press X to select",Config.W/2 - 64,Config.H/2 + 100, Color.new(255, 255, 255, _fadeFrame), Font.default)
    }

    inOutQuad(t, b, c, d) {
        t = t / d * 2
        if( t < 1 ) {
            return Math.abs(Math.floor(((c / 2) * (t.pow(2))) + b))
        } else {
            return  Math.abs(Math.floor(((-c / 2) * ((t - 1) * (t - 3) - 1)) + b))
        }
            
        
    }

}

/*
(Character length * 8 ) / 2 
*/