if (keyboard_check(vk_control)
&& keyboard_check_pressed(ord("R"))) {
	game_restart();
}

if (global.gameState == GAME_STATE.PLAYING
|| global.gameState == GAME_STATE.GAME_OVER) {
	camera_set_view_pos(view_camera[0], obj_player.x - room_width / 2, obj_player.y - room_height / 2);
	
	global.deathLine = min(obj_player.y + global.deathLineDistance, global.deathLine);
	if (global.deathLineDelay == 0) {
		global.deathLine -= global.deathLineSpeed;
	} else {
		--global.deathLineDelay;
	}

	/*if (mouse_check_button_pressed(mb_left)) {
		instance_create_depth(mouse_x, mouse_y, 0, obj_enemy);
	}
	if (mouse_check_button_pressed(mb_right)) {
		instance_create_depth(mouse_x, mouse_y, 0, obj_booster);
	}
	if (mouse_check_button_pressed(mb_middle)) {
		instance_create_depth(mouse_x, mouse_y, 0, obj_laser);
	}
	if (keyboard_check_pressed(ord("Q"))) {
		addArmor();
	}*/
	
	if (enemyCreatingTimer > 0) {
		--enemyCreatingTimer;
	} else {
		enemyCreatingTimer = max(10, enemyCreatingDelay + random(enemyCreatingDelayRandom) - (score div 500));
		instance_create_depth(
			camera_get_view_x(view_camera[0]) + random(room_width),
			obj_player.y + room_height / 2 + sprite_get_height(spr_enemy),
			0,
			obj_enemy
		);
	}
	
	if (obj_player.y < -5000) {
		if (laserCreatingTimer > 0) {
			--laserCreatingTimer;
		} else {
			laserCreatingTimer = max(50, laserCreatingDelay + random(laserCreatingDelayRandom) - (score div 300));
			instance_create_depth(
				camera_get_view_x(view_camera[0]) + random(room_width),
				obj_player.y,
				0,
				obj_laser
			);
		}
	}
	
	if (boosterCreatingTimer > 0) {
		--boosterCreatingTimer;
	} else if (instance_number(obj_booster) < 3) {
		boosterCreatingTimer = boosterCreatingDelay + random(boosterCreatingDelayRandom);
		instance_create_depth(
			camera_get_view_x(view_camera[0]) + random(room_width),
			obj_player.y - room_height * 4 + sprite_get_height(spr_enemy),
			0,
			obj_booster
		);
	}
	
	if (global.gameState == GAME_STATE.PLAYING
	&& keyboard_check_pressed(vk_escape)) {
		physics_pause_enable(true);
		global.gameState = GAME_STATE.PAUSE;
		obj_player.splashPressed = false;
	}
} else if (global.gameState == GAME_STATE.WAITING) {
	if (keyboard_check_pressed(vk_space)
	|| keyboard_check_pressed(vk_enter)) {
		physics_pause_enable(false);
		audio_stop_sound(snd_mainMenu);
		global.gameState = GAME_STATE.PLAYING;
	}
	if (keyboard_check_pressed(vk_escape)) {
		goToMenu();
	}
} else if (global.gameState = GAME_STATE.MAIN_MENU) {
	if (menuSelectTimer > 0) {
		--menuSelectTimer;
	}
	if (menuSelectTimer == 0) {
		if (keyboard_check(ord("W"))
		|| keyboard_check(vk_up)) {
			menuSelectTimer = menuSelectDelay;
			--global.menuSelected;
			if (global.menuSelected < 0) {
				global.menuSelected = array_length_1d(global.menuTexts) - 1;
			}
			if (global.sound) {
				audio_play_sound(snd_click, 0, false);
			}
		}
		if (keyboard_check(ord("S"))
		|| keyboard_check(vk_down)) {
			menuSelectTimer = menuSelectDelay;
			++global.menuSelected;
			if (global.menuSelected > array_length_1d(global.menuTexts) - 1) {
				global.menuSelected = 0;
			}
			if (global.sound) {
				audio_play_sound(snd_click, 0, false);
			}
		}
	}
	if (keyboard_check_pressed(vk_space)
	|| keyboard_check_pressed(vk_enter)
	|| keyboard_check_pressed(ord("J"))) {
		script_execute(global.menuEvents[global.menuSelected]);
	}
	if (keyboard_check_pressed(vk_escape)) {
		game_end();
	}
} else if (global.gameState == GAME_STATE.PAUSE) {
	if (menuSelectTimer > 0) {
		--menuSelectTimer;
	}
	if (menuSelectTimer == 0) {
		if (keyboard_check(ord("W"))
		|| keyboard_check(vk_up)) {
			menuSelectTimer = menuSelectDelay;
			--global.menuSelected;
			if (global.menuSelected < 0) {
				global.menuSelected = array_length_1d(global.pauseMenuTexts) - 1;
			}
			if (global.sound) {
				audio_play_sound(snd_click, 0, false);
			}
		}
		if (keyboard_check(ord("S"))
		|| keyboard_check(vk_down)) {
			menuSelectTimer = menuSelectDelay;
			++global.menuSelected;
			if (global.menuSelected > array_length_1d(global.pauseMenuTexts) - 1) {
				global.menuSelected = 0;
			}
			if (global.sound) {
				audio_play_sound(snd_click, 0, false);
			}
		}
	}
	if (keyboard_check_pressed(vk_space)
	|| keyboard_check_pressed(vk_enter)
	|| keyboard_check_pressed(ord("J"))) {
		script_execute(global.pauseMenuEvents[global.menuSelected]);
	}
	if (keyboard_check_pressed(vk_escape)) {
		continueGame();
	}
}
if (global.gameState == GAME_STATE.GAME_OVER) {
	if (keyboard_check_pressed(vk_space)
	|| keyboard_check_pressed(vk_enter)
	|| keyboard_check_pressed(ord("J"))) {
		instance_destroy(obj_enemy);
		instance_destroy(obj_laser);
		instance_destroy(obj_booster);
		instance_destroy(obj_player);
		global.deathLine = 0;
		instance_create_depth(room_width / 2, 0, 0, obj_player);
		instance_create_depth(room_width / 2, -sprite_get_height(spr_player) - sprite_get_height(spr_booster), 0, obj_booster);
		camera_set_view_pos(view_camera[0], obj_player.x - room_width / 2, obj_player.y - room_height / 2);
		score = 0;
		global.newScore = false;
		physics_pause_enable(true);
		global.gameState = GAME_STATE.WAITING;
	}
	if (keyboard_check_pressed(vk_escape)) {
		goToMenu();
	}
}