mirror of
https://github.com/2006-Scape/apollo.git
synced 2026-07-03 08:39:11 +00:00
Add support for player / npc following
Implements a new MobExtension plugin which adds 'follow', and 'chase' mixins that allow the mob to follow behind another mob, and chase them while keeping at a safe distance to fire projectiles. Also adds a new public method 'raycast' to CollisionManager, for drawing a line through the world using bresenham's line algorithm whille checking for any impenetrable objects.
This commit is contained in:
+58
@@ -0,0 +1,58 @@
|
||||
on :message, :walk do |player, msg|
|
||||
player.reset_interacting_mob
|
||||
end
|
||||
|
||||
on :message, :player_action do |player, msg|
|
||||
## todo: need a better way of mapping option numbers to their purpose
|
||||
player.follow($world.player_repository.get(msg.index)) if msg.option == 3
|
||||
end
|
||||
|
||||
##
|
||||
# A <code>MobExtension</code> for making a <code>Mob</code> trail behind, or chase a given mob.
|
||||
module FollowingMobExtension
|
||||
##
|
||||
# Follow a mob and trail behind them.
|
||||
def follow(mob)
|
||||
do_follow(self, mob, behind: true)
|
||||
end
|
||||
|
||||
##
|
||||
# Chase a mob (with the intention of getting in front of them), also optionally
|
||||
# stopping within a projectile distance when at a position where a projectile can
|
||||
# reach the target.
|
||||
def chase(mob, projectile_distance: nil)
|
||||
do_follow(self, mob, front: true, projectile_distance: projectile_distance)
|
||||
end
|
||||
end
|
||||
|
||||
MobExtension::register(FollowingMobExtension)
|
||||
|
||||
private
|
||||
|
||||
def do_follow(source, mob, behind: false, front: false, projectile_distance: nil)
|
||||
source.interacting_mob = mob
|
||||
|
||||
schedule 0, true do |task|
|
||||
# stop the task unless we're still interacting with the other mob.
|
||||
unless self.interacting_mob.eql? mob
|
||||
task.stop
|
||||
next
|
||||
end
|
||||
|
||||
next unless source.walking_queue.size <= 1
|
||||
|
||||
distance = mob.position.get_distance(source.position)
|
||||
|
||||
unless projectile_distance.nil?
|
||||
next if distance <= projectile_distance &&
|
||||
$world.collision_manager.raycast(source.position, mob.position)
|
||||
end
|
||||
|
||||
if distance > 15
|
||||
reset_interacting_mob
|
||||
end
|
||||
|
||||
walk_to(mob, behind: behind, front: front)
|
||||
end
|
||||
end
|
||||
|
||||
Executable
+16
@@ -0,0 +1,16 @@
|
||||
<?xml version="1.0"?>
|
||||
<plugin>
|
||||
<id>mob-following</id>
|
||||
<version>1</version>
|
||||
<name>Following</name>
|
||||
<description>Adds following for mobs.</description>
|
||||
<authors>
|
||||
<author>Steve Soltys</author>
|
||||
</authors>
|
||||
<scripts>
|
||||
<script>following.rb</script>
|
||||
</scripts>
|
||||
<dependencies>
|
||||
<dependency>mob-walk-to</dependency>
|
||||
</dependencies>
|
||||
</plugin>
|
||||
Reference in New Issue
Block a user