Workin on another script for Mirroring event movement. Im having some trouble in getting a small array of Mirror Events to sort properly. Im not even sure what the goal should be.
Basics:
0 = Vertical Mirror
1 = Horizontal Mirror
2 = Mirror Both
3 = Copy Player
I wrote a custom Comparator, but its not giving me the right sort order.
# Sort by farthest Event of Movement Direction
$game_map.mirror_events.sort! {|a,b| mirror_comparator(a, b, d)}
Then, this is what I have for my Mirror Comparator:
# a : first event to compare
# b : second event to compare
# d : direction
# - <=> returns 1 if a > b, 0 if a == b, -1 if a < b for Array sort method
#--------------------------------------------------------------------------
def mirror_comparator(a, b, d)
# Shorthand - Mirror Values and Logical Coordinates
am, bm, ax, bx, ay, by = a.mirror, b.mirror, a.x, b.x, a.y, b.y
# Compare X and Y values depending on Direction and Mirror Values
if d == 2 # Down
return ([0,2].include?(am) or [0,2].include?(bm)) ? ay <=> by : by <=> ay
elsif d == 8 # Right
return ([0,2].include?(am) or [0,2].include?(bm)) ? by <=> ay : ay <=> by
elsif d == 4 # Left
return ([1,2].include?(am) or [1,2].include?(bm)) ? bx <=> ax : ax <=> bx
elsif d == 6 # Right
return ([1,2].include?(am) or [1,2].include?(bm)) ? ax <=> bx : bx <=> ax
end
# Default (0 means the two objects compared are equal)
return 0
end
The intention of the code is to sort by the events coordinates. When mirroring the player moving to the Right, code puts the event farthest on the Right first (I think), unless one of the two compared events (a and b) moves the other way, then it should sort by event farthest to the left first. The events are sorted so they do not collide by moving the event farthest on the right to the right first, then each sequential event to the left. Well, thats the plan anyway...
Im running into trouble sorting 3 events. 2 works fine. The first time they move, the events are sorted improperly causing a collision, but the second time they move, they are sorted correctly. The two Mirror Events that are bumping into each other moving Right (which is what I do not want) are .mirror 0 and 3 so they should be falling into the else loop in the code above.
Ive tried resorting several times and changing the direction. I need something that is consistent.
Replacing d == 6 with just bx <=> ax works, but only the first time, then it causes improper sorting later.
Suggestions?