Skip to content

Commit

Permalink
fix some issues
Browse files Browse the repository at this point in the history
  • Loading branch information
tsukinoko-kun committed Jul 9, 2024
1 parent 9e90b97 commit ac38f83
Show file tree
Hide file tree
Showing 8 changed files with 99 additions and 62 deletions.
8 changes: 4 additions & 4 deletions .github/workflows/release.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ jobs:
- name: Build
run: |
cargo build --release --target x86_64-unknown-linux-gnu
cargo build --release --target x86_64-unknown-linux-gnu --features "storage"
- name: Prepare package
run: |
Expand Down Expand Up @@ -85,7 +85,7 @@ jobs:

- name: Build
run: |
cargo build --release --target x86_64-pc-windows-msvc
cargo build --release --target x86_64-pc-windows-msvc --features "storage"
- name: Prepare package
run: |
Expand Down Expand Up @@ -135,7 +135,7 @@ jobs:
- name: Build
run: |
cargo build --release --target x86_64-apple-darwin
cargo build --release --target x86_64-apple-darwin --features "storage"
- name: Prepare Package
run: |
Expand Down Expand Up @@ -182,7 +182,7 @@ jobs:
- name: Build
run: |
cargo build --release --target aarch64-apple-darwin
cargo build --release --target aarch64-apple-darwin --features "storage"
- name: Prepare Package
run: |
Expand Down
37 changes: 10 additions & 27 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 4 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,12 @@ version = "0.1.0"
edition = "2021"
license-file = "LICENSE"

[features]
storage = []

[dependencies]
bevy = { version = "0.14.0", features = ["wav"] }
bevy-persistent = { version = "0.6.0", features = ["toml"] }
bevy-persistent = { version = "0.6.0", features = ["bincode"] }
bevy_prng = { version = "0.7.1", features = ["wyrand"] }
bevy_rand = { version = "0.7.1", features = ["wyrand"] }
bevy_ui = { version = "0.14.0" }
Expand Down
26 changes: 16 additions & 10 deletions src/gameplay/enemy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

use bevy::audio::PlaybackMode;
use bevy::prelude::*;
#[cfg(feature = "storage")]
use bevy_persistent::Persistent;
use bevy_prng::WyRand;
use bevy_rand::prelude::GlobalEntropy;
Expand All @@ -31,9 +32,9 @@ use crate::gameplay::projectile::*;
use crate::persistent::Score;
use crate::state::{AppState, ON_EXIT_GAMEPLAY};

const ENEMY_SPEED: f32 = 40.0;
const ENEMY_THRESHOLD: f32 = ENEMY_SPEED * 2.0;
const ENEMY_MAX_COUNT: usize = 10;
const ENEMY_SPEED: f32 = 70.0;
const ENEMY_THRESHOLD: f32 = 64.0;
const ENEMY_MAX_COUNT: usize = 64;

pub struct EnemyPlugin;

Expand Down Expand Up @@ -89,18 +90,22 @@ fn spawn_enemy(

let radius = rng.next_u32() as f32 / u32::MAX as f32 * 2.0 * std::f32::consts::PI;
let distance =
rng.next_u32() as f32 / u32::MAX as f32 * ENEMY_SPEED * 10.0 + ENEMY_SPEED * 17.5;
rng.next_u32() as f32 / u32::MAX as f32 * ENEMY_SPEED * 15.0 + ENEMY_SPEED * 15.0;

commands.spawn((
Enemy::default(),
Health::new(1.0),
SpriteBundle {
texture,
transform: Transform::from_scale(Vec3::splat(1.0)).with_translation(Vec3::new(
player_transform.translation.x + distance * radius.cos(),
player_transform.translation.y + distance * radius.sin(),
0.0,
)),
transform: Transform {
translation: Vec3::new(
player_transform.translation.x + distance * radius.cos(),
player_transform.translation.y + distance * radius.sin(),
0.0,
),
scale: Vec3::splat(1.0),
..default()
},
..default()
},
TextureAtlas {
Expand Down Expand Up @@ -155,7 +160,8 @@ fn projectile_hit_enemy(
mut enemy_q: Query<(Entity, &Transform, &mut Health), With<Enemy>>,
projectile_q: Query<(Entity, &Transform), With<Projectile>>,
asset_server: Res<AssetServer>,
mut score: ResMut<Persistent<Score>>,
#[cfg(feature = "storage")] mut score: ResMut<Persistent<Score>>,
#[cfg(not(feature = "storage"))] mut score: ResMut<Score>,
) {
for (projectile_entity, projectile_transform) in projectile_q.iter() {
for (enemy_entity, enemy_transform, mut enemy_health) in enemy_q.iter_mut() {
Expand Down
7 changes: 6 additions & 1 deletion src/gameplay/hud.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
*/

use bevy::prelude::*;
#[cfg(feature = "storage")]
use bevy_persistent::Persistent;

use crate::gameplay::health::Health;
Expand Down Expand Up @@ -112,7 +113,11 @@ fn update_health_bar(
}
}

fn update_score_text(mut query: Query<&mut Text, With<ScoreText>>, score: Res<Persistent<Score>>) {
fn update_score_text(
mut query: Query<&mut Text, With<ScoreText>>,
#[cfg(feature = "storage")] score: Res<Persistent<Score>>,
#[cfg(not(feature = "storage"))] score: Res<Score>,
) {
for mut text in query.iter_mut() {
text.sections[0].value = format!(
"Score: {}\nHigh Score: {}",
Expand Down
8 changes: 7 additions & 1 deletion src/mainmenu/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ extern crate bevy;

use bevy::prelude::*;

use crate::persistent::Score;
use crate::state::AppState;
use crate::style::*;

Expand All @@ -28,7 +29,11 @@ pub struct MainMenuPlugin;
#[derive(Component, Debug)]
struct MainMenu;

fn spawn_ui(mut commands: Commands) {
fn spawn_ui(
mut commands: Commands,
#[cfg(feature = "storage")] score: Res<bevy_persistent::Persistent<Score>>,
#[cfg(not(feature = "storage"))] score: Res<Score>,
) {
let container = NodeBundle {
style: Style {
width: Val::Percent(100.0),
Expand All @@ -49,6 +54,7 @@ fn spawn_ui(mut commands: Commands) {
.spawn((MainMenu, container))
.with_children(|parent| {
parent.spawn(text_title("Mageanoid"));
parent.spawn(text(format!("High Score: {}", score.high_score)));
parent.spawn(v_space(20.0));
parent.spawn(start_btn).with_children(|parent| {
parent.spawn(start_btn_text);
Expand Down
57 changes: 40 additions & 17 deletions src/persistent.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,9 @@
*/

use bevy::prelude::*;
use bevy_persistent::prelude::*;
use serde::{Deserialize, Serialize};

use crate::state::{AppState, ON_ENTER_GAMEPLAY};
use crate::state::*;

#[derive(Resource, Serialize, Deserialize, Default, Debug)]
pub struct Score {
Expand All @@ -40,34 +39,58 @@ impl Score {
}

fn setup(mut commands: Commands) {
let config_dir = dirs::config_dir().unwrap().join("mageanoid");
let resource = Persistent::<Score>::builder()
.name("score")
.format(StorageFormat::Toml)
.path(config_dir.join("score.toml"))
.default(Score::default())
.build()
.expect("failed to initialize score resource");
#[cfg(feature = "storage")]
{
if let Some(config_dir) = dirs::config_dir() {
let resource = bevy_persistent::Persistent::<Score>::builder()
.name("score")
.format(bevy_persistent::StorageFormat::Bincode)
.path(config_dir.join("mageanoid").join("score"))
.default(Score::default())
.revertible(true)
.revert_to_default_on_deserialization_errors(true)
.build()
.expect("failed to initialize score resource");

commands.insert_resource(resource);
commands.insert_resource(resource);
}
}
#[cfg(not(feature = "storage"))]
{
commands.insert_resource(Score::default());
}
}

info!("Score resource initialized");
#[cfg(feature = "storage")]
fn reset_score(mut score: ResMut<bevy_persistent::Persistent<Score>>) {
score.reset();
}

fn reset_score(mut score: ResMut<Persistent<Score>>) {
#[cfg(not(feature = "storage"))]
fn reset_score(mut score: ResMut<Score>) {
score.reset();
}

fn persist(score: Res<Persistent<Score>>) {
#[cfg(feature = "storage")]
fn persist(score: Res<bevy_persistent::Persistent<Score>>) {
score.persist().expect("failed to persist score");
}

pub struct PersistentPlugin;

impl Plugin for PersistentPlugin {
fn build(&self, app: &mut App) {
app.add_systems(Startup, setup)
.add_systems(ON_ENTER_GAMEPLAY, reset_score)
.add_systems(OnExit(AppState::InGame), persist);
#[cfg(feature = "storage")]
{
app.add_systems(PreStartup, setup)
.add_systems(ON_ENTER_GAMEPLAY, reset_score)
.add_systems(OnExit(AppState::InGame), persist);
}

#[cfg(not(feature = "storage"))]
{
app.add_systems(PreStartup, setup)
.add_systems(ON_ENTER_GAMEPLAY, reset_score);
}
}
}
13 changes: 12 additions & 1 deletion src/style.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,11 +54,22 @@ pub struct InteractiveButtonBundle {
pub id: ButtonId,
}

pub fn text(content: impl Into<String>) -> TextBundle {
TextBundle::from_section(
content,
TextStyle {
font_size: 32.0,
color: Color::WHITE,
..default()
},
)
}

pub fn text_title(content: impl Into<String>) -> TextBundle {
TextBundle::from_section(
content,
TextStyle {
font_size: 100.0,
font_size: 75.0,
color: Color::WHITE,
..default()
},
Expand Down

0 comments on commit ac38f83

Please sign in to comment.