Added random obj spawn + set giants dir on start

This commit is contained in:
Amazed 2020-04-18 18:44:36 +02:00
parent b37f4c5121
commit c03e87bc21
7 changed files with 131 additions and 41 deletions

30
3d.gd
View File

@ -7,6 +7,7 @@ extends Spatial
export var fast_close := true
var mouse_mode: String = "CAPTURED"
var objmgr
onready var camera = get_node("Player/Head/Camera")
@ -16,7 +17,12 @@ func _ready() -> void:
if fast_close:
print("** 'Esc' to close 'Shift + F1' to release mouse **")
var giantsscript = load("res://objectmanager.gd")
var objmgr = giantsscript.ObjectManager.new()
var f = File.new()
f.open("res://dir.txt", File.READ)
var dir = f.get_as_text()
f.close()
objmgr = giantsscript.ObjectManager.new()
objmgr.GIANTS_PATH = dir
# var giants_dir_browser = FileDialog.new()
# giants_dir_browser.mode = FileDialog.MODE_OPEN_DIR
# giants_dir_browser.window_title = "Select your Giants directory"
@ -25,6 +31,7 @@ func _ready() -> void:
# giants_dir_browser.popup_centered()
objmgr.build()
# var m = objmgr.load_model("mc_nothrust_l0.gbs")
# var m = objmgr.load_model("ripper.gbs")
var m = objmgr.load_model("RP_command.gbs")
# print(m.tex)
# print(m.point_uv)
@ -46,6 +53,23 @@ func _input(event: InputEvent) -> void:
"VISIBLE":
Input.set_mouse_mode(Input.MOUSE_MODE_CAPTURED)
mouse_mode = "CAPTURED"
if event.is_action_pressed("spawn_object"):
var random_key = ""
var rand = RandomNumberGenerator.new()
rand.randomize()
var keys = objmgr.index.keys()
while not random_key.ends_with(".gbs"):
var rand_i = rand.randi_range(0, len(keys)-1)
random_key = keys[rand_i]
var m = objmgr.load_model(random_key)
print("Loading "+random_key)
var meshinstance = objmgr.load_model_mesh(m)
var pos = camera.get_global_transform().origin
meshinstance.transform.origin.x = pos.x
meshinstance.transform.origin.y = pos.y
meshinstance.transform.origin.z = pos.z
add_child(meshinstance)
func _process(delta):
var pos = camera.get_global_transform().origin
@ -56,7 +80,3 @@ func _process(delta):
func _on_Button_pressed():
get_tree().quit()
func _on_GiantsDirectory_dir_selected(dir):
pass # Replace with function body.

52
3d.tscn

File diff suppressed because one or more lines are too long

22
FileDialog.gd Normal file
View File

@ -0,0 +1,22 @@
extends FileDialog
func _on_FileDialog_ready():
popup_centered()
func _on_FileDialog_dir_selected(dir):
var d = Directory.new()
d.open(dir)
if not d.file_exists("Giants.exe"):
return
var f = File.new()
f.open("res://dir.txt", File.WRITE)
f.store_string(dir)
f.close()
get_tree().change_scene("res://3d.tscn")
func _on_FileDialog_popup_hide():
popup_centered()

View File

@ -1,3 +1,5 @@
tool
extends MeshInstance
var signature

View File

@ -33,17 +33,19 @@ class ObjectManager:
var compression = file.get_8()
var name_length = file.get_8()
var name = file.get_buffer(name_length).get_string_from_ascii()
name = name.to_lower()
if name.ends_with(".tga") or name.ends_with(".gbs"):
index[name] = file.get_path_absolute()
if name.ends_with(".gbs"):
print(name)
func read_file_in_gzp(filename: String) -> StreamPeerBuffer:
filename = filename.to_lower()
if not filename in index:
return null
var gzp_file = index[filename]
var res = StreamPeerBuffer.new()
var file = File.new()
if not file.open(gzp_file, File.READ) == OK:
print("Could not open "+filename)
print("Could not open "+gzp_file)
return res
var checksum = file.get_32()
assert(checksum == 0x6608F101)
@ -59,7 +61,7 @@ class ObjectManager:
var compression = file.get_8()
var name_length = file.get_8()
var name = file.get_buffer(name_length).get_string_from_ascii()
if name == filename:
if name.to_lower() == filename:
var current_offset = file.get_position()
file.seek(content_offset)
var data = file.get_buffer(compressed_size)
@ -68,9 +70,13 @@ class ObjectManager:
data = _decompress(data, original_size)
res.data_array = data
file.close()
if res.data_array.size() == 0:
print("Could not open "+filename)
return null
return res
func load_model(gbs_file: String):
gbs_file = gbs_file.to_lower()
var pos = 0
var gbs_stream = read_file_in_gzp(gbs_file)
var model = load("res://model.gd").Model.new()
@ -78,7 +84,6 @@ class ObjectManager:
model.magic = gbs_stream.get_32()
model.u1 = gbs_stream.get_32()
model.basepoints = gbs_stream.get_32()
print("basepoints="+str(model.basepoints))
model.basepoint = []
model.basepoint.resize(model.basepoints)
for s in range(model.basepoints):
@ -96,7 +101,6 @@ class ObjectManager:
model.points = gbs_stream.get_32()
model.point_1.resize(model.points)
print("points="+str(model.points))
if model.u1 == 7:
model.point_2.resize(model.points)
model.point_uv.resize(model.points)
@ -121,7 +125,6 @@ class ObjectManager:
model.point_c.append(b)
var i = gbs_stream.get_32()
print("i="+str(i))
model.ref1.resize(i)
for s in range(i):
model.ref1[s] = []
@ -133,12 +136,10 @@ class ObjectManager:
model.ref1[s][4] = gbs_stream.get_32()
model.parts = gbs_stream.get_32()
print("parts="+str(model.parts))
model.part.resize(model.parts)
for p in range(model.parts):
model.part[p] = modelitem_gd.ModelItem.new()
model.part[p].objname = gbs_stream.get_string(32)
print("Objname: "+model.part[p].objname)
model.part[p].objindex = gbs_stream.get_32()
model.part[p].refs = gbs_stream.get_32()
model.part[p].wordz = gbs_stream.get_32()
@ -152,8 +153,8 @@ class ObjectManager:
model.part[p].triangle[s][2] = gbs_stream.get_16()
model.part[p].refstart = gbs_stream.get_32()
model.part[p].refnum = gbs_stream.get_32()
model.part[p].texture = gbs_stream.get_string(32)
model.part[p].bumptexture = gbs_stream.get_string(32)
model.part[p].texture = gbs_stream.get_string(32).to_lower()
model.part[p].bumptexture = gbs_stream.get_string(32).to_lower()
model.part[p].falloff = gbs_stream.get_float()
model.part[p].blend = gbs_stream.get_float()
model.part[p].flags = gbs_stream.get_32()
@ -200,6 +201,8 @@ class ObjectManager:
if not tmpDirectory.dir_exists("res://tmp"):
tmpDirectory.make_dir("res://tmp")
var stream = read_file_in_gzp(texture_file)
if stream == null:
return null
var tmpFile = File.new()
tmpFile.open("res://tmp/"+texture_file, File.WRITE)
tmpFile.store_buffer(stream.data_array)
@ -209,7 +212,7 @@ class ObjectManager:
var tex = ImageTexture.new()
img.load("res://tmp/"+texture_file)
tex.create_from_image(img)
# tmpDirectory.remove("res://tmp/"+texture_file)
tmpDirectory.remove("res://tmp/"+texture_file)
return tex
func load_model_mesh(model) -> MeshInstance:
@ -220,13 +223,18 @@ class ObjectManager:
st.begin(Mesh.PRIMITIVE_TRIANGLES)
var mat = SpatialMaterial.new()
mat.params_cull_mode = SpatialMaterial.CULL_DISABLED
mat.flags_unshaded = true
if model.tex[i]:
mat.albedo_texture = model.tex[i]
if mat.albedo_texture.has_alpha():
# mat.flags_transparent = true
# mat.params_blend_mode = SpatialMaterial.BLEND_MODE_MIX
pass
else:
mat.vertex_color_use_as_albedo = true
st.set_material(mat)
assert(len(model.basepoint) % 3 == 0)
# assert(len(model.basepoint) % 3 == 0)
for j in range(len(model.part[i].triangle)):
for k in range(3):

View File

@ -16,7 +16,7 @@ _global_script_class_icons={
[application]
config/name="GiantsEdit"
run/main_scene="res://3d.tscn"
run/main_scene="res://start.tscn"
config/icon="res://icon.png"
[display]
@ -62,6 +62,11 @@ mouse_input={
"events": [ Object(InputEventKey,"resource_local_to_scene":false,"resource_name":"","device":0,"alt":false,"shift":true,"control":false,"meta":false,"command":false,"pressed":false,"scancode":16777244,"unicode":0,"echo":false,"script":null)
]
}
spawn_object={
"deadzone": 0.5,
"events": [ Object(InputEventMouseButton,"resource_local_to_scene":false,"resource_name":"","device":0,"alt":false,"shift":false,"control":false,"meta":false,"command":false,"button_mask":0,"position":Vector2( 0, 0 ),"global_position":Vector2( 0, 0 ),"factor":1.0,"button_index":1,"pressed":false,"doubleclick":false,"script":null)
]
}
[rendering]

25
start.tscn Normal file
View File

@ -0,0 +1,25 @@
[gd_scene load_steps=2 format=2]
[ext_resource path="res://FileDialog.gd" type="Script" id=1]
[node name="Control" type="Control"]
anchor_right = 1.0
anchor_bottom = 1.0
__meta__ = {
"_edit_use_anchors_": false
}
[node name="FileDialog" type="FileDialog" parent="."]
margin_right = 307.0
margin_bottom = 130.0
popup_exclusive = true
window_title = "Open Giants directory"
resizable = true
mode = 2
access = 2
current_dir = "/home/tasty/Jeux/GiantsEdit"
current_path = "/home/tasty/Jeux/GiantsEdit/"
script = ExtResource( 1 )
[connection signal="dir_selected" from="FileDialog" to="FileDialog" method="_on_FileDialog_dir_selected"]
[connection signal="popup_hide" from="FileDialog" to="FileDialog" method="_on_FileDialog_popup_hide"]
[connection signal="ready" from="FileDialog" to="FileDialog" method="_on_FileDialog_ready"]