Skip to content

Commit

Permalink
items in inventory and locations now take into account multiples of t…
Browse files Browse the repository at this point in the history
…he same item; items dropped don't get destroyed but are instead put into the current location for later; updated internal name for location paths so old save games are invalid
  • Loading branch information
michaelchadwick committed Sep 21, 2015
1 parent 5b1e957 commit 601ac05
Show file tree
Hide file tree
Showing 11 changed files with 212 additions and 121 deletions.
136 changes: 68 additions & 68 deletions data/default_world.yaml

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions lib/gemwarrior/entities/items/bookcase.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ def initialize
def use(world)
forest_southwest = world.location_by_name('forest-southwest')

if forest_southwest.locs_connected[:west].eql? false
if forest_southwest.paths[:west].eql? false
if !self.used
puts 'Many books look interesting, including one specific volume entitled "Use Me Again to Attempt Unlocking the Way", by Not Very Subtle.'
puts
Expand All @@ -33,7 +33,7 @@ def use(world)

case answer
when 'mineral'
forest_southwest.locs_connected[:west] = true
forest_southwest.paths[:west] = true
puts
Animation.run(phrase: OPEN_TEXT)
puts
Expand Down
67 changes: 48 additions & 19 deletions lib/gemwarrior/entities/location.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,27 @@ class Location < Entity
ERROR_ITEM_REMOVE_INVALID = 'That item cannot be removed as it does not exist here.'

attr_accessor :coords,
:locs_connected,
:paths,
:danger_level,
:monster_level_range,
:items,
:monsters_abounding,
:bosses_abounding,
:checked_for_monsters

def initialize(options)
self.name = options.fetch(:name)
self.description = options.fetch(:description)
self.coords = options.fetch(:coords)
self.paths = options.fetch(:paths)
self.danger_level = options.fetch(:danger_level)
self.monster_level_range = options.fetch(:monster_level_range)
self.items = options.fetch(:items)
self.monsters_abounding = options.fetch(:monsters_abounding)
self.bosses_abounding = options.fetch[:bosses_abounding]
self.checked_for_monsters = false
end

def describe
desc_text = name.ljust(30).upcase.colorize(:green)
desc_text << coords.values.to_a.to_s.colorize(:white)
Expand All @@ -40,19 +53,6 @@ def describe_detailed
desc_text
end

def initialize(options)
self.name = options.fetch(:name)
self.description = options.fetch(:description)
self.coords = options.fetch(:coords)
self.locs_connected = options.fetch(:locs_connected)
self.danger_level = options.fetch(:danger_level)
self.monster_level_range = options.fetch(:monster_level_range)
self.items = options.fetch(:items)
self.monsters_abounding = options.fetch(:monsters_abounding)
self.bosses_abounding = options.fetch[:bosses_abounding]
self.checked_for_monsters = false
end

def contains_item?(item_name)
self.items.map{|i| i.name.downcase}.include?(item_name.downcase)
end
Expand All @@ -66,7 +66,7 @@ def has_boss?(boss_name)
end

def add_item(item_name_to_add)
all_items = GameItems.data || GameWeapons.data
all_items = GameItems.data | GameWeapons.data | GameArmor.data
all_items.each do |game_item|
if game_item.name.eql?(item_name_to_add)
self.items.push(game_item)
Expand All @@ -78,7 +78,7 @@ def add_item(item_name_to_add)

def remove_item(item_name)
if contains_item?(item_name)
items.reject! { |item| item.name == item_name }
self.items.delete_at(items.index(items.find { |i| i.name.downcase == item_name.downcase }))
else
ERROR_ITEM_REMOVE_INVALID
end
Expand All @@ -100,7 +100,7 @@ def has_loc_to_the?(direction)
when 'w'
direction = 'west'
end
locs_connected[direction.to_sym]
paths[direction.to_sym]
end

def monster_by_name(monster_name)
Expand Down Expand Up @@ -132,7 +132,36 @@ def should_spawn_monster?
end

def list_items
items.length > 0 ? items.map(&:name) : []
if items.empty?
[]
else
item_hash = {}
self.items.map(&:name).each do |i|
i_sym = i.to_sym
if item_hash.keys.include? i_sym
item_hash[i_sym] += 1
else
item_hash[i_sym] = 1
end
end

if item_hash.length == 1
item_hash.each do |i, q|
q > 1 ? "#{q} #{i}s" : i
end
else
item_arr = []
item_hash.each do |i, q|
if q > 1
item_arr.push("#{i.to_s.colorize(:yellow)}#{'s'.colorize(:yellow)} x#{q}")
else
item_arr.push(i)
end
end

return item_arr
end
end
end

def list_monsters
Expand All @@ -145,7 +174,7 @@ def list_bosses

def list_paths
valid_paths = []
locs_connected.each do |key, value|
self.paths.each do |key, value|
if value
valid_paths.push(key.to_s)
end
Expand Down
2 changes: 1 addition & 1 deletion lib/gemwarrior/entities/monsters/bosses/jaspern.rb
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ def river_bridge_success(world)

# unlock northward travel
river_bridge.description = 'The path northward on this well-constructed bridge is no longer blocked after your brutal scuffle with Jaspern, and yet the flowing river below seems unperturbed.'
river_bridge.locs_connected[:north] = true
river_bridge.paths[:north] = true
return
end

Expand Down
5 changes: 3 additions & 2 deletions lib/gemwarrior/entities/player.rb
Original file line number Diff line number Diff line change
Expand Up @@ -76,9 +76,10 @@ def check_self(show_pic = true)

abilities = ''
if special_abilities.empty?
abilities = 'none...yet(?)'
abilities = " none...yet(?)\n"
else
special_abilities.each do |sp|
abilities << "\n"
abilities << " #{Formatting.upstyle(sp.to_s).ljust(15).colorize(:yellow)}: #{PlayerLevels.get_ability_description(sp)}\n"
end
end
Expand All @@ -96,7 +97,7 @@ def check_self(show_pic = true)
self_text << " (#{net_defense} w/ #{armor_slot})".colorize(:yellow) unless net_defense.nil?
self_text << "\n"
self_text << "DEXTERITY : #{self.dexterity}\n"
self_text << "ABILITIES :\n"
self_text << "ABILITIES :"
self_text << "#{abilities}"

if GameOptions.data['debug_mode']
Expand Down
2 changes: 1 addition & 1 deletion lib/gemwarrior/evaluator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -583,7 +583,7 @@ def parse(input)
if param1.nil?
ERROR_DROP_PARAM_MISSING
else
world.player.inventory.drop_item(param1)
world.player.inventory.drop_item(param1, player_cur_location)
end
when 'equip', 'eq'
if param1.nil?
Expand Down
5 changes: 4 additions & 1 deletion lib/gemwarrior/game.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@
require 'colorize'
require 'matrext'

require_relative 'entities/armor/iron_helmet'
require_relative 'entities/items/herb'
require_relative 'entities/weapons/dagger'
require_relative 'misc/animation'
require_relative 'misc/audio'
require_relative 'misc/formatting'
Expand All @@ -18,7 +21,7 @@ module Gemwarrior
class Game
# CONSTANTS
INVENTORY_DEFAULT = Inventory.new
INVENTORY_DEBUG = Inventory.new([Herb.new])
INVENTORY_DEBUG = Inventory.new([Herb.new, Herb.new, Herb.new])
ROX_DEFAULT = 0
ROX_DEBUG = 300

Expand Down
24 changes: 24 additions & 0 deletions lib/gemwarrior/game_assets.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,10 @@ def self.add obj
def self.data
@@data ||= []
end

def self.get(name)
self.data.find { |i| i.name.downcase == name.downcase }
end
end

module GameCreatures
Expand All @@ -28,6 +32,10 @@ def self.add obj
def self.data
@@data ||= []
end

def self.get(name)
self.data.find { |i| i.name.downcase == name.downcase }
end
end

module GameItems
Expand All @@ -39,6 +47,10 @@ def self.add obj
def self.data
@@data ||= []
end

def self.get(name)
self.data.find { |i| i.name.downcase == name.downcase }
end
end

module GameMonsters
Expand All @@ -50,6 +62,10 @@ def self.add obj
def self.data
@@data ||= []
end

def self.get(name)
self.data.find { |i| i.name.downcase == name.downcase }
end
end

module GamePeople
Expand All @@ -61,6 +77,10 @@ def self.add obj
def self.data
@@data ||= []
end

def self.get(name)
self.data.find { |i| i.name.downcase == name.downcase }
end
end

module GameWeapons
Expand All @@ -72,5 +92,9 @@ def self.add obj
def self.data
@@data ||= []
end

def self.get(name)
self.data.find { |i| i.name.downcase == name.downcase }
end
end
end
83 changes: 59 additions & 24 deletions lib/gemwarrior/inventory.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ class Inventory
ERROR_ITEM_EQUIP_NONARMAMENT = 'That item cannot be equipped.'
ERROR_ITEM_UNEQUIP_INVALID = 'You do not possess anything called that to unequip.'
ERROR_ITEM_UNEQUIP_NONARMAMENT = 'That item cannot be unequipped.'
VOWELS = 'aeiou'

attr_accessor :items,
:weapon,
Expand All @@ -29,8 +30,50 @@ def is_empty?
self.items.nil? || self.items.empty?
end

def contents
is_empty? ? nil : list_contents
end

def list_contents
is_empty? ? '[empty]' : "#{self.items.map(&:name).join(', ')}"
if is_empty?
'You possess nothing.'
else
item_hash = {}
self.items.map(&:name).each do |i|
i_sym = i.to_sym
if item_hash.keys.include? i_sym
item_hash[i_sym] += 1
else
item_hash[i_sym] = 1
end
end

if item_hash.length == 1
item_hash.each do |i, q|
if q > 1
return "You have #{q} #{i.to_s.colorize(:yellow)}#{'s'.colorize(:yellow)}."
else
article = VOWELS.include?(i[0]) ? 'an' : 'a'
return "You have #{article} #{i.to_s.colorize(:yellow)}."
end
end
else
item_list_text = 'You have '
item_arr = []
item_hash.each do |i, q|
if q > 1
item_arr.push("#{q} #{i.to_s.colorize(:yellow)}#{'s'.colorize(:yellow)}")
else
article = VOWELS.include?(i[0]) ? 'an' : 'a'
item_arr.push("#{article} #{i.to_s.colorize(:yellow)}")
end
end

item_arr[-1].replace("and #{item_arr[-1]}.")

return item_list_text << item_arr.join(', ')
end
end
end

def contains_item?(item_name)
Expand Down Expand Up @@ -77,10 +120,10 @@ def equip_item(item_name)
i.equipped = true
if i.is_weapon
self.weapon = i
return "The #{i.name} has taken charge, and been equipped."
return "The #{i.name.colorize(:yellow)} has taken charge, and been equipped."
elsif i.is_armor
self.armor = i
return "The #{i.name} has fortified, and has been equipped."
return "The #{i.name.colorize(:yellow)} has fortified, and has been equipped."
end
else
return ERROR_ITEM_EQUIP_NONARMAMENT
Expand All @@ -100,10 +143,10 @@ def unequip_item(item_name)
i.equipped = false
if i.is_weapon
self.weapon = nil
return "The #{i.name} has been demoted to unequipped."
return "The #{i.name.colorize(:yellow)} has been demoted to unequipped."
elsif i.is_armor
self.armor = nil
return "The #{i.name} has been demoted to unequipped."
return "The #{i.name.colorize(:yellow)} has been demoted to unequipped."
end
else
return ERROR_ITEM_UNEQUIP_NONARMAMENT
Expand All @@ -128,7 +171,7 @@ def add_item(item_name, cur_loc = nil, player = nil)
# stats
player.items_taken += 1

return "Added #{item_name} to your increasing collection of bits of tid.".colorize(:green)
return "#{"Added".colorize(:green)} #{item_name.colorize(:yellow)} #{"to your increasing collection of bits of tid".colorize(:green)}."
else
return ERROR_ITEM_ADD_UNTAKEABLE.colorize(:red)
end
Expand All @@ -138,29 +181,21 @@ def add_item(item_name, cur_loc = nil, player = nil)
ERROR_ITEM_ADD_INVALID.colorize(:red)
end

def drop_item(item_name, cur_loc)
if contains_item?(item_name)
remove_item(item_name)
cur_loc.add_item(item_name)
"You dropped #{item_name.colorize(:yellow)}."
else
ERROR_ITEM_REMOVE_INVALID
end
end

def remove_item(item_name)
self.items.delete_at(self.items.map(&:name).index(item_name) || self.items.length)
unless self.weapon.nil?
self.weapon = nil if self.weapon.name.eql?(item_name)
end
end

def drop_item(item_name)
if contains_item?(item_name)
print "Are you sure you want to permanently throw away #{item_name}? (y/n) "
answer = gets.chomp.downcase

case answer
when 'y', 'yes'
remove_item(item_name)

return "The #{item_name} has been thrown on the ground, but far out of reach, and you're much too lazy to go get it now, so it's as good as gone."
else
return "You decide to keep #{item_name} for now."
end
else
ERROR_ITEM_REMOVE_INVALID
end
end
end
end
Loading

0 comments on commit 601ac05

Please sign in to comment.