Skip to content

Commit

Permalink
Bee sprite and wing animations
Browse files Browse the repository at this point in the history
  • Loading branch information
CarsonWebster committed Dec 9, 2023
1 parent 07e3cdd commit 6f95a8f
Show file tree
Hide file tree
Showing 7 changed files with 114 additions and 23 deletions.
34 changes: 34 additions & 0 deletions Cargo.lock

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

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ bevy_ecs_tilemap = { git = "https://github.com/divark/bevy_ecs_tilemap", branch
bevy_ecs_ldtk = { git = "https://github.com/Trouv/bevy_ecs_ldtk.git", branch = "feat/bevy-0.12" }
bevy-inspector-egui = "0.21.0"
quadtree = { git = "https://github.com/MickHarrigan/quadtree.git" }
time = "0.3.30"


[build-dependencies]
Expand Down
53 changes: 48 additions & 5 deletions src/bees.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ impl Plugin for BeesPlugin {
.add_systems(OnEnter(GameState::Playing), setup)
.add_systems(Update, create_boid_group.run_if(in_state(GameState::Playing)))
.add_systems(Update, place_bee.run_if(in_state(GameState::Playing)))
.add_systems(Update, animate_wings.run_if(in_state(GameState::Playing)))
.add_systems(
Update,
(build_or_update_quadtree, update_boids, move_system).run_if(
Expand Down Expand Up @@ -154,6 +155,33 @@ fn setup(mut _commands: Commands) {
// });
}

#[derive(Component)]
struct AnimationIndices {
first: usize,
last: usize,
}
#[derive(Component, Deref, DerefMut)]
struct AnimationTimer(Timer);

fn animate_wings(
time: Res<Time>,
mut query: Query<(
&AnimationIndices,
&mut AnimationTimer,
&mut TextureAtlasSprite,
)>,
) {
for (indices, mut timer, mut sprite) in &mut query {
timer.tick(time.delta());
if timer.just_finished() {
sprite.index = if sprite.index == indices.last {
indices.first
} else {
sprite.index + 1
};
}
}
}

fn place_bee(
mut commands: Commands,
Expand All @@ -162,10 +190,9 @@ fn place_bee(
mouse_input: Res<Input<MouseButton>>,
) {
if mouse_input.just_pressed(MouseButton::Right) {
commands.spawn((
SpriteSheetBundle {
texture_atlas: textures.planes.clone(),
sprite: TextureAtlasSprite::new(11),
let bee_entity = commands.spawn((
SpriteBundle {
texture: textures.beebody1.clone(),
transform: Transform::from_xyz(
mouse_position.0.x,
mouse_position.0.y,
Expand All @@ -179,7 +206,23 @@ fn place_bee(
Highlightable,
Collider::new(5.0),
Velocity::default(),
));
)).id();

// Create the wings
let texture_atlas_handle = textures.bee1wingmap.clone();

// Spawn the wings as a child of the bee body
commands.entity(bee_entity).with_children(|parent| {
parent.spawn(SpriteSheetBundle {
texture_atlas: textures.bee1wingmap.clone(),
sprite: TextureAtlasSprite::new(0), // Set the initial sprite index
transform: Transform::from_xyz(0.0, 0.0, 1.0), // Adjust the position of the wings relative to the bee body
..Default::default()
})
.insert(AnimationIndices { first: 0, last: 3 - 1 })
.insert(AnimationTimer(Timer::from_seconds(0.3, TimerMode::Repeating)));
});

info!("Bee spawned");
}
}
28 changes: 14 additions & 14 deletions src/boids.rs
Original file line number Diff line number Diff line change
@@ -1,34 +1,34 @@
use crate::{
bees::{Body, Team},
world::LdtkLevel,
// world::LdtkLevel,
};
use bevy::prelude::*;
use bevy_ecs_ldtk::prelude::*;
// use bevy_ecs_ldtk::prelude::*;

use crate::bees::{BoidGroup, Collider, Velocity};
use crate::tilemap::LevelData;

#[derive(Component)]
pub struct Boid;

pub fn create_boid_group(
mut comms: Commands,
level: Res<Assets<LdtkProject>>,
handle: Res<LdtkLevel>,
// level: Res<Assets<LdtkProject>>,
// handle: Res<LdtkLevel>,
level_data: Res<LevelData>,
mut loaded: Local<bool>,
) {
if *loaded {
return;
}
if let Some(data) = level.get(&handle.0) {
let height = data.iter_root_levels().next().unwrap().px_hei;
let width = data.iter_root_levels().next().unwrap().px_wid;
comms.spawn(BoidGroup::new(
Vec2::new(0., 0.),
Vec2::new(width as f32, height as f32),
Team(0),
));
*loaded = true;
}
let height = level_data.level_height;
let width = level_data.level_width;
comms.spawn(BoidGroup::new(
Vec2::new(0., 0.),
Vec2::new(width, height),
Team(0),
));
*loaded = true;
}

pub fn update_boids(
Expand Down
4 changes: 2 additions & 2 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -72,8 +72,8 @@ impl Plugin for GamePlugin {
CameraPlugin,
MapPlugin,
// WorldPlugin,
// InteractionsPlugin,
// BeesPlugin,
InteractionsPlugin,
BeesPlugin,
));

#[cfg(debug_assertions)]
Expand Down
15 changes: 14 additions & 1 deletion src/loading.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ use crate::GameState;
use bevy::prelude::*;
use bevy_asset_loader::prelude::*;
use bevy_kira_audio::AudioSource;
use bevy_asset_loader::asset_collection::AssetCollection;

pub struct LoadingPlugin;

Expand Down Expand Up @@ -31,8 +32,10 @@ pub struct AudioAssets {
pub struct TextureAssets {
#[asset(path = "textures/bevy.png")]
pub bevy: Handle<Image>,

#[asset(path = "textures/github.png")]
pub github: Handle<Image>,

#[asset(texture_atlas(
tile_size_x = 16.,
tile_size_y = 16.,
Expand All @@ -45,6 +48,7 @@ pub struct TextureAssets {
))]
#[asset(path = "textures/shmup.png")]
pub shmup: Handle<TextureAtlas>,

#[asset(texture_atlas(
tile_size_x = 32.,
tile_size_y = 32.,
Expand All @@ -57,6 +61,15 @@ pub struct TextureAssets {
))]
#[asset(path = "textures/airplanes.png")]
pub planes: Handle<TextureAtlas>,

// #[asset(texture_atlas(tile_size_x = 720., tile_size_y = 720., columns = 4, rows = 1, padding_x = 0., padding_y = 0., offset_x = 0., offset_y = 0.))]
#[asset(path = "textures/ground/ground.png")]
pub ground: Handle<Image>
pub ground: Handle<Image>,

#[asset(texture_atlas(tile_size_x = 720., tile_size_y = 720., columns = 3, rows = 1, padding_x = 0., padding_y = 0., offset_x = 0., offset_y = 0.))]
#[asset(path = "textures/bee1/wingmap.png")]
pub bee1wingmap: Handle<TextureAtlas>,

#[asset(path = "textures/bee1/beebody.png")]
pub beebody1: Handle<Image>,
}
2 changes: 1 addition & 1 deletion src/tilemap.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use bevy::prelude::*;
use bevy_ecs_tilemap::prelude::*;
use crate::loading::TextureAssets;

use rand::Rng;
use rand::Rng;

pub struct MapPlugin;
impl Plugin for MapPlugin {
Expand Down

0 comments on commit 6f95a8f

Please sign in to comment.