Code:
module LDT_UPGRADE_MENU
#Confirmation texts
CONFIRM_TITLE = " upgrade?"
CONFIRM_TRUE = "Yes"
CONFIRM_FALSE = "No"
#icon ids
#upgrade icons (villager, house,...)
ICON_IDS = [121,232,292,281]
#cost icons (wood, gold, ...)
ICON_COST_IDS = [331,262,350,147]
#resource names
UPGRADE_NAMES = ["Villagers","House","Fisherman","Church"]
#cost names
COST_NAMES = ["Wood","Gold","Stone","Sword"]
#upgrade costs
UPGRADE_COSTS = [[{0=>5,1=>10},{0=>10,1=>25},{0=>20,1=>50},{0=>50,1=>200}],#villager
[{0=>5,1=>10,2=>5},{0=>10,1=>25,2=>15},{0=>20,1=>50,2=>50}],#house
[{0=>10,1=>5}],#fisherman
[{0=>50,1=>500,2=>50}],#church
]
#upgrade description
UPGRADE_DESCR = ["The \\C[5]inhabitants\\C[0] of your village.","This is your residence.","The guy who provides you with fish.","The place your inhabitants can pray."]
#variable for each upgrade (e.g. 1 = $game_variables[1])
UPGRADE_VARS = [101,102,103,104]
#variable for each cost (e.g. 1 = $game_variables[1])
COST_VARS = [105,106,107,108]
#Symbol displayed if max Level is reached
MAX_LEVEL_TEXT = "MAX"
#Graphics
#Start position vertical (0 = top, 480 = botton)
G_START_Y = 0
#start position horizontal (0 = left, 640 = right)
G_START_X = 0
#Width of the first Window
G_WIDTH_1 = 280
#Width of the second Window
G_WIDTH_2 = 240
#Confirmation window alignment (1 = horiz., 2= vert.)
G_CONFIRM_ALIGN = 2
#color if condition is not met
BAD_COLOR = 2
end
# call the upgrade menu
# items needs to be an array
# e.g. call_upgrade_menu([0]) will call the menu for villager upgrades
# e.g. call_upgrade_menu([0,3]) will call the menu for villager & church upgr.
def call_upgrade_menu(items)
return false unless(items.is_a?(Array))
$upgrade_items = items
SceneManager.call(LDT_Upgrade_Scene)
end
class LDT_Upgrade_Scene < Scene_Base
def start
super
@ui = 0
create_title_window()
create_selection_window
create_cost_window
create_confirmation_windows
create_background
@title_window.set_text(LDT_UPGRADE_MENU::UPGRADE_DESCR[$upgrade_items[@ui]])
end
def create_title_window()
@title_window = LDT_Upgrade_Window_Title.new(LDT_UPGRADE_MENU::G_START_X,LDT_UPGRADE_MENU::G_START_Y,1,Graphics.width)
@title_window.set_text("")
end
def create_selection_window()
ty = LDT_UPGRADE_MENU::G_START_Y + @title_window.height
th = Graphics.height - @title_window.height#$upgrade_items.size * 24 + 32
@upgrade_window = LDT_Upgrade_Window.new(LDT_UPGRADE_MENU::G_START_X,ty,LDT_UPGRADE_MENU::G_WIDTH_1,th)
@upgrade_window.viewport = @viewport
@upgrade_window.set_handler(:ok, method(:on_menu_ok))
@upgrade_window.set_handler(:cancel, method(:return_scene))
@upgrade_window.activate
@upgrade_window.select(0)
@upgrade_window.show
end
def create_cost_window()
ty = LDT_UPGRADE_MENU::G_START_Y + @title_window.height
tx = LDT_UPGRADE_MENU::G_START_X + LDT_UPGRADE_MENU::G_WIDTH_1
tw = Graphics.width - tx
th = Graphics.height - @title_window.height
@cost_window = LDT_Upgrade_Cost_Window.new(tx,ty,tw,th)
@cost_window.viewport = @viewport
@cost_window.show
end
def create_background
@background_sprite = Sprite.new
@background_sprite.bitmap = SceneManager.background_bitmap
@background_sprite.color.set(16, 16, 16, 128)
end
def create_confirmation_windows()
tx = LDT_UPGRADE_MENU::G_START_X + ( LDT_UPGRADE_MENU::G_WIDTH_1 - LDT_UPGRADE_MENU::G_WIDTH_2 ) / 2
ty = LDT_UPGRADE_MENU::G_START_Y
@confirm_title_window = LDT_Confirm_Window_Title.new(tx,ty,1,LDT_UPGRADE_MENU::G_WIDTH_2)
@confirm_title_window.set_text("")
@confirm_title_window.hide
@confirm_title_window.viewport = @viewport
th = 2 * 24 + 32
ty = ty + @confirm_title_window.height
@confirm_window = LDT_Upgrade_Window_Confirmation.new(tx,ty,LDT_UPGRADE_MENU::G_WIDTH_2,th)
@confirm_window.viewport = @viewport
@confirm_window.set_handler(:ok, method(:on_conf_ok))
@confirm_window.set_handler(:cancel, method(:on_conf_cancel))
@confirm_window.hide
end
def on_menu_ok
ti = @upgrade_window.index
if(@upgrade_window.current_item_enabled?)
ttext = get_upgrade_name(ti) + LDT_UPGRADE_MENU::CONFIRM_TITLE
@confirm_title_window.set_text(ttext)
tth = @upgrade_window.index * 24 + 40 + @title_window.height
if((tth + @confirm_title_window.height + @confirm_window.height) > Graphics.height)
tth = tth - 24 - @confirm_title_window.height - @confirm_window.height
end
@confirm_title_window.y = tth
@confirm_window.y = tth + @confirm_title_window.height
@confirm_title_window.show
@confirm_window.activate
@confirm_window.select(0)
@confirm_window.show
else
@upgrade_window.activate
end
end
def on_conf_ok
if(@confirm_window.index >= 1)
on_conf_cancel
else
ti = $upgrade_items[@upgrade_window.index]
#loop on all costs of current upgrade level and reduce cor. variable
LDT_UPGRADE_MENU::UPGRADE_COSTS[ti][$game_variables[LDT_UPGRADE_MENU::UPGRADE_VARS[ti]]].each do |tk,tv|
$game_variables[LDT_UPGRADE_MENU::COST_VARS[tk]]-= tv
end
#increase upgrade variable by 1
$game_variables[LDT_UPGRADE_MENU::UPGRADE_VARS[ti]] += 1
refresh_windows
on_conf_cancel
end
end
def refresh_windows
@upgrade_window.refresh
@cost_window.refresh
end
def update()
super()
if(@upgrade_window.index != @ui)
@ui = @upgrade_window.index
@cost_window.set_upgrade_index($upgrade_items[@ui])
@cost_window.refresh
@title_window.set_text(LDT_UPGRADE_MENU::UPGRADE_DESCR[$upgrade_items[@ui]])
end
end
def on_conf_cancel
@confirm_window.deactivate
@confirm_window.hide
@confirm_title_window.hide
@upgrade_window.activate
end
def get_upgrade_name(index)
return LDT_UPGRADE_MENU::UPGRADE_NAMES[$upgrade_items[index]]
end
end
class LDT_Upgrade_Cost_Window < Window_Base
def initialize(x, y, width,height)
super(x,y,width,height)#get_uc_height)
@iu = 0
refresh
end
def set_upgrade_index(index)
@iu = index
end
def refresh
contents.clear
LDT_UPGRADE_MENU::COST_NAMES.each_with_index do |tn,ti|
draw_costs(ti)
end
end
def draw_costs(index)
tx = 0
ty = 0 + index * 24
tw = self.width - 24
if(cost_relevant?(index))
contents.font.color.alpha = 255
else
contents.font.color.alpha = translucent_alpha
end
draw_icon(get_icon_num(index), tx, ty, cost_relevant?(index))
draw_text(tx + 26, ty, tw - 26, 24, get_cost_name(index))
if(cost_relevant?(index))
if(get_resource(index) < get_upgrade_cost(index))
change_color(text_color(LDT_UPGRADE_MENU::BAD_COLOR))
end
draw_text(tx,ty,tw,24,get_upgrade_cost_text(index),2)
change_color(normal_color())
end
end
def get_uc_height()
return LDT_UPGRADE_MENU::COST_NAMES.size * 24 + 32
end
def cost_relevant?(index)
return false unless(LDT_UPGRADE_MENU::UPGRADE_COSTS[@iu][$game_variables[LDT_UPGRADE_MENU::UPGRADE_VARS[@iu]]])
return LDT_UPGRADE_MENU::UPGRADE_COSTS[@iu][$game_variables[LDT_UPGRADE_MENU::UPGRADE_VARS[@iu]]].has_key?(index)
end
def get_icon_num(index)
return LDT_UPGRADE_MENU::ICON_COST_IDS[index]
end
def get_cost_name(index)
text = LDT_UPGRADE_MENU::COST_NAMES[index] + "("
text = text + get_resource(index).to_s + ")"
return text
end
def get_resource(index)
return $game_variables[LDT_UPGRADE_MENU::COST_VARS[index]]
end
def get_upgrade_cost_text(index)
return get_upgrade_cost(index).to_s
end
def get_upgrade_cost(index)
LDT_UPGRADE_MENU::UPGRADE_COSTS[@iu][$game_variables[LDT_UPGRADE_MENU::UPGRADE_VARS[@iu]]][index]
end
end
class LDT_Upgrade_Window < Window_Selectable
def initialize(x, y, width, height)
super(x,y,width,height)
refresh
end
def item_max
$upgrade_items ? $upgrade_items.size : 1
end
def item
$upgrade_items && index >= 0 ? LDT_UPGRADE_MENU::UPGRADE_NAMES[$upgrade_items[index]] : nil
end
def refresh
create_contents
draw_all_items
end
def draw_item(index)
rect = item_rect(index)
rect.width -= 4
if(current_item_enabled?(index))
contents.font.color.alpha = 255
else
contents.font.color.alpha = translucent_alpha
end
draw_icon(get_icon_num(index), rect.x, rect.y, current_item_enabled?(index))
draw_text(rect.x + 26, rect.y, rect.width - 26, rect.height, get_upgrade_name(index))#, rect.x, rect.y)
draw_text(rect,get_upgrade_lvl(index),2)
end
def get_upgrade_lvl(index)
return $game_variables[LDT_UPGRADE_MENU::UPGRADE_VARS[up_index(index)]]
end
def up_index(index)
return $upgrade_items[index]
end
def get_icon_num(index)
return LDT_UPGRADE_MENU::ICON_IDS[up_index(index)]
end
def get_upgrade_name(index)
return LDT_UPGRADE_MENU::UPGRADE_NAMES[up_index(index)]
end
def current_item_enabled?(tindex = index)
return false if(tindex < 0)
tc = get_costs(tindex)
return false if(tc == LDT_UPGRADE_MENU::MAX_LEVEL_TEXT)
tc.each do |tk,tv|
return false if($game_variables[LDT_UPGRADE_MENU::COST_VARS[tk]] < tv)
end
return true
end
def get_costs(index)
tcosts = LDT_UPGRADE_MENU::UPGRADE_COSTS[up_index(index)][$game_variables[LDT_UPGRADE_MENU::UPGRADE_VARS[up_index(index)]]]
return tcosts ? tcosts : LDT_UPGRADE_MENU::MAX_LEVEL_TEXT
end
end
class LDT_Upgrade_Window_Confirmation < Window_Selectable
def initialize(x, y, width, height)
super(x,y,width,height)
@items = [LDT_UPGRADE_MENU::CONFIRM_TRUE,LDT_UPGRADE_MENU::CONFIRM_FALSE]
refresh
end
def item_max
@items ? @items.size : 1
end
def item
@items && index >= 0 ? @items[index] : nil
end
def refresh
create_contents
draw_all_items
end
def draw_item(index)
rect = item_rect(index)
rect.width -= 4
draw_text(rect,get_item_name(index))
end
def get_item_name(index)
return @items[index]
end
def get_costs(index)
return $game_variables[LDT_UPGRADE_MENU::COST_VARS[index]]
end
end
class LDT_Confirm_Window_Title < Window_Base
def initialize(x,y,line_number = 1, tw = nil)
tw = (tw ? tw : Graphics.width)
super(x, y, tw, fitting_height(line_number))
@text2 = ""
end
def refresh
contents.clear
draw_text_ex(4, 0, @text)
end
def set_text(text)
if text != @text
@text = text
refresh
end
end
end
class LDT_Upgrade_Window_Title < Window_Base
def initialize(x,y,line_number = 1, tw = nil)
tw = (tw ? tw : Graphics.width)
super(x, y, tw, fitting_height(line_number))
end
def refresh
contents.clear
draw_text_ex(4, 0, @text)
end
def set_text(text)
if text != @text
@text = text
refresh
end
end
end