there has to be a better way to do this
alias artifact_setup_later setup
def setup(actor_id)
@artifact1_id = @artifact2_id = @artifact3_id = @artifact4_id =
@artifact5_id = @artifact6_id = 0
@rechargeing_artifacts = {}
@recharge_artifacts = []
artifact_setup_later(actor_id)
end
def add_recharging_artifact(id, turns)
@rechargeing_artifacts[id] = turns
@recharge_artifacts.push(id)
end
def artifact_can_use?(id)
return artifacts.include?(id) ? @recharge_artifacts.include?(id) ? false : true : false
end
def update_recharging_artifact
recharged = []
@rechargeing_artifacts.each_key { |id|
@rechargeing_artifacts[id] -= 1
if @rechargeing_artifacts[id] <= 0
recharged.push(id)
end
}
if recharged != []
recharged.each { |id2|
@rechargeing_artifacts.delete(id2)
@recharge_artifacts.delete(id2)
}
end
end
those methods are added to the game actor class at a part of my artifact system and as you can see I'm using both a hash and an array for determining if an artifact is recharging and there has to be a better way. now, to see if an artifact is recharging, all I do is test to see if its armor id is included in @recharge_artifacts but I also need to connect the Id number and the number of battle turns left till it recharges. and I don't think using a 2-D array will work because I need to test for id inclusion in the array, any idea how how to improve the system? its running on a crappy n00b workaround right now.
Yes, you don't need the array. You can easily get an array of all IDs used by using hash.keys which would then return all IDs that are currently active. When removing them from the hash, you need to use hash.delete(ID) to make sure you remove it completely (so it doesn't happen that you use hash[ID] and get a nil value because there's a nil value actually stored corresponding to that key).
From a first look at your code you only need to change "@recharge_artifacts.include?(id)" to "@rechargeing_artifacts.has_key?(id)" and remove all other lines that use @recharge_artifacts.
BTW, it's "recharging", not "rechargeing". xD
EDIT:
return artifacts.include?(id) ? @recharge_artifacts.include?(id) ? false : true : false
This is kind of redunant as the results of those methods already do return boolean values. It's also confusing a bit because of a double condition. This is better:
return (artifacts.include?(id) && !@recharge_artifacts.include?(id))
I also suggest the use of parenthesis ( () brackets) to distinguish better what part of the line is used in which condition. In your original line it would look like this:
return (artifacts.include?(id) ? (@recharge_artifacts.include?(id) ? false : true) : false)
wait @recharging_artifacts.keys would return an array of all the armor ids that have values mapped to them?
so @recharging_artifacts.has_key?(id) could replace my @recharge_artifacts.include?(id) ? cool thankyou Blizz