mirror of
https://github.com/2006-Scape/apollo.git
synced 2026-07-07 00:38:46 +00:00
Housekeeping
This commit is contained in:
@@ -0,0 +1,49 @@
|
||||
require 'java'
|
||||
|
||||
java_import 'org.apollo.game.message.impl.DisplayCrossbonesMessage'
|
||||
java_import 'org.apollo.game.model.entity.Player'
|
||||
|
||||
# Registers an area action.
|
||||
def area_action(name, &block)
|
||||
AREA_ACTIONS[name] = action = AreaAction.new
|
||||
action.instance_eval(&block)
|
||||
end
|
||||
|
||||
AREA_ACTIONS = {}
|
||||
|
||||
private
|
||||
|
||||
# An action that is called when a player enters or exits an area.
|
||||
class AreaAction
|
||||
|
||||
# Sets the block to be called when the player enters the area.
|
||||
def on_entry(&block)
|
||||
@on_enter = block
|
||||
end
|
||||
|
||||
# Sets the block to be called while the player is in the area.
|
||||
def while_in(&block)
|
||||
@while_in = block
|
||||
end
|
||||
|
||||
# Sets the block to be called when the player exits the area.
|
||||
def on_exit(&block)
|
||||
@on_exit = block
|
||||
end
|
||||
|
||||
# Called when the player has entered an area this action is registered to.
|
||||
def entered(player, position)
|
||||
@on_enter.call(player, position) unless @on_enter.nil?
|
||||
end
|
||||
|
||||
# Called while the player is in area this action is registered to.
|
||||
def inside(player, position)
|
||||
@while_in.call(player, position) unless @while_in.nil?
|
||||
end
|
||||
|
||||
# Called when the player has exited an area this action is registered to.
|
||||
def exited(player, position)
|
||||
@on_exit.call(player, position) unless @on_exit.nil?
|
||||
end
|
||||
|
||||
end
|
||||
@@ -0,0 +1,108 @@
|
||||
require 'java'
|
||||
|
||||
java_import 'org.apollo.game.model.Position'
|
||||
java_import 'org.apollo.game.model.entity.EntityType'
|
||||
java_import 'org.apollo.game.model.entity.Player'
|
||||
|
||||
# Creates a new area and registers it with the supplied coordinates.
|
||||
def area(hash)
|
||||
failure_message = 'Hash must contain a name, coordinates, and actions pair.'
|
||||
fail failure_message unless hash.has_keys?(:name, :coordinates, :actions)
|
||||
|
||||
name, coordinates, actions = hash[:name], hash[:coordinates], hash[:actions]
|
||||
|
||||
actions = [actions] if actions.is_a?(Symbol)
|
||||
actions.map! { |action| AREA_ACTIONS[action] }
|
||||
@areas << Area.new(name, coordinates, actions)
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
# A map of coordinates (as an array) to areas.
|
||||
@areas = []
|
||||
|
||||
# An area of the game world.
|
||||
class Area
|
||||
|
||||
def initialize(name, coordinates, actions)
|
||||
@name = name
|
||||
@coordinates = coordinates
|
||||
@actions = actions
|
||||
end
|
||||
|
||||
def min_x # TODO: better data structure and methods than this
|
||||
@coordinates[0]
|
||||
end
|
||||
|
||||
def min_y
|
||||
@coordinates[1]
|
||||
end
|
||||
|
||||
def max_x
|
||||
@coordinates[2]
|
||||
end
|
||||
|
||||
def max_y
|
||||
@coordinates[3]
|
||||
end
|
||||
|
||||
def height
|
||||
@coordinates[4]
|
||||
end
|
||||
|
||||
# Called when the player has entered the area.
|
||||
def entered(player, position)
|
||||
@actions.each { |action| action.entered(player, position) }
|
||||
end
|
||||
|
||||
# Called when the player has moved, but is still inside the area (and was in the area before).
|
||||
def inside(player, position)
|
||||
@actions.each { |action| action.inside(player, position) }
|
||||
end
|
||||
|
||||
# Called when the player has exited the area.
|
||||
def exited(player, position)
|
||||
@actions.each { |action| action.exited(player, position) }
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
on :login do |event|
|
||||
player = event.player
|
||||
|
||||
@areas.each do |area|
|
||||
area.entered(player) if player.position.inside(area)
|
||||
end
|
||||
end
|
||||
|
||||
# Listen for the MobPositionUpdateEvent and update the area listeners if appropriate.
|
||||
on :mob_position_update do |event|
|
||||
mob = event.mob
|
||||
next unless mob.entity_type == EntityType::PLAYER
|
||||
|
||||
old = mob.position
|
||||
updated = event.next
|
||||
@areas.each do |area|
|
||||
was_inside = old.inside(area)
|
||||
next_inside = updated.inside(area)
|
||||
|
||||
if was_inside
|
||||
next_inside ? area.inside(mob, updated) : area.exited(mob, updated)
|
||||
else
|
||||
area.entered(mob, updated) if next_inside
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
# The existing Position class.
|
||||
class Position
|
||||
|
||||
# Returns whether or not this Position is inside the specified Area.
|
||||
def inside(area)
|
||||
return false if x < area.min_x || x > area.max_x || y < area.min_y || y > area.max_y
|
||||
z = area.height
|
||||
|
||||
z.nil? || z == height
|
||||
end
|
||||
|
||||
end
|
||||
@@ -0,0 +1,15 @@
|
||||
<?xml version="1.0"?>
|
||||
<plugin>
|
||||
<id>areas</id>
|
||||
<version>0.9</version>
|
||||
<name>Areas</name>
|
||||
<description>Adds support for areas.</description>
|
||||
<authors>
|
||||
<author>Major</author>
|
||||
</authors>
|
||||
<scripts>
|
||||
<script>actions.rb</script>
|
||||
<script>areas.rb</script>
|
||||
</scripts>
|
||||
<dependencies />
|
||||
</plugin>
|
||||
Reference in New Issue
Block a user