1
1
use crate :: prelude:: * ;
2
2
3
+ #[ derive( Hash , Eq , PartialEq , Copy , Clone ) ]
4
+ pub 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 Default for TextureManager {
28
+ fn default ( ) -> Self {
29
+ Self :: new ( )
30
+ }
31
+ }
32
+
33
+ impl TextureManager {
34
+ pub fn new ( ) -> Self {
35
+ let mut textures = HashMap :: new ( ) ;
36
+ textures. insert ( TextureName :: Air , [ -1.0 , -1.0 ] ) ;
37
+ textures. insert ( TextureName :: Stone , [ 0.0 , 0.0 ] ) ;
38
+ textures. insert ( TextureName :: Dirt , [ 0.25 , 0.0 ] ) ;
39
+ textures. insert ( TextureName :: GrassTop , [ 0.5 , 0.0 ] ) ;
40
+ textures. insert ( TextureName :: GrassSide , [ 0.75 , 0.0 ] ) ;
41
+ textures. insert ( TextureName :: Bedrock , [ 0.0 , 0.25 ] ) ;
42
+ textures. insert ( TextureName :: RedSand , [ 0.25 , 0.25 ] ) ;
43
+ textures. insert ( TextureName :: BrownTerracotta , [ 0.5 , 0.25 ] ) ;
44
+ textures. insert ( TextureName :: CyanTerracotta , [ 0.75 , 0.25 ] ) ;
45
+ textures. insert ( TextureName :: GrayTerracotta , [ 0.0 , 0.5 ] ) ;
46
+ textures. insert ( TextureName :: LightGrayTerracotta , [ 0.25 , 0.5 ] ) ;
47
+ textures. insert ( TextureName :: OrangeTerracotta , [ 0.5 , 0.5 ] ) ;
48
+ textures. insert ( TextureName :: RedTerracotta , [ 0.75 , 0.5 ] ) ;
49
+ textures. insert ( TextureName :: Terracotta , [ 0.0 , 0.75 ] ) ;
50
+ textures. insert ( TextureName :: YellowTerracotta , [ 0.25 , 0.75 ] ) ;
51
+ Self { textures }
52
+ }
53
+
54
+ pub fn get_texture_uv ( & self , name : TextureName ) -> Option < & TextureUV > {
55
+ self . textures . get ( & name)
56
+ }
57
+ }
58
+
3
59
pub struct Block {
4
60
pub id : BlockId ,
5
- pub texture_names : [ & ' static str ; 6 ] ,
61
+ pub texture_names : [ TextureName ; 6 ] ,
6
62
pub is_solid : bool ,
7
63
}
8
64
@@ -17,61 +73,71 @@ macro_rules! add_block {
17
73
}
18
74
19
75
pub static BLOCKS : [ Block ; 14 ] = [
20
- add_block ! ( BlockId :: Air , [ "air" ; 6 ] , false ) ,
76
+ add_block ! ( BlockId :: Air , [ TextureName :: Air ; 6 ] , false ) ,
21
77
add_block ! (
22
78
BlockId :: Grass ,
23
79
[
24
- "grass_top" ,
25
- "dirt" ,
26
- "grass_side" ,
27
- "grass_side" ,
28
- "grass_side" ,
29
- "grass_side" ,
80
+ TextureName :: GrassTop ,
81
+ TextureName :: Dirt ,
82
+ TextureName :: GrassSide ,
83
+ TextureName :: GrassSide ,
84
+ TextureName :: GrassSide ,
85
+ TextureName :: GrassSide ,
30
86
] ,
31
87
true
32
88
) ,
33
- add_block ! ( BlockId :: Dirt , [ "dirt" ; 6 ] , true ) ,
34
- add_block ! ( BlockId :: Stone , [ "stone" ; 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 ) ,
89
+ add_block ! ( BlockId :: Dirt , [ TextureName :: Dirt ; 6 ] , true ) ,
90
+ add_block ! ( BlockId :: Stone , [ TextureName :: Stone ; 6 ] , true ) ,
91
+ add_block ! ( BlockId :: Bedrock , [ TextureName :: Bedrock ; 6 ] , true ) ,
92
+ add_block ! ( BlockId :: RedSand , [ TextureName :: RedSand ; 6 ] , true ) ,
93
+ add_block ! (
94
+ BlockId :: BrownTerracotta ,
95
+ [ TextureName :: BrownTerracotta ; 6 ] ,
96
+ true
97
+ ) ,
98
+ add_block ! (
99
+ BlockId :: CyanTerracotta ,
100
+ [ TextureName :: CyanTerracotta ; 6 ] ,
101
+ true
102
+ ) ,
103
+ add_block ! (
104
+ BlockId :: GrayTerracotta ,
105
+ [ TextureName :: GrayTerracotta ; 6 ] ,
106
+ true
107
+ ) ,
40
108
add_block ! (
41
109
BlockId :: LightGrayTerracotta ,
42
- [ "light_gray_terracotta" ; 6 ] ,
110
+ [ TextureName :: LightGrayTerracotta ; 6 ] ,
111
+ true
112
+ ) ,
113
+ add_block ! (
114
+ BlockId :: OrangeTerracotta ,
115
+ [ TextureName :: OrangeTerracotta ; 6 ] ,
116
+ true
117
+ ) ,
118
+ add_block ! (
119
+ BlockId :: RedTerracotta ,
120
+ [ TextureName :: RedTerracotta ; 6 ] ,
121
+ true
122
+ ) ,
123
+ add_block ! ( BlockId :: Terracotta , [ TextureName :: Terracotta ; 6 ] , true ) ,
124
+ add_block ! (
125
+ BlockId :: YellowTerracotta ,
126
+ [ TextureName :: YellowTerracotta ; 6 ] ,
43
127
true
44
128
) ,
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 ) ,
49
129
] ;
50
130
51
- impl Block {
52
- pub fn get_texture_uvs ( texture_name : & str ) -> Option < [ f32 ; 2 ] > {
53
- match texture_name {
54
- "stone" => Some ( [ 0.0 , 0.0 ] ) ,
55
- "dirt" => Some ( [ 0.25 , 0.0 ] ) ,
56
- "grass_top" => Some ( [ 0.5 , 0.0 ] ) ,
57
- "grass_side" => Some ( [ 0.75 , 0.0 ] ) ,
58
- "bedrock" => Some ( [ 0.0 , 0.25 ] ) ,
59
- "red_sand" => Some ( [ 0.25 , 0.25 ] ) ,
60
- "brown_terracotta" => Some ( [ 0.5 , 0.25 ] ) ,
61
- "cyan_terracotta" => Some ( [ 0.75 , 0.25 ] ) ,
62
- "gray_terracotta" => Some ( [ 0.0 , 0.5 ] ) ,
63
- "light_gray_terracotta" => Some ( [ 0.25 , 0.5 ] ) ,
64
- "orange_terracotta" => Some ( [ 0.5 , 0.5 ] ) ,
65
- "red_terracotta" => Some ( [ 0.75 , 0.5 ] ) ,
66
- "terracotta" => Some ( [ 0.0 , 0.75 ] ) ,
67
- "yellow_terracotta" => Some ( [ 0.25 , 0.75 ] ) ,
68
- _ => None ,
69
- }
70
- }
131
+ type TextureUV = [ f32 ; 2 ] ;
71
132
72
- pub fn get_block_face_uvs ( block_id : BlockId , face : CubeFace ) -> Option < [ f32 ; 2 ] > {
133
+ impl Block {
134
+ pub fn get_block_face_uvs (
135
+ block_id : BlockId ,
136
+ face : CubeFace ,
137
+ texture_manager : & TextureManager ,
138
+ ) -> Option < [ f32 ; 2 ] > {
73
139
let block = & BLOCKS [ block_id as usize ] ;
74
140
let texture_name = block. texture_names [ face as usize ] ;
75
- Self :: get_texture_uvs ( texture_name)
141
+ texture_manager . get_texture_uv ( texture_name) . copied ( )
76
142
}
77
143
}
0 commit comments