Game Development Reference
In-Depth Information
As skeletons will be able to pursue both Flynn and Archie, we are using a variable
called
target_obj
to store which of the two is currently being chased. The variables
target_hidden
and
target_dist
will be used as part of the line-of-sight tests, and
pursuing
is self-explanatory. Both
gravity
and
speed
are initialized here in case the
skeleton comes back into this state after a fall, and the final four variables are the start
and end positions of the line-of-sight test. These last four could easily be temporary
variables elsewhere, but putting them here will allow us to draw the lines on screen,
too. You'll see that in operation a bit later.
2.
Reopen the
Step
event of
obj_skeleton_land
and insert these lines at the start of the
code (after the first curly bracket):
1:
x1 = x+(facing*20);
2:
y1 = y-48;
3:
x2 = target_obj.x;
4:
y2 = target_obj.y;
5:
6:
target_dist = point_distance(x1, y1, x2, y2);
7:
target_hidden = collision_line(x1, y1, x2, y2, obj_solid, true, true) ||
8:
collision_line(x1, y1, x2, y2, obj_enemy, true, false);
9:
10:
if( target_obj.x > x )
11:
target_dirn = FACE_RIGHT;
12:
else
13:
target_dirn = FACE_LEFT;
Lines 1-4 set up the start and end positions of the line-of-sight test. This begins just in
front of the skeletons eyes and ends at the origin of the target object (the center of the
sprite for both Flynn and Archie). Line 6 then calculates the distance between these
two points, as the skeletons will have a limited viewing distance.
Line 7 performs a collision test by checking if there are any collisions with
obj_solid
between the start and end points. The last parameter (set to
true
)
determines whether the calling object (the skeleton instance) should be disregarded
from collisions in the test. It makes no difference in this case, as
obj_skeleton_land
is
not a kind of
obj_solid
so such a collision would not be included anyway.
Line 8 performs a second collision test on the same line, but this time for
obj_enemy
and with the final parameter set to
false
. As
obj_skeleton_land
is a kind of
obj_enemy
, it will be included in the collision test this time. This means that it will
detect a collision with itself if the target is behind the skeleton, as the line will pass
through the skeletons head. If either this collision test
or
the previous one returns true,
then
target_hidden
will also be true, as the results are combined with a logical
or
operator (“
||
”).
Finally, lines 10-13 determine the direction of the target based on its x position
relative to the skeleton.
Search WWH ::
Custom Search