mirror of
https://github.com/2006-Scape/apollo.git
synced 2026-07-05 16:49:04 +00:00
Housekeeping
This commit is contained in:
@@ -0,0 +1,44 @@
|
||||
require 'set'
|
||||
|
||||
# Contains door-related constants.
|
||||
module DoorConstants
|
||||
|
||||
# TODO: GameObjectOrientation enumeration in Apollo's core?
|
||||
# The orientation of a door.
|
||||
module Orientation
|
||||
WEST = 0
|
||||
NORTH = 1
|
||||
EAST = 2
|
||||
SOUTH = 3
|
||||
end
|
||||
|
||||
# The size of a door object.
|
||||
DOOR_SIZE = 1
|
||||
|
||||
# Door object ids that have a hinge on the left side.
|
||||
LEFT_HINGE_DOORS = Set.new [1516, 1536, 1533]
|
||||
|
||||
# Door object ids that have a hinge on the right side.
|
||||
RIGHT_HINGE_DOORS = Set.new [1519, 1530, 4465, 4467, 3014, 3017, 3018, 3019]
|
||||
|
||||
# The hash of orientations that a door will translate to when opened.
|
||||
ORIENTATIONS = {
|
||||
|
||||
# Orientations for doors that have a hinge on the left side.
|
||||
left_side_hinge: {
|
||||
Orientation::NORTH => Orientation::WEST,
|
||||
Orientation::SOUTH => Orientation::EAST,
|
||||
Orientation::WEST => Orientation::SOUTH,
|
||||
Orientation::EAST => Orientation::NORTH
|
||||
},
|
||||
|
||||
# Orientations for doors that have a hinge on the right side.
|
||||
right_side_hinge: {
|
||||
Orientation::NORTH => Orientation::EAST,
|
||||
Orientation::SOUTH => Orientation::WEST,
|
||||
Orientation::WEST => Orientation::NORTH,
|
||||
Orientation::EAST => Orientation::SOUTH
|
||||
}
|
||||
}
|
||||
|
||||
end
|
||||
@@ -0,0 +1,54 @@
|
||||
|
||||
java_import 'org.apollo.game.action.DistancedAction'
|
||||
java_import 'org.apollo.game.model.event.Event'
|
||||
|
||||
private
|
||||
|
||||
# A distanced action which opens a door.
|
||||
class OpenDoorAction < DistancedAction
|
||||
include DoorConstants
|
||||
|
||||
attr_reader :door
|
||||
|
||||
def initialize(mob, door)
|
||||
super(0, true, mob, door.position, DOOR_SIZE)
|
||||
@door = door
|
||||
end
|
||||
|
||||
def executeAction
|
||||
if $world.submit(OpenDoorEvent.new(mob, @door))
|
||||
mob.turn_to(@door.position)
|
||||
DoorUtil.toggle(@door)
|
||||
end
|
||||
|
||||
stop
|
||||
end
|
||||
|
||||
def equals(other)
|
||||
get_class == other.get_class && @door == other.door
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
# A PlayerEvent that is fired when a player attempts to open a door.
|
||||
class OpenDoorEvent < PlayerEvent
|
||||
attr_reader :door
|
||||
|
||||
def initialize(player, door)
|
||||
super(player)
|
||||
@door = door
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
|
||||
# Listens for FirstObjectActions performed on doors.
|
||||
on :message, :first_object_action do |player, message|
|
||||
id = message.id
|
||||
|
||||
if DoorUtil.door?(id)
|
||||
door = DoorUtil.get_door_object(message.position, id)
|
||||
player.start_action(OpenDoorAction.new(player, door)) unless door.nil?
|
||||
end
|
||||
end
|
||||
|
||||
@@ -0,0 +1,16 @@
|
||||
<?xml version="1.0"?>
|
||||
<plugin>
|
||||
<id>door</id>
|
||||
<version>1</version>
|
||||
<name>Doors</name>
|
||||
<description>Adds support for doors throughout the game.</description>
|
||||
<authors>
|
||||
<author>Shiver</author>
|
||||
</authors>
|
||||
<scripts>
|
||||
<script>constants.rb</script>
|
||||
<script>util.rb</script>
|
||||
<script>door.rb</script>
|
||||
</scripts>
|
||||
<dependencies/>
|
||||
</plugin>
|
||||
@@ -0,0 +1,83 @@
|
||||
require 'java'
|
||||
|
||||
java_import 'org.apollo.game.model.Position'
|
||||
java_import 'org.apollo.game.model.area.Region'
|
||||
java_import 'org.apollo.game.model.entity.Entity'
|
||||
java_import 'org.apollo.game.model.entity.obj.DynamicGameObject'
|
||||
|
||||
# Contains door-related utility methods.
|
||||
module DoorUtil
|
||||
include DoorConstants
|
||||
|
||||
# A hash containing currently toggled door objects mapped to the original door objects.
|
||||
TOGGLED_DOORS = {}
|
||||
|
||||
# Translates a door's position in the direction of its orientation.
|
||||
def self.translate_door_position(door)
|
||||
position = door.position
|
||||
|
||||
case door.orientation
|
||||
when Orientation::WEST
|
||||
Position.new(position.x - 1, position.y, position.height)
|
||||
when Orientation::EAST
|
||||
Position.new(position.x + 1, position.y, position.height)
|
||||
when Orientation::NORTH
|
||||
Position.new(position.x, position.y + 1, position.height)
|
||||
when Orientation::SOUTH
|
||||
Position.new(position.x, position.y - 1, position.height)
|
||||
else fail "Unsupported orientation #{door.orientation}."
|
||||
end
|
||||
end
|
||||
|
||||
# Translates the orientation of a door to a toggled position.
|
||||
def self.translate_door_orientation(door)
|
||||
object_id = door.id
|
||||
orientation = door.orientation
|
||||
|
||||
if RIGHT_HINGE_DOORS.include?(object_id)
|
||||
return ORIENTATIONS[:right_side_hinge][orientation]
|
||||
elsif LEFT_HINGE_DOORS.include?(object_id)
|
||||
return ORIENTATIONS[:left_side_hinge][orientation]
|
||||
end
|
||||
|
||||
fail 'Given object was not registered as a door.'
|
||||
end
|
||||
|
||||
# Toggles the given door.
|
||||
def self.toggle(door)
|
||||
region = $world.region_repository.from_position(door.position)
|
||||
region.remove_entity(door)
|
||||
|
||||
if TOGGLED_DOORS.include?(door)
|
||||
original_door = TOGGLED_DOORS.delete(door)
|
||||
|
||||
original_region = $world.region_repository.from_position(original_door.position)
|
||||
original_region.add_entity(original_door)
|
||||
else
|
||||
position = translate_door_position(door)
|
||||
orientation = translate_door_orientation(door)
|
||||
type = door.type
|
||||
|
||||
toggled_door = DynamicGameObject.create_public($world, door.id, position, type, orientation)
|
||||
|
||||
toggled_region = $world.region_repository.from_position(position)
|
||||
toggled_region.add_entity(toggled_door)
|
||||
|
||||
TOGGLED_DOORS[toggled_door] = door
|
||||
end
|
||||
end
|
||||
|
||||
# Gets the door object at the given position, if it exists.
|
||||
def self.get_door_object(position, object_id)
|
||||
region = $world.region_repository.from_position(position)
|
||||
objects = region.get_entities(position, EntityType::DYNAMIC_OBJECT, EntityType::STATIC_OBJECT)
|
||||
objects.each { |game_object| return game_object if game_object.id == object_id }
|
||||
nil
|
||||
end
|
||||
|
||||
# Checks if the given game object id is a door.
|
||||
def self.door?(object_id)
|
||||
RIGHT_HINGE_DOORS.include?(object_id) || LEFT_HINGE_DOORS.include?(object_id)
|
||||
end
|
||||
|
||||
end
|
||||
Reference in New Issue
Block a user