# Duane's Dungeon - a rogue-like game # Copyright (C) 2023 Duane Robertson # state.gd - basic finite machine state # This program is free software: you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by # the Free Software Foundation, either version 3 of the License, or # (at your option) any later version. # This program is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more details. # You should have received a copy of the GNU General Public License # along with this program. If not, see . # States can stack by adding children. # If state has a child, it defers to the child. # If state has no child, it accepts input and updates itself. # still need to enable/disable controls # two types of input: player commands and buttons # All interface components belong to a state. extends Node2D class_name State var game:Node2D var sname := 'state' var stacked func _unhandled_input(event): if stacked: return input(event) # Only one state should be taking input. get_viewport().set_input_as_handled() func _ready(): game = get_node('/root/Game') func enter(): pass func exit(state=null): # A stacked state calls exit with no argument, to return. if state == null: get_parent().stacked = null else: ########################################### # This allows gui changes before creating a dungeon. # It might be better to create it in a thread, but # the dungeon isn't thread-safe. ########################################### await RenderingServer.frame_post_draw ########################################### get_parent().add_child(state.new()) get_tree().queue_delete(self) func input(_event): pass func stack(state): var new_state = state.new() add_child(new_state) stacked = new_state