diff options
author | jacopograndi <jacopo.grandi@outlook.it> | 2022-01-29 11:21:18 +0100 |
---|---|---|
committer | jacopograndi <jacopo.grandi@outlook.it> | 2022-01-29 11:21:18 +0100 |
commit | 6360c7a2faeb21d797e8b6feae4db79908399534 (patch) | |
tree | 690864fed62c005195b22bf81d016bbdda082d55 /scripts | |
parent | 7b09cbabdf0461e7b15d161206f643e3ce73af9c (diff) |
dict add
Diffstat (limited to 'scripts')
-rw-r--r-- | scripts/control.gd | 23 | ||||
-rw-r--r-- | scripts/globals.gd | 1 | ||||
-rw-r--r-- | scripts/gui_turret_detail.gd | 6 | ||||
-rw-r--r-- | scripts/turret.gd | 101 |
4 files changed, 74 insertions, 57 deletions
diff --git a/scripts/control.gd b/scripts/control.gd index 68b0bb4..2fd7164 100644 --- a/scripts/control.gd +++ b/scripts/control.gd @@ -82,6 +82,13 @@ func build_option (st, sttype): opts += [ { "type": "text", "name": "back" } ] Globals.StateType.MODULES: + opts += [ { "type": "text", "name": "add" } ] + opts += [ { "type": "text", "name": "back" } ] + + Globals.StateType.MODULES_PICK: + for k in load_turrets.modules: + var mod = load_turrets.modules[k] + opts += [ { "type": "text", "name": mod.name } ] opts += [ { "type": "text", "name": "back" } ] gui.bottom_bar.picker.build(opts) @@ -222,9 +229,25 @@ func do (action, par = {}): Globals.StateType.MODULES: selected = par.name match par.name: + "add": + statetype = Globals.StateType.MODULES_PICK + build_option(state, statetype) "back": statetype = Globals.StateType.TURRET build_option(state, statetype) + + Globals.StateType.MODULES_PICK: + selected = par.name + match par.name: + "back": + statetype = Globals.StateType.MODULES + build_option(state, statetype) + _: + statetype = Globals.StateType.MODULES + var turr = turret_holder.get_node(editing_turret) + turr.mods += [par.name] + turr.make_info_mod() + build_option(state, statetype) _: to_pick() diff --git a/scripts/globals.gd b/scripts/globals.gd index c6862a0..dc30b38 100644 --- a/scripts/globals.gd +++ b/scripts/globals.gd @@ -14,6 +14,7 @@ enum StateType { VOXEL_PALETTE TARGETING MODULES + MODULES_PICK } enum PlayerActions { diff --git a/scripts/gui_turret_detail.gd b/scripts/gui_turret_detail.gd index 74e382e..ec5d9a1 100644 --- a/scripts/gui_turret_detail.gd +++ b/scripts/gui_turret_detail.gd @@ -61,7 +61,9 @@ func _process(delta): var highlight = null if gui.control.state == Globals.PlayerState.EDIT and \ - gui.control.statetype == Globals.StateType.TURRET: + (gui.control.statetype == Globals.StateType.TURRET or \ + gui.control.statetype == Globals.StateType.MODULES or \ + gui.control.statetype == Globals.StateType.MODULES_PICK): var turret_name = gui.control.editing_turret highlight = _turret_holder.get_node(turret_name) @@ -72,7 +74,7 @@ func _process(delta): placing = gui.control.selected if highlight != null: - info = highlight.info + info = highlight.info_mod elif placing != null: info = load_turrets.info[placing] elif hovering != null: diff --git a/scripts/turret.gd b/scripts/turret.gd index 0d7302f..f365898 100644 --- a/scripts/turret.gd +++ b/scripts/turret.gd @@ -24,65 +24,56 @@ var ray : PackedScene var info : Dictionary var info_mod : Dictionary -var mods = ["damage", "double damage", "cooldown"] +var mods = [] -func make_info_mod (): - info_mod.clear() - var add_eff = {} - var mul_eff = {} - for k in info: - if info[k] is Dictionary: - info_mod[k] = {} - add_eff[k] = {} - mul_eff[k] = {} - for kk in info[k]: - info_mod[k][kk] = info[k][kk] - else: - info_mod[k] = info[k] - - for m in mods: - var mod = load_turrets.modules[m] - - for eff in mod.get("add", {}): - if mod.add[eff] is Dictionary: - for kk in mod.add[eff]: - if not eff in add_eff[eff]: add_eff[eff][kk] = 0 - add_eff[eff][kk] += mod.add[eff][kk] - else: - if not eff in add_eff: add_eff[eff] = 0 - add_eff[eff] += mod.add[eff] - - for eff in mod.get("mul", {}): - if mod.mul[eff] is Dictionary: - for kk in mod.mul[eff]: - if not eff in mul_eff[eff]: mul_eff[eff][kk] = 0 - mul_eff[eff][kk] *= mod.mul[eff][kk] - else: - if not eff in mul_eff: mul_eff[eff] = 0 - mul_eff[eff] *= mod.mul[eff] - - for k in add_eff: - if add_eff[k] is Dictionary: - for kk in add_eff[k]: - if not kk in info_mod[k]: info_mod[k][kk] = 0 - info_mod[k][kk] += add_eff[k][kk] +func dict_get (d, keys : Array): + var val = d + if keys != [] and d != null: + var key = keys.pop_front() + val = dict_get(d.get(key, null), keys) + return val + +func complete (prev, chain): + for k in prev: + if prev[k] is Dictionary: + complete(prev[k], chain + [k]) else: - if not k in info_mod: info_mod[k] = 0 - info_mod[k] += add_eff[k] - - for k in mul_eff: - if mul_eff[k] is Dictionary: - for kk in mul_eff[k]: - if not kk in info_mod[k]: info_mod[k][kk] = 0 - info_mod[k][kk] *= 1 + mul_eff[k][kk] + for m in mods: + var mod = load_turrets.modules[m] + if not mod.has("add"): continue + var children = dict_get(mod, ["add"] + chain) + if children == null: continue + for h in children: + if children[h] is int or children[h] is float: + if not prev.has(h): + prev[h] = 0 + break + +func traverse (prev, next, chain): + for k in prev: + if prev[k] is Dictionary: + if not next.has(k): next[k] = {} + traverse(prev[k], next[k], chain + [k]) else: - if not k in info_mod: info_mod[k] = 0 - info_mod[k] *= 1 + mul_eff[k] + var add = 0 + var mul = 0 + for m in mods: + var mod = load_turrets.modules[m] + var x = dict_get(mod, ["add"] + chain + [k]) + if x is int or x is float: add += x + var y = dict_get(mod, ["mul"] + chain + [k]) + if y is int or y is float: mul += y + if prev[k] is int or prev[k] is float: + next[k] = prev[k] + add + next[k] = next[k] * (1+mul) + else: next[k] = prev[k] + +func make_info_mod (): + info_mod.clear() + complete(info, []) + traverse(info, info_mod, []) - print(add_eff) - print(mul_eff) - print(info) - print(info_mod) + print(info, info_mod) func _ready(): var root = get_tree().root.get_node("world") |