~comcloudway/little_town

e12fe3048deb04550a7ec718cf75738e9c2f6ca2 — Jakob Meier 1 year, 8 months ago c2210ec
Fixed render priority
2 files changed, 22 insertions(+), 4 deletions(-)

M src/game/types.rs
M src/screens/build.rs
M src/game/types.rs => src/game/types.rs +14 -0
@@ 1,4 1,5 @@
use nanoserde::{DeJson, SerJson};
use std::cmp::Ordering;

/// three dimensional coordinate
///    z


@@ 14,6 15,7 @@ pub struct Pos3 {
    pub z: i64
}
impl Pos3 {
    /// initialize a new 3 dimensional position
    pub fn new(x: i64, y: i64, z: i64) -> Self {
        Self {
            x,


@@ 21,8 23,20 @@ impl Pos3 {
            z
        }
    }
    pub fn cmp(&self, pos: &Pos3) -> Ordering {
        if self == pos {
           return Ordering::Equal;
        }

        if self.x + self.y == pos.x+pos.y {
            return self.z.cmp(&pos.z);
        }

        return (self.x+self.y).cmp(&(pos.x+pos.y));
    }
}


/// Directions
#[derive(Clone, DeJson, SerJson)]
pub enum Direction {

M src/screens/build.rs => src/screens/build.rs +8 -4
@@ 25,7 25,6 @@ use crate::p2p::{
    GameAction
};


/// image width
pub const TEXTURE_WIDTH: f32 = 256.0;
/// actual width inside ob cube


@@ 227,6 226,7 @@ impl BuildScreen {
        }
    }
    /// adds a world to a build screen
    #[cfg(feature = "multiplayer")]
    fn upgrade(&mut self, world: World) {
        self.world = Some(world);
    }


@@ 259,7 259,7 @@ impl GameComponent for BuildScreen {

            // order: x (inc) -> y (inc) -> z (inc)
            let mut render_order: Vec<(&Pos3, &(Block, Direction))> = world.grid.iter().collect();
            render_order.sort_by_key(|(pos, _)| pos.x + pos.y*2 + pos.z*3 );
            render_order.sort_by(|(p1, _), (p2, _)| p1.cmp(p2));

            for (pos, (block, dir)) in render_order.iter() {
                let texture = block.get_texture().await.get_dir(dir);


@@ 269,7 269,7 @@ impl GameComponent for BuildScreen {
                let (x,y) = get_screen_coords(pos, world.cam.scale, world.cam.center);

                if x >= -width && x <= screen_width()
                    && y >= -width && y <= screen_height() {
                    && y >= -height && y <= screen_height() {
                        // render block
                        draw_texture_ex(
                            texture,


@@ 406,7 406,7 @@ impl GameComponent for BuildScreen {
                                }
                        }
                        // weight axis
                        in_path.sort_by_key(|pos| pos.x + pos.y*2 + pos.z*3 );
                        in_path.sort_by(|p1, p2| p1.cmp(p2));
                        if let Some(pos) = in_path.last() {
                            // position of clicked block
                            // because it is the last block in de render queue


@@ 435,6 435,8 @@ impl GameComponent for BuildScreen {
                                        if let Some((block, _)) = world.destroy_block(&pos) {
                                            // transmit block destruction
                                            // if multiplayer mode enabled
                                            #[cfg(feature = "multiplayer")]
                                            #[cfg(feature = "multiplayer")]
                                            if let Some(mp) = &mut self.multiplayer {
                                                mp.perform_action(GameAction::RemoveBlock(pos.clone(), block));
                                            }


@@ 472,6 474,7 @@ impl GameComponent for BuildScreen {

                                            // transmit block placement
                                            // if multiplayer mode enabled
                                            #[cfg(feature = "multiplayer")]
                                            if let Some(mp) = &mut self.multiplayer {
                                                mp.perform_action(GameAction::PlaceBlock(pos, block.clone(), world.inventory.direction.clone()));
                                            }


@@ 557,6 560,7 @@ impl GameComponent for BuildScreen {

                        if is_key_pressed(KeyCode::Q) || is_key_pressed(KeyCode::Escape) {

                            #[cfg(feature = "multiplayer")]
                            if let Some(mp) = &mut self.multiplayer {
                                mp.close();
                            }