refactor node structure
This commit is contained in:
parent
8189a16fee
commit
f35d27b518
11 changed files with 311 additions and 233 deletions
100
scripts/Main.gd
100
scripts/Main.gd
|
@ -1,107 +1,33 @@
|
|||
# OK - "Cube Clicker 2000" where you click a spot and a cube appears there.
|
||||
# OK - Then add different shapes or colors of cubes.
|
||||
# OK - Then clamp their positions to be on a grid.
|
||||
# - Then make it so that when you add yellow cubes, yellow desire meter goes down and green meter goes up.
|
||||
# - Then click a bunch of grey cubes in a row and have them automatically flatten out into flat grey cubes (roads).
|
||||
# - Then click and drag to draw the lines of grey cubes.
|
||||
# - etc.
|
||||
|
||||
|
||||
# https://github.com/dfloer/SC2k-docs
|
||||
|
||||
|
||||
class_name Main
|
||||
extends Node2D
|
||||
|
||||
signal worldgen_ready
|
||||
signal set_camera_position(pos:Vector2)
|
||||
|
||||
var start_new_game_pressed:bool = false
|
||||
var bus:EventBus
|
||||
|
||||
# The idea is for the user to be able to choose the map from GUI later
|
||||
var map_filenames:Array = [
|
||||
"res://maps/tampere_10x10km_1000px.png",
|
||||
"res://maps/tampere_10x10km_1024px.png",
|
||||
"res://maps/varkaus_256x256px_test.png",
|
||||
"res://maps/tampere_256px.png",
|
||||
"res://maps/tampere_10x10km_4096px.png"
|
||||
]
|
||||
var map_filename:String = map_filenames[3]
|
||||
|
||||
var _world_generator:WorldGenerator
|
||||
|
||||
func _init():
|
||||
func _init() -> void:
|
||||
# DisplayServer.window_set_size(
|
||||
# #Vector2i(Globals.DEFAULT_X_RES, Globals.DEFAULT_Y_RES)
|
||||
# Vector2i(3800,2000)
|
||||
# )
|
||||
Globals.CAMERA_POSITION = Vector2(16*256/2, 16*256/2)
|
||||
pass
|
||||
|
||||
|
||||
func start_new_game():
|
||||
# create a new world with worldgenerator
|
||||
_world_generator = WorldGenerator.new()
|
||||
if !_world_generator.generate_world(map_filename):
|
||||
push_error("World generation failed :-(")
|
||||
quit_game()
|
||||
|
||||
# connections are made from GUI
|
||||
|
||||
# tell other classes they can start working after loading is done
|
||||
emit_signal("worldgen_ready")
|
||||
|
||||
self.find_child("MainMenu").hide()
|
||||
self.find_child("Game").show()
|
||||
self.find_child("UILayer").show()
|
||||
|
||||
# center camera to world map
|
||||
emit_signal(
|
||||
"set_camera_position",
|
||||
Vector2(Globals.map_size / 2.0 * Globals.TILE_SIZE_X,
|
||||
Globals.map_size / 2.0 * Globals.TILE_SIZE_Y)
|
||||
)
|
||||
|
||||
func _on_mainmenu_button_pressed(button:int):
|
||||
match button:
|
||||
Globals.MAINMENU_NEW_GAME:
|
||||
start_new_game()
|
||||
self.find_child("Menu_NewGame").disabled = true
|
||||
self.find_child("Menu_ResumeGame").disabled = false
|
||||
Globals.MAINMENU_LOAD_GAME:
|
||||
pass
|
||||
Globals.MAINMENU_RESUME_GAME:
|
||||
# TODO save camera position before opening menu, restore camera position when closing menu
|
||||
self.find_child("Game").process_mode = PROCESS_MODE_INHERIT
|
||||
toggle_main_menu_visibility()
|
||||
Globals.MAINMENU_QUIT_GAME:
|
||||
quit_game()
|
||||
_:
|
||||
push_error("Error: Main: unknown signal at _on_mainmenu_button_pressed: ", button)
|
||||
|
||||
func _unhandled_input(event) -> void:
|
||||
if event.is_action_pressed("open_main_menu"):
|
||||
# move mainmenu to current game camera position
|
||||
var mainmenu_pos = Globals.CAMERA_POSITION
|
||||
mainmenu_pos.x -= DisplayServer.window_get_size(0).x/2
|
||||
mainmenu_pos.y -= DisplayServer.window_get_size(0).y/2
|
||||
self.find_child("MainMenu").position = mainmenu_pos
|
||||
|
||||
# show the menu
|
||||
toggle_main_menu_visibility()
|
||||
#await get_tree().create_timer(0.2).timeout
|
||||
self.find_child("Game").process_mode = PROCESS_MODE_DISABLED
|
||||
func _ready() -> void:
|
||||
pause_game()
|
||||
bus = find_child("EventBus")
|
||||
bus.set_camera_position(Vector2(16*256/2, 16*256/2))
|
||||
|
||||
|
||||
func toggle_main_menu_visibility():
|
||||
var items = [find_child("Game"), find_child("UILayer"), find_child("MainMenu")]
|
||||
func pause_game() -> void:
|
||||
get_tree().paused = true
|
||||
|
||||
|
||||
func unpause_game() -> void:
|
||||
get_tree().paused = false
|
||||
|
||||
for item in items:
|
||||
if !item:
|
||||
push_error("Error: While toggling main menu visibility. Missing UI element " + item)
|
||||
if item.visible:
|
||||
item.hide()
|
||||
continue
|
||||
item.show()
|
||||
|
||||
func quit_game():
|
||||
get_tree().get_root().propagate_notification(NOTIFICATION_WM_CLOSE_REQUEST)
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue