1
1
use crate :: prelude:: * ;
2
2
3
+ #[ derive( Hash , Eq , PartialEq , Copy , Clone ) ]
4
+ enum TextureName {
5
+ Air ,
6
+ Stone ,
7
+ Dirt ,
8
+ GrassTop ,
9
+ GrassSide ,
10
+ Bedrock ,
11
+ RedSand ,
12
+ BrownTerracotta ,
13
+ CyanTerracotta ,
14
+ GrayTerracotta ,
15
+ LightGrayTerracotta ,
16
+ OrangeTerracotta ,
17
+ RedTerracotta ,
18
+ Terracotta ,
19
+ YellowTerracotta ,
20
+ }
21
+
22
+ #[ derive( Resource ) ]
23
+ pub struct TextureManager {
24
+ textures : HashMap < TextureName , TextureUV > ,
25
+ }
26
+
27
+ impl TextureManager {
28
+ pub fn new ( ) -> Self {
29
+ let mut textures = HashMap :: new ( ) ;
30
+ textures. insert ( TextureName :: Air , [ -1.0 , -1.0 ] ) ;
31
+ textures. insert ( TextureName :: Stone , [ 0.0 , 0.0 ] ) ;
32
+ textures. insert ( TextureName :: Dirt , [ 0.25 , 0.0 ] ) ;
33
+ textures. insert ( TextureName :: GrassTop , [ 0.5 , 0.0 ] ) ;
34
+ textures. insert ( TextureName :: GrassSide , [ 0.75 , 0.0 ] ) ;
35
+ textures. insert ( TextureName :: Bedrock , [ 0.0 , 0.25 ] ) ;
36
+ textures. insert ( TextureName :: RedSand , [ 0.25 , 0.25 ] ) ;
37
+ textures. insert ( TextureName :: BrownTerracotta , [ 0.5 , 0.25 ] ) ;
38
+ textures. insert ( TextureName :: CyanTerracotta , [ 0.75 , 0.25 ] ) ;
39
+ textures. insert ( TextureName :: GrayTerracotta , [ 0.0 , 0.5 ] ) ;
40
+ textures. insert ( TextureName :: LightGrayTerracotta , [ 0.25 , 0.5 ] ) ;
41
+ textures. insert ( TextureName :: OrangeTerracotta , [ 0.5 , 0.5 ] ) ;
42
+ textures. insert ( TextureName :: RedTerracotta , [ 0.75 , 0.5 ] ) ;
43
+ textures. insert ( TextureName :: Terracotta , [ 0.0 , 0.75 ] ) ;
44
+ textures. insert ( TextureName :: YellowTerracotta , [ 0.25 , 0.75 ] ) ;
45
+ Self { textures }
46
+ }
47
+
48
+ pub fn get_texture_uv ( & self , name : TextureName ) -> Option < & TextureUV > {
49
+ self . textures . get ( & name)
50
+ }
51
+ }
52
+
3
53
pub struct Block {
4
54
pub id : BlockId ,
5
- pub texture_names : [ & ' static TextureUV ; 6 ] ,
55
+ pub texture_names : [ TextureName ; 6 ] ,
6
56
pub is_solid : bool ,
7
57
}
8
58
@@ -17,59 +67,43 @@ macro_rules! add_block {
17
67
}
18
68
19
69
pub static BLOCKS : [ Block ; 14 ] = [
20
- add_block ! ( BlockId :: Air , [ & AIR_TEXTURE ; 6 ] , false ) ,
70
+ add_block ! ( BlockId :: Air , [ TextureName :: Air ; 6 ] , false ) ,
21
71
add_block ! (
22
72
BlockId :: Grass ,
23
73
[
24
- & GRASS_TOP ,
25
- & DIRT_TEXTURE ,
26
- & GRASS_SIDE ,
27
- & GRASS_SIDE ,
28
- & GRASS_SIDE ,
29
- & GRASS_SIDE ,
74
+ TextureName :: GrassTop ,
75
+ TextureName :: Dirt ,
76
+ TextureName :: GrassSide ,
77
+ TextureName :: GrassSide ,
78
+ TextureName :: GrassSide ,
79
+ TextureName :: GrassSide ,
30
80
] ,
31
81
true
32
82
) ,
33
- add_block ! ( BlockId :: Dirt , [ & DIRT_TEXTURE ; 6 ] , true ) ,
34
- add_block ! ( BlockId :: Stone , [ & STONE_TEXTURE ; 6 ] , true ) ,
35
- add_block ! ( BlockId :: Bedrock , [ & BEDROCK ; 6 ] , true ) ,
36
- add_block ! ( BlockId :: RedSand , [ & RED_SAND ; 6 ] , true ) ,
37
- add_block ! ( BlockId :: BrownTerracotta , [ & BROWN_TERRACOTTA ; 6 ] , true ) ,
38
- add_block ! ( BlockId :: CyanTerracotta , [ & CYAN_TERRACOTTA ; 6 ] , true ) ,
39
- add_block ! ( BlockId :: GrayTerracotta , [ & GRAY_TERRACOTTA ; 6 ] , true ) ,
83
+ add_block ! ( BlockId :: Dirt , [ TextureName :: Dirt ; 6 ] , true ) ,
84
+ add_block ! ( BlockId :: Stone , [ TextureName :: Stone ; 6 ] , true ) ,
85
+ add_block ! ( BlockId :: Bedrock , [ TextureName :: Bedrock ; 6 ] , true ) ,
86
+ add_block ! ( BlockId :: RedSand , [ TextureName :: RedSand ; 6 ] , true ) ,
87
+ add_block ! ( BlockId :: BrownTerracotta , [ TextureName :: BrownTerracotta ; 6 ] , true ) ,
88
+ add_block ! ( BlockId :: CyanTerracotta , [ TextureName :: CyanTerracotta ; 6 ] , true ) ,
89
+ add_block ! ( BlockId :: GrayTerracotta , [ TextureName :: GrayTerracotta ; 6 ] , true ) ,
40
90
add_block ! (
41
91
BlockId :: LightGrayTerracotta ,
42
- [ & LIGHT_GRAY_TERRACOTTA ; 6 ] ,
92
+ [ TextureName :: LightGrayTerracotta ; 6 ] ,
43
93
true
44
94
) ,
45
- add_block ! ( BlockId :: OrangeTerracotta , [ & ORANGE_TERRACOTTA ; 6 ] , true ) ,
46
- add_block ! ( BlockId :: RedTerracotta , [ & RED_TERRACOTTA ; 6 ] , true ) ,
47
- add_block ! ( BlockId :: Terracotta , [ & TERRACOTTA ; 6 ] , true ) ,
48
- add_block ! ( BlockId :: YellowTerracotta , [ & YELLOW_TERRACOTTA ; 6 ] , true ) ,
95
+ add_block ! ( BlockId :: OrangeTerracotta , [ TextureName :: OrangeTerracotta ; 6 ] , true ) ,
96
+ add_block ! ( BlockId :: RedTerracotta , [ TextureName :: RedTerracotta ; 6 ] , true ) ,
97
+ add_block ! ( BlockId :: Terracotta , [ TextureName :: Terracotta ; 6 ] , true ) ,
98
+ add_block ! ( BlockId :: YellowTerracotta , [ TextureName :: YellowTerracotta ; 6 ] , true ) ,
49
99
] ;
50
100
51
101
type TextureUV = [ f32 ; 2 ] ;
52
102
53
- const AIR_TEXTURE : TextureUV = [ -1.0 , -1.0 ] ;
54
- const STONE_TEXTURE : TextureUV = [ 0.0 , 0.0 ] ;
55
- const DIRT_TEXTURE : TextureUV = [ 0.25 , 0.0 ] ;
56
- const GRASS_TOP : TextureUV = [ 0.5 , 0.0 ] ;
57
- const GRASS_SIDE : TextureUV = [ 0.75 , 0.0 ] ;
58
- const BEDROCK : TextureUV = [ 0.0 , 0.25 ] ;
59
- const RED_SAND : TextureUV = [ 0.25 , 0.25 ] ;
60
- const BROWN_TERRACOTTA : TextureUV = [ 0.5 , 0.25 ] ;
61
- const CYAN_TERRACOTTA : TextureUV = [ 0.75 , 0.25 ] ;
62
- const GRAY_TERRACOTTA : TextureUV = [ 0.0 , 0.5 ] ;
63
- const LIGHT_GRAY_TERRACOTTA : TextureUV = [ 0.25 , 0.5 ] ;
64
- const ORANGE_TERRACOTTA : TextureUV = [ 0.5 , 0.5 ] ;
65
- const RED_TERRACOTTA : TextureUV = [ 0.75 , 0.5 ] ;
66
- const TERRACOTTA : TextureUV = [ 0.0 , 0.75 ] ;
67
- const YELLOW_TERRACOTTA : TextureUV = [ 0.25 , 0.75 ] ;
68
-
69
103
impl Block {
70
- pub fn get_block_face_uvs ( block_id : BlockId , face : CubeFace ) -> Option < [ f32 ; 2 ] > {
104
+ pub fn get_block_face_uvs ( block_id : BlockId , face : CubeFace , texture_manager : & TextureManager ) -> Option < [ f32 ; 2 ] > {
71
105
let block = & BLOCKS [ block_id as usize ] ;
72
- let texture_uv = block. texture_names [ face as usize ] ;
73
- Some ( * texture_uv )
106
+ let texture_name = block. texture_names [ face as usize ] ;
107
+ texture_manager . get_texture_uv ( texture_name ) . copied ( )
74
108
}
75
109
}
0 commit comments