~comcloudway/little_town

30ef27e06fa2349098ea126b255e5e51872d324f — Jakob Meier 1 year, 1 month ago 6a13111
Added block-hover highlight
to show which block is being focused right now
1 files changed, 38 insertions(+), 1 deletions(-)

M src/screens/build.rs
M src/screens/build.rs => src/screens/build.rs +38 -1
@@ 250,6 250,37 @@ impl GameComponent for BuildScreen {
            let mut render_order: Vec<(&Pos3, &(Block, Direction))> = world.grid.iter().collect();
            render_order.sort_by(|(p1, _), (p2, _)| p1.cmp(p2));


            let (mx, my) = mouse_position();

            // list of positions in render que for given pixel
            // (mx, my)
            let mut in_path: Vec<&Pos3> = Vec::new();

            for (pos, _) in &render_order {
                let (x, y) = get_screen_coords(pos, world.cam.scale, world.cam.center);

                if mx >= x
                    && mx <= TEXTURE_WIDTH.mul_add(world.cam.scale, x)
                    && my >= TEXTURE_Y_WHITESPACE.mul_add(world.cam.scale, y)
                    && my <= TEXTURE_HEIGHT.mul_add(world.cam.scale, y)
                {
                    // check if mouse is above transparent area
                    // and skip block if necessary
                    if Face::from_xy(mx - x, my - y, world.cam.scale).is_none() {
                        continue;
                    }

                    // block in mouse path
                    in_path.push(pos);
                }
            }
            // weight axis
            //in_path.sort_by(|p1, p2| p1.cmp(p2));
            let focused = in_path.last();


            // draw world
            for (pos, (block, dir)) in &render_order {
                let texture = block.get_texture().await.get_dir(dir);
                let width = TEXTURE_WIDTH * world.cam.scale;


@@ 258,12 289,18 @@ impl GameComponent for BuildScreen {
                let (x, y) = get_screen_coords(pos, world.cam.scale, world.cam.center);

                if x >= -width && x <= screen_width() && y >= -height && y <= screen_height() {
                    let mut color = WHITE;
                    if let Some(focused_block_pos) = focused {
                        if focused_block_pos == pos {
                            color = LIGHTGRAY;
                        }
                    }
                    // render block
                    draw_texture_ex(
                        texture,
                        x,
                        y,
                        WHITE,
                        color,
                        DrawTextureParams {
                            dest_size: Some(Vec2::new(width, height)),
                            source: None,