|
1 | 1 | use std::{cell::Cell, collections::HashMap, rc::Rc}; |
2 | 2 |
|
3 | | -use game::loading::load_level; |
| 3 | +use game::{loading::load_level, parsing::parse_level_download_response}; |
4 | 4 | use macroquad::prelude::*; |
5 | 5 | use miniquad::conf::Icon; |
6 | 6 | use image::ImageReader; |
@@ -256,9 +256,46 @@ async fn main() { |
256 | 256 | active: false |
257 | 257 | }; |
258 | 258 |
|
| 259 | + let mut level_download_button = Button::new( |
| 260 | + || screen_width() - 140.0, |
| 261 | + || 75.0, |
| 262 | + || 130.0, |
| 263 | + || 75.0, |
| 264 | + "Download", |
| 265 | + 15, |
| 266 | + false |
| 267 | + ); |
| 268 | + |
| 269 | + let mut level_id_textbox = TextBox { |
| 270 | + rect: Rect { |
| 271 | + x: screen_width() - 140.0, |
| 272 | + y: 10.0, |
| 273 | + w: 130.0, |
| 274 | + h: 55.0 |
| 275 | + }, |
| 276 | + text: "Level ID".to_string(), |
| 277 | + text_size: 18, |
| 278 | + max_length: 6, |
| 279 | + spaces_allowed: false, |
| 280 | + active: false |
| 281 | + }; |
| 282 | + |
| 283 | + |
| 284 | + |
| 285 | + let mut level_play_button = Button::new( |
| 286 | + || screen_width() as f32 / 2.0 - 100.0, |
| 287 | + || screen_height() as f32 / 2.0 - 50.0, |
| 288 | + || 200.0, |
| 289 | + || 100.0, |
| 290 | + "Play", |
| 291 | + 20, |
| 292 | + false |
| 293 | + ); |
| 294 | + |
259 | 295 | // Url's for server requests |
260 | 296 | let main_url = "http://georays.puppet57.xyz/php-code/".to_string(); |
261 | 297 | let latest_version_url: String = format!("{}get-latest-version.php", main_url).to_string(); |
| 298 | + let download_url: String = format!("{}download-level.php", main_url); |
262 | 299 |
|
263 | 300 | println!("Defining important game variables.."); |
264 | 301 | let game_state: Shared<GameState> = Shared::<GameState>(Rc::new(Cell::new(GameState::Menu))); |
@@ -425,6 +462,18 @@ async fn main() { |
425 | 462 | let mut grnd_green: String = "".to_string(); |
426 | 463 | let mut grnd_blue: String = "".to_string(); |
427 | 464 |
|
| 465 | + let mut level_id: String = "".to_string(); |
| 466 | + |
| 467 | + // Values for server responses |
| 468 | + let mut level_download_response: String = "".to_string(); |
| 469 | + let mut online_level_name = "".to_string(); |
| 470 | + let mut online_level_desc = "".to_string(); |
| 471 | + let mut online_level_data = "".to_string(); |
| 472 | + let mut online_level_diff: u8 = 0; |
| 473 | + let mut online_level_rated: bool = false; |
| 474 | + let mut online_level_creator = "".to_string(); |
| 475 | + let mut show_level_not_found: bool = false; |
| 476 | + |
428 | 477 | println!("Loading mods..."); |
429 | 478 | let mod_paths_kinda = std::fs::read_dir("./mods").unwrap(); |
430 | 479 | let mut mod_contents: Vec<String> = vec![]; |
@@ -1035,10 +1084,83 @@ async fn main() { |
1035 | 1084 |
|
1036 | 1085 | GameState::SearchPage => { |
1037 | 1086 | back_button.update(delta_time); |
| 1087 | + level_download_button.update(delta_time); |
1038 | 1088 |
|
1039 | 1089 | if back_button.is_clicked() { |
1040 | 1090 | game_state.0.set(GameState::CreatorMenu); |
1041 | 1091 | } |
| 1092 | + |
| 1093 | + if level_download_button.is_clicked() { |
| 1094 | + level_download_response = ureq::get(download_url.clone()) |
| 1095 | + .query("id", &level_id) |
| 1096 | + .call() |
| 1097 | + .unwrap() |
| 1098 | + .into_body() |
| 1099 | + .read_to_string() |
| 1100 | + .unwrap(); |
| 1101 | + |
| 1102 | + |
| 1103 | + if level_download_response.clone().contains(";;;;;") { |
| 1104 | + parse_level_download_response( |
| 1105 | + level_download_response.clone(), |
| 1106 | + &mut online_level_name, |
| 1107 | + &mut online_level_desc, |
| 1108 | + &mut online_level_diff, |
| 1109 | + &mut online_level_rated, |
| 1110 | + &mut online_level_creator, |
| 1111 | + &mut online_level_data |
| 1112 | + ); |
| 1113 | + |
| 1114 | + game_state.0.set(GameState::LevelPage); |
| 1115 | + } else { |
| 1116 | + show_level_not_found = true; |
| 1117 | + } |
| 1118 | + } |
| 1119 | + |
| 1120 | + if level_id_textbox.is_clicked() { |
| 1121 | + level_id_textbox.active = true |
| 1122 | + } |
| 1123 | + |
| 1124 | + if level_id_textbox.is_not_clicked() { |
| 1125 | + level_id_textbox.active = false |
| 1126 | + } |
| 1127 | + |
| 1128 | + level_id_textbox.input(&mut level_id); |
| 1129 | + } |
| 1130 | + |
| 1131 | + GameState::LevelPage => { |
| 1132 | + back_button.update(delta_time); |
| 1133 | + level_play_button.update(delta_time); |
| 1134 | + |
| 1135 | + if back_button.is_clicked() { |
| 1136 | + game_state.0.set(GameState::SearchPage); |
| 1137 | + } |
| 1138 | + |
| 1139 | + if level_play_button.is_clicked() { |
| 1140 | + let load_level_result = load_level( |
| 1141 | + online_level_data.clone(), |
| 1142 | + &mut obj_grid, |
| 1143 | + &mut cc_1001, |
| 1144 | + &mut cc_1002, |
| 1145 | + &mut current_song, |
| 1146 | + true, |
| 1147 | + main_levels.clone() |
| 1148 | + ); |
| 1149 | + |
| 1150 | + stop_audio(&sink); |
| 1151 | + play_audio_path( |
| 1152 | + ¤t_song, |
| 1153 | + master_volume, |
| 1154 | + false, |
| 1155 | + &sink |
| 1156 | + ); |
| 1157 | + |
| 1158 | + if load_level_result == "ok" { |
| 1159 | + game_state.0.set(GameState::Playing); |
| 1160 | + } else { |
| 1161 | + println!("Problem loading level: {}", load_level_result); |
| 1162 | + } |
| 1163 | + } |
1042 | 1164 | } |
1043 | 1165 | } |
1044 | 1166 |
|
@@ -1615,7 +1737,43 @@ async fn main() { |
1615 | 1737 | } |
1616 | 1738 | ); |
1617 | 1739 |
|
| 1740 | + if show_level_not_found { |
| 1741 | + draw_text_pro( |
| 1742 | + &level_download_response, |
| 1743 | + screen_width() / 2.0 - measure_text_ex(&level_download_response, 30, &font) / 2.0, |
| 1744 | + screen_height() / 2.0, |
| 1745 | + 30, |
| 1746 | + RED, |
| 1747 | + &font |
| 1748 | + ); |
| 1749 | + } |
| 1750 | + |
| 1751 | + back_button.draw(false, None, 1.0, false, &font); |
| 1752 | + level_download_button.draw(false, None, 1.0, false, &font); |
| 1753 | + level_id_textbox.draw(level_id.clone(), &font); |
| 1754 | + } |
| 1755 | + |
| 1756 | + GameState::LevelPage => { |
| 1757 | + draw_texture_ex( |
| 1758 | + &default_bg_no_gradient, |
| 1759 | + -50.0, |
| 1760 | + -75.0, |
| 1761 | + Color::from_rgba(20, 20, 20, 255), |
| 1762 | + DrawTextureParams { |
| 1763 | + dest_size: Some(Vec2 { |
| 1764 | + x: default_bg_no_gradient.width() * screen_width() as f32 * 0.0008, |
| 1765 | + y: default_bg_no_gradient.height() * screen_width() as f32 * 0.0008 |
| 1766 | + }), |
| 1767 | + source: None, |
| 1768 | + rotation: 0.0, |
| 1769 | + flip_x: false, |
| 1770 | + flip_y: false, |
| 1771 | + pivot: None |
| 1772 | + } |
| 1773 | + ); |
| 1774 | + |
1618 | 1775 | back_button.draw(false, None, 1.0, false, &font); |
| 1776 | + level_play_button.draw(false, None, 1.0, false, &font); |
1619 | 1777 | } |
1620 | 1778 | } |
1621 | 1779 |
|
|
0 commit comments