-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathVersusAIMenu.adept
137 lines (112 loc) · 4.27 KB
/
VersusAIMenu.adept
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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
import 'main.adept'
import 'AIDifficulty.adept'
struct VersusAIMenu (
buttons <String> List,
actions <func() void> List,
hovered_button int,
is_back_button_hovered bool
) {
func setup {
this.buttons.add("Easy")
this.buttons.add("Medium")
this.buttons.add("Hard")
this.buttons.add("Cheater")
this.actions.add(func &doVersusEasyAI)
this.actions.add(func &doVersusMediumAI)
this.actions.add(func &doVersusHardAI)
this.actions.add(func &doVersusCheaterAI)
this.hovered_button = 0 // 0 == none
this.is_back_button_hovered = false
}
func enter {}
func exit {}
func __defer__ {
// Due to the compiler not yet automatically generating __defer__ functions
// via func& lookup, we have to manually ensure __defer__ is generated
}
func step {
this.hovered_button = 0
captMouseViewPosition(undef mouseX float, undef mouseY float)
repeat static this.buttons.length {
if isMouseOverVersusAIMenuButton(mouseX, mouseY, idx as int + 1) {
this.hovered_button = idx as int + 1
}
}
this.is_back_button_hovered = isButtonHovered(this, func &getBackButtonPosition(*VersusAIMenu, *float, *float), mouseX, mouseY)
}
func click(mouseX, mouseY float, button int){
unless button == 1, return
repeat static this.actions.length {
if isMouseOverVersusAIMenuButton(mouseX, mouseY, idx as int + 1) {
action func() void = this.actions.get(idx)
if action as ptr, action()
sfx.play(sfx.button)
return
}
}
this.is_back_button_hovered = isButtonHovered(this, func &getBackButtonPosition(*VersusAIMenu, *float, *float), mouseX, mouseY)
if this.is_back_button_hovered {
gamedata.setScene('versusmenu')
sfx.play(sfx.back)
return
}
}
func draw {
each button_text String in this.buttons {
getVersusAIMenuButtonPosition(idx + 1, undef button_x float, undef button_y float)
drawButton(button_text, button_x, button_y, this.hovered_button == idx + 1)
}
this.getBackButtonPosition(undef back_x float, undef back_y float)
drawButton("back", back_x, back_y, this.is_back_button_hovered)
}
func getBackButtonPosition(out x, y *float){
*x = 16.0f
*y = captViewHeight() - 16.0f - MAIN_MENU_BUTTON_HEIGHT
}
}
func getVersusAIMenuButtonPosition(button int, out x, y *float){
// NOTE: 'button' starts at 1
middle_x float = captViewWidth() / 2.0f
middle_y float = captViewHeight() / 2.0f
y_starting_offset float = 64.0f
y_run float = MAIN_MENU_BUTTON_HEIGHT + 16.0f
x_start float = middle_x - MAIN_MENU_BUTTON_WIDTH / 2.0f
y_start float = middle_y - y_starting_offset
y_start += y_run * (button - 1) as float
*x = x_start
*y = y_start
}
func isMouseOverVersusAIMenuButton(mouse_x, mouse_y float, button int) bool {
getVersusAIMenuButtonPosition(button, undef x float, undef y float)
return mouse_x > x && mouse_x < x + MAIN_MENU_BUTTON_WIDTH &&
mouse_y > y && mouse_y < y + MAIN_MENU_BUTTON_HEIGHT
}
func startVersusAIGame(difficulty AIDifficulty) {
gamedata.manager.emulate_network = true
gamedata.interpreter.clearRoomMovements()
gamedata.interpreter.clearAgents()
gamedata.interpreter.ignoreNextJoinMessageFrom(gamedata.player_name)
gamedata.gamekind = GameKind::VERSUS_AI
gamedata.ai_name = getAIName(difficulty)
gamedata.ai_difficulty = difficulty
gamedata.player_names.clear()
gamedata.player_names.add(gamedata.player_name)
gamedata.player_names.add(gamedata.ai_name)
gamedata.setScene('cardgame')
}
func doVersusEasyAI {
soundtrack.fadeOutIntoNextSong()
startVersusAIGame(AIDifficulty::EASY)
}
func doVersusMediumAI {
soundtrack.fadeOutIntoNextSong()
startVersusAIGame(AIDifficulty::MEDIUM)
}
func doVersusHardAI {
soundtrack.fadeOutIntoNextSong()
startVersusAIGame(AIDifficulty::HARD)
}
func doVersusCheaterAI {
soundtrack.fadeOutIntoNextSong()
startVersusAIGame(AIDifficulty::CHEATER)
}