forked from chris-hinson/waste
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstart_menu.rs
390 lines (367 loc) · 13 KB
/
start_menu.rs
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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
#![allow(unused)]
use crate::backgrounds::{Tile, WIN_H, WIN_W};
use crate::camera::MenuCamera;
use crate::game_client::*;
use crate::player::Player;
use crate::{battle, GameState};
use bevy::{prelude::*, ui::*};
use iyes_loopless::prelude::*;
use local_ip_address::local_ip;
use rand::seq::SliceRandom;
use std::fmt::format;
use std::net::{IpAddr, Ipv4Addr, SocketAddr, UdpSocket};
use std::sync::mpsc::{channel, Receiver, Sender};
const START_MENU_BACKGROUND: &str = "backgrounds/start_screen.png";
pub(crate) const TEXT_COLOR: Color = Color::rgb(0.9, 0.9, 0.9);
pub(crate) const NORMAL_BUTTON: Color = Color::rgb(0.15, 0.15, 0.15);
pub(crate) const HOVERED_BUTTON: Color = Color::rgb(0.25, 0.25, 0.25);
pub(crate) const PRESSED_BUTTON: Color = Color::rgb(0.75, 0.35, 0.35);
pub struct MainMenuPlugin;
#[derive(Component)]
pub(crate) struct MainMenuBackground;
#[derive(Component)]
pub(crate) struct StartButton;
#[derive(Component)]
pub(crate) struct CreditsButton;
#[derive(Component)]
pub(crate) struct MultiplayerButton;
#[derive(Component)]
pub(crate) struct StartMenuUIElement;
#[derive(Component)]
pub(crate) struct HelpButton;
//Builds plugin called MainMenuPlugin
impl Plugin for MainMenuPlugin {
fn build(&self, app: &mut App) {
app.add_enter_system(GameState::Start, setup_menu)
.add_system_set(
ConditionSet::new()
// Only run handlers on Start state
.run_in_state(GameState::Start)
.with_system(start_button_handler)
.with_system(credits_button_handler)
.with_system(multiplayer_button_handler)
.with_system(help_button_handler)
.into(),
)
.add_exit_system(GameState::Start, despawn_start_menu);
}
}
// Clears buttons from screen when ran
// Should be run after START button is pressed
fn despawn_start_menu(
mut commands: Commands,
button_query: Query<Entity, With<Button>>,
camera_query: Query<Entity, With<MenuCamera>>,
background_query: Query<Entity, With<MainMenuBackground>>,
) {
// Despawn buttons
for b in button_query.iter() {
commands.entity(b).despawn_recursive();
}
// Despawn cameras
for c in camera_query.iter() {
commands.entity(c).despawn();
}
// Despawn Main Menu Background
for bckg in background_query.iter() {
commands.entity(bckg).despawn();
}
}
pub(crate) fn start_button_handler(
mut interaction_query: Query<
(&Interaction, &mut UiColor, &Children),
(Changed<Interaction>, With<StartButton>),
>,
mut text_query: Query<&mut Text>,
mut commands: Commands,
) {
for (interaction, mut color, children) in &mut interaction_query {
let mut text = text_query
.get_mut(*children.iter().next().unwrap())
.unwrap();
match *interaction {
Interaction::Clicked => {
text.sections[0].value = "START GAME".to_string();
*color = PRESSED_BUTTON.into();
commands.insert_resource(NextState(GameState::StartPlaying));
}
Interaction::Hovered => {
text.sections[0].value = "START GAME".to_string();
*color = HOVERED_BUTTON.into();
}
Interaction::None => {
text.sections[0].value = "START GAME".to_string();
*color = NORMAL_BUTTON.into();
}
}
}
}
pub(crate) fn credits_button_handler(
mut interaction_query: Query<
(&Interaction, &mut UiColor, &Children),
(Changed<Interaction>, With<CreditsButton>),
>,
mut text_query: Query<&mut Text>,
mut commands: Commands,
//game_client: Res<GameClient>,
// game_channel: Res<GameChannel>,
) {
for (interaction, mut color, children) in &mut interaction_query {
let mut text = text_query
.get_mut(*children.iter().next().unwrap())
.unwrap();
match *interaction {
Interaction::Clicked => {
text.sections[0].value = "CREDITS".to_string();
*color = PRESSED_BUTTON.into();
commands.insert_resource(NextState(GameState::Credits));
}
Interaction::Hovered => {
text.sections[0].value = "CREDITS".to_string();
*color = HOVERED_BUTTON.into();
}
Interaction::None => {
text.sections[0].value = "CREDITS".to_string();
*color = NORMAL_BUTTON.into();
}
}
}
}
pub(crate) fn multiplayer_button_handler(
mut interaction_query: Query<
(&Interaction, &mut UiColor, &Children),
(Changed<Interaction>, With<MultiplayerButton>),
>,
mut text_query: Query<&mut Text>,
mut commands: Commands,
) {
for (interaction, mut color, children) in &mut interaction_query {
let mut text = text_query
.get_mut(*children.iter().next().unwrap())
.unwrap();
match *interaction {
Interaction::Clicked => {
text.sections[0].value = "MULTIPLAYER".to_string();
*color = PRESSED_BUTTON.into();
commands.insert_resource(NextState(GameState::MultiplayerMenu));
}
Interaction::Hovered => {
text.sections[0].value = "MULTIPLAYER".to_string();
*color = HOVERED_BUTTON.into();
}
Interaction::None => {
text.sections[0].value = "MULTIPLAYER".to_string();
*color = NORMAL_BUTTON.into();
}
}
}
}
pub(crate) fn help_button_handler(
mut interaction_query: Query<
(&Interaction, &mut UiColor, &Children),
(Changed<Interaction>, With<HelpButton>),
>,
mut text_query: Query<&mut Text>,
mut commands: Commands,
) {
for (interaction, mut color, children) in &mut interaction_query {
let mut text = text_query
.get_mut(*children.iter().next().unwrap())
.unwrap();
match *interaction {
Interaction::Clicked => {
text.sections[0].value = "HELP".to_string();
*color = PRESSED_BUTTON.into();
commands.insert_resource(NextState(GameState::Help));
}
Interaction::Hovered => {
text.sections[0].value = "HELP".to_string();
*color = HOVERED_BUTTON.into();
}
Interaction::None => {
text.sections[0].value = "HELP".to_string();
*color = NORMAL_BUTTON.into();
}
}
}
}
fn setup_menu(
mut commands: Commands,
asset_server: Res<AssetServer>,
cameras: Query<
Entity,
(
With<Camera2d>,
Without<MenuCamera>,
Without<Player>,
Without<Tile>,
),
>,
) {
// -----------------------------------------------------------------------------------------------------------
// Get an address to bind socket to
// ::bind() creates a new socket bound to the address, and a NEW BINDING
// CANNOT BE MADE to the same addr:port thereafter
let socket_addresses = get_addr();
println!("Choosing socket address from: {:?}", socket_addresses);
let udp_socket = UdpSocket::bind(&socket_addresses[..]).unwrap();
let socket_addr = udp_socket
.local_addr()
.expect("Couldn't retrieve local address from socket.");
// Set our UDP socket not to block since we need to run in frame-by-frame systems
udp_socket.set_nonblocking(true).unwrap();
info!("Successfully bound socket to {}", socket_addr);
commands.insert_resource(GameClient {
// Pass socket info over since we will need to pass the socket
// around listener/sender systems frequently
socket: SocketInfo {
socket_addr,
udp_socket,
},
// Default initialize player to client type
player_type: crate::game_client::PlayerType::Client,
});
// -----------------------------------------------------------------------------------------------------------
cameras.for_each(|camera| {
commands.entity(camera).despawn();
});
//creates camera for UI
let camera = Camera2dBundle::default();
commands.spawn_bundle(camera).insert(MenuCamera);
commands
.spawn_bundle(SpriteBundle {
texture: asset_server.load(START_MENU_BACKGROUND),
transform: Transform::from_xyz(0., 0., 0.),
..default()
})
.insert(MainMenuBackground);
commands
.spawn_bundle(ButtonBundle {
style: Style {
size: Size::new(Val::Px(300.0), Val::Px(65.0)),
// center button
margin: UiRect::all(Val::Auto),
// horizontally center child text
justify_content: JustifyContent::Center,
// vertically center child text
align_items: AlignItems::Center,
..default()
},
color: NORMAL_BUTTON.into(),
..default()
})
.with_children(|parent| {
parent.spawn_bundle(TextBundle::from_section(
"START GAME",
TextStyle {
font: asset_server.load("buttons/PressStart2P.ttf"),
font_size: 28.0,
color: TEXT_COLOR,
},
));
})
.insert(StartButton)
.insert(StartMenuUIElement);
// MULTIPLAYER BUTTON
commands
.spawn_bundle(ButtonBundle {
style: Style {
size: Size::new(Val::Px(325.0), Val::Px(65.0)),
// center button
margin: UiRect::all(Val::Auto),
// horizontally center child text
justify_content: JustifyContent::Center,
// vertically center child text
align_items: AlignItems::Center,
position_type: PositionType::Absolute,
position: UiRect {
bottom: Val::Px(275.),
left: Val::Px((WIN_W * 0.75) / 2.),
..default()
},
..default()
},
color: NORMAL_BUTTON.into(),
..default()
})
.with_children(|parent| {
parent.spawn_bundle(TextBundle::from_section(
"MULTIPLAYER",
TextStyle {
font: asset_server.load("buttons/PressStart2P.ttf"),
font_size: 28.0,
color: TEXT_COLOR,
},
));
})
.insert(MultiplayerButton)
.insert(StartMenuUIElement);
// CREDITS BUTTON
commands
.spawn_bundle(ButtonBundle {
style: Style {
size: Size::new(Val::Px(225.0), Val::Px(65.0)),
// center button
margin: UiRect::all(Val::Auto),
// horizontally center child text
justify_content: JustifyContent::Center,
// vertically center child text
align_items: AlignItems::Center,
position_type: PositionType::Absolute,
position: UiRect {
bottom: Val::Px(200.),
left: Val::Px((WIN_W * 0.825) / 2.),
..default()
},
..default()
},
color: NORMAL_BUTTON.into(),
..default()
})
.with_children(|parent| {
parent.spawn_bundle(TextBundle::from_section(
"CREDITS",
TextStyle {
font: asset_server.load("buttons/PressStart2P.ttf"),
font_size: 28.0,
color: TEXT_COLOR,
},
));
})
.insert(CreditsButton)
.insert(StartMenuUIElement);
// HELP BUTTON
commands
.spawn_bundle(ButtonBundle {
style: Style {
size: Size::new(Val::Px(125.0), Val::Px(65.0)),
// center button
margin: UiRect::all(Val::Auto),
// horizontally center child text
justify_content: JustifyContent::Center,
// vertically center child text
align_items: AlignItems::Center,
position_type: PositionType::Absolute,
position: UiRect {
bottom: Val::Px(120.),
left: Val::Px((WIN_W * 0.900) / 2.),
..default()
},
..default()
},
color: NORMAL_BUTTON.into(),
..default()
})
.with_children(|parent| {
parent.spawn_bundle(TextBundle::from_section(
"HELP",
TextStyle {
font: asset_server.load("buttons/PressStart2P.ttf"),
font_size: 28.0,
color: TEXT_COLOR,
},
));
})
.insert(HelpButton)
.insert(StartMenuUIElement);
}