@@ -9,104 +9,78 @@ pub struct FaceTexture(u32);
9
9
#[ derive( Clone , Copy , Debug ) ]
10
10
pub struct BlockTypeConfigs {
11
11
pub id : u32 ,
12
- // The amount to add to the block id (because some blocks have more than 1 texture)
13
- pub step : u32 ,
14
- // Different texture for top face
15
- pub top_texture : Option < FaceTexture > ,
16
- // Different texture for bottom face
17
- pub bottom_texture : Option < FaceTexture > ,
12
+ // Integers representing the nth texture to use.
13
+ pub textures : [ FaceTexture ; 3 ] , // 1: Lateral texture, 2: Top texture, 3: Bottom texture
18
14
pub is_translucent : bool ,
19
15
}
20
16
17
+ impl BlockTypeConfigs {
18
+ pub fn get ( block_type : BlockType ) -> BlockTypeConfigs {
19
+ match block_type {
20
+ BlockType :: Grass => BlockTypeConfigs {
21
+ id : 0 ,
22
+ textures : [ FaceTexture ( 6 ) , FaceTexture ( 7 ) , FaceTexture ( 8 ) ] ,
23
+ is_translucent : false ,
24
+ } ,
25
+ BlockType :: Dirt => BlockTypeConfigs {
26
+ id : 1 ,
27
+ textures : [ FaceTexture ( 0 ) , FaceTexture ( 0 ) , FaceTexture ( 0 ) ] ,
28
+ is_translucent : false ,
29
+ } ,
30
+
31
+ BlockType :: Water => BlockTypeConfigs {
32
+ id : 2 ,
33
+ textures : [ FaceTexture ( 1 ) , FaceTexture ( 1 ) , FaceTexture ( 1 ) ] ,
34
+ is_translucent : true ,
35
+ } ,
36
+
37
+ BlockType :: Wood => BlockTypeConfigs {
38
+ id : 3 ,
39
+ textures : [ FaceTexture ( 4 ) , FaceTexture ( 5 ) , FaceTexture ( 5 ) ] ,
40
+ is_translucent : false ,
41
+ } ,
42
+ BlockType :: Leaf => BlockTypeConfigs {
43
+ id : 4 ,
44
+ textures : [ FaceTexture ( 2 ) , FaceTexture ( 2 ) , FaceTexture ( 2 ) ] ,
45
+ is_translucent : false ,
46
+ } ,
47
+ BlockType :: Stone => BlockTypeConfigs {
48
+ id : 5 ,
49
+ textures : [ FaceTexture ( 3 ) , FaceTexture ( 3 ) , FaceTexture ( 3 ) ] ,
50
+ is_translucent : false ,
51
+ } ,
52
+ }
53
+ }
54
+ }
55
+
21
56
#[ repr( u32 ) ]
22
- #[ derive( Clone , Copy , Debug ) ]
57
+ #[ derive( Clone , Copy , Debug , PartialEq ) ]
23
58
pub enum BlockType {
24
- Grass ( BlockTypeConfigs ) ,
25
- Dirt ( BlockTypeConfigs ) ,
26
- Water ( BlockTypeConfigs ) ,
27
- Wood ( BlockTypeConfigs ) ,
28
- Leaf ( BlockTypeConfigs ) ,
29
- Stone ( BlockTypeConfigs ) ,
59
+ Grass ,
60
+ Dirt ,
61
+ Water ,
62
+ Wood ,
63
+ Leaf ,
64
+ Stone ,
30
65
}
31
66
impl BlockType {
67
+ pub fn get_config ( & self ) -> BlockTypeConfigs {
68
+ BlockTypeConfigs :: get ( * self )
69
+ }
70
+ pub fn to_id ( & self ) -> u32 {
71
+ self . get_config ( ) . id
72
+ }
32
73
pub fn from_id ( id : u32 ) -> BlockType {
33
74
match id {
34
- 0 => Self :: dirt ( ) ,
35
- 1 => Self :: water ( ) ,
36
- 2 => Self :: leaf ( ) ,
37
- 3 => Self :: stone ( ) ,
38
- 4 => Self :: wood ( ) ,
39
- 5 => Self :: grass ( ) ,
75
+ 0 => Self :: Grass ,
76
+ 1 => Self :: Dirt ,
77
+ 2 => Self :: Water ,
78
+ 3 => Self :: Wood ,
79
+ 4 => Self :: Leaf ,
80
+ 5 => Self :: Stone ,
40
81
_ => panic ! ( "Invalid id" ) ,
41
82
}
42
83
}
43
- pub fn to_id ( & self ) -> u32 {
44
- // meh
45
- match self {
46
- Self :: Grass ( f) => f. id ,
47
- Self :: Dirt ( f) => f. id ,
48
- Self :: Water ( f) => f. id ,
49
- Self :: Wood ( f) => f. id ,
50
- Self :: Leaf ( f) => f. id ,
51
- Self :: Stone ( f) => f. id ,
52
- }
53
- }
54
-
55
- pub fn dirt ( ) -> Self {
56
- Self :: Dirt ( BlockTypeConfigs {
57
- id : 0 ,
58
- step : 0 ,
59
- bottom_texture : None ,
60
- top_texture : None ,
61
- is_translucent : false ,
62
- } )
63
- }
64
- pub fn water ( ) -> Self {
65
- Self :: Water ( BlockTypeConfigs {
66
- id : 1 ,
67
- step : 0 ,
68
- bottom_texture : None ,
69
- top_texture : None ,
70
- is_translucent : true ,
71
- } )
72
- }
73
- pub fn leaf ( ) -> Self {
74
- Self :: Leaf ( BlockTypeConfigs {
75
- id : 2 ,
76
- step : 0 ,
77
- bottom_texture : None ,
78
- top_texture : None ,
79
- is_translucent : false ,
80
- } )
81
- }
82
- pub fn stone ( ) -> Self {
83
- Self :: Stone ( BlockTypeConfigs {
84
- id : 3 ,
85
- step : 0 ,
86
- bottom_texture : None ,
87
- top_texture : None ,
88
- is_translucent : false ,
89
- } )
90
- }
91
- pub fn wood ( ) -> Self {
92
- Self :: Wood ( BlockTypeConfigs {
93
- id : 4 ,
94
- step : 0 ,
95
- bottom_texture : Some ( FaceTexture ( 1 ) ) ,
96
- top_texture : Some ( FaceTexture ( 1 ) ) ,
97
- is_translucent : false ,
98
- } )
99
- }
100
-
101
- pub fn grass ( ) -> Self {
102
- Self :: Grass ( BlockTypeConfigs {
103
- id : 5 ,
104
- step : 1 ,
105
- bottom_texture : Some ( FaceTexture ( 2 ) ) ,
106
- top_texture : Some ( FaceTexture ( 1 ) ) ,
107
- is_translucent : false ,
108
- } )
109
- }
110
84
}
111
85
impl BlockType {
112
86
const U_STONE_THRESHOLD : u32 = 20 ;
@@ -118,59 +92,52 @@ impl BlockType {
118
92
let scaler = ( y as f32 - Self :: U_STONE_THRESHOLD as f32 ) / 10.0 ;
119
93
let res = t + scaler;
120
94
if res > 1.0 {
121
- BlockType :: stone ( )
95
+ BlockType :: Stone
122
96
} else {
123
- BlockType :: dirt ( )
97
+ BlockType :: Dirt
124
98
}
125
99
} else if y < Self :: L_STONE_THRESHOLD {
126
- BlockType :: stone ( )
100
+ BlockType :: Stone
127
101
} else {
128
- BlockType :: dirt ( )
102
+ BlockType :: Dirt
129
103
}
130
104
}
131
105
}
132
106
133
107
const TEXTURE_SIZE : u32 = 256 ;
134
108
const BLOCK_PER_ROW : u32 = 8 ;
109
+ // 32px per block
135
110
const BLOCK_OFFSET : u32 = TEXTURE_SIZE / BLOCK_PER_ROW ;
136
111
const BLOCK_OFFSET_NORMALIZED : f32 = BLOCK_OFFSET as f32 / TEXTURE_SIZE as f32 ;
137
112
138
113
fn get_base_coords ( config : & BlockTypeConfigs , face_dir : FaceDirections ) -> glam:: Vec2 {
139
114
let face_offset = match face_dir {
140
- FaceDirections :: Top => config. top_texture . unwrap_or ( FaceTexture ( 0 ) ) ,
141
- FaceDirections :: Bottom => config. bottom_texture . unwrap_or ( FaceTexture ( 0 ) ) ,
142
- _ => FaceTexture ( 0 ) ,
115
+ FaceDirections :: Top => config. textures [ 1 ] ,
116
+ FaceDirections :: Bottom => config. textures [ 2 ] ,
117
+ _ => config . textures [ 0 ] ,
143
118
} ;
119
+ let y_offset = ( face_offset. 0 / BLOCK_PER_ROW ) as f32 ;
120
+ let x_offset = ( face_offset. 0 % BLOCK_PER_ROW ) as f32 ;
144
121
145
- let position = config. id + config. step + face_offset. 0 ;
146
- let wrap = position / BLOCK_PER_ROW ;
147
-
148
- let low_bound = 1.0 - ( BLOCK_OFFSET_NORMALIZED + ( BLOCK_OFFSET_NORMALIZED * wrap as f32 ) ) ;
149
- let left_bound = ( position as f32 % BLOCK_PER_ROW as f32 ) / BLOCK_PER_ROW as f32 ;
150
- glam:: vec2 ( left_bound, low_bound)
122
+ let low_bound = y_offset * BLOCK_OFFSET_NORMALIZED + BLOCK_OFFSET_NORMALIZED ;
123
+ let left_bound = x_offset * BLOCK_OFFSET_NORMALIZED ;
124
+ return glam:: vec2 ( left_bound, low_bound) ;
151
125
}
152
126
fn get_tex_coords ( config : & BlockTypeConfigs , face_dir : FaceDirections ) -> [ [ f32 ; 2 ] ; 4 ] {
153
127
let bc = get_base_coords ( config, face_dir) ;
154
128
[
155
129
[ bc. x , bc. y ] ,
156
- [ bc. x , bc. y + BLOCK_OFFSET_NORMALIZED ] ,
130
+ [ bc. x , bc. y - BLOCK_OFFSET_NORMALIZED ] ,
157
131
[
158
132
bc. x + BLOCK_OFFSET_NORMALIZED ,
159
- bc. y + BLOCK_OFFSET_NORMALIZED ,
133
+ bc. y - BLOCK_OFFSET_NORMALIZED ,
160
134
] ,
161
135
[ bc. x + BLOCK_OFFSET_NORMALIZED , bc. y ] ,
162
136
]
163
137
}
164
138
165
139
impl TexturedBlock for BlockType {
166
140
fn get_texcoords ( & self , face_dir : FaceDirections ) -> [ [ f32 ; 2 ] ; 4 ] {
167
- match self {
168
- BlockType :: Grass ( config) => get_tex_coords ( config, face_dir) ,
169
- BlockType :: Dirt ( config) => get_tex_coords ( config, face_dir) ,
170
- BlockType :: Water ( config) => get_tex_coords ( config, face_dir) ,
171
- BlockType :: Stone ( config) => get_tex_coords ( config, face_dir) ,
172
- BlockType :: Wood ( config) => get_tex_coords ( config, face_dir) ,
173
- BlockType :: Leaf ( config) => get_tex_coords ( config, face_dir) ,
174
- }
141
+ get_tex_coords ( & self . get_config ( ) , face_dir)
175
142
}
176
143
}
0 commit comments