-
Notifications
You must be signed in to change notification settings - Fork 87
/
blocks.gd
262 lines (223 loc) · 6.03 KB
/
blocks.gd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
# Container for all block types.
# It is not a resource because it references scripts that can depend on it,
# causing cycles. So instead, it's more convenient to make it a node in the tree.
# IMPORTANT: Needs to be first in tree. Other nodes may use it in _ready().
extends Node
const Block = preload("./block.gd")
const Util = preload("res://common/util.gd")
const ROTATION_TYPE_NONE = 0
const ROTATION_TYPE_AXIAL = 1
const ROTATION_TYPE_Y = 2
const ROTATION_TYPE_CUSTOM_BEHAVIOR = 3
const ROTATION_Y_NEGATIVE_X = 0
const ROTATION_Y_POSITIVE_X = 1
const ROTATION_Y_NEGATIVE_Z = 2
const ROTATION_Y_POSITIVE_Z = 3
const _opposite_y_rotation = [
ROTATION_Y_POSITIVE_X,
ROTATION_Y_NEGATIVE_X,
ROTATION_Y_POSITIVE_Z,
ROTATION_Y_NEGATIVE_Z
]
const _y_dir = [
Vector3(-1, 0, 0),
Vector3(1, 0, 0),
Vector3(0, 0, -1),
Vector3(0, 0, 1)
]
const ROOT = "res://blocky_game/blocks"
const AIR_ID = 0
class RawMapping:
var block_id := 0
var variant_index := 0
var _voxel_library := preload("res://blocky_game/blocks/voxel_library.tres")
var _blocks = []
var _raw_mappings = []
func _init():
print("Constructing blocks.gd")
_create_block({
"name": "air",
"directory": "",
"gui_model": "",
"rotation_type": ROTATION_TYPE_NONE,
"voxels": ["air"],
"transparent": true
})
_create_block({
"name": "dirt",
"gui_model": "dirt.obj",
"rotation_type": ROTATION_TYPE_NONE,
"voxels": ["dirt"],
"transparent": false
})
_create_block({
"name": "grass",
"gui_model": "grass.obj",
"rotation_type": ROTATION_TYPE_NONE,
"voxels": ["grass"],
"transparent": false
})
_create_block({
"name": "log",
"gui_model": "log_y.obj",
"rotation_type": ROTATION_TYPE_AXIAL,
"voxels": ["log_x", "log_y", "log_z"],
"transparent": false
})
_create_block({
"name": "planks",
"gui_model": "planks.obj",
"rotation_type": ROTATION_TYPE_NONE,
"voxels": ["planks"],
"transparent": false
})
_create_block({
"name": "stairs",
"gui_model": "stairs_nx.obj",
"rotation_type": ROTATION_TYPE_Y,
"voxels": ["stairs_nx", "stairs_px", "stairs_nz", "stairs_pz"],
"transparent": false
})
_create_block({
"name": "tall_grass",
"gui_model": "tall_grass.obj",
"rotation_type": ROTATION_TYPE_NONE,
"voxels": ["tall_grass"],
"transparent": true,
"backface_culling": false
})
_create_block({
"name": "glass",
"gui_model": "glass.obj",
"rotation_type": ROTATION_TYPE_NONE,
"voxels": ["glass"],
"transparent": true,
"backface_culling": true
})
_create_block({
"name": "water",
"gui_model": "water_full.obj",
"rotation_type": ROTATION_TYPE_NONE,
"voxels": ["water_full", "water_top"],
"transparent": true,
"backface_culling": true
})
_create_block({
"name": "rail",
"gui_model": "rail_x.obj",
"rotation_type": ROTATION_TYPE_CUSTOM_BEHAVIOR,
"voxels": [
# Order matters, see rail.gd
"rail_x", "rail_z",
"rail_turn_nx", "rail_turn_px", "rail_turn_nz", "rail_turn_pz",
"rail_slope_nx", "rail_slope_px","rail_slope_nz", "rail_slope_pz"
],
"transparent": true,
"backface_culling": true,
"behavior": "rail.gd"
})
_create_block({
"name": "leaves",
"gui_model": "leaves.obj",
"rotation_type": ROTATION_TYPE_NONE,
"voxels": ["leaves"],
"transparent": true
})
_create_block({
"name": "dead_shrub",
"gui_model": "dead_shrub.obj",
"rotation_type": ROTATION_TYPE_NONE,
"voxels": ["dead_shrub"],
"transparent": true,
"backface_culling": false
})
func get_block(id: int) -> Block:
assert(id >= 0)
return _blocks[id]
func get_model_library() -> VoxelBlockyLibrary:
return _voxel_library
func get_block_by_name(block_name: String) -> Block:
for b in _blocks:
if b.base_info.name == block_name:
return b
assert(false)
return null
# Gets the corresponding block ID and variant index from a raw voxel value
func get_raw_mapping(raw_id: int) -> RawMapping:
assert(raw_id >= 0)
var rm = _raw_mappings[raw_id]
assert(rm != null)
return rm
func get_block_count() -> int:
return len(_blocks)
func _create_block(params: Dictionary):
_defaults(params, {
"rotation_type": ROTATION_TYPE_NONE,
"transparent": false,
"backface_culling": true,
"directory": params.name,
"behavior": ""
})
var block : Block
if params.behavior != "":
# Block with special behavior
var behavior_path := str(ROOT, "/", params.directory, "/", params.behavior)
var behavior = load(behavior_path)
block = behavior.new()
else:
# Generic
block = Block.new()
# Fill in base info
var base_info = block.base_info
base_info.id = len(_blocks)
for i in len(params.voxels):
var vname = params.voxels[i]
var id = _voxel_library.get_voxel_index_from_name(vname)
if id == -1:
push_error("Could not find voxel named {0}".format([vname]))
assert(id != -1)
params.voxels[i] = id
var rm = RawMapping.new()
rm.block_id = base_info.id
rm.variant_index = i
if id >= len(_raw_mappings):
_raw_mappings.resize(id + 1)
_raw_mappings[id] = rm
base_info.name = params.name
base_info.directory = params.directory
base_info.rotation_type = params.rotation_type
base_info.voxels = params.voxels
base_info.transparent = params.transparent
base_info.backface_culling = params.backface_culling
if base_info.directory != "":
base_info.gui_model_path = str(ROOT, "/", params.directory, "/", params.gui_model)
var sprite_path = str(ROOT, "/", params.directory, "/", params.name, "_sprite.png")
base_info.sprite_texture = load(sprite_path)
_blocks.append(block)
add_child(block)
func _notification(what):
match what:
NOTIFICATION_PREDELETE:
print("Deleting blocks.gd")
static func _defaults(d, defaults):
for k in defaults:
if not d.has(k):
d[k] = defaults[k]
static func get_y_rotation_from_look_dir(dir: Vector3) -> int:
var a = Util.get_direction_id4(Vector2(dir.x, dir.z))
match a:
0:
return ROTATION_Y_NEGATIVE_X
1:
return ROTATION_Y_NEGATIVE_Z
2:
return ROTATION_Y_POSITIVE_X
3:
return ROTATION_Y_POSITIVE_Z
_:
assert(false)
return -1
static func get_y_dir_vec(yid: int) -> Vector3:
return _y_dir[yid]
static func get_opposite_y_dir(yid: int) -> int:
return _opposite_y_rotation[yid]