refactor mods
This commit is contained in:
parent
6adc38c35c
commit
467f147142
17 changed files with 77 additions and 113 deletions
|
|
@ -1,67 +0,0 @@
|
|||
use bevy::prelude::*;
|
||||
// Source : https://bevy.org/examples/2d-rendering/sprite-sheet/
|
||||
|
||||
fn main() {
|
||||
App::new()
|
||||
.add_plugins(DefaultPlugins.set(ImagePlugin::default_nearest())) // prevents blurry sprites
|
||||
.add_systems(Startup, setup)
|
||||
.add_systems(Update, animate_sprite)
|
||||
.run();
|
||||
}
|
||||
|
||||
#[derive(Component, Default)]
|
||||
pub struct AnimationIndices {
|
||||
pub first: usize,
|
||||
pub last: usize,
|
||||
}
|
||||
|
||||
impl AnimationIndices {
|
||||
pub fn change(&mut self, first: usize, second: usize) {
|
||||
self.first = first;
|
||||
self.last = second;
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Component, Deref, DerefMut)]
|
||||
pub struct AnimationTimer(pub Timer);
|
||||
|
||||
pub fn animate_sprite(time: Res<Time>, mut query: Query<(&AnimationIndices, &mut AnimationTimer, &mut Sprite)>) {
|
||||
for (indices, mut timer, mut sprite) in &mut query {
|
||||
timer.tick(time.delta());
|
||||
|
||||
if timer.just_finished()
|
||||
&& let Some(atlas) = &mut sprite.texture_atlas
|
||||
{
|
||||
atlas.index = if atlas.index >= indices.last || atlas.index < indices.first {
|
||||
indices.first
|
||||
} else {
|
||||
atlas.index + 1
|
||||
};
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[allow(dead_code)]
|
||||
fn setup(
|
||||
mut commands: Commands,
|
||||
asset_server: Res<AssetServer>,
|
||||
mut texture_atlas_layouts: ResMut<Assets<TextureAtlasLayout>>,
|
||||
) {
|
||||
let texture = asset_server.load("textures/rpg/chars/gabe/gabe-idle-run.png");
|
||||
let layout = TextureAtlasLayout::from_grid(UVec2::splat(24), 7, 1, None, None);
|
||||
let texture_atlas_layout = texture_atlas_layouts.add(layout);
|
||||
// Use only the subset of sprites in the sheet that make up the run animation
|
||||
let animation_indices = AnimationIndices { first: 1, last: 6 };
|
||||
|
||||
commands.spawn(Camera2d);
|
||||
|
||||
commands.spawn((
|
||||
Sprite::from_atlas_image(
|
||||
texture,
|
||||
TextureAtlas { layout: texture_atlas_layout, index: animation_indices.first },
|
||||
),
|
||||
Transform::from_scale(Vec3::splat(6.0)),
|
||||
animation_indices,
|
||||
AnimationTimer(Timer::from_seconds(0.1, TimerMode::Repeating)),
|
||||
));
|
||||
}
|
||||
|
|
@ -4,11 +4,11 @@ use bevy::prelude::*;
|
|||
use rand::Rng;
|
||||
|
||||
use crate::{
|
||||
density_grid::DensityObject,
|
||||
kirby::Kirby,
|
||||
life::{DamageDealer, Life},
|
||||
physics_body::PhysicsBody,
|
||||
sphere_collider::SphereCollider,
|
||||
core::kirby::Kirby,
|
||||
core::life::{DamageDealer, Life},
|
||||
physics::density_grid::DensityObject,
|
||||
physics::physics_body::PhysicsBody,
|
||||
physics::sphere_collider::SphereCollider,
|
||||
};
|
||||
|
||||
#[derive(Component)]
|
||||
|
|
@ -7,10 +7,6 @@ pub struct Counter {
|
|||
}
|
||||
|
||||
impl Counter {
|
||||
pub fn new() -> Self {
|
||||
Counter { count: 0 }
|
||||
}
|
||||
|
||||
pub fn increase(&mut self, delta: u32) {
|
||||
self.count += delta;
|
||||
}
|
||||
|
|
@ -3,10 +3,10 @@ use bevy::prelude::*;
|
|||
const KIRBY_SCALE: f32 = 3.0;
|
||||
|
||||
use crate::{
|
||||
animation::{AnimationIndices, AnimationTimer},
|
||||
life::{DamageDealer, Life},
|
||||
physics_body::PhysicsBody,
|
||||
sphere_collider::SphereCollider,
|
||||
core::life::{DamageDealer, Life},
|
||||
juice::animation::{AnimationIndices, AnimationTimer},
|
||||
physics::physics_body::PhysicsBody,
|
||||
physics::sphere_collider::SphereCollider,
|
||||
};
|
||||
|
||||
#[derive(Component)]
|
||||
|
|
@ -1,10 +1,10 @@
|
|||
use bevy::prelude::*;
|
||||
|
||||
use crate::{
|
||||
bubble::Bubble,
|
||||
counter::{BubbleExplodedCountThisFrame, BubbleSuckedCountThisFrame, KirbyHitCountThisFrame},
|
||||
kirby::{Kirby, SuckArea},
|
||||
sphere_collider::SphereCollider,
|
||||
core::bubble::Bubble,
|
||||
core::counter::{BubbleExplodedCountThisFrame, BubbleSuckedCountThisFrame, KirbyHitCountThisFrame},
|
||||
core::kirby::{Kirby, SuckArea},
|
||||
physics::sphere_collider::SphereCollider,
|
||||
};
|
||||
|
||||
#[derive(Component)]
|
||||
4
src/core/mod.rs
Normal file
4
src/core/mod.rs
Normal file
|
|
@ -0,0 +1,4 @@
|
|||
pub mod bubble;
|
||||
pub mod counter;
|
||||
pub mod kirby;
|
||||
pub mod life;
|
||||
|
|
@ -1 +0,0 @@
|
|||
pub static MAX_MAP_WIDTH: usize = 1024;
|
||||
34
src/juice/animation.rs
Normal file
34
src/juice/animation.rs
Normal file
|
|
@ -0,0 +1,34 @@
|
|||
use bevy::prelude::*;
|
||||
// Source : https://bevy.org/examples/2d-rendering/sprite-sheet/
|
||||
|
||||
#[derive(Component, Default)]
|
||||
pub struct AnimationIndices {
|
||||
pub first: usize,
|
||||
pub last: usize,
|
||||
}
|
||||
|
||||
impl AnimationIndices {
|
||||
pub fn change(&mut self, first: usize, second: usize) {
|
||||
self.first = first;
|
||||
self.last = second;
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Component, Deref, DerefMut)]
|
||||
pub struct AnimationTimer(pub Timer);
|
||||
|
||||
pub fn animate_sprite(time: Res<Time>, mut query: Query<(&AnimationIndices, &mut AnimationTimer, &mut Sprite)>) {
|
||||
for (indices, mut timer, mut sprite) in &mut query {
|
||||
timer.tick(time.delta());
|
||||
|
||||
if timer.just_finished()
|
||||
&& let Some(atlas) = &mut sprite.texture_atlas
|
||||
{
|
||||
atlas.index = if atlas.index >= indices.last || atlas.index < indices.first {
|
||||
indices.first
|
||||
} else {
|
||||
atlas.index + 1
|
||||
};
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -1,8 +1,8 @@
|
|||
use std::f32::consts::TAU;
|
||||
|
||||
use bevy::{prelude::*, render::renderer};
|
||||
use bevy::prelude::*;
|
||||
|
||||
use crate::counter::KirbyHitCountThisFrame;
|
||||
use crate::core::counter::KirbyHitCountThisFrame;
|
||||
|
||||
#[derive(Component)]
|
||||
pub struct MyCamera {
|
||||
2
src/juice/mod.rs
Normal file
2
src/juice/mod.rs
Normal file
|
|
@ -0,0 +1,2 @@
|
|||
pub mod animation;
|
||||
pub mod camera;
|
||||
37
src/main.rs
37
src/main.rs
|
|
@ -1,31 +1,22 @@
|
|||
use animation::animate_sprite;
|
||||
use bevy::prelude::*;
|
||||
use bevy::window::WindowResolution;
|
||||
use bubble::{bubble_move, bubble_spawn};
|
||||
use camera::spawn_camera;
|
||||
use density_grid::{DensityGrid, density_grid_force, density_grid_update, init_density_object};
|
||||
use life::despawn_if_dead;
|
||||
use life::{bubble_receive_damage, kirby_receive_damage};
|
||||
use map::MapBounds;
|
||||
use physics_body::physics_integrate;
|
||||
|
||||
use crate::bubble::BubblePlugin;
|
||||
use crate::camera::MyCameraPlugin;
|
||||
use crate::counter::CounterPlugin;
|
||||
use crate::density_grid::DensityGridPlugin;
|
||||
use crate::kirby::KirbyPlugin;
|
||||
mod physics;
|
||||
use physics::density_grid::DensityGridPlugin;
|
||||
use physics::physics_body::physics_integrate;
|
||||
|
||||
mod core;
|
||||
use core::bubble::BubblePlugin;
|
||||
use core::counter::CounterPlugin;
|
||||
use core::kirby::KirbyPlugin;
|
||||
use core::life::{bubble_receive_damage, despawn_if_dead, kirby_receive_damage};
|
||||
|
||||
mod animation;
|
||||
mod bubble;
|
||||
mod camera;
|
||||
mod counter;
|
||||
mod density_grid;
|
||||
mod globals;
|
||||
mod kirby;
|
||||
mod life;
|
||||
mod map;
|
||||
mod physics_body;
|
||||
mod sphere_collider;
|
||||
use map::map::MapBounds;
|
||||
|
||||
mod juice;
|
||||
use juice::animation::animate_sprite;
|
||||
use juice::camera::MyCameraPlugin;
|
||||
|
||||
const MAP_SIZE: u32 = 1000;
|
||||
|
||||
|
|
|
|||
1
src/map/mod.rs
Normal file
1
src/map/mod.rs
Normal file
|
|
@ -0,0 +1 @@
|
|||
pub mod map;
|
||||
|
|
@ -3,7 +3,8 @@ use std::f32::consts::TAU;
|
|||
use bevy::prelude::*;
|
||||
use rand::Rng;
|
||||
|
||||
use crate::{globals::MAX_MAP_WIDTH, physics_body::PhysicsBody};
|
||||
pub static MAX_MAP_WIDTH: usize = 1024;
|
||||
use crate::physics::physics_body::PhysicsBody;
|
||||
|
||||
const CELL_SIZE: usize = 8;
|
||||
const MAX_MAP_SIZE: usize = MAX_MAP_WIDTH * 2;
|
||||
3
src/physics/mod.rs
Normal file
3
src/physics/mod.rs
Normal file
|
|
@ -0,0 +1,3 @@
|
|||
pub mod density_grid;
|
||||
pub mod physics_body;
|
||||
pub mod sphere_collider;
|
||||
|
|
@ -1,6 +1,6 @@
|
|||
use bevy::prelude::*;
|
||||
|
||||
use crate::map::MapBounds;
|
||||
use crate::map::map::MapBounds;
|
||||
|
||||
#[derive(Component)]
|
||||
#[require(Transform)]
|
||||
|
|
@ -1,4 +1,4 @@
|
|||
use bevy::{prelude::*, render::renderer};
|
||||
use bevy::prelude::*;
|
||||
|
||||
#[derive(Component, Default)]
|
||||
#[require(Transform)]
|
||||
Loading…
Add table
Add a link
Reference in a new issue