diff options
author | jacopograndi <jacopo.grandi@outlook.it> | 2022-01-16 17:14:36 +0100 |
---|---|---|
committer | jacopograndi <jacopo.grandi@outlook.it> | 2022-01-16 17:14:36 +0100 |
commit | 5781b7dad68c3e01d83365f73b0d35d9e5252f97 (patch) | |
tree | dada65ab375ed8287b33e6ffe023f5df5e666d24 /scripts | |
parent | 8e5358aaefa1df729ffc02047e53b62705d6b3b1 (diff) |
control system
Diffstat (limited to 'scripts')
-rw-r--r-- | scripts/bullet.gd | 2 | ||||
-rw-r--r-- | scripts/control.gd | 153 | ||||
-rw-r--r-- | scripts/enemies.gd | 2 | ||||
-rw-r--r-- | scripts/globals.gd | 24 | ||||
-rw-r--r-- | scripts/gui.gd | 6 | ||||
-rw-r--r-- | scripts/gui_bottom_bar.gd | 56 | ||||
-rw-r--r-- | scripts/gui_picker.gd | 67 | ||||
-rw-r--r-- | scripts/gui_top_bar.gd | 11 | ||||
-rw-r--r-- | scripts/gui_turret.gd | 26 | ||||
-rw-r--r-- | scripts/gui_turret_detail.gd | 37 | ||||
-rw-r--r-- | scripts/load_map.gd | 4 | ||||
-rw-r--r-- | scripts/load_shapes.gd | 6 | ||||
-rw-r--r-- | scripts/load_turrets.gd | 8 | ||||
-rw-r--r-- | scripts/movement.gd | 62 | ||||
-rw-r--r-- | scripts/path.gd | 8 | ||||
-rw-r--r-- | scripts/placer.gd | 144 | ||||
-rw-r--r-- | scripts/pointer.gd | 28 | ||||
-rw-r--r-- | scripts/turret.gd | 2 |
18 files changed, 436 insertions, 210 deletions
diff --git a/scripts/bullet.gd b/scripts/bullet.gd index 3716a4e..2a5df41 100644 --- a/scripts/bullet.gd +++ b/scripts/bullet.gd @@ -13,7 +13,7 @@ var bounce = false var hitlist = [] func _ready(): - _enemies_holder = get_tree().root.get_child(0).find_node("enemies") + _enemies_holder = get_tree().root.get_node("world").find_node("enemies") var _err = $Area.connect("body_entered", self, "collided") func _physics_process(delta): diff --git a/scripts/control.gd b/scripts/control.gd new file mode 100644 index 0000000..2e31667 --- /dev/null +++ b/scripts/control.gd @@ -0,0 +1,153 @@ +extends Node + +var ineditor = false +var state = Globals.PlayerState.PICK +var statetype = Globals.StateType.TURRET +var selected = "" +var editing_turret = "" + +var gui : Node +var pointer : Node +var turret_holder : Node +var load_turrets : Node +var path : Node +var world : VoxelMesh + +func fetch (): + if load_turrets != null: return + var root = get_tree().root.get_node("world") + gui = root.get_node("gui") + pointer = root.get_node("pointer") + world = root.get_node("world") + path = root.get_node("path") + turret_holder = root.get_node("turrets") + var saveload = root.get_node("saveload") + load_turrets = saveload.get_node("load_turrets") + if !load_turrets.loaded: yield(load_turrets, "done_loading") + +func _ready (): + fetch() + _refresh() + build_option (state, statetype) + +func build_option (st, sttype): + var opts = [] + match st: + Globals.PlayerState.PICK, Globals.PlayerState.PLACE: + match sttype: + Globals.StateType.TURRET: + for t in load_turrets.get_base_turrets(): + opts += [ { "type": "turret buy", "name": t.name } ] + Globals.StateType.ATTACH: + opts += [ { "type": "text", "name": "attach" } ] + Globals.StateType.PATH: + opts += [ { "type": "text", "name": "start path" } ] + opts += [ { "type": "text", "name": "path" } ] + opts += [ { "type": "text", "name": "end path" } ] + Globals.StateType.VOXEL: + for i in world.voxel_set.size(): + var details = world.voxel_set.get_voxel(i) + var color = Color(1, 0, 1) + if details.has("color"): + color = details.color + opts += [ { "type": "color", "name": i, "color": color} ] + + Globals.PlayerState.EDIT: + match sttype: + Globals.StateType.TURRET: + var tname = turret_holder.get_node(editing_turret).info.name + for t in load_turrets.get_upg_turrets(tname): + opts += [ { "type": "turret upg", "name": t.name } ] + opts += [ { "type": "text", "name": "sell" } ] + opts += [ { "type": "text", "name": "priority" } ] + opts += [ { "type": "text", "name": "modules" } ] + opts += [ { "type": "text", "name": "back" } ] + + gui.bottom_bar.picker.build(opts) + +func _refresh (): + gui.refresh(ineditor) + pointer.refresh(state, statetype, selected) + +func to_pick (): + selected = "" + editing_turret = "" + state = Globals.PlayerState.PICK + build_option(state, statetype) + +func do (action, par = {}): + print(str(action) + " " + str(par)) + fetch() + match state: + Globals.PlayerState.PICK: + match action: + Globals.PlayerActions.PICK: + selected = par.selected + state = Globals.PlayerState.PLACE + + Globals.PlayerActions.SELECT: + match statetype: + Globals.StateType.TURRET: + selected = "" + editing_turret = par.selected + state = Globals.PlayerState.EDIT + build_option(state, statetype) + _: to_pick() + + Globals.PlayerActions.CHANGE_TYPE: + selected = "" + editing_turret = "" + state = Globals.PlayerState.PICK + statetype = par.statetype + build_option(state, statetype) + + Globals.PlayerState.PLACE: + match action: + Globals.PlayerActions.PLACE: + match statetype: + Globals.StateType.TURRET: + editing_turret = par.placed + state = Globals.PlayerState.EDIT + build_option(state, statetype) + + Globals.PlayerActions.DELETE: pass + Globals.PlayerActions.CANCEL: to_pick() + + Globals.PlayerActions.CHANGE_TYPE: + selected = "" + editing_turret = "" + state = Globals.PlayerState.PICK + statetype = par.statetype + build_option(state, statetype) + + Globals.PlayerState.EDIT: + match action: + Globals.PlayerActions.PICK: + match statetype: + Globals.StateType.TURRET: + selected = par.selected + match par.selected: + "back": + to_pick() + _: to_pick() + + Globals.PlayerActions.CANCEL: to_pick() + + Globals.PlayerActions.CHANGE_TYPE: + selected = "" + editing_turret = "" + state = Globals.PlayerState.PICK + statetype = par.statetype + build_option(state, statetype) + _refresh() + + +func gui_editor_toggle_event (): + ineditor = !ineditor + gui.refresh(ineditor) + path.refresh_path(ineditor) + +func gui_start_wave_event (): + path.refresh_path(ineditor) + #_enemies.spawn() + gui.refresh(ineditor) diff --git a/scripts/enemies.gd b/scripts/enemies.gd index 37c1f6b..62c64ea 100644 --- a/scripts/enemies.gd +++ b/scripts/enemies.gd @@ -30,7 +30,7 @@ var colors = [ func _ready(): - var root = get_tree().root.get_child(0) + var root = get_tree().root.get_node("world") _fx_holder = root.get_node("fx") _path = root.get_node("path") _resources = root.get_node("player").get_node("resources") diff --git a/scripts/globals.gd b/scripts/globals.gd new file mode 100644 index 0000000..8c66f03 --- /dev/null +++ b/scripts/globals.gd @@ -0,0 +1,24 @@ +extends Node + +enum PlayerState { + PICK, + PLACE, + EDIT +} + +enum StateType { + TURRET, + ATTACH, + PATH, + VOXEL, + VOXEL_PALETTE +} + +enum PlayerActions { + CHANGE_TYPE, + PICK, + PLACE, + SELECT, + DELETE, + CANCEL +} diff --git a/scripts/gui.gd b/scripts/gui.gd index 03a2e04..71eaf76 100644 --- a/scripts/gui.gd +++ b/scripts/gui.gd @@ -1,21 +1,21 @@ extends Control var player : Node -var _in_editor : bool +var control : Node var bottom_bar : Control var top_bar : Control func _fetch (): - var root = get_tree().root.get_child(0) + var root = get_tree().root.get_node("world") player = root.get_node("player") + control = player.get_node("control") if bottom_bar == null: bottom_bar = get_node("bottom_bar") if top_bar == null: top_bar = get_node("top_bar") func _ready(): _fetch() - bottom_bar.build() func refresh (in_editor : bool): _fetch() diff --git a/scripts/gui_bottom_bar.gd b/scripts/gui_bottom_bar.gd index 0a7b7b8..bd63a51 100644 --- a/scripts/gui_bottom_bar.gd +++ b/scripts/gui_bottom_bar.gd @@ -1,41 +1,41 @@ extends Control -var _in_editor : bool -var voxel_picker : Control -var map_tool_picker : Control -var turret_picker : Control - -var _load_turrets : Node +var editor_bar : Control +var picker : Control var gui : Control func _fetch (): if gui == null: gui = get_parent() - if voxel_picker == null: voxel_picker = $voxels - if map_tool_picker == null: map_tool_picker = $map_tools - if turret_picker == null: turret_picker = $turrets - - var root = get_tree().root.get_child(0) - if _load_turrets == null: - _load_turrets = root.get_node("saveload").get_node("load_turrets") - -func build (): - _fetch() - turret_picker.build(_load_turrets.get_base_turrets()) - voxel_picker.build() - map_tool_picker.build() + if editor_bar == null: editor_bar = $editor_bar + if picker == null: picker = $picker func refresh (in_editor : bool): _fetch() - _in_editor = in_editor - if _in_editor: - voxel_picker.visible = true - map_tool_picker.visible = true + if in_editor: + editor_bar.visible = true else: - voxel_picker.visible = false - map_tool_picker.visible = false + editor_bar.visible = false - turret_picker.refresh(gui.player.sel) - voxel_picker.refresh(gui.player.sel) - map_tool_picker.refresh(gui.player.sel) + picker.refresh() + +func _on_turrets_button_up(): + gui.control.do(Globals.PlayerActions.CHANGE_TYPE, + { "statetype": Globals.StateType.TURRET }) + +func _on_path_button_up(): + gui.control.do(Globals.PlayerActions.CHANGE_TYPE, + { "statetype": Globals.StateType.PATH }) + +func _on_attach_button_up(): + gui.control.do(Globals.PlayerActions.CHANGE_TYPE, + { "statetype": Globals.StateType.ATTACH }) + +func _on_voxel_button_up(): + gui.control.do(Globals.PlayerActions.CHANGE_TYPE, + { "statetype": Globals.StateType.VOXEL }) + +func _on_palette_button_up(): + gui.control.do(Globals.PlayerActions.CHANGE_TYPE, + { "statetype": Globals.StateType.VOXEL_PALETTE }) diff --git a/scripts/gui_picker.gd b/scripts/gui_picker.gd index 21f6b4c..7a20842 100644 --- a/scripts/gui_picker.gd +++ b/scripts/gui_picker.gd @@ -1,7 +1,8 @@ extends Panel var _hbox : HBoxContainer -var _gui_button : Resource = load("res://scenes/gui/gui_turret.tscn") +var _gui_button : Resource = load("res://scenes/gui/gui_button.tscn") +var _gui_turret_button : Resource = load("res://scenes/gui/gui_turret.tscn") var _options = [] @@ -15,37 +16,57 @@ func _fetch (): func build (options : Array = []): _fetch() + hovering = "" - if options.size() > 0: - for n in _hbox.get_children(): n.queue_free() + for child in _hbox.get_children(): + child.disconnect("mouse_entered", self, "_on_gui_turret_mouse_entered") + child.disconnect("mouse_exited", self, "_on_gui_turret_mouse_exited") + child.disconnect("pressed", self, "_on_gui_turret_pressed") + child.queue_free() - _options = options - for t in _options: - var gt : TextureButton = _gui_button.instance() - gt.name = t.name - _hbox.add_child(gt) - gt.init(t) + _options = options + for opt in _options: + var button = null + if opt.type == "turret buy": + button = _gui_turret_button.instance() + _hbox.add_child(button) + button.init(opt.name) + if opt.type == "turret upg": + button = _gui_turret_button.instance() + _hbox.add_child(button) + button.init(opt.name) + if opt.type == "text": + button = _gui_button.instance() + _hbox.add_child(button) + button.get_node("hbox").get_node("name_label").text = opt.name + if opt.type == "color": + button = _gui_button.instance() + _hbox.add_child(button) + button.get_node("hbox").get_node("name_label").text = "" + button.get_node("color").color = opt.color + + if button != null: + button.option = opt.name + else: print("no option for " + str(opt)) for child in _hbox.get_children(): - if child.get_signal_connection_list("pressed").size() == 0: - child.connect("mouse_entered", self, "_on_gui_turret_mouse_entered", [child.name]) - child.connect("mouse_exited", self, "_on_gui_turret_mouse_exited", [child.name]) - child.connect("pressed", self, "_on_gui_turret_pressed", [child.name]) + child.connect("mouse_entered", self, "_on_gui_turret_mouse_entered", [child.option]) + child.connect("mouse_exited", self, "_on_gui_turret_mouse_exited", [child.option]) + child.connect("pressed", self, "_on_gui_turret_pressed", [child.option]) -func refresh (sel): +func refresh (): _fetch() - if sel.type == "idle": + if gui.control.state == Globals.PlayerState.PICK: for child in _hbox.get_children(): child.picked = false - if sel.type == name: + if gui.control.selected == name: for child in _hbox.get_children(): - child.picked = child.name == sel.name + child.picked = child.name == gui.control.selected -func _on_gui_turret_mouse_entered(name : String): - hovering = name; +func _on_gui_turret_mouse_entered(option : String): + hovering = option; -func _on_gui_turret_mouse_exited(name : String): +func _on_gui_turret_mouse_exited(option : String): hovering = "" -func _on_gui_turret_pressed(name : String): - print(self.name + ' ' + name) - gui.player.selected_event(name, self.name) +func _on_gui_turret_pressed(option : String): + gui.control.do(Globals.PlayerActions.PICK, { "selected": option }) diff --git a/scripts/gui_top_bar.gd b/scripts/gui_top_bar.gd index a002e87..6c086ce 100644 --- a/scripts/gui_top_bar.gd +++ b/scripts/gui_top_bar.gd @@ -1,6 +1,5 @@ extends Control -var _in_editor : bool var _editor_button : Button var _hbox : HBoxContainer @@ -8,16 +7,14 @@ var _resources : Node var _gui : Control func refresh (in_editor : bool): - _in_editor = in_editor - - var root = get_tree().root.get_child(0) + var root = get_tree().root.get_node("world") _resources = root.get_node("player").get_node("resources") if _gui == null: _gui = get_parent() if _hbox == null: _hbox = $panel/resources/HBoxContainer if _editor_button == null: _editor_button = $panel/editor_button - if _in_editor: _editor_button.text = "Editor" + if in_editor: _editor_button.text = "Editor" else: _editor_button.text = "Playmode" func _process (_delta): @@ -25,7 +22,7 @@ func _process (_delta): _hbox.get_node(r).text = str(_resources[r]) + r func _on_editor_button_down(): - _gui.player.gui_editor_toggle_event() + _gui.control.gui_editor_toggle_event() func _on_wave_button_pressed(): - _gui.player.gui_start_wave_event() + _gui.control.gui_start_wave_event() diff --git a/scripts/gui_turret.gd b/scripts/gui_turret.gd index 9faeb7a..55e3f5a 100644 --- a/scripts/gui_turret.gd +++ b/scripts/gui_turret.gd @@ -1,6 +1,8 @@ extends TextureButton -var _turret : Dictionary +var option : String + +var info : Dictionary var _color_rect : ColorRect var _name_label : Label @@ -17,23 +19,25 @@ var enabled : bool = false var _rotate_timer : float -func init (turret : Dictionary): - _turret = turret +func init (turret_name : String): + var root = get_tree().root.get_node("world") + var saveload = root.get_node("saveload") + var load_turrets = saveload.get_node("load_turrets") + if !load_turrets.loaded: yield(load_turrets, "done_loading") + info = load_turrets.info[turret_name] + _color_rect = $ColorRect _name_label = $hbox/name_label _cash_label = $cash_label _viewport = $viewport _spinner = $viewport/spinner - var root = get_tree().root.get_child(0) _resources = root.get_node("player").get_node("resources") - _name_label.text = turret.name - _cash_label.text = _resources.dict_to_str(turret.cost) + _name_label.text = turret_name + _cash_label.text = _resources.dict_to_str(info.cost) - var load_turrets = root.get_node("saveload").get_node("load_turrets") - if !load_turrets.loaded: yield(load_turrets, "done_loading") - var model = load_turrets.models[turret.model_name] + var model = load_turrets.models[info.model_name] _mesh = model.instance() _spinner.add_child(_mesh) @@ -44,10 +48,10 @@ func _process(delta): if _spinner == null: _spinner = $viewport/spinner if _spinner != null: if _resources == null: - var root = get_tree().root.get_child(0) + var root = get_tree().root.get_node("world") _resources = root.get_node("player").get_node("resources") - var afforded = _resources.greater_than(_turret.cost) + var afforded = _resources.greater_than(info.cost) if afforded: _color_rect.color = Color(0,0,0,0) else: _color_rect.color = Color(0,0,0,0.5) diff --git a/scripts/gui_turret_detail.gd b/scripts/gui_turret_detail.gd index df89c14..f895a6e 100644 --- a/scripts/gui_turret_detail.gd +++ b/scripts/gui_turret_detail.gd @@ -2,6 +2,7 @@ extends Panel var hbox_labels var hbox_global +var _turret_holder : Node var _turret_values : Resource = load("res://scenes/gui/gui_turret_values.tscn") var load_turrets : Node @@ -9,11 +10,14 @@ var gui : Control func _fetch (): if gui == null: gui = get_parent() - if hbox_labels != null: return; + if load_turrets != null: return; + + var root = get_tree().root.get_node("world") + _turret_holder = root.get_node("turrets") + hbox_labels = $"hbox_global/hbox_labels" hbox_global = $"hbox_global" - var root = get_tree().root.get_child(0) load_turrets = root.get_node("saveload").get_node("load_turrets") if !load_turrets.loaded: yield(load_turrets, "done_loading") @@ -46,12 +50,29 @@ func _process(delta): var info = null; - var hovering = gui.bottom_bar.turret_picker.hovering - if gui.player.highlight != null: - info = gui.player.highlight.info - elif gui.player.sel.type == "turrets": - info = load_turrets.info[gui.player.sel.name] - elif hovering != "": + var hovering = null + if gui.control.state == Globals.PlayerState.PICK and \ + gui.control.statetype == Globals.StateType.TURRET and \ + gui.bottom_bar.picker.hovering != "": + hovering = gui.bottom_bar.picker.hovering + + var highlight = null + if gui.control.state == Globals.PlayerState.EDIT and \ + gui.control.statetype == Globals.StateType.TURRET: + var turret_name = gui.control.editing_turret + highlight = _turret_holder.get_node(turret_name) + + var placing = null + if gui.control.state == Globals.PlayerState.PLACE and \ + gui.control.statetype == Globals.StateType.TURRET and \ + gui.control.selected != "": + placing = gui.control.selected + + if highlight != null: + info = highlight.info + elif placing != null: + info = load_turrets.info[placing] + elif hovering != null: info = load_turrets.info[hovering] elif gui.player.placer.colliding \ and "turrets" in gui.player.placer.colliding_group: diff --git a/scripts/load_map.gd b/scripts/load_map.gd index 8130997..1e4405c 100644 --- a/scripts/load_map.gd +++ b/scripts/load_map.gd @@ -10,7 +10,7 @@ func _ready(): map_load() func fetch (): - var root = get_tree().root.get_child(0) + var root = get_tree().root.get_node("world") if _world == null: _world = root.get_node("world") if _path_holder == null: @@ -105,6 +105,8 @@ func map_load(): set_map_state(state) print("loaded") + _path_holder.refresh_path(false) + func _process(_delta): if Input.is_action_just_released("save"): diff --git a/scripts/load_shapes.gd b/scripts/load_shapes.gd index f3de4ae..308f111 100644 --- a/scripts/load_shapes.gd +++ b/scripts/load_shapes.gd @@ -1,11 +1,13 @@ extends Node -var loaded = false var saveload : Node var models : Dictionary + +var loaded = false +signal done_loading func get_saveload(): - if saveload == null: saveload = get_tree().root.get_child(0).get_node("saveload") + if saveload == null: saveload = get_tree().root.get_node("world").get_node("saveload") func _ready(): get_saveload() diff --git a/scripts/load_turrets.gd b/scripts/load_turrets.gd index 2b96652..ddb7800 100644 --- a/scripts/load_turrets.gd +++ b/scripts/load_turrets.gd @@ -17,7 +17,13 @@ func _ready(): loaded = true func get_saveload(): - if saveload == null: saveload = get_tree().root.get_child(0).get_node("saveload") + if saveload == null: saveload = get_tree().root.get_node("world").get_node("saveload") + +func get_upg_turrets(turret_name): + var results = [] + for turr in info[turret_name].get("upgrades", []): + results += [info[turr]] + return results func get_base_turrets(): var results = [] diff --git a/scripts/movement.gd b/scripts/movement.gd index 3247a2f..51deffd 100644 --- a/scripts/movement.gd +++ b/scripts/movement.gd @@ -1,7 +1,5 @@ extends KinematicBody -var in_editor : bool = false - var vel = Vector3() var camera = Camera @@ -15,78 +13,22 @@ var _pivot var _pivot_dist var _orbit_timer -var _world : VoxelMesh - -var _gui : Control - var _turret_holder : Node var _attach_point_holder : Node var path_holder : Node var load_turrets : Node -var ptr : Node var _enemies : Node -var sel = { - "type": "idle", - "name": "" -} - -var highlight : Spatial - func _ready(): camera = $camera placer = $placer - var root = get_tree().root.get_child(0) + var root = get_tree().root.get_node("world") path_holder = root.get_node("path") - _gui = root.get_node("gui") _enemies = root.get_node("enemies") - ptr = root.find_node("pointer"); - - var saveload = root.get_node("saveload") - load_turrets = saveload.get_node("load_turrets") - - _gui.refresh(in_editor) - - refresh_path() - -func refresh_gui(): - _gui.refresh(in_editor) - -func gui_editor_toggle_event (): - in_editor = !in_editor - _gui.refresh(in_editor) - refresh_path() - -func gui_start_wave_event (): - refresh_path() - _enemies.spawn() - _gui.refresh(in_editor) - -func refresh_path(): - if in_editor: - path_holder.show() - else: - var _res = path_holder.load_nodes() - path_holder.hide() - -func selected_event (sel_name, sel_type): - sel.name = sel_name - sel.type = sel_type - _gui.refresh(in_editor) - - if sel_type == "turrets": - for child in ptr.get_children(): - if child.name != "base": child.queue_free(); - var info = load_turrets.info[sel.name] - var model = load_turrets.models[info.model_name] - var instance_model = model.instance() - instance_model.name = "preview" - ptr.add_child(instance_model) - elif sel_type != "": - ptr.get_node("base").visible = true + func look_free (m): self.transform.basis = self.transform.basis.rotated(Vector3.UP, m.x) diff --git a/scripts/path.gd b/scripts/path.gd index f8c0fb7..3774bfe 100644 --- a/scripts/path.gd +++ b/scripts/path.gd @@ -8,6 +8,14 @@ const dirs = [ var nodes = [] + +func refresh_path (vis): + if vis: + show() + else: + var _res = load_nodes() + hide() + func next_node (node, dir): var pos : Vector3 = node.transform.origin diff --git a/scripts/placer.gd b/scripts/placer.gd index dbff4d7..6547d02 100644 --- a/scripts/placer.gd +++ b/scripts/placer.gd @@ -1,5 +1,6 @@ extends Spatial +var control : Node var player : Spatial var world : VoxelMesh var turret_holder : Node @@ -19,9 +20,10 @@ var collision_point : Vector3 func _ready (): player = get_parent() + control = player.get_node("control") resources = get_parent().get_node("resources") - var root = get_tree().root.get_child(0) + var root = get_tree().root.get_node("world") world = root.get_node("world") turret_holder = root.get_node("turrets") attach_point_holder = root.get_node("attach") @@ -47,11 +49,12 @@ func check_overlap_pointer(): var shape = BoxShape.new() shape.extents = Vector3(0.24, 0.24, 0.24) - var info = load_turrets.info[player.sel.name] - if info.has("collider"): - if info.collider == "sphere": - shape = SphereShape.new() - shape.radius = 0.24 + if control.statetype == Globals.StateType.TURRET: + var info = load_turrets.info[control.selected] + if info.has("collider"): + if info.collider == "sphere": + shape = SphereShape.new() + shape.radius = 0.24 var params: = PhysicsShapeQueryParameters.new() params.set_shape(shape) @@ -90,7 +93,7 @@ func _inst_turret (pos, rot): instance.transform.basis = Basis(rot); instance.refresh_normal() - var info = load_turrets.info[player.sel.name] + var info = load_turrets.info[control.selected] var model = load_turrets.models[info.model_name] var instance_model = model.instance() instance_model.name = "model" @@ -114,12 +117,14 @@ func _inst_turret (pos, rot): sb.get_node("CollisionShapeSphere").disabled = false; else: sb.get_node("CollisionShapeSphere").queue_free(); + return instance func _inst_path_start (pos, rot): var instance = load_scenes.path_start.instance() path_holder.add_child(instance) instance.transform.origin = pos + normal * 0.25; instance.transform.basis = Basis(rot); + return instance func _inst_path (pos, rot): var instance = load_scenes.path.instance() @@ -128,12 +133,14 @@ func _inst_path (pos, rot): instance.transform.basis = Basis(rot); instance.set_name("path") colliding_node.transform.basis = Basis(rot); + return instance func _inst_path_end (pos, rot): var instance = load_scenes.path_end.instance() path_holder.add_child(instance) instance.transform.origin = pos + normal * 0.25 instance.transform.basis = Basis(rot); + return instance func _inst_attach (pos, rot): var instance = load_scenes.attach_point.instance() @@ -143,23 +150,24 @@ func _inst_attach (pos, rot): return instance func place (group, inst : FuncRef): - if Input.is_action_just_pressed("use"): - if colliding && group in colliding_group: - var overlap = check_overlap_pointer() - print(overlap) - if overlap == "clear": - inst.call_func(ptr.transform.origin, ptr.transform.basis.get_rotation_quat()) - return "ok" + if colliding && group in colliding_group: + var overlap = check_overlap_pointer() + if overlap == "clear": + var obj = inst.call_func( + ptr.transform.origin, + ptr.transform.basis.get_rotation_quat()) + return obj + return null func delete (group): - if Input.is_action_just_pressed("cancel"): - if colliding && group in colliding_group: - colliding_node.queue_free() - + if colliding && group in colliding_group: + colliding_node.queue_free() + return true + return false func _pointer (): - if player.sel.name == "" or player.sel.type == "": ptr.visible = false - else: ptr.visible = true + if control.state == Globals.PlayerState.PLACE: ptr.visible = true + else: ptr.visible = false var space: PhysicsDirectSpaceState = get_world().direct_space_state as PhysicsDirectSpaceState var mouse2d = get_viewport().get_mouse_position() @@ -169,7 +177,7 @@ func _pointer (): ptr.transform.origin = to var mask = 0b1101 - if player.sel.name.find("path") != -1: mask = 0b1111 + if control.statetype == Globals.StateType.PATH: mask = 0b1111 var voxelpos = null; @@ -203,47 +211,57 @@ func _pointer (): colliding_group = [] ptr.visible = false - var placed = "not ok" - match player.sel.type: - "map_tools": - match player.sel.name: - "start path": - place("path", funcref(self, "_inst_path_start")) - delete("path") - "path": - place("path", funcref(self, "_inst_path")) - delete("path") - "end path": - place("path", funcref(self, "_inst_path_end")) - delete("path") - "attach": - place("voxels", funcref(self, "_inst_attach")) - delete("attach") - "voxels": - if voxelpos != null: - var pos = Voxel.world_to_grid(voxelpos) + if control.state == Globals.PlayerState.PICK: + match control.statetype: + Globals.StateType.TURRET: if Input.is_action_just_pressed("use"): - world.set_voxel(pos + normal, int(player.sel.name)) - world.update_mesh() - if Input.is_action_just_pressed("cancel"): - world.erase_voxel(pos) - world.update_mesh() - "turrets": - placed = place("attach", funcref(self, "_inst_turret")) - if placed == "ok" and !player.in_editor: - resources.sub(load_turrets.info[player.sel.name].cost) - delete("turret") - - "idle": - if Input.is_action_just_pressed("use"): - if "turrets" in colliding_group: - player.highlight = colliding_node - else: player.highlight = null - + if "turrets" in colliding_group: + control.do(Globals.PlayerActions.SELECT, + { "selected": colliding_node.name }) - if placed == "ok": - player.sel.name = "" - player.sel.type = "idle" - if ptr.has_node("preview"): - ptr.get_node("preview").queue_free() - player.refresh_gui() + if control.state == Globals.PlayerState.PLACE: + var inst = null + var g = null + match control.statetype: + Globals.StateType.TURRET: + g = "attach" + inst = funcref(self, "_inst_turret") + Globals.StateType.ATTACH: + g = "voxels" + inst = funcref(self, "_inst_attach") + Globals.StateType.VOXEL: + if voxelpos != null: + var pos = Voxel.world_to_grid(voxelpos) + if Input.is_action_just_pressed("use"): + var overlap = check_overlap_pointer() + if overlap == "clear": + world.set_voxel(pos + normal, int(control.selected)) + world.update_mesh() + control.do(Globals.PlayerActions.PLACE) + if Input.is_action_just_pressed("cancel"): + world.erase_voxel(pos) + world.update_mesh() + control.do(Globals.PlayerActions.DELETE) + Globals.StateType.PATH: + g = "path" + match control.selected: + "start path": + g = "voxel" + inst = funcref(self, "_inst_path_start") + "path": inst = funcref(self, "_inst_path") + "end path": inst = funcref(self, "_inst_path_end") + + if inst != null: + if Input.is_action_just_pressed("use"): + var placed = place(g, inst); + if placed != null: + control.do(Globals.PlayerActions.PLACE, + { "placed": placed.name }) + else: + control.do(Globals.PlayerActions.CANCEL) + if Input.is_action_just_pressed("cancel"): + var has_deleted = delete(g) + if has_deleted: + control.do(Globals.PlayerActions.DELETE) + else: + control.do(Globals.PlayerActions.CANCEL) diff --git a/scripts/pointer.gd b/scripts/pointer.gd new file mode 100644 index 0000000..8401ec6 --- /dev/null +++ b/scripts/pointer.gd @@ -0,0 +1,28 @@ +extends Spatial + +var load_turrets : Node + +func fetch (): + if load_turrets != null: return + var root = get_tree().root.get_node("world") + + load_turrets = root.get_node("saveload").get_node("load_turrets") + if !load_turrets.loaded: yield(load_turrets, "done_loading") + +func refresh (state, statetype, turret_name : String = ""): + fetch() + for child in get_children(): + if child.name != "base": child.queue_free(); + get_node("base").visible = false + + if state == Globals.PlayerState.PLACE: + if statetype == Globals.StateType.TURRET: + if turret_name != "": + var info = load_turrets.info[turret_name] + var model = load_turrets.models[info.model_name] + var instance_model = model.instance() + instance_model.name = "preview" + add_child(instance_model) + get_node("base").visible = false + else: + get_node("base").visible = true diff --git a/scripts/turret.gd b/scripts/turret.gd index 59d6146..6fbddeb 100644 --- a/scripts/turret.gd +++ b/scripts/turret.gd @@ -21,7 +21,7 @@ var info : Dictionary var cooldown_timer = 0 func _ready(): - var root = get_tree().root.get_child(0) + var root = get_tree().root.get_node("world") _path = root.get_node("path") _enemies = root.get_node("enemies") _projectiles_holder = root.get_node("projectiles") |