User Tools

Site Tools


devlogs:1_5_2022

Sunday, May 1st 2022

Still following this tutorial.

Shit

fn position_translation(windows: Res<Windows>, mut q: Query<(&Position, &mut Transform)>) {
    fn convert(pos: f32, bound_window: f32, bound_game: f32) -> f32 {
        let tile_size = bound_window / bound_game;
        pos / bound_game * bound_window - (bound_window / 2.) + (tile_size / 2.)
    }
    let window = windows.get_primary().unwrap();
    for (pos, mut transform) in q.iter_mut() {
        transform.translation = Vec3::new(
            convert(pos.x as f32, window.width() as f32, ARENA_WIDTH as f32),
            convert(pos.y as f32, window.height() as f32, ARENA_HEIGHT as f32),
            0.0,
        );
    }
}

explination from the dood 1).

The position translation: if an item’s x coordinate is at 5 in our system, the width in our system is 10, and the window width is 200, then the coordinate should be 5 / 10 * 200 - 200 / 2. We subtract half the window width because our coordinate system starts at the bottom left, and Translation starts from the center. We then add half the size of a single tile, because we want our sprites bottom left corner to be at the bottom left of a tile, not the center.

lol.jpg

fn spawn_segment(mut commands: Commands, position: Position) -> Entity {
    commands
        .spawn_bundle(SpriteBundle{
            sprite: Sprite{
                color: SNAKE_SEGMENT_COLOR,
                ..default()
            },
            ..default()
        })
        .insert(SnakeSegment)
        .insert(position)
        .insert(Size::square(0.65))
        .id() // This is the entity as it is essentially an ID that connects some data together
}

The above code is interesting as its returning an Entity, which is done with accessing the id() from the spawned spritebundle.

The entity is essentially an ID that connects some data together




ggs

Yea this is sloooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooow 🤞🤞🤞🤞 on actually understanding this stuff better.

1)
the person whom wrote the tutorial

Hallo

Enter your comment. Wiki syntax is allowed:
T K O B C
 
devlogs/1_5_2022.txt · Last modified: 2023/10/19 15:25 (external edit)