Files
apollo/data/plugins/combat/combat_action.rb
T
Gary Tierney 3082fade1d Add ranged combat support
* Clean up the CombatAction and Attack code to make it easier to use for
  range.

* Add collision detection to the distance checks before attacking in the
  CombatAction.

* Create a Ruby DSL for defining projectile types and fix the
  ProjectileUpdateOperation so it uses the correct position offset.

* Fix the packet structure of the HintIconMessageEncoder.
2016-02-29 22:24:26 +00:00

67 lines
1.6 KiB
Ruby

java_import 'org.apollo.game.action.Action'
java_import 'org.apollo.game.model.entity.EntityType'
class CombatAction < Action
def initialize(source, once = false)
super(0, true, source)
mob.attacking = true
@combat_state = get_combat_state(source)
@attack_timer = 100
@once = once
end
def can_attack?
next_attack = @combat_state.next_attack true
target = @combat_state.target
current_distance = mob.position.get_distance target.position
attack_collision_type = next_attack.range > 1 ? EntityType::PROJECTILE : EntityType::NPC
in_range = current_distance <= next_attack.range && !$world.intersects(mob.position, target.position, attack_collision_type)
unless in_range
mob.follow @combat_state.target, next_attack.range
return false
end
mob.attack_timer >= next_attack.speed
end
def try_attack
next_attack = @combat_state.next_attack false
if mob.is_a? Player
next_attack.requirements.each { |requirement| requirement.validate mob }
next_attack.requirements.each { |requirement| requirement.apply! mob }
end
next_attack.do(mob, @combat_state.target)
rescue AttackRequirementException => e
mob.send_message e.message
ensure
mob.attack_timer = 0
end
def execute
if @combat_state.target.nil? || !@combat_state.target.is_active || @combat_state.next_attack(true).nil?
stop
return
end
return unless can_attack?
try_attack
stop if @once
end
def stop
super
mob.attacking = false
mob.following = -1
@combat_state.reset
end
end