diff --git a/lib/aethyr/core/objects/game_object.rb b/lib/aethyr/core/objects/game_object.rb index b730ba25b586c2ed42505dc02c3ffd2d0b031f17..cb1ec4e6f2fdc627c957b312c9adedb369f05fc2 100644 --- a/lib/aethyr/core/objects/game_object.rb +++ b/lib/aethyr/core/objects/game_object.rb @@ -5,10 +5,12 @@ require 'aethyr/core/objects/traits/lexicon' require 'aethyr/core/util/guid' require 'aethyr/core/objects/info/info' require 'aethyr/core/event' +require 'aethyr/core/util/defaults' #Base class for all game objects, including players. Should be subclassed to do anything useful. class GameObject < Publisher include Lexicon + include Defaults attr_reader :short_desc, :game_object_id, :alt_names, :generic, :article, :sex, :show_in_look, :actions, :balance, :admin, :manager attr_accessor :container, :show_in_look, :actions, :pose, :visible, :comment, :movable, :quantity, :info @@ -17,6 +19,16 @@ class GameObject < Publisher alias :can? :respond_to? alias :goid :game_object_id + Defaults::default(:gender) do + if @sex == 'm' + Lexicon::Gender::MASCULINE + elsif @sex == 'f' + Lexicon::Gender::FEMININE + else + Lexicon::Gender::NEUTER + end + end + #Creates a new GameObject. Most of this long list of parameters is simply ignored at creation time, #because they can all be set later. def initialize(game_object_id = nil, container = nil, name = "", alt_names = Array.new, short_desc = "Nothing interesting here.", long_desc = "", generic = "", sex = "n", article = "a") @@ -37,13 +49,6 @@ class GameObject < Publisher @generic = generic #The sex of the object @sex = sex - if @sex == 'm' - @gender = Lexicon::Gender::MASCULINE - elsif @sex == 'f' - @gender = Lexicon::Gender::FEMININE - else - @gender = Lexicon::Gender::NEUTER - end #The article of the object ('a','an',etc) @article = article @visible = true @@ -76,6 +81,8 @@ class GameObject < Publisher @plural = nil @actions = Set.new @admin = false + + load_defaults end def attributes diff --git a/lib/aethyr/core/util/defaults.rb b/lib/aethyr/core/util/defaults.rb new file mode 100644 index 0000000000000000000000000000000000000000..1a03839e91ef75188274975695a2ca89f80ea223 --- /dev/null +++ b/lib/aethyr/core/util/defaults.rb @@ -0,0 +1,31 @@ +module Defaults + @@defaults = {} + + def self.default(attribute_raw, &block) + default_exists = @@defaults.key? self.name.to_s + local_defaults = default_exists ? @@defaults[self.name.to_s] : [] + + attribute = "@".concat(attribute_raw.to_s).to_sym + + local_defaults.push({:attribute => attribute, :block => block}) + @@defaults[self.name.to_s] = local_defaults if not default_exists + end + + def load_defaults + return if not @@defaults.key? self.class.name.to_s + local_defaults = @@defaults[self.class.name.to_s] + local_defaults.each do |default| + attribute = default[:attribute] + block = default[:block] + if defined?(attribute).nil? + self.set_default(attribute, &block) + end + end + end + + private + + def set_default(attribute) + self.set_instance_variable(attribute, yield) + end +end