-
Notifications
You must be signed in to change notification settings - Fork 0
/
OnlineMenu.adept
133 lines (107 loc) · 4.87 KB
/
OnlineMenu.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
import 'main.adept'
struct OnlineMenu (
is_back_button_hovered, is_create_button_hovered bool,
listing_hovered int // 0 == none
) {
func setup {}
func enter {
gamedata.interpreter.setMonitorListings(true)
gamedata.gamekind = GameKind::ONLINE
gamedata.manager.emulate_network = false
}
func exit {
gamedata.interpreter.setMonitorListings(false)
}
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 {
if gamedata.interpreter.updatedListings(def listing_names <String> List) {
gamedata.listings.clear()
each String in listing_names, gamedata.listings.add(roomListing(it))
}
captMouseViewPosition(undef mouseX float, undef mouseY float)
this.calculateWhetherBackButtonIsHovered(mouseX, mouseY)
this.calculateWhetherCreateButtonIsHovered(mouseX, mouseY)
this.calculateWhetherListingIsHovered(mouseX, mouseY)
}
func click(x, y float, button int){
unless button == 1, return
this.calculateWhetherBackButtonIsHovered(x, y)
if this.is_back_button_hovered {
gamedata.setScene('mainmenu')
sfx.play(sfx.back)
return
}
this.calculateWhetherCreateButtonIsHovered(x, y)
if this.is_create_button_hovered {
if gamedata.manager.getIsOnline(), gamedata.setScene('createmenu')
sfx.play(sfx.button)
return
}
if this.calculateWhetherListingIsHovered(x, y) {
this.joinRoom(gamedata.listings.getPointer(this.listing_hovered - 1).name)
sfx.play(sfx.button)
}
}
func draw {
drawButton("back", this, func &getBackButtonPosition(*OnlineMenu, *float, *float), this.is_back_button_hovered)
drawButton("create", this, func &getCreateButtonPosition(*OnlineMenu, *float, *float), this.is_create_button_hovered)
notice_message String = gamedata.manager.getIsOnline() ? (gamedata.listings.length == 0 ? "No Rooms" : "") : "You are Offline"
unless notice_message.empty() {
drawTextCentered(notice_message)
return
}
each RoomListing in gamedata.listings {
room_name String = it.name.length > 10 ? it.name.sub(0, 9) + "~" : it.name
this.getListingButtonPosition(idx + 1, undef listing_x float, undef listing_y float)
drawButton(room_name, listing_x, listing_y, this.listing_hovered == idx + 1)
}
}
func getListingButtonPosition(id usize, out x, y *float) {
// NOTE: 'id' starts at 1
total_height float = gamedata.listings.length as float * MAIN_MENU_BUTTON_HEIGHT + (gamedata.listings.length - 1) as float * 16.0f
y_offset float = captViewHeight() / 2.0f - total_height / 2.0f + (MAIN_MENU_BUTTON_HEIGHT + 16.0f) * (id - 1) as float
*x = captViewWidth() / 2.0f - MAIN_MENU_BUTTON_WIDTH / 2.0f
*y = y_offset
}
func getBackButtonPosition(out x, y *float){
*x = 16.0f
*y = captViewHeight() - 16.0f - MAIN_MENU_BUTTON_HEIGHT
}
func getCreateButtonPosition(out x, y *float){
*x = 16.0f
*y = captViewHeight() - 16.0f * 2.0f - MAIN_MENU_BUTTON_HEIGHT * 2.0f
}
func calculateWhetherBackButtonIsHovered(mouseX, mouseY float) bool {
this.is_back_button_hovered = isButtonHovered(this, func &getBackButtonPosition(*OnlineMenu, *float, *float), mouseX, mouseY)
return this.is_back_button_hovered
}
func calculateWhetherCreateButtonIsHovered(mouseX, mouseY float) bool {
this.is_create_button_hovered = isButtonHovered(this, func &getCreateButtonPosition(*OnlineMenu, *float, *float), mouseX, mouseY)
return this.is_create_button_hovered
}
func calculateWhetherListingIsHovered(mouseX, mouseY float) bool {
this.listing_hovered = 0
each RoomListing in gamedata.listings {
this.getListingButtonPosition(idx + 1, undef x float, undef y float)
if isButtonHovered(x, y, mouseX, mouseY) {
this.listing_hovered = idx + 1
return true
}
}
return false
}
func joinRoom(name String) {
gamedata.interpreter.clearRoomMovements()
gamedata.interpreter.clearAgents()
gamedata.interpreter.ignoreNextJoinMessageFrom(gamedata.player_name)
gamedata.manager.writeOutgoing("$join " + name + "\n")
gamedata.manager.writeOutgoing("$lobby\n")
gamedata.manager.emulate_network = false
gamedata.room_name = name
gamedata.is_in_room = true
gamedata.setScene('lobby')
}
}