Skip to content

Commit

Permalink
Add support for multiple world types
Browse files Browse the repository at this point in the history
  • Loading branch information
Janorico committed May 20, 2023
1 parent ae3de05 commit 331a44b
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 12 deletions.
38 changes: 33 additions & 5 deletions scenes/startup.tscn
Original file line number Diff line number Diff line change
Expand Up @@ -219,17 +219,45 @@ texture_pressed = ExtResource( 4 )
texture_hover = ExtResource( 3 )

[node name="NewWorldDialog" type="ConfirmationDialog" parent="."]
margin_right = 356.0
margin_bottom = 74.0
margin_right = 280.0
margin_bottom = 116.0
window_title = "New World"

[node name="NameLineEdit" type="LineEdit" parent="NewWorldDialog"]
[node name="GridContainer" type="GridContainer" parent="NewWorldDialog"]
margin_left = 8.0
margin_top = 8.0
margin_right = 348.0
margin_bottom = 40.0
margin_right = 297.0
margin_bottom = 72.0
columns = 2

[node name="NameLabel" type="Label" parent="NewWorldDialog/GridContainer"]
margin_top = 5.0
margin_right = 60.0
margin_bottom = 27.0
text = "Name: "

[node name="NameLineEdit" type="LineEdit" parent="NewWorldDialog/GridContainer"]
margin_left = 64.0
margin_right = 289.0
margin_bottom = 32.0
rect_min_size = Vector2( 225, 0 )
placeholder_text = "Type a cool name..."

[node name="TypeLabel" type="Label" parent="NewWorldDialog/GridContainer"]
margin_top = 39.0
margin_right = 60.0
margin_bottom = 61.0
text = "Type: "

[node name="TypeOptionButton" type="OptionButton" parent="NewWorldDialog/GridContainer"]
margin_left = 64.0
margin_top = 36.0
margin_right = 289.0
margin_bottom = 64.0
text = "Forest"
items = [ "Forest", null, false, 0, null, "Desert", null, false, 1, null, "Boreal", null, false, 2, null ]
selected = 0

[node name="DeleteConfirmation" type="ConfirmationDialog" parent="."]
margin_right = 218.0
margin_bottom = 74.0
Expand Down
14 changes: 9 additions & 5 deletions scripts/Startup.gd
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@ const MAIN_SCENE: PackedScene = preload("res://scenes/game/main.tscn")
const WORLDS_DIR: String = "user://worlds/"
onready var worlds_list: ItemList = $MarginContainer/Page1/WorldsList
onready var message_label: Label = $Message/Label
onready var new_world_name: LineEdit = $NewWorldDialog/NameLineEdit
onready var new_world_name: LineEdit = $NewWorldDialog/GridContainer/NameLineEdit
onready var new_world_type: OptionButton = $NewWorldDialog/GridContainer/TypeOptionButton
onready var delete_confirmation: ConfirmationDialog = $DeleteConfirmation
onready var animation_player: AnimationPlayer = $AnimationPlayer
export var map_icon: Texture = preload("res://assets/icons/icons8-world-map-96.png")
Expand Down Expand Up @@ -46,9 +47,11 @@ func get_dir() -> Directory:
return dir


func open_world(world: String):
func open_world(world: String, type):
var main = MAIN_SCENE.instance()
main.get_node("Game").initial_world_path = get_world_path(world)
var game_child = main.get_node("Game")
game_child.initial_world_path = get_world_path(world)
game_child.initial_world_type = type
get_parent().add_child(main)
queue_free()
get_tree().current_scene = main
Expand All @@ -65,16 +68,17 @@ func show_message(msg: String):

func _on_new_world_dialog_confirmed():
var wname = new_world_name.text
var wtype = new_world_type.text
if wname == "":
show_message("Name is empty!")
else:
open_world(wname)
open_world(wname, wtype)


func _on_open_button_pressed():
var selected_items = worlds_list.get_selected_items()
if selected_items.size() > 0:
open_world(worlds[selected_items[0]])
open_world(worlds[selected_items[0]], null)
else:
show_message("No selection!")

Expand Down
6 changes: 5 additions & 1 deletion scripts/game/Chunk.gd
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ var rng = RandomNumberGenerator.new()
# Make this load from a file
const texture_atlas_size = Vector2(8, 4)

var generator = load("res://scripts/game/chunk_generators/ForestGenerator.gd")
var generator

const v = [
Vector3(0, 0, 0), #0
Expand Down Expand Up @@ -64,6 +64,10 @@ var st = SurfaceTool.new()
var cst = SurfaceTool.new()


func _init(world_type: String):
generator = load("res://scripts/game/chunk_generators/%sGenerator.gd" % world_type)


func _ready():
mat.albedo_texture.set_flags(2)

Expand Down
2 changes: 1 addition & 1 deletion scripts/game/ProcWorld.gd
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ func change_block(cx, cz, bx, by, bz, t):
func _load_chunk(cx, cz):
var c_pos = Vector2(cx, cz)
if not _loaded_chunks.has(c_pos):
var c = Chunk.new()
var c = Chunk.new(world_data.get("type", "Forest"))
c.generate(self, cx, cz)
c.update()
add_child(c)
Expand Down
3 changes: 3 additions & 0 deletions scripts/game/WorldScript.gd
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
extends Spatial

var initial_world_path: String = "user://worlds/default"
var initial_world_type = null
var pw
onready var player = $Player
onready var block_outline = $BlockOutline
Expand All @@ -14,6 +15,8 @@ var chunk_pos = Vector2()
func _ready():
print("CREATING WORLD")
pw = ProcWorld.new(initial_world_path)
if initial_world_type != null:
pw.world_data["type"] = initial_world_type
add_child(pw)
# warning-ignore:return_value_discarded
self.connect("tree_exiting", self, "_on_tree_exiting")
Expand Down

0 comments on commit 331a44b

Please sign in to comment.