mirror of
https://github.com/2006-Scape/apollo.git
synced 2026-07-03 08:39:11 +00:00
Add attributes system.
This commit is contained in:
@@ -0,0 +1,67 @@
|
||||
require 'java'
|
||||
|
||||
java_import 'org.apollo.game.model.Entity'
|
||||
|
||||
# Maps attribute names (i.e. symbols) to attribute definitions.
|
||||
ATTRIBUTE_DEFINITIONS = {}
|
||||
|
||||
class Entity
|
||||
|
||||
# The map of strings to attributes.
|
||||
def attributes
|
||||
@attributes ||= {}
|
||||
end
|
||||
|
||||
# Overridies method_missing
|
||||
def method_missing(symbol, *args)
|
||||
name = symbol.to_s.strip
|
||||
|
||||
if name[-1] == "="
|
||||
raise "Error - expected argument count of 1, received #{args.length}" unless args.length == 1
|
||||
|
||||
name = name[0...-1].strip # Drop the equals and preceeding whitespace
|
||||
attributes[name] = args[0]
|
||||
elsif ATTRIBUTE_DEFINITIONS[name] == nil
|
||||
super(symbol, *args)
|
||||
else
|
||||
return attributes[name] || ATTRIBUTE_DEFINITIONS[name].default
|
||||
end
|
||||
end
|
||||
|
||||
def to_s
|
||||
return value.to_s
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
# An attribute belonging to an entity.
|
||||
class AttributeDefinition
|
||||
attr_reader :default, :persistence
|
||||
|
||||
def initialize(default, persistence=:transient)
|
||||
@default = default
|
||||
@type = get_type
|
||||
@persistence = persistence
|
||||
end
|
||||
|
||||
def to_s
|
||||
return "[AttributeDefinition - default=#{default}, type=#{type}, persistence=#{persistence}]."
|
||||
end
|
||||
|
||||
private # Gets the type of an attribute from its default value.
|
||||
def get_type
|
||||
case @default
|
||||
when Fixnum, Integer then type = :number
|
||||
when String then type = :string
|
||||
when Symbol then type = :symbol
|
||||
when TrueClass, FalseClass then type = :boolean
|
||||
else raise "Error - #{value} has an unrecognised attribute type of #{value.class}."
|
||||
end
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
# Declares an attribute which can then be assigned.
|
||||
def declare_attribute(name, default, persistence=:transient)
|
||||
ATTRIBUTE_DEFINITIONS[name.to_s] = AttributeDefinition.new(default, persistence)
|
||||
end
|
||||
@@ -0,0 +1,14 @@
|
||||
<?xml version="1.0"?>
|
||||
<plugin>
|
||||
<id>attributes</id>
|
||||
<version>0.9</version>
|
||||
<name>Attributes</name>
|
||||
<description>Adds an attribute system for entites.</description>
|
||||
<authors>
|
||||
<author>Major</author>
|
||||
</authors>
|
||||
<scripts>
|
||||
<script>attributes.rb</script>
|
||||
</scripts>
|
||||
<dependencies />
|
||||
</plugin>
|
||||
Reference in New Issue
Block a user