~comcloudway/little_town

0a37f84263b96090d1b7da963b9d352b448c41ee — Jakob Meier 1 year, 8 months ago 5181da6
Added all blocks from base pack
M src/blocks.rs => src/blocks.rs +392 -15
@@ 1,34 1,411 @@
use crate::textures::{
    AssetStore,
    DirectionalTexture
};
use std::collections::HashMap;
use crate::textures::DirectionalTexture;

static mut CACHE: Option<HashMap<&str, DirectionalTexture>> = None;

#[derive(Eq, Hash, Clone, PartialEq)]
pub enum Block {
    Dirt,
    GrassCenter
    Bridge,

    BuildingCenter,
    BuildingCenterBeige,
    BuildingCorner,
    BuildingCornerBeige,
    BuildingDoor,
    BuildingDoorBeige,
    BuildingDoorWindows,
    BuildingDoorWindowsBeige,
    BuildingWindow,
    BuildingWindowBeige,
    BuildingWindows,
    BuildingWindowsBeige,

    CastleBend,
    CastleCenter,
    CastleCorner,
    CastleGate,
    CastleGateOpen,
    CastleSlope,
    CastleTower,
    CastleTowerBeige,
    CastleTowerBrown,
    CastleTowerGreen,
    CastleTowerPurple,
    CastleWall,
    CastelWindow,

    DirtCenter,
    DirtLow,

    GrassCenter,
    GrassCorner,
    GrassPath,
    GrassPathBend,
    GrassPathCorner,
    GrassPathCrossing,
    GrassPathEnd,
    GrassPathEndSquare,
    GrassPathSlope,
    GrassPathSplit,
    GrassRiver,
    GrassRiverBend,
    GrassRiverBridge,
    GrassRiverCorner,
    GrassRiverCrossing,
    GrassRiverEnd,
    GrassRiverEndSquare,
    GrassRiverSlope,
    GrassRiverSplit,
    GrassSlope,
    GrassSlopeConcave,
    GrassSlopeConvex,
    GrassWater,
    GrassWaterConcave,
    GrassWaterConvex,
    GrassWaterRiver,

    RocksDirt,
    RocksGrass,

    RoofChurchBeige,
    RoofChuchBrown,
    RoofChurchGreen,
    RoofChurchPurple,
    RoofGableBeige,
    RoofGableBrown,
    RoofGableGreen,
    RoofGablePurple,
    RoofPointBeige,
    RoofPointBrown,
    RoofPointGreen,
    RoofPointPurple,
    RoofRoundBeige,
    RoofRoundBrown,
    RoofRoundGreen,
    RoofRoundPurple,
    RoofRoundedBeige,
    RoofRoundedBrown,
    RoofRoundedGreen,
    RoofRoundedPurple,
    RoofSlantBeige,
    RoofSlantBrown,
    RoofSlantGreen,
    RoofSlantPurple,

    StructureArch,
    StructureHigh,
    StructureLow,

    /// tree_multiple_
    Trees,
    /// tree_single_
    Tree,

    WaterCenter,
    WaterFall
}
impl Block {
    /// returns the texture for the block
    /// with the direction the block is facing
    pub fn get_texture<'a>(&self, assets: &'a AssetStore) -> &'a DirectionalTexture {
        match self {
            Block::Dirt => &assets.blocks.dirt,
            Block::GrassCenter => &assets.blocks.grass_center
    pub async fn get_texture(&self) -> DirectionalTexture {
        let file_name = match self {
            Block::Bridge => "bridge",
            Block::BuildingCenter => "building_center",
            Block::BuildingCenterBeige => "building_centerBeige",
            Block::BuildingCorner => "building_corner",
            Block::BuildingCornerBeige => "building_cornerBeige",
            Block::BuildingDoor => "building_door",
            Block::BuildingDoorBeige => "building_doorBeige",
            Block::BuildingDoorWindows => "building_doorWindows",
            Block::BuildingDoorWindowsBeige => "building_doorWindowsBeige",
            Block::BuildingWindow => "building_window",
            Block::BuildingWindowBeige => "building_windowBeige",
            Block::BuildingWindows => "building_windows",
            Block::BuildingWindowsBeige => "building_windowsBeige",

            Block::CastleBend => "castle_bend",
            Block::CastleCenter => "castle_center",
            Block::CastleCorner => "castle_corner",
            Block::CastleGate => "castle_gate",
            Block::CastleGateOpen => "castle_gateOpen",
            Block::CastleSlope => "castle_slope",
            Block::CastleTower => "castle_tower",
            Block::CastleTowerBeige => "castle_towerBeige",
            Block::CastleTowerBrown => "castle_towerBrown",
            Block::CastleTowerGreen => "castle_towerGreen",
            Block::CastleTowerPurple => "castle_towerPurple",
            Block::CastleWall => "castle_wall",
            Block::CastelWindow => "castle_window",

            Block::DirtCenter => "dirt_center",
            Block::DirtLow => "dirt_low",

            Block::GrassCenter => "grass_center",
            Block::GrassCorner => "grass_corner",
            Block::GrassPath => "grass_path",
            Block::GrassPathBend => "grass_pathBend",
            Block::GrassPathCorner => "grass_pathCorner",
            Block::GrassPathCrossing => "grass_pathCrossing",
            Block::GrassPathEnd => "grass_pathEnd",
            Block::GrassPathEndSquare => "grass_pathEndSquare",
            Block::GrassPathSlope => "grass_pathSlope",
            Block::GrassPathSplit => "grass_pathSplit",
            Block::GrassRiver => "grass_river",
            Block::GrassRiverBend => "grass_riverBend",
            Block::GrassRiverBridge => "grass_riverBridge",
            Block::GrassRiverCorner => "grass_riverCorner",
            Block::GrassRiverCrossing => "grass_riverCrossing",
            Block::GrassRiverEnd => "grass_riverEnd",
            Block::GrassRiverEndSquare => "grass_riverEndSquare",
            Block::GrassRiverSlope => "grass_riverSlope",
            Block::GrassRiverSplit => "grass_riverSplit",
            Block::GrassSlope => "grass_slope",
            Block::GrassSlopeConcave => "grass_slopeConcave",
            Block::GrassSlopeConvex => "grass_slopeConvex",
            Block::GrassWater => "grass_water",
            Block::GrassWaterConcave => "grass_waterConcave",
            Block::GrassWaterConvex => "grass_waterConvex",
            Block::GrassWaterRiver => "grass_waterRiver",

            Block::RocksDirt => "rocks_dirt",
            Block::RocksGrass => "rocks_grass",

            Block::RoofChurchBeige => "roof_churchBeige",
            Block::RoofChuchBrown => "roof_churchBrown",
            Block::RoofChurchGreen => "roof_churchGreen",
            Block::RoofChurchPurple => "roof_churchPurple",
            Block::RoofGableBeige => "roof_gableBeige",
            Block::RoofGableBrown => "roof_gableBrown",
            Block::RoofGableGreen => "roof_gableGreen",
            Block::RoofGablePurple => "roof_gablePurple",
            Block::RoofPointBeige => "roof_pointBeige",
            Block::RoofPointBrown => "roof_pointBrown",
            Block::RoofPointGreen => "roof_pointGreen",
            Block::RoofPointPurple => "roof_pointPurple",
            Block::RoofRoundBeige => "roof_roundBeige",
            Block::RoofRoundBrown => "roof_roundBrown",
            Block::RoofRoundGreen => "roof_roundGreen",
            Block::RoofRoundPurple => "roof_roundPurple",
            Block::RoofRoundedBeige => "roof_roundedBeige",
            Block::RoofRoundedBrown => "roof_roundedBrown",
            Block::RoofRoundedGreen => "roof_roundedGreen",
            Block::RoofRoundedPurple => "roof_roundedPurple",
            Block::RoofSlantBeige => "roof_slantBeige",
            Block::RoofSlantBrown => "roof_slantBrown",
            Block::RoofSlantGreen => "roof_slantGreen",
            Block::RoofSlantPurple => "roof_slantPurple",

            Block::StructureArch => "structure_arch",
            Block::StructureHigh => "structure_high",
            Block::StructureLow => "structure_low",

            Block::Trees => "tree_multiple",
            Block::Tree => "tree_single",

            Block::WaterCenter => "water_center",
            Block::WaterFall => "water_fall"
        };


        unsafe {
            if CACHE.is_none() {
                CACHE = Some(HashMap::new());
            }

            if let Some(map) = &CACHE {
                if let Some(tx) = map.get(file_name) {
                    return tx.clone();
                }
            }
        };

        let texture = DirectionalTexture::from_auto_png(file_name).await;

        unsafe {
            if let Some(map) = &mut CACHE {
                map.insert(file_name, texture.clone());
            }
        }

        return texture.clone();
    }
    /// returns a list of all blocks
    pub fn all() -> Vec<Block> {
        vec![
            Block::Dirt,
            Block::GrassCenter
            Block::Bridge,
            Block::BuildingCenter,
            Block::BuildingCenterBeige,
            Block::BuildingCorner,
            Block::BuildingCornerBeige,
            Block::BuildingDoor,
            Block::BuildingDoorBeige,
            Block::BuildingDoorWindows,
            Block::BuildingDoorWindowsBeige,
            Block::BuildingWindow,
            Block::BuildingWindowBeige,
            Block::BuildingWindows,
            Block::BuildingWindowsBeige,
            Block::CastleBend,
            Block::CastleCenter,
            Block::CastleCorner,
            Block::CastleGate,
            Block::CastleGateOpen,
            Block::CastleSlope,
            Block::CastleTower,
            Block::CastleTowerBeige,
            Block::CastleTowerBrown,
            Block::CastleTowerGreen,
            Block::CastleTowerPurple,
            Block::CastleWall,
            Block::CastelWindow,
            Block::DirtCenter,
            Block::DirtLow,
            Block::GrassCenter,
            Block::GrassCorner,
            Block::GrassPath,
            Block::GrassPathBend,
            Block::GrassPathCorner,
            Block::GrassPathCrossing,
            Block::GrassPathEnd,
            Block::GrassPathEndSquare,
            Block::GrassPathSlope,
            Block::GrassPathSplit,
            Block::GrassRiver,
            Block::GrassRiverBend,
            Block::GrassRiverBridge,
            Block::GrassRiverCorner,
            Block::GrassRiverCrossing,
            Block::GrassRiverEnd,
            Block::GrassRiverEndSquare,
            Block::GrassRiverSlope,
            Block::GrassRiverSplit,
            Block::GrassSlope,
            Block::GrassSlopeConcave,
            Block::GrassSlopeConvex,
            Block::GrassWater,
            Block::GrassWaterConcave,
            Block::GrassWaterConvex,
            Block::GrassWaterRiver,
            Block::RocksDirt,
            Block::RocksGrass,
            Block::RoofChurchBeige,
            Block::RoofChuchBrown,
            Block::RoofChurchGreen,
            Block::RoofChurchPurple,
            Block::RoofGableBeige,
            Block::RoofGableBrown,
            Block::RoofGableGreen,
            Block::RoofGablePurple,
            Block::RoofPointBeige,
            Block::RoofPointBrown,
            Block::RoofPointGreen,
            Block::RoofPointPurple,
            Block::RoofRoundBeige,
            Block::RoofRoundBrown,
            Block::RoofRoundGreen,
            Block::RoofRoundPurple,
            Block::RoofRoundedBeige,
            Block::RoofRoundedBrown,
            Block::RoofRoundedGreen,
            Block::RoofRoundedPurple,
            Block::RoofSlantBeige,
            Block::RoofSlantBrown,
            Block::RoofSlantGreen,
            Block::RoofSlantPurple,
            Block::StructureArch,
            Block::StructureHigh,
            Block::StructureLow,
            Block::Trees,
            Block::Tree,
            Block::WaterCenter,
            Block::WaterFall
        ]
    }

    pub fn get_name(&self) -> &str {
        match self {
            Block::Dirt => "Dirt",
            Block::GrassCenter => "Grass",
            Block::Bridge => "Bridge",
            Block::BuildingDoor | Block::BuildingDoorBeige => "Building door",
            Block::BuildingDoorWindows | Block::BuildingDoorWindowsBeige => "Building door",
            Block::BuildingCenter | Block::BuildingCenterBeige => "Building",
            Block::BuildingCorner | Block::BuildingCornerBeige => "Building corner",
            Block::BuildingWindow | Block::BuildingWindowBeige => "Building window",
            Block::BuildingWindows | Block::BuildingWindowsBeige => "Building windows",
            Block::CastleBend => "Castle bend",
            Block::CastleCenter | Block::CastleCorner => "Castle",
            Block::CastleGate => "Castle Gate",
            Block::CastleGateOpen => "Open castle gate",
            Block::CastleSlope => "Castle slope",
            Block::CastleTower
                | Block::CastleTowerBeige
                | Block::CastleTowerBrown
                | Block::CastleTowerGreen
                | Block::CastleTowerPurple => "Castle tower",
            Block::CastleWall => "Castle wall",
            Block::CastelWindow => "Castle window",

            Block::DirtCenter | Block::DirtLow => "Dirt",
            Block::GrassCenter | Block::GrassCorner => "Grass",
            Block::GrassPath
                | Block::GrassPathBend
                | Block::GrassPathCorner
                | Block::GrassPathCrossing
                | Block::GrassPathEnd
                | Block::GrassPathSplit
                | Block::GrassPathEndSquare => "Grass path",
            Block::GrassPathSlope => "Grass slope",
            Block::GrassRiver
                | Block::GrassRiverBend
                | Block::GrassRiverBridge
                | Block::GrassRiverCorner
                | Block::GrassRiverCrossing
                | Block::GrassRiverEnd
                | Block::GrassRiverEndSquare
                | Block::GrassRiverSplit
                | Block::GrassRiverSlope => "River",
            Block::GrassSlope
                | Block::GrassSlopeConcave
                | Block::GrassSlopeConvex => "Grass slope",
            Block::GrassWater
                | Block::GrassWaterConcave
                | Block::GrassWaterConvex
                | Block::GrassWaterRiver => "Grass water",
            Block::RocksDirt | Block::RocksGrass => "Rocks",
            Block::RoofChurchBeige
                | Block::RoofChuchBrown
                | Block::RoofChurchGreen
                | Block::RoofChurchPurple => "Church roof",
            Block::RoofGableBeige
                | Block::RoofGableBrown
                | Block::RoofGableGreen
                | Block::RoofGablePurple => "Gable roof",
            Block::RoofPointBeige
                | Block::RoofPointBrown
                | Block::RoofPointGreen
                | Block::RoofPointPurple => "Point roof",
            Block::RoofRoundBeige
                | Block::RoofRoundBrown
                | Block::RoofRoundGreen
                | Block::RoofRoundPurple => "Round roof",
            Block::RoofRoundedBeige
                | Block::RoofRoundedBrown
                | Block::RoofRoundedGreen
                | Block::RoofRoundedPurple => "Rounded roof",
            Block::RoofSlantBeige
                | Block::RoofSlantBrown
                | Block::RoofSlantGreen
                | Block::RoofSlantPurple => "Slant roof",
            Block::StructureArch
                | Block::StructureHigh
                | Block::StructureLow => "Scaffold",
            Block::Trees => "Trees",
            Block::Tree => "Tree",
            Block::WaterCenter => "Water",
            Block::WaterFall => "Waterfall"
        }
    }
}
impl Default for Block {
    fn default() -> Self {
        Block::StructureHigh
    }
}

M src/main.rs => src/main.rs +2 -1
@@ 1,5 1,6 @@
#![feature(int_roundings)]
#![feature(let_chains)]
#![feature(async_fn_in_trait)]

use macroquad::prelude::*;



@@ 27,7 28,7 @@ async fn main() {
    loop {
        clear_background(Color::from_rgba(215, 189, 165, 255));

        screen.draw(&assets);
        screen.draw(&assets).await;

        match screen.ev_loop() {
            GameEvent::Quit => break,

M src/screens/build.rs => src/screens/build.rs +4 -4
@@ 152,10 152,10 @@ impl Face {
}

impl GameComponent for BuildScreen {
    fn draw(&self, assets: &AssetStore) {
    async fn draw(&self, assets: &AssetStore) {
        if self.show_inv {
            // draw inventory
            self.inv.draw(&assets);
            self.inv.draw(&assets).await;
            return;
        }
        // order: x (inc) -> y (inc) -> z (inc)


@@ 163,7 163,7 @@ impl GameComponent for BuildScreen {
        render_order.sort_by_key(|(pos, _)| pos.x + pos.y*2 + pos.z*3 );

        for (pos, (block, dir)) in render_order.iter() {
            let texture = block.get_texture(&assets).get_dir(dir);
            let texture = block.get_texture().await.get_dir(dir);
            let width = TEXTURE_WIDTH * self.cam.scale;
            let height = TEXTURE_HEIGHT * self.cam.scale;



@@ 219,7 219,7 @@ impl GameComponent for BuildScreen {
            if let Some(block) = &self.inv.selected {
                // draw block
                draw_texture_ex(
                    block.get_texture(&assets).get_dir(&self.inv.direction),
                    block.get_texture().await.get_dir(&self.inv.direction),
                    tx + slot_render_dim / 6.0,
                    ty + width / 2.0
                        - (slot_render_dim * 2.0/3.0 * TEXTURE_HEIGHT / TEXTURE_WIDTH) / 2.0

M src/screens/inventory.rs => src/screens/inventory.rs +2 -2
@@ 87,7 87,7 @@ const SLOTS_PER_PAGE: usize = 9;


impl GameComponent for Inventory {
    fn draw(&self, assets: &AssetStore) {
    async fn draw(&self, assets: &AssetStore) {
        let mid_x = screen_width()/2.0;
        let mid_y = screen_height()/2.0;



@@ 148,7 148,7 @@ impl GameComponent for Inventory {
                if let Some((block, amount)) = list.get(self.page * SLOTS_PER_PAGE + x + y * hslot_count) {
                    // draw block
                    draw_texture_ex(
                        block.get_texture(&assets).get_dir(&self.direction),
                        block.get_texture().await.get_dir(&self.direction),
                        tx + slot_render_dim / 6.0,
                        ty + slot_dim / 2.0 - TEXTURE_HEIGHT / 2.0 + TEXTURE_Y_WHITESPACE / 2.0,
                        WHITE,

M src/screens/mod.rs => src/screens/mod.rs +3 -3
@@ 16,10 16,10 @@ pub enum Screen {
    Build(BuildScreen),
}
impl GameComponent for Screen {
    fn draw(&self, assets: &AssetStore) {
    async fn draw(&self, assets: &AssetStore) {
        match self {
            Screen::Welcome(w) => w.draw(&assets),
            Screen::Build(b) => b.draw(&assets),
            Screen::Welcome(w) => w.draw(&assets).await,
            Screen::Build(b) => b.draw(&assets).await,
        }
    }
    fn ev_loop(&mut self) -> GameEvent {

M src/screens/welcome.rs => src/screens/welcome.rs +1 -1
@@ 18,7 18,7 @@ impl WelcomeScreen {
    }
}
impl GameComponent for WelcomeScreen {
    fn draw(&self, assets: &AssetStore) {
    async fn draw(&self, assets: &AssetStore) {
        draw_centered_text(
            "Welcome",
            screen_width()/2.0, screen_height()/2.0,

M src/textures.rs => src/textures.rs +6 -14
@@ 28,7 28,6 @@ fn in_font_folder(path: &str) -> String {
pub struct AssetStore {
    pub font: Font,
    pub ui: UIAssetCollection,
    pub blocks: BlockAssetCollection
}
pub struct UIAssetCollection {
    pub long_button: (Texture2D, Texture2D),


@@ 38,6 37,7 @@ pub struct UIAssetCollection {
    pub arrow_right: Texture2D,
    pub close_button: Texture2D
}
#[derive(Clone)]
pub struct DirectionalTexture {
    pub north: Texture2D,
    pub south: Texture2D,


@@ 45,19 45,19 @@ pub struct DirectionalTexture {
    pub west: Texture2D,
}
impl DirectionalTexture {
    async fn from_auto_png(name: &str) -> Self {
    pub async fn from_auto_png(name: &str) -> Self {
        Self {
            north: load_texture(
                &in_base_folder(&format!("{}_N.png", name)))
                &in_base_folder(&format!("Tiles/{}_N.png", name)))
                .await.expect("Unable to load texture N"),
            south: load_texture(
                &in_base_folder(&format!("{}_S.png", name)))
                &in_base_folder(&format!("Tiles/{}_S.png", name)))
                .await.expect("Unable to load texture S"),
            west: load_texture(
                &in_base_folder(&format!("{}_W.png", name)))
                &in_base_folder(&format!("Tiles/{}_W.png", name)))
                .await.expect("Unable to load texture W"),
            east: load_texture(
                &in_base_folder(&format!("{}_E.png", name)))
                &in_base_folder(&format!("Tiles/{}_E.png", name)))
                .await.expect("Unable to load texture E"),
        }
    }


@@ 70,10 70,6 @@ impl DirectionalTexture {
        }
    }
}
pub struct BlockAssetCollection {
    pub dirt: DirectionalTexture,
    pub grass_center: DirectionalTexture
}

impl AssetStore {
    /// loads all the textures


@@ 113,10 109,6 @@ impl AssetStore {
                    &in_ui_folder("PNG/iconCross_brown.png"))
                    .await.expect("Unable to load texture")
            },
            blocks: BlockAssetCollection {
                dirt: DirectionalTexture::from_auto_png("Tiles/dirt_center").await,
                grass_center: DirectionalTexture::from_auto_png("Tiles/grass_center").await
            }
        }
    }
}

M src/types.rs => src/types.rs +1 -1
@@ 48,7 48,7 @@ pub enum Direction {
/// but the serve different purposes
pub trait GameComponent {
    /// Callback used to redraw the component
    fn draw(&self, assets: &AssetStore) {}
    async fn draw(&self, assets: &AssetStore) {}
    /// Callback used to perform background tasks
    /// e.g. keyboard input
    /// NOTE: this is currently not being run asynchronously