Skip to content

Commit

Permalink
fix Game Assets load error
Browse files Browse the repository at this point in the history
todo:choose character page
修复了因为资源缺少导致的游戏资源加载错误,下一步要整合角色选择页面。
  • Loading branch information
cloudhu committed Nov 28, 2024
1 parent 350c9ec commit 23876ff
Show file tree
Hide file tree
Showing 8 changed files with 166 additions and 74 deletions.
15 changes: 15 additions & 0 deletions Cargo.lock

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

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ version = "0.1.3"
edition = "2021"

[dependencies]
bevy_asset_loader = "0.21.0"
bevy_asset_loader = { version = "0.21.0", features = [ "2d","standard_dynamic_assets"] }
bevy_kira_audio = { version = "0.20.0", features = ["ogg"] }
rand = "0.9.0-alpha.2"
bevy-parallax = "0.10.0"
Expand Down
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ todo……
______
# 2. 开发记录
## 2.1 已实现列表
- [x] 使用`cargo generate thebevyflock/bevy_new_2d`生成的2d游戏模板作为[游戏基础开发框架](https://github.com/TheBevyFlock/bevy_new_2d)
- [x] 使用`cargo generate thebevyflock/bevy_new_2d`生成的2d游戏模板作为[2D游戏基础开发框架bevy_new_2d](https://github.com/TheBevyFlock/bevy_new_2d)
- [x][ASCII Space Shooter](https://github.com/JamesHDuffield/ascii-rust)开源项目Copy核心逻辑代码,并优化结构;
- [x] 暂停游戏 Pause/Resume
- [x] WASM支持 support wasm
Expand Down Expand Up @@ -67,7 +67,7 @@ ______
## 3.1 游戏引擎
Game Engine:[Bevy](https://bevyengine.org/)
## 3.2 游戏模板
- 使用`cargo generate thebevyflock/bevy_new_2d`生成的2d游戏模板
- 使用`cargo generate thebevyflock/bevy_new_2d`生成的[bevy_new_2d 游戏模板](https://github.com/TheBevyFlock/bevy_new_2d)
-[ASCII Space Shooter](https://github.com/JamesHDuffield/ascii-rust)开源项目Copy核心逻辑代码

## 3.3 编译优化
Expand Down
Binary file added assets/texture/juggernaut_character.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added assets/texture/juggernaut_character_outline.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
108 changes: 107 additions & 1 deletion src/assets/game_assets.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ use bevy::asset::Handle;
use bevy::prelude::*;
use bevy_asset_loader::prelude::*;
use bevy_kira_audio::AudioSource;
use bevy_asset_loader::standard_dynamic_asset::StandardDynamicAssetCollection;
use bevy_rapier2d::plugin::{RapierConfiguration, TimestepMode};
use crate::assets::player_assets::PlayerAssets;

#[derive(Resource, AssetCollection)]
pub struct Fonts {
Expand Down Expand Up @@ -72,6 +75,109 @@ pub(super) fn plugin(app: &mut App) {
.continue_to_state(AppStates::MainMenu)
.load_collection::<Fonts>()
.load_collection::<AudioAssets>()
.load_collection::<Music>(),
.load_collection::<Music>()
.with_dynamic_assets_file::<StandardDynamicAssetCollection>(
"player_assets.assets.ron",
)
.with_dynamic_assets_file::<StandardDynamicAssetCollection>(
"projectile_assets.assets.ron",
)
.with_dynamic_assets_file::<StandardDynamicAssetCollection>("mob_assets.assets.ron")
.with_dynamic_assets_file::<StandardDynamicAssetCollection>(
"consumable_assets.assets.ron",
)
.with_dynamic_assets_file::<StandardDynamicAssetCollection>(
"item_assets.assets.ron",
)
.with_dynamic_assets_file::<StandardDynamicAssetCollection>(
"effect_assets.assets.ron",
)
.with_dynamic_assets_file::<StandardDynamicAssetCollection>(
"game_audio_assets.assets.ron",
)
.with_dynamic_assets_file::<StandardDynamicAssetCollection>("ui_assets.assets.ron")
.load_collection::<PlayerAssets>()
// .load_collection::<ProjectileAssets>()//TODO:Assets need to be loaded
// .load_collection::<MobAssets>()
// .load_collection::<ItemAssets>()
// .load_collection::<ConsumableAssets>()
// .load_collection::<EffectAssets>()
// .load_collection::<GameAudioAssets>()
// .load_collection::<UiAssets>(),
);

app.edit_schedule(OnEnter(AppStates::InGame), |schedule| {
schedule.configure_sets(
(
GameEnterSet::Initialize,
GameEnterSet::BuildLevel,
GameEnterSet::SpawnPlayer,
GameEnterSet::BuildUi,
)
.chain(),
);
});

app.configure_sets(
Update,
(
//GameUpdateSet::Enter,
GameUpdateSet::Level,
GameUpdateSet::Spawn,
GameUpdateSet::NextLevel,
GameUpdateSet::UpdateUi,
GameUpdateSet::SetTargetBehavior,
GameUpdateSet::ContactCollision,
GameUpdateSet::IntersectionCollision,
GameUpdateSet::ExecuteBehavior,
GameUpdateSet::ApplyDisconnectedBehaviors,
GameUpdateSet::Movement,
GameUpdateSet::Abilities,
GameUpdateSet::ChangeState,
GameUpdateSet::Cleanup,
)
.chain(),
);

app.add_systems(
OnEnter(AppStates::InGame),
setup_physics.in_set(GameEnterSet::Initialize),
);
}

#[derive(Debug, Hash, PartialEq, Eq, Clone, SystemSet)]
pub enum GameEnterSet {
Initialize,
BuildLevel,
SpawnPlayer,
BuildUi,
}

#[derive(Debug, Hash, PartialEq, Eq, Clone, SystemSet)]
pub enum GameUpdateSet {
Enter,
Level,
Spawn,
NextLevel,
UpdateUi,
Movement,
Abilities,
SetTargetBehavior, // TODO: replace with more general set
ExecuteBehavior,
ContactCollision,
IntersectionCollision,
ApplyDisconnectedBehaviors,
ChangeState,
Cleanup,
}

// setup rapier
fn setup_physics(mut rapier_config: ResMut<RapierConfiguration>) {
rapier_config.timestep_mode = TimestepMode::Fixed {
dt: 1.0 / 60.0,
substeps: 1,
};
rapier_config.physics_pipeline_active = true;
rapier_config.query_pipeline_active = true;
rapier_config.gravity = Vec2::ZERO;
}
69 changes: 40 additions & 29 deletions src/gameplay/player.rs
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,7 @@ fn spawn_player(
) {
// check if more than one player is playing
let is_multiplayer = players_resource.player_data.get(1).is_some();
println!("spawning player is_multiplayer:{},player_data:{}",is_multiplayer,players_resource.player_data.get(0).is_some());
for (player_id, maybe_player_data) in players_resource
.player_data
.iter()
Expand All @@ -207,7 +208,7 @@ fn spawn_player(
character.collider_dimensions.x * game_parameters.sprite_scale / 2.0;
let collider_size_hy =
character.collider_dimensions.y * game_parameters.sprite_scale / 2.0;

println!("spawning Player");
// create player component from character
let player_bundle = PlayerBundle::from(character).with_id(player_id);

Expand All @@ -216,35 +217,44 @@ fn spawn_player(
player_entity
.insert(SpriteBundle {
texture: player_assets.get_asset(&character.character_type),
transform: Transform::from_translation(Vec3 {
x: 100.0,
y: 100.0,
z: RenderLayer::Player.as_z(),
}),
..Default::default()
})
.insert(RigidBody::Dynamic)
.insert(LockedAxes::ROTATION_LOCKED_Z)
.insert(Transform {
translation: if is_multiplayer {
Vec3::new(
if matches!(player_id, PlayerIDComponent::One) {
-game_parameters.player_spawn_distance
} else {
game_parameters.player_spawn_distance
},
0.0,
if matches!(player_id, PlayerIDComponent::One) {
RenderLayer::Player.as_z()
} else {
RenderLayer::Player.as_z() + 0.2
},
)
} else {
Vec3::ZERO
},
scale: Vec3::new(
game_parameters.sprite_scale,
game_parameters.sprite_scale,
1.0,
),
..Default::default()
})
// .insert(Transform {
// translation: if is_multiplayer {
// Vec3::new(
// if matches!(player_id, PlayerIDComponent::One) {
// -game_parameters.player_spawn_distance-100.0
// } else {
// game_parameters.player_spawn_distance+100.0
// },
// 100.0,
// if matches!(player_id, PlayerIDComponent::One) {
// RenderLayer::Player.as_z()
// } else {
// RenderLayer::Player.as_z() + 0.2
// },
// )
// } else {
// Vec3 {
// x: 100.0,
// y: 100.0,
// z: RenderLayer::Player.as_z(),
// }
// },
// scale: Vec3::new(
// game_parameters.sprite_scale*8.0,
// game_parameters.sprite_scale*8.0,
// 1.0,
// ),
// ..Default::default()
// })
.insert(InputManagerBundle::<PlayerAction> {
action_state: ActionState::default(),
input_map: match player_data.input {
Expand Down Expand Up @@ -272,7 +282,8 @@ fn spawn_player(
parent.spawn_slot_1_ability(&abilities_res, &character.slot_1_ability);
parent.spawn_slot_2_ability(&abilities_res, &character.slot_2_ability);
})
.insert(ShipBundle {
.insert(
ShipBundle {
physics: Physics::new(config.drag),
engine: Engine::new_with_steering(
config.power,
Expand Down Expand Up @@ -310,8 +321,8 @@ fn spawn_player(
..Default::default()
})
.insert(Transform::from_translation(Vec3::new(
0.0,
0.0,
100.0,
100.0,
RenderLayer::Player.as_z() + 0.1,
)));
});
Expand Down
42 changes: 1 addition & 41 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -97,10 +97,7 @@ impl Plugin for AppPlugin {
);

// Spawn the main camera.
app.add_systems(Startup, spawn_camera).add_systems(
OnEnter(AppStates::InGame),
setup_physics.in_set(GameEnterSet::Initialize),
);
app.add_systems(Startup, spawn_camera);

// Add other plugins.
app.add_plugins((
Expand Down Expand Up @@ -136,32 +133,6 @@ enum AppSet {
Update,
}

#[derive(Debug, Hash, PartialEq, Eq, Clone, SystemSet)]
pub enum GameEnterSet {
Initialize,
BuildLevel,
SpawnPlayer,
BuildUi,
}

#[derive(Debug, Hash, PartialEq, Eq, Clone, SystemSet)]
pub enum GameUpdateSet {
Enter,
Level,
Spawn,
NextLevel,
UpdateUi,
Movement,
Abilities,
SetTargetBehavior, // TODO: replace with more general set
ExecuteBehavior,
ContactCollision,
IntersectionCollision,
ApplyDisconnectedBehaviors,
ChangeState,
Cleanup,
}

#[cfg(not(target_arch = "wasm32"))]
fn get_display_config() -> DisplayConfig {
use ron::de::from_str;
Expand Down Expand Up @@ -258,14 +229,3 @@ fn spawn_camera(mut commands: Commands, mut create_parallax: EventWriter<CreateP
..default()
},));
}

// setup rapier
fn setup_physics(mut rapier_config: ResMut<RapierConfiguration>) {
rapier_config.timestep_mode = TimestepMode::Fixed {
dt: 1.0 / 60.0,
substeps: 1,
};
rapier_config.physics_pipeline_active = true;
rapier_config.query_pipeline_active = true;
rapier_config.gravity = Vec2::ZERO;
}

0 comments on commit 23876ff

Please sign in to comment.