Skip to content

Instantly share code, notes, and snippets.

@AdamKyle
Last active December 20, 2015 01:09
Show Gist options
  • Save AdamKyle/6046623 to your computer and use it in GitHub Desktop.
Save AdamKyle/6046623 to your computer and use it in GitHub Desktop.
This script creates a shop script to be used with the custom currencies script.
#==============================================================================
# Guild Curency Shop
# ----------------------------------------------------------------------------
# Version: 1.0
# Author: Kalacious (AKA: VindictivePersonality)
# License: GPL 3.0
#
# Compatibillity Issues
# ----------------------------------------------------------------------------
#
# There should be NO compatibillity issues what so ever. How ever if there are,
# Please let me know.
#
# REQUIREMENTS!
# ----------------------------------------------------------------------------
#
# Requires the currency generator script.
#
# Any things that requires the currency to be passed in, MUST exist
# in the currency hash thatis created in the currency generator script.
#
# TOS
# ----------------------------------------------------------------------------
# If you know the GPL version 3, you know the answer, but this still applies:
#
# Script for RGSS 3 ONLY adds abillity to generate shops.
# Copyright (C) 2013 Kalacious (AKA VindictivePersonality)
#
# 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 <http://www.gnu.org/licenses/>
#
#
#==============================================================================
# How to Use?
# -----------
#
# This script is suppose to be used with currency script, infact you need it
# in order to use this script :D.
#
# This script also is designed to create either one shop based on one currency
# or you can use the script methods to create multiple shops based on one
# currencie.
#
# We are going to dwelve into how to use the hash array to create a shop and
# how to create a shop for multiple currencies based on the script calls we
# have out ligned below.
#
# SHOPS::SHOPS_HASH
# -----------------
#
# This hash is used to create a shop object based on the the currency and
# the items passed in. Please note that ALL the arrays in the hash MUST
# be the same length or we may cause errors and issues.
#
# This hash must be created as such:
#
# SHOPS_HASH = {
# 'Currency' => {
# :item_type => [0, 0, 0] #0 item, 1 weapon, 2 armor.
# :item_id => [0, 1, 2] #item_id's.
# :price => [10, 10, 10] #price for each item.
# }
# }
#
# This hash example, will create one shop with three items using the
# key as he currency to which the shop uses when buying or selling objects.
#
# Please note, the key in this hash, the main key, needs to exist in the
# currency hash in the currency script. If not then this script will fail.
# YOU CANNOT USE GOLD! This script only works with custom currencies.
#
# So now that we have a hash object, go ahead and call the shop. Wait how
# do we do that? Don't worry we will get to that. Lets relax for now and move
# on to creating a master inventory list via script calls
#
# $kc_shop
# --------
#
# This object will allow you to create a master inventroy list based on the
# the items you pass it. Much like the hash object above you will use
# these script calls to create a master inventory list and then pass it to a
# shop object.
#
# $kc_shop.create_item(item_type, item_id, price)
#
# --> This will create ONE object in the @inventory, which
# is called the the master inventory list.
# This is what you use to create your item for the shop.
#
# $kc_shop.inventory
#
# --> This will return you that master inventory list with
# all the items in it.
#
#
# $kc_shop.create
#
# --> DONT call me. there is no need for that. You would only
# create the array from the hash below twice.
#
# So how do I use this to create a shop?
#
# You would call the first two script calls like such:
#
#
# $kc_shop.create_item(0, 1, 10)
# $kc_shop.create_item(0, 2, 10)
# $kc_shop.create_item(0, 3, 10)
# puts $kc_shop.inventory #[[0, 1, 10][0, 2, 10][0, 3, 10]]
#
# Ok so we either created a hash object for the shop or we created the
# master inventory via the script calls. HOW DO WE CALL THE SHOP?!
#
# Module GS
# ---------
#
# This module is used for one of two things, calling a shop or ....
# calling a shop. The difference between the way you call the shop
# is what you pass in and which method you call.
#
# call_shop(master_inventory, 'curr', puchase_only = false)
#
# ---> The master_inventory is $kc_shop.inventory pased in.
# curr is the currency you want the shop to repersent.
# purchase_only is set as false by default.
#
# ---> Use this ONLY if you are using $kc_shop as a script
# call to create items.
#
# create_shop('curr', purchase_only = false)
#
# ---> This is called ONLY if you are using the hash, you pass in the
# currency from the hash, the main key, this then uses that to
# create a shop based on that currency in the hash below.
#
#==============================================================================
# ** SHOPS
#------------------------------------------------------------------------------
# This module is used to set up a shop for a particular currency. This will
# only create ONE shop per currency. You cannot create multiple shops per
# currency.
#==============================================================================
module SHOPS
#--------------------------------------------------------------------------
# * Create your shop objects here based on a particular vocab.
#--------------------------------------------------------------------------
SHOPS_HASH = {
'GC' => {
:item_type => [0, 0],
:item_id => [4, 5, 6],
:price => [10, 14],
},
'RT' => {
:item_type => [0, 0],
:item_id => [1, 2],
:price => [10, 14],
}
}
end
#==============================================================================
# ** GS (GuildShop)
#------------------------------------------------------------------------------
# This module is used to create a shop and pass in an inventory or used
# to create a currency based shop based on the hash above.
#==============================================================================
module GS
#--------------------------------------------------------------------------
# * Call a shop based on the master_invenotry list, type of
# currency the shop uses and eather it's purchase only or not.
#--------------------------------------------------------------------------
def self.call_shop(master_inventory, curr, purchase_only = false)
SceneManager.call(Scene_Guild_Shop)
SceneManager.scene.prepare(master_inventory, curr, purchase_only)
end
#--------------------------------------------------------------------------
# * Create a shop based on the currency, from the hash above and
# weather the shop is purchase only or not.
#--------------------------------------------------------------------------
def self.create_shop(curr, purchase_only = false)
$kc_shop.create(curr)
SceneManager.call(Scene_Guild_Shop)
SceneManager.scene.prepare($kc_shop.inventory, curr, purchase_only)
end
end
#==============================================================================
# ** DataManager
#------------------------------------------------------------------------------
# This module manages the database and game objects. Almost all of the
# global variables used by the game are initialized by this module.
#==============================================================================
class << DataManager
alias kalacious_guild_shops_game_objects create_game_objects
def create_game_objects
kalacious_guild_shops_game_objects
$kc_shop = GS_Setup.new
end
end
#==============================================================================
# ** Game_Guild_ShopObject
#------------------------------------------------------------------------------
# This creates a new shop object that only accepts the item_type,
# item_id and price of the said item.
#==============================================================================
class Game_Guild_ShopObject
attr_reader :item_type
attr_reader :item_id
attr_reader :price
#--------------------------------------------------------------------------
# * Create a shop based on the currency, from the hash above and
# weather the shop is purchase only or not.
#--------------------------------------------------------------------------
def initialize(item_type, item_id, price)
@item_type = item_type
@item_id = item_id
@price = price
end
#--------------------------------------------------------------------------
# * Returns @data_type[@item_type] based on the type.
#--------------------------------------------------------------------------
def data_item
if @item_type == 0
return @data_items[@item_id]
end
if @item_type == 1
return @data_weapons[@item_id]
end
if @item_type == 2
return @data_armors[@item_id]
end
end
#--------------------------------------------------------------------------
# * Get the price of the item.
#--------------------------------------------------------------------------
def get_price
return @price
end
end
#==============================================================================
# ** GS_Setup
#------------------------------------------------------------------------------
# Used to create a master inventory based on either the item_type
# item_id and the price or the currency vocab and the hash above.
#==============================================================================
class GS_Setup
#--------------------------------------------------------------------------
# * Create an empty inventory object.
#--------------------------------------------------------------------------
def initialize
@inventory = []
end
#--------------------------------------------------------------------------
# * Return the inventory object.
#--------------------------------------------------------------------------
def inventory
@inventory
end
#--------------------------------------------------------------------------
# * Create an inventory object based on the vocab and the hash above.
#--------------------------------------------------------------------------
def create(curr_vocab)
SHOPS::SHOPS_HASH.each do |key, value|
if key == curr_vocab and $kc_currency.exists?(curr_vocab)
value[:item_type].size.times { |i|
item = create_item_object(value[:item_type][i], value[:item_id][i],
value[:price][i])
@inventory.push(item)
}
end
end
end
#--------------------------------------------------------------------------
# * Create the Item assuming it doesn't already exist.
#--------------------------------------------------------------------------
def create_item(item_type, item_id, price)
if @inventory.any?
@inventory.each do |inventory|
if inventory[0] == item_type and inventory[1] == item_id and inventory[2] == price
return false
end
end
end
item = create_item_object(item_type, item_id, price)
@inventory.push(item)
end
#--------------------------------------------------------------------------
# * PRIVATE - Creates the item array in the form of:
# [item_type, item_id, price]
#--------------------------------------------------------------------------
private
def create_item_object(item_type, item_id, price)
item = Game_Guild_ShopObject.new(item_type, item_id, price)
goods = [item.item_type, item.item_id, item.price]
return goods
end
end
#==============================================================================
# ** Window_Guild_ShopBuy
#------------------------------------------------------------------------------
# Creates a item list based on the item object passed in.
#==============================================================================
class Window_Guild_ShopBuy < Window_ShopBuy
#--------------------------------------------------------------------------
# * Makes the item list based on the object passed in.
#--------------------------------------------------------------------------
def make_item_list
@data = []
@price = {}
@shop_goods.each do |goods|
case goods[0]
when 0; item = $data_items[goods[1]]
when 1; item = $data_weapons[goods[1]]
when 2; item = $data_armors[goods[1]]
end
if item
@data.push(item)
@price[item] = goods[2]
end
end
end
end
#==============================================================================
# ** Window Guild Currency
#------------------------------------------------------------------------------
# Exactly the same as Window_Gold, the only difference is instead of getting
# and displaying only gold, we get and display any currency passed in.
#
# REQUIRES: Currency Script to work.
#==============================================================================
class Window_Guild_Currency < Window_Base
#--------------------------------------------------------------------------
# * Create a window, and set the @currency to the currency passed in.
#--------------------------------------------------------------------------
def initialize(curr)
super(0, 0, window_width, fitting_height(1))
@currency = curr
refresh
end
#--------------------------------------------------------------------------
# * Return a width of 160.
#--------------------------------------------------------------------------
def window_width
return 160
end
#--------------------------------------------------------------------------
# * Clear the contents, draw the currency value.
#--------------------------------------------------------------------------
def refresh
contents.clear
draw_currency_value(value, currency_unit, 4, 0, contents.width - 8)
end
#--------------------------------------------------------------------------
# * Whats the currency unit?
#--------------------------------------------------------------------------
def currency_unit
@currency.curr_sym
end
#--------------------------------------------------------------------------
# * Get this currencies value.
#--------------------------------------------------------------------------
def value
@currency.value
end
#--------------------------------------------------------------------------
# * Upon open, refresh.
#--------------------------------------------------------------------------
def open
refresh
super
end
end
#==============================================================================
# ** Scene_Guild_Shop
#------------------------------------------------------------------------------
# Extends the Scene_Shop and creates, essentially, our own scene shop.
#==============================================================================
class Scene_Guild_Shop < Scene_Shop
#--------------------------------------------------------------------------
# * Prepare the inventory, currency and weather its purchase only or not.
#--------------------------------------------------------------------------
def prepare(master_inventory, curr, purchase_only = false)
@currency = $kc_currency.currency(curr)
@goods = master_inventory
@purchase_only = purchase_only
end
#--------------------------------------------------------------------------
# * Create the currency window. (we use create_gold_window).
#--------------------------------------------------------------------------
def create_gold_window
@gold_window = Window_Guild_Currency.new(@currency)
@gold_window.viewport = @viewport
@gold_window.x = Graphics.width - @gold_window.width
@gold_window.y = @help_window.height
end
#--------------------------------------------------------------------------
# * Create the buy window.
#--------------------------------------------------------------------------
def create_buy_window
wy = @dummy_window.y
wh = @dummy_window.height
@buy_window = Window_Guild_ShopBuy.new(0, wy, wh, @goods)
@buy_window.viewport = @viewport
@buy_window.help_window = @help_window
@buy_window.status_window = @status_window
@buy_window.hide
@buy_window.set_handler(:ok, method(:on_buy_ok))
@buy_window.set_handler(:cancel, method(:on_buy_cancel))
end
#--------------------------------------------------------------------------
# * What happens upn buy?
#--------------------------------------------------------------------------
def do_buy(number)
$kc_currency.decrease(@currency.curr_sym, number * buying_price)
$game_party.gain_item(@item, number)
end
#--------------------------------------------------------------------------
# * What happens upon sell?
#--------------------------------------------------------------------------
def do_sell(number)
$kc_currency.increase_currency(@currency.curr_sym, number * selling_price)
$game_party.lose_item(@item, number)
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment