Add consumables base and support food consumption.

This commit is contained in:
Major-
2014-02-23 22:58:56 +00:00
parent 86b8eea4f4
commit 16b0ffaeeb
3 changed files with 117 additions and 0 deletions
+41
View File
@@ -0,0 +1,41 @@
require 'java'
# A map of item ids to consumables.
CONSUMABLES = {}
CONSUME_ANIMATION_ID = 829
# An item that can be consumed to produce a skill effect.
class Consumable
attr_reader :name, :id
def initialize(name, id, sound_id)
@name = name.to_s.gsub(/_/, ' ')
@id = id
@sound_id = sound_id
end
def consume(player)
# Override to provide specific functionality.
end
end
# Appends a consumable to the map, with its id as the key.
def append_consumable(consumable)
CONSUMABLES[consumable.id] = consumable
end
# Intercepts the first item option event and consumes the consumable, if necessary.
on :event, :item_option do |ctx, player, event|
if (event.option == 1)
consumable = CONSUMABLES[event.id]
unless consumable == nil
player.inventory.reset(event.slot)
player.play_animation(Animation.new(CONSUME_ANIMATION_ID))
consumable.consume(player)
ctx.break_handler_chain
end
end
end
+61
View File
@@ -0,0 +1,61 @@
require 'java'
java_import 'org.apollo.game.model.Animation'
java_import 'org.apollo.game.model.Player'
java_import 'org.apollo.game.model.Skill'
EAT_FOOD_SOUND = 317
# Todo support other foods (e.g. pizza)
# Represents an edible piece of food, such as bread or fish.
class Food < Consumable
def initialize(name, id, restoration)
super(name, id, EAT_FOOD_SOUND)
@restoration = restoration
end
# Restore the appropriate amount of hitpoints when consumed.
def consume(player)
hitpoints = player.skill_set.skill(HITPOINTS_SKILL_ID)
new_curr = hitpoints.current_level + @restoration
new_curr = hitpoints.maximum_level if new_curr > hitpoints.maximum_level
player.skill_set.set_skill(HITPOINTS_SKILL_ID, Skill.new(hitpoints.experience, new_curr, hitpoints.maximum_level))
player.send_message("You eat the #{name}.", true)
end
end
# Appends a food item to the list of consumables.
def append_food(hash)
raise 'Hash must contain a name, id, and a restoration value.' unless (hash.has_key?(:name) && hash.has_key?(:id) && hash.has_key?(:restoration))
name = hash[:name]; id = hash[:id]; restoration = hash[:restoration]
append_consumable(Food.new(name, id, restoration))
end
append_food :name => :bread, :id => 2309, :restoration => 4
append_food :name => :cooked_meat, :id => 2142, :restoration => 3
append_food :name => :cooked_chicken, :id => 2140, :restoration => 3
append_food :name => :ugthanki_meat, :id => 1861, :restoration => 3
append_food :name => :anchovies, :id => 319, :restoration => 1
append_food :name => :shrimps, :id => 315, :restoration => 3
append_food :name => :sardine, :id => 325, :restoration => 3
append_food :name => :herring, :id => 347, :restoration => 5
append_food :name => :mackeral, :id => 355, :restoration => 5
append_food :name => :trout, :id => 333, :restoration => 7
append_food :name => :cod, :id => 339, :restoration => 7
append_food :name => :pike, :id => 351, :restoration => 8
append_food :name => :salmon, :id => 329, :restoration => 9
append_food :name => :tuna, :id => 361, :restoration => 10
append_food :name => :lobster, :id => 379, :restoration => 12
append_food :name => :bass, :id => 365, :restoration => 13
append_food :name => :swordfish, :id => 373, :restoration => 14
append_food :name => :monkfish, :id => 7946, :restoration => 16
append_food :name => :shark, :id => 385, :restoration => 20
append_food :name => :sea_turtle, :id => 397, :restoration => 21
append_food :name => :manta_ray, :id => 391, :restoration => 22
+15
View File
@@ -0,0 +1,15 @@
<?xml version="1.0"?>
<plugin>
<id>consumables</id>
<version>0.1</version>
<name>Consumables</name>
<description>Adds support for consumables (e.g. potions, food).</description>
<authors>
<author>Major</author>
</authors>
<scripts>
<script>consumable.rb</script>
<script>food.rb</script>
</scripts>
<dependencies />
</plugin>