class RuneRb::Doors::DoorManager

Public Class Methods

change_double_state(door) click to toggle source
# File app/services/doors.rb, line 156
def self.change_double_state(door)
  # Left
  x_off = 0
  y_off = 0
  face = door.orig_face
  
  if door.open
    face = 1 + door.face % 2
    
    x_off = -1 if door.orig_face == 1 || door.orig_face == 2 || door.orig_face == 3
    y_off = -1 if door.orig_face == 0
  else
    face = 2 * (-door.face/3.floor + 1) + (door.face - 1) % 2
    
    x_off = 1 if door.orig_face == 2
    x_off = -1 if door.orig_face == 0
    y_off = 1 if door.orig_face == 1
    y_off = -1 if door.orig_face == 3
  end
  
  if x_off != 0 || y_off != 0
   WORLD.region_manager.get_local_players(door.location, false).each {|p|
     p.io.send_replace_object(door.location, p.last_location, -1, 0, 0)
   }
  end
  
  door.id -= 1 if door.open
  door.id += 1 if !door.open
  door.face = face
  door.location = door.location.transform x_off, y_off, 0
  
  if door.id != nil
    WORLD.region_manager.get_local_players(door.location, false).each {|p|
      p.io.send_replace_object(door.location, p.last_location, door.id, door.face, 0)
    }
  end
  
  # Right
  x_off = 0
  y_off = 0
  face = door.r_door_orig_face
  
  if door.open
    face = 3 if door.r_door_orig_face == 0
    face = 0 if door.r_door_orig_face == 1
    face = 1 if door.r_door_orig_face == 2
    face = 2 if door.r_door_orig_face == 3
            
    x_off = 1 if door.r_door_orig_face == 0
    x_off = -1 if door.r_door_orig_face == 1 || door.r_door_orig_face == 3
    y_off = -1 if door.r_door_orig_face == 2
  else
    face = 1 if door.r_door_orig_face == 0
    face = 2 if door.r_door_orig_face == 1
    face = 3 if door.r_door_orig_face == 2
    face = 2 if door.r_door_orig_face == 3
            
    x_off = 1 if door.r_door_orig_face == 2
    x_off = -1 if door.r_door_orig_face == 0
    y_off = 1 if door.r_door_orig_face == 1
    y_off = -1 if door.r_door_orig_face == 3
  end
  
  if x_off != 0 || y_off != 0
   WORLD.region_manager.get_local_players(door.r_door_location, false).each {|p|
     p.io.send_replace_object(door.r_door_location, p.last_location, -1, 0, 0)
   }
  end
  
  door.r_door_id -= 1 if door.open
  door.r_door_id += 1 if !door.open
  door.r_door_face = face
  door.r_door_location = door.r_door_location.transform x_off, y_off, 0
  
  if door.r_door_id != nil
    WORLD.region_manager.get_local_players(door.location, false).each {|p|
      p.io.send_replace_object(door.r_door_location, p.last_location, door.r_door_id, door.r_door_face, 0)
    }
  end
end
get_double_data(id, loc) click to toggle source
# File app/services/doors.rb, line 241
def self.get_double_data(id, loc)
  @@double_data.find {|door| door[:id] == id && door[:location] == loc }
end

Public Instance Methods

change_state(door) click to toggle source
# File app/services/doors.rb, line 93
def change_state(door)
  x_off = 0
  y_off = 0
  face = door.orig_face
  
  if door.type == 0
    if door.open
      face = (door.face - 1) & 3
      
      x_off = 1 if door.orig_face == 1
      x_off = -1 if door.orig_face == 3
      y_off = 1 if door.orig_face == 0
      y_off = -1 if door.orig_face == 2
    else
      face = (door.face + 1) & 3
      
      x_off = 1 if door.orig_face == 2
      x_off = -1 if door.orig_face == 0
      y_off = 1 if door.orig_face == 1
      y_off = -1 if door.orig_face == 3
    end
  elsif door.type == 9
    if door.open
      face = 3 - door.face
    else
      face = (door.face - 1) & 3
    end
    
    x_off = 1 if door.orig_face == 0 || door.orig_face == 1
    x_off = -1 if door.orig_face == 2 || door.orig_face == 3
  end
  
  if x_off != 0 || y_off != 0
    WORLD.region_manager.get_local_players(door.location, false).each {|p|
      p.io.send_replace_object(door.location, p.last_location, -1, 0, door.type)
    }
  end
  
  door.id -= 1 if door.open
  door.id += 1 if !door.open
  door.face = face
  door.location = door.location.transform x_off, y_off, 0
end
get_data(id, loc) click to toggle source
# File app/services/doors.rb, line 237
def get_data(id, loc)
  @@single_data.find {|door| door[:id] == id && door[:location] == loc }
end
handle_door(id, loc) click to toggle source
# File app/services/doors.rb, line 67
def handle_door(id, loc)
  door = WORLD.object_manager.objects.find {|d| d && d.id == id && d.location == loc }
  
  if door != nil
    # Reset cause it's already changed
    door.reset
    
    WORLD.object_manager.objects.delete door
  else
    data = get_data id, loc
    return unless data != nil
  
    open = @@open_single_doors.find {|val| val == id} != nil
    
    # Change door for the first time
    WORLD.object_manager.objects << door = Door.new(data, open)
    
    # Move and rotate
    change_state door
    
    WORLD.region_manager.get_local_players(door.location, false).each {|p|
      p.io.send_replace_object(door.location, p.last_location, door.id, door.face, door.type)
    }
  end
end
handle_double_door(id, loc) click to toggle source
# File app/services/doors.rb, line 137
def handle_double_door(id, loc)
  door = WORLD.object_manager.objects.find {|d| d.kind_of?(DoubleDoor) && (d.id == id && d.location == loc || d.r_door_id == id && d.r_door_location == loc) }

  if door != nil
    # Reset cause it's already changed
    door.reset
    
    WORLD.object_manager.objects.delete door
  else
    data = DoorManager.get_double_data id, loc
    return unless data != nil
  
    open = @@open_double_doors.find {|val| val == id} != nil
    
    # Change door for the first time
    WORLD.object_manager.objects << door = DoubleDoor.new(data, open)
  end
end
load_double_doors() click to toggle source
# File app/services/doors.rb, line 41
def load_double_doors
  d = XmlSimple.xml_in("data/doors_double.xml")
  d["door"].each_with_index {|row, idx|
   @@double_data << 
      {
       :id => row['id'].to_i,
       :location => RuneRb::Model::Location.new(row['x'].to_i, row['y'].to_i, row['z'].to_i),
       :face => row['face'].to_i,
       :type => 0
      }
  }
  
  @@double_data.each {|door|
    handler = HOOKS[:obj_click1][door[:id]]
      
    if !handler.instance_of?(Proc)
      on_obj_option(door[:id]) {|player, loc|
        return unless player.location.within_interaction_distance?(loc)
        
        player.walking_queue.reset
        handle_double_door door[:id], loc
      }
    end
  }
end
load_single_doors() click to toggle source
# File app/services/doors.rb, line 16
def load_single_doors
  d = XmlSimple.xml_in("data/doors_single.xml")
  d["door"].each_with_index {|row, idx|
    @@single_data << 
      {
        :id => row['id'].to_i,
        :location => RuneRb::Model::Location.new(row['x'].to_i, row['y'].to_i, row['z'].to_i),
        :face => row['face'].to_i,
        :type => row['type'].to_i
      }
  }
  
  @@single_data.each {|door|
    handler = HOOKS[:obj_click1][door[:id]]
    if !handler.instance_of?(Proc)
      on_obj_option(door[:id]) {|player, loc|
        return unless player.location.within_interaction_distance?(loc)
        
        player.walking_queue.reset
        handle_door door[:id], loc
      }
    end
  }
end