I’m working on a clone of Flappy Bird.
I’m using Macroquad.rs and Bevy ECS.
However, I’ve been using a lot of cloning operations when handling textures for the bird and barriers.
I tried simply adding lifetime annotations to the components, but the compiler complains that it requires a ‘static lifetime.
What should I do?
Here’s some relevant code. The entire project can be found here.
components.rs
/// Rend a sprite centered at the position
#[derive(Component, Debug)]
pub struct TextureRender {
pub texture: Texture2D,
pub params: DrawTextureParams,
}
impl TextureRender {
pub fn new(texture: Texture2D, params: DrawTextureParams) -> Self {
Self {
texture,
params
}
}
}
systems.rs
pub fn draw_texture_in_position(query: Query<(&TextureRender, &Position)>) {
for (texture, position) in &query {
draw_texture_ex(
&texture.texture,
position.x - texture.params.dest_size.unwrap_or(texture.texture.size()).x / 2.,
position.y - texture.params.dest_size.unwrap_or(texture.texture.size()).y / 2.,
WHITE,
texture.params.clone(),
)
}
}
main.rs
let bird_img = load_texture("bird.png").await.unwrap();
let barrier = load_texture("barrier.png").await.unwrap();
/// Some code ...
(bird, latest) = restart(&mut world, bird_img.clone(), &barrier, size, params.clone());
fn restart(mut world: &mut World, bird: Texture2D, barrier: &Texture2D, size: Vec2, params: DrawTextureParams) -> (Entity, Entity) {
let bird = world
.spawn((
Position::new(100., 100.),
TextureRender::new(bird, params),
RectangleCollider::new(size.x, size.y),
Flapable::new(BIRD_FLAP, KeyCode::Space),
DieWhenOutOfScreen,
Velocity::new(0., 0.),
Gravity,
))
.id();
// let mut lastx: f32 = 1000.;
let (_, latest) = spawn_barrier_randomly(1000., &mut world, &barrier);
(bird, latest)
}
fn spawn_barrier(
x: f32,
gap_pos: f32,
gap_radius: f32,
world: &mut World,
barrier: &Texture2D,
) -> (Entity, Entity) {
let size = barrier.size();
(
world
.spawn((
Position::new(x, gap_pos - gap_radius - size.y / 2.),
TextureRender::new(
barrier.clone(),
DrawTextureParams {
rotation: PI,
..Default::default()
},
),
RectangleCollider::new(size.x, size.y),
Velocity::new(-BARRIER_LEFT_SPEED, 0.),
))
.id(),
world
.spawn((
Position::new(x, gap_pos + gap_radius + size.y / 2.),
TextureRender::new(barrier.clone(), Default::default()),
RectangleCollider::new(size.x, size.y),
Velocity::new(-BARRIER_LEFT_SPEED, 0.),
))
.id(),
)
}
I have tired to simply add lifetime annotations to my component, but the compiler complains that he wants ‘static.
How can I do to avoid these clone?
ozqs is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.