#Bevy Entity Creation

17 messages · Page 1 of 1 (latest)

bronze wedge
#

Hello, I want to learn how the ECS data driven works, and I have done this simple code:

use bevy::prelude::*;

#[derive(Component)]
pub struct Position {
    pub x: f32,
    pub y: f32,
}

#[derive(Component)]
pub struct Velocity {
    pub x: f32,
    pub y: f32,
}

#[derive(Component)]
pub struct Health {
    pub health: i32,
    pub can_loose_health: bool,
}

#[derive(Component)]
pub struct Damage {
    pub damage: i32,
    pub can_be_damage: bool,
}

#[derive(Bundle)]
pub struct EntityBundle {
    pub position: Position,
    pub velocity: Velocity,
    pub health: Health,
    pub damage: Damage,
    pub sprite: Sprite,
}

pub struct EntityPlugin;
impl Plugin for EntityPlugin {
    fn build(&self, app: &mut App) {
        app.add_systems(Startup, create_entities)
            .add_systems(Update, update_movement_by_keyboard);
    }
}

pub fn create_entities(mut commands: Commands, asset_server: Res<AssetServer>)  {
    commands.spawn(EntityBundle {
        position: Position { x: 0.0, y: 0.0 },
        velocity: Velocity { x: 5.0, y: 5.0 },
        health: Health {
            health: 100,
            can_loose_health: true,
        },
        damage: Damage {
            damage: 10,
            can_be_damage: true,
        },
        sprite: Sprite::from_image(
            asset_server.load("Main Characters\\Pink Man\\Idle (32x32).png"),
        ),
    });
}

pub fn update_movement_by_keyboard(
    keyboard_input: Res<ButtonInput<KeyCode>>,
    mut query: Query<(&mut Position, &Velocity)>,
) {
    for (mut position, velocity) in query.iter_mut() {
        if keyboard_input.pressed(KeyCode::KeyW) {
            position.y += velocity.y;
        }
        if keyboard_input.pressed(KeyCode::KeyS) {
            position.y -= velocity.y;
        }
        if keyboard_input.pressed(KeyCode::KeyA) {
            position.x -= velocity.x;
        }
        if keyboard_input.pressed(KeyCode::KeyD) {
            position.x += velocity.x;
        }
    }
}```
#

the position doesnt work, I'm not sure how to draw the sprites as they are spritesheet ect.. help

short sphinx
#

What do you mean by "the position doesn't work?" Bevy doesn't magically know how to use components you make. There is a built-in Transform component.

low fern
bronze wedge
short sphinx
#

There are numerous examples and crates that you might reference/use. What have you looked at so far?

bronze wedge
#

but yea, i'm really new to bevy and the ECS

#

I do love the idea for ECS instead of the boring and ugly OOP approach

short sphinx
bronze wedge
short sphinx
#

I'm asking this because there are examples showing what you want to do.

bronze wedge
#

they using old stuff, but the idea is still great

bronze wedge
short sphinx
#

The examples in the main bevy repo are up to date.

bronze wedge
#

alright, I want to do the sprite sheet animation which I'm looking at the code right now 😄