Commit f7961cf7 authored by Adam Gausmann's avatar Adam Gausmann
Browse files

Run map generator on a thread; add loading screen

parent 5cdc3aee
62676
61523
C:\Users\adam\Desktop\Godot_v3.2.3-stable_mono_win64\Godot_v3.2.3-stable_mono_win64.exe
......@@ -6,14 +6,9 @@
[ext_resource path="res://ActionManager.gd" type="Script" id=4]
[ext_resource path="res://actors/Enemy.tscn" type="PackedScene" id=5]
[ext_resource path="res://ActorManager.gd" type="Script" id=6]
[ext_resource path="res://assets/MaredivRegular-yeg3.ttf" type="DynamicFontData" id=7]
[sub_resource type="DynamicFont" id=1]
size = 22
font_data = ExtResource( 7 )
[sub_resource type="Theme" id=2]
default_font = SubResource( 1 )
[ext_resource path="res://src/main_theme.tres" type="Theme" id=7]
[ext_resource path="res://assets/wolf.png" type="Texture" id=8]
[ext_resource path="res://src/Indicator.gd" type="Script" id=9]
[sub_resource type="StyleBoxFlat" id=3]
bg_color = Color( 0.141176, 0.0627451, 0.160784, 1 )
......@@ -46,7 +41,7 @@ position = Vector2( 512, 272 )
anchor_top = 1.0
anchor_bottom = 1.0
grow_vertical = 0
theme = SubResource( 2 )
theme = ExtResource( 7 )
__meta__ = {
"_edit_use_anchors_": false
}
......@@ -89,3 +84,49 @@ margin_right = 586.0
margin_bottom = 30.0
custom_colors/font_color = Color( 0.752941, 0.498039, 0, 1 )
text = "HIGH 1"
[node name="LoadingScreen" type="PanelContainer" parent="GUI"]
pause_mode = 2
anchor_right = 1.0
anchor_bottom = 1.0
theme = ExtResource( 7 )
__meta__ = {
"_edit_use_anchors_": false
}
[node name="CenterContainer" type="CenterContainer" parent="GUI/LoadingScreen"]
margin_left = 7.0
margin_top = 7.0
margin_right = 1017.0
margin_bottom = 593.0
[node name="VBoxContainer" type="VBoxContainer" parent="GUI/LoadingScreen/CenterContainer"]
margin_left = 415.0
margin_top = 260.0
margin_right = 595.0
margin_bottom = 326.0
[node name="LoadingText" type="Label" parent="GUI/LoadingScreen/CenterContainer/VBoxContainer"]
margin_right = 180.0
margin_bottom = 30.0
rect_min_size = Vector2( 150, 0 )
custom_colors/font_color = Color( 0.8, 0.8, 0.8, 1 )
text = "Loading Level..."
[node name="Indicator" type="HBoxContainer" parent="GUI/LoadingScreen/CenterContainer/VBoxContainer"]
margin_top = 34.0
margin_right = 180.0
margin_bottom = 66.0
rect_min_size = Vector2( 180, 0 )
script = ExtResource( 9 )
[node name="Padding" type="Control" parent="GUI/LoadingScreen/CenterContainer/VBoxContainer/Indicator"]
margin_bottom = 32.0
[node name="Wolf" type="TextureRect" parent="GUI/LoadingScreen/CenterContainer/VBoxContainer/Indicator"]
margin_left = 4.0
margin_right = 36.0
margin_bottom = 32.0
rect_min_size = Vector2( 32, 32 )
texture = ExtResource( 8 )
stretch_mode = 1
......@@ -170,7 +170,7 @@ func _ready():
#Add enitites to list
refresh()
tick_loop()
#tick_loop()
return
......
extends HBoxContainer
var time = 0.0
const STEP_INTERVAL = 0.2
const STEPS = 5
# Called when the node enters the scene tree for the first time.
func _ready():
pass
# Called every frame. 'delta' is the elapsed time since the previous frame.
func _process(delta):
time += delta
var step_index = int(time / STEP_INTERVAL)
var step_mod = step_index % (2 * STEPS)
var flipped = step_mod >= STEPS
if flipped:
step_mod = (2 * STEPS) - step_mod - 1
var wolf = get_node("./Wolf")
var padding = get_node("./Padding")
padding.rect_min_size.x = wolf.rect_size.x * step_mod
wolf.flip_h = flipped
......@@ -91,7 +91,6 @@ const FILLED = [ [WALL ,WALL ,WALL ],
# When referencing an index in the grid, use [y][x] format
var grid = []
func _init():
pass
......@@ -479,15 +478,16 @@ func spawn_chests():
grid[spawn.y][spawn.x].elligible_hole = false
# Generates a new map
func generate_map(fill_percent, width, height):
func generate_map(fill_percent, width, height, mutex):
rooms.clear()
enemy_spawns.clear()
print("init")
initialize_grid(fill_percent, width, height)
for i in range(0, 3):
smooth_grid(width, height)
replace_invalids(width, height)
assign_rooms(width, height)
......
......@@ -8,6 +8,12 @@ const BOULDER_PERCENT = 0.80
var drawer = DRAWER.new()
var lines = []
# A cross-thread variable, protected by the mutex, used to query if the
# map update is still running.
var update_running = false
var mutex = Mutex.new()
var update_thread = null
# Declare member variables here
export(int, 100) var fill_percent = 45
export var width = 50
......@@ -233,12 +239,30 @@ func update_tile(x, y):
# Generate a new map and update the level map on screen accordingly
func update_map():
# Pause all updates
get_tree().paused = true
mutex.lock()
var already_running = update_running
update_running = true
mutex.unlock()
if already_running:
return
update_thread = Thread.new()
var err = update_thread.start(self, "_update_map_inner")
if err != OK:
print("ERROR: cannot start update thread, invoking manually")
_update_map_inner(null)
func _update_map_inner(userdata):
if !lines.empty():
for line in lines:
if line != null:
line.free()
map_gen.generate_map(fill_percent, width, height)
map_gen.generate_map(fill_percent, width, height, mutex)
# Make sure enemies do not spawn inside a collidable tile
for coord in map_gen.enemy_spawns:
break_obstacle(coord.x, coord.y)
......@@ -248,11 +272,13 @@ func update_map():
var tele_direction = Vector2(spawn_coord.x - player.get_coord(player.position).x, player.get_coord(player.position).y - spawn_coord.y)
player.teleport(tele_direction.x, tele_direction.y)
for x in width:
for y in height:
update_tile(x, y)
update_dirty_quadrants()
# This has issues running in a thread, so deferring it to _process()
# (this stuff is a lot less heavy than the rest of the map gen code, so
# it doesn't cause much delay/freezing.)
#for x in width:
# for y in height:
# update_tile(x, y)
#update_dirty_quadrants()
# Place visual representations of the lines in the minimum tree
# for line in map_gen.minimum_tree:
......@@ -263,6 +289,10 @@ func update_map():
# lines.back().add_point(Vector2(line[0].x * 16, line[0].y * 16))
# lines.back().add_point(Vector2(line[1].x * 16, line[1].y * 16))
# add_child(lines.back())
mutex.lock()
update_running = false
mutex.unlock()
# Update the collidable tile at the given coord to a non-collidable version
func break_obstacle(x, y):
......@@ -302,6 +332,7 @@ func break_obstacle(x, y):
# Called when the node enters the scene tree for the first time.
func _ready():
pause_mode = Node.PAUSE_MODE_PROCESS
#update_map()
pass
......@@ -311,4 +342,21 @@ func _unhandled_input(event):
update_map()
func _process(delta):
pass
mutex.lock()
var loading = update_running
mutex.unlock()
get_node("/root/Main/GUI/LoadingScreen").visible = loading
if not loading and update_thread != null:
update_thread.wait_to_finish()
update_thread = null
# Deferred update from _update_map_inner
for x in width:
for y in height:
update_tile(x, y)
update_dirty_quadrants()
# Done loading, unpause
get_tree().paused = false
[gd_resource type="Theme" load_steps=3 format=2]
[ext_resource path="res://assets/MaredivRegular-yeg3.ttf" type="DynamicFontData" id=1]
[sub_resource type="DynamicFont" id=1]
size = 22
font_data = ExtResource( 1 )
[resource]
default_font = SubResource( 1 )
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment