Improve attributes code.

This commit is contained in:
Major-
2015-03-02 03:57:15 +00:00
parent d74d2fc987
commit 03c1fe16ac
14 changed files with 217 additions and 149 deletions
+16 -16
View File
@@ -16,7 +16,7 @@ java_import 'org.apollo.game.model.entity.attr.StringAttribute'
# Declares an attribute and adds its definition.
def declare_attribute(name, default, persistence=:transient)
raise "Attribute #{name} clashes with an existing variable." if (Player.method_defined?(name) || Mob.method_defined?(name) || Npc.method_defined?(name))
AttributeMap::add_definition(name.to_s, AttributeDefinition.new(default, get_persistence(persistence), get_type(default)))
AttributeMap::define(name.to_s, AttributeDefinition.new(default, get_persistence(persistence), get_type(default)))
end
@@ -25,35 +25,35 @@ private
# The existing Mob class.
class Mob
# Overrides method_missing
# Overrides method_missing to implement the functionality.
def method_missing(symbol, *args)
name = symbol.to_s.strip
if name[-1] == "="
raise "Expected argument count of 1, received #{args.length}" unless args.length == 1
name = name[0...-1].strip # Drop the equals
name = name[0...-1].strip # Drop the equals and trim whitespace.
set_attribute(name, to_attribute(args[0]))
elsif AttributeMap::get_definition(name).nil?
super(symbol, *args)
else
attribute = get_attribute(name); definition = AttributeMap::get_definition(name)
value = attribute.nil? ? definition.default : attribute.value
return (definition.type == AttributeType::SYMBOL) ? value.to_sym : value
attribute = get_attribute(name)
value = attribute.value
return (attribute.type == AttributeType::SYMBOL) ? value.to_sym : value
end
end
end
# Gets the appropriate attribute for the specified value.
def to_attribute(value)
case value
when String, Symbol then return StringAttribute.new(value.to_s, value.is_a?(Symbol))
when Integer, Float then return NumericalAttribute.new(value)
when TrueClass, FalseClass then return BooleanAttribute.new(value)
else raise "Undefined attribute type #{value.class}."
end
def to_attribute(value)
case value
when String, Symbol then return StringAttribute.new(value.to_s, value.is_a?(Symbol))
when Integer, Float then return NumericalAttribute.new(value)
when TrueClass, FalseClass then return BooleanAttribute.new(value)
else raise "Undefined attribute type #{value.class}."
end
end
# Gets the attribute type of the specified value.
@@ -72,5 +72,5 @@ end
def get_persistence(persistence)
raise "Undefined persistence type #{persistence}." unless [ :persistent, :transient ].include?(persistence)
return (persistence == :persistent) ? AttributePersistence::SERIALIZED : AttributePersistence::TRANSIENT
return (persistence == :persistent) ? AttributePersistence::PERSISTENT : AttributePersistence::TRANSIENT
end