From 19fc75b0e1c2961723fe7dca7e55539ba9b21dcd Mon Sep 17 00:00:00 2001 From: Sergei Surovtsev Date: Mon, 1 Jan 2024 14:12:19 +0300 Subject: [PATCH] refactoring --- src/components.rs | 106 ------------ src/controls.rs | 5 +- src/enemies.rs | 411 ---------------------------------------------- src/events.rs | 116 +------------ src/gameplay.rs | 4 +- src/items.rs | 264 ----------------------------- src/ldtk.rs | 98 +++++------ src/main.rs | 39 +---- src/sprites.rs | 52 +++++- 9 files changed, 108 insertions(+), 987 deletions(-) delete mode 100644 src/enemies.rs delete mode 100644 src/items.rs diff --git a/src/components.rs b/src/components.rs index a682b58..5a52a1b 100644 --- a/src/components.rs +++ b/src/components.rs @@ -2,12 +2,6 @@ use bevy::prelude::*; use bevy_ecs_ldtk::prelude::*; use bevy_rapier2d::prelude::*; -use crate::{ - enemies::{create_mierda_bundle, Mierda}, - items::{create_pizza_bundle, Pizza}, - sprites::*, -}; - #[derive(Clone, Debug, Default, Bundle, LdtkIntCell)] pub struct ColliderBundle { pub collider: Collider, @@ -37,103 +31,3 @@ impl From<&EntityInstance> for ColliderBundle { } } } - -#[derive(Copy, Clone, Eq, PartialEq, Debug, Default, Component, Reflect)] -pub struct Player { - pub health: u8, -} - -#[derive(Default, Bundle)] -pub struct PlayerBundle { - pub sprite_bundle: SpriteSheetBundle, - pub character_animation: CharacterAnimation, - pub animation_timer: AnimationTimer, - pub player: Player, - pub collider_bundle: ColliderBundle, - pub active_events: ActiveEvents, - // pub particle_system_bundle: ParticleSystemBundle, -} - -#[derive(Copy, Clone, Eq, PartialEq, Debug, Default, Component)] -pub struct Wall; - -#[derive(Clone, Debug, Default, Bundle, LdtkIntCell)] -pub struct WallBundle { - wall: Wall, - sensor: Sensor, -} - -impl FromWorld for PlayerSpritesheets { - fn from_world(world: &mut World) -> Self { - let world = world.cell(); - - let asset_server_world_borrow = world.get_resource::(); - let asset_server = asset_server_world_borrow.as_deref().unwrap(); - - let mut texture_atlasses_world_borrow = world.get_resource_mut::>(); - let texture_atlasses = texture_atlasses_world_borrow.as_deref_mut().unwrap(); - - let player_atlas_1 = load_texture_atlas( - PLAYER_ASSET_SHEET_1, - asset_server, - SHEET_1_COLUMNS, - SHEET_1_ROWS, - None, - 64., - texture_atlasses, - ); - - let player_atlas_2 = load_texture_atlas( - PLAYER_ASSET_SHEET_2, - asset_server, - SHEET_2_COLUMNS, - SHEET_2_ROWS, - None, - 64. * 3., - texture_atlasses, - ); - - let mierda_atlas = load_texture_atlas( - MIERDA_ASSET_SHEET, - asset_server, - 5, - 1, - None, - 16.0, - texture_atlasses, - ); - - PlayerSpritesheets { - player_atlas_1, - player_atlas_2, - mierda_atlas, - } - } -} - -pub(crate) fn fix_missing_ldtk_entities( - asset_server: Res, - texture_atlasses: ResMut>, - mut commands: Commands, - los_mierdas: Query<(Entity, &Mierda), Without>, - los_pizzas: Query<(Entity, &Pizza), Without>, -) { - let asset_server = asset_server.into_inner(); - let texture_atlasses = texture_atlasses.into_inner(); - - for (e, _) in los_mierdas.iter().filter(|(_, m)| !m.is_dummy) { - let bundle = create_mierda_bundle(asset_server, texture_atlasses, false); - commands.entity(e).insert(( - bundle.collider_bundle, - bundle.direction_update_time, - Visibility::Visible, - )); - } - - for (e, _) in los_pizzas.iter().filter(|(_, m)| !m.is_dummy) { - let bundle = create_pizza_bundle(asset_server, texture_atlasses, false); - commands - .entity(e) - .insert((bundle.collider_bundle, Visibility::Visible)); - } -} diff --git a/src/controls.rs b/src/controls.rs index c586c98..8a15b9b 100644 --- a/src/controls.rs +++ b/src/controls.rs @@ -2,7 +2,10 @@ use bevy::prelude::*; use bevy_rapier2d::prelude::*; use pecs::prelude::*; -use crate::{components::*, events::*, sprites::*}; +use crate::{ + entities::player::{Player, PlayerAttackEvent}, + sprites::*, +}; pub fn controls( mut commands: Commands, diff --git a/src/enemies.rs b/src/enemies.rs deleted file mode 100644 index 53825d9..0000000 --- a/src/enemies.rs +++ /dev/null @@ -1,411 +0,0 @@ -use std::time::Duration; - -use bevy::prelude::*; -use bevy_ecs_ldtk::prelude::*; -use bevy_particle_systems::*; -use bevy_rapier2d::prelude::Velocity; -use bevy_rapier2d::prelude::*; -use pecs::prelude::*; -use rand::Rng; - -use crate::{ - components::{ColliderBundle, Player}, - events::PlayerHitEvent, - sprites::*, - utils::*, -}; - -// ----------- -// Compontents -// ----------- - -#[derive(Component, Clone, Default, Reflect)] -pub struct DirectionUpdateTime { - /// track when the bomb should explode (non-repeating timer) - pub timer: Timer, -} - -#[derive(Clone, PartialEq, Debug, Default, Component, Reflect)] -pub struct Mierda { - pub move_direction: Vec2, - pub health: u8, - pub hit_at: Option, - pub is_dummy: bool, -} - -#[derive(Clone, Default, Bundle)] -pub struct MierdaBundle { - pub sprite_bundle: SpriteSheetBundle, - pub mierda: Mierda, - pub collider_bundle: ColliderBundle, - pub direction_update_time: DirectionUpdateTime, -} - -// ---- -// LDTK -// ---- - -impl LdtkEntity for MierdaBundle { - fn bundle_entity( - entity_instance: &EntityInstance, - _layer_instance: &LayerInstance, - _: Option<&Handle>, - _: Option<&TilesetDefinition>, - asset_server: &AssetServer, - texture_atlasses: &mut Assets, - ) -> MierdaBundle { - let is_dummy = *entity_instance - .get_bool_field("is_dummy") - .expect("expected entity to have non-nullable name string field"); - create_mierda_bundle(asset_server, texture_atlasses, is_dummy) - } -} - -pub fn create_mierda_bundle( - asset_server: &AssetServer, - texture_atlasses: &mut Assets, - is_dummy: bool, -) -> MierdaBundle { - let rotation_constraints = LockedAxes::ROTATION_LOCKED; - - let collider_bundle = ColliderBundle { - collider: Collider::cuboid(8., 8.), - rigid_body: RigidBody::Dynamic, - friction: Friction { - coefficient: 20.0, - combine_rule: CoefficientCombineRule::Min, - }, - rotation_constraints, - ..Default::default() - }; - - let atlas_handle = load_texture_atlas( - MIERDA_ASSET_SHEET, - asset_server, - 5, - 1, - None, - 16., - texture_atlasses, - ); - - let sprite_bundle = SpriteSheetBundle { - texture_atlas: atlas_handle, - sprite: TextureAtlasSprite::new(4), - ..default() - }; - - let mierda = Mierda { - health: 100, - move_direction: Vec2 { - x: rand::random::() * 2.0 - 1.0, - y: rand::random::() * 2.0 - 1.0, - } - .normalize(), - hit_at: None, - is_dummy, - }; - - MierdaBundle { - sprite_bundle, - collider_bundle, - mierda, - direction_update_time: DirectionUpdateTime { - timer: Timer::new(Duration::from_secs(5), TimerMode::Once), - }, - } -} - -// --------- -// Mierda AI -// --------- - -pub fn mierda_activity(time: Res