From e449e827eef7d046ad4aeb47a3053872c934cb2e Mon Sep 17 00:00:00 2001 From: Gerkiz Date: Wed, 29 Jul 2020 20:23:37 +0200 Subject: [PATCH] added more functions to rpg --- maps/mountain_fortress_v3/main.lua | 1 + modules/rpg/settings.lua | 5 +- modules/rpg/spells.lua | 16 ---- modules/rpg/table.lua | 118 ++++++++++++++++++++++++----- 4 files changed, 101 insertions(+), 39 deletions(-) diff --git a/maps/mountain_fortress_v3/main.lua b/maps/mountain_fortress_v3/main.lua index 933dd1e7..7e4d167b 100644 --- a/maps/mountain_fortress_v3/main.lua +++ b/maps/mountain_fortress_v3/main.lua @@ -244,6 +244,7 @@ function Public.reset_map() RPG_Settings.enable_stone_path(true) RPG_Settings.enable_one_punch(true) RPG_Settings.enable_one_punch_globally(false) + RPG_Settings.disable_cooldowns_on_spells() Group.reset_groups() diff --git a/modules/rpg/settings.lua b/modules/rpg/settings.lua index 67993fa2..f05656d4 100644 --- a/modules/rpg/settings.lua +++ b/modules/rpg/settings.lua @@ -396,8 +396,7 @@ function Public.extra_settings(player) } ) - local new_spells, names = RPG.rebuild_spells() - RPG.set_spells_table(new_spells) + local spells, names = RPG.rebuild_spells() local conjure_label_style = conjure_label.style conjure_label_style.horizontally_stretchable = true @@ -411,7 +410,7 @@ function Public.extra_settings(player) conjure_gui_input = create_input_element(conjure_input, 'dropdown', false, names, rpg_t[player.index].dropdown_select_index) - for _, entity in pairs(new_spells) do + for _, entity in pairs(spells) do if entity.type == 'item' then conjure_label.tooltip = conjure_label.tooltip .. diff --git a/modules/rpg/spells.lua b/modules/rpg/spells.lua index ddb54638..443c98f9 100644 --- a/modules/rpg/spells.lua +++ b/modules/rpg/spells.lua @@ -286,22 +286,6 @@ function Public.conjure_items() return spells end -function Public.rebuild_spells() - local spells = Public.conjure_items() - - local new_spells = {} - local spell_names = {} - - for i = 1, #spells do - if spells[i].enabled then - new_spells[#new_spells + 1] = spells[i] - spell_names[#spell_names + 1] = spells[i].name - end - end - - return new_spells, spell_names -end - Public.projectile_types = { ['explosives'] = {name = 'grenade', count = 0.5, max_range = 32, tick_speed = 1}, ['land-mine'] = {name = 'grenade', count = 1, max_range = 32, tick_speed = 1}, diff --git a/modules/rpg/table.lua b/modules/rpg/table.lua index 68e13fae..d8641091 100644 --- a/modules/rpg/table.lua +++ b/modules/rpg/table.lua @@ -7,7 +7,7 @@ local Gui = require 'utils.gui' local this = { rpg_extra = {}, rpg_t = {}, - rpg_spells = {} + rpg_spells = Spells.conjure_items() } --! Gui Frames @@ -122,7 +122,6 @@ function Public.reset_table() ['small-worm-turret'] = 16, ['spitter-spawner'] = 64 } - this.rpg_spells = {} end --- Gets value from table @@ -275,36 +274,116 @@ function Public.enable_one_punch_globally(value) return this.rpg_extra.enable_one_punch_globally end ---- Retrieves the spell table. +--- Retrieves the spells table or a given spell. ---@param key function Public.get_spells(key) - if key then + if this.rpg_spells[key] then return this.rpg_spells[key] else return this.rpg_spells end end +--- Disables a spell. +---@param key +-- Table would look like: +-- Public.disable_spell({1, 2, 3, 4, 5, 6, 7, 8}) +function Public.disable_spell(key) + if type(key) == 'table' then + for _, k in pairs(key) do + this.rpg_spells[k].enabled = false + end + elseif this.rpg_spells[key] then + this.rpg_spells[key].enabled = false + end +end + +--- Clears the spell table. +function Public.clear_spell_table() + this.rpg_spells = {} +end + --- Adds a spell to the rpg_spells ----@param key ----@param value -function Public.set_spells(key, value) - if key and value then - this.rpg_spells[key] = value - return this.rpg_spells[key] - elseif key then - return this.rpg_spells[key] - else - return this.rpg_spells +---@param tbl +function Public.set_new_spell(tbl) + if tbl then + if not tbl.name then + return error('A spell requires a name. ', 2) + end + if not tbl.obj_to_create then + return error('A spell requires an object to create. ', 2) + end + if not tbl.target then + return error('A spell requires position. ', 2) + end + if not tbl.amount then + return error('A spell requires an amount of creation. ', 2) + end + if not tbl.range then + return error('A spell requires a range. ', 2) + end + if not tbl.damage then + return error('A spell requires damage. ', 2) + end + if not tbl.force then + return error('A spell requires a force. ', 2) + end + if not tbl.level then + return error('A spell requires a level. ', 2) + end + if not tbl.type then + return error('A spell requires a type. ', 2) + end + if not tbl.mana_cost then + return error('A spell requires mana_cost. ', 2) + end + if not tbl.tick then + return error('A spell requires tick. ', 2) + end + if not tbl.enabled then + return error('A spell requires enabled. ', 2) + end + + this.rpg_spells[#this.rpg_spells + 1] = tbl end end ---- Defines the spell table ----@param tbl
-function Public.set_spells_table(tbl) - if tbl then - this.rpg_spells = tbl +--- This rebuilds all spells. Make sure to make changes on_init if you don't +-- want all spells enabled. +function Public.rebuild_spells() + local spells = this.rpg_spells + + local new_spells = {} + local spell_names = {} + + for i = 1, #spells do + if spells[i].enabled then + new_spells[#new_spells + 1] = spells[i] + spell_names[#spell_names + 1] = spells[i].name + end end + + this.rpg_spells = new_spells + + return new_spells, spell_names +end + +--- This will disable the cooldown of all spells. +function Public.disable_cooldowns_on_spells() + local spells = this.rpg_spells + + local new_spells = {} + + for i = 1, #spells do + if spells[i].enabled then + spells[i].tick = 0 + new_spells[#new_spells + 1] = spells[i] + end + end + + this.rpg_spells = new_spells + + return new_spells end Public.get_projectiles = Spells.projectile_types @@ -314,7 +393,6 @@ Public.discard_button_name = discard_button_name Public.draw_main_frame_name = draw_main_frame_name Public.main_frame_name = main_frame_name Public.settings_button_name = settings_button_name -Public.rebuild_spells = Spells.rebuild_spells local on_init = function() Public.reset_table()