def push_an_object(self, pusher, col):
"""
Pushes a pushable other GameObject (assuming that this other object also has a PlatformerPhysics Component).
:param pusher: the Sprite that's actively pushing against the other GameObject
:param col: the Collision object (that caused the push) returned by the collision detector method
"""
pushee = col.sprite2 # the object being pushed
orig_x = pushee.rect.x
pushee_phys = pushee.components["physics"]
# calculate the amount to move in x-direction based on vx_max and the collision-separation
move_x = - col.separate[0] * abs(pushee_phys.vx_max / col.direction_veloc)
# adjust x-speed based on vx_max
self.vx = math.copysign(pushee_phys.vx_max, col.direction_veloc)
# first move rock, then do a x-collision detection of the rock, then fix that collision (if any) -> only then move the pusher
pushee.move(move_x, 0)
# TODO: be careful not to overwrite the col object that's currently still being used by this method's caller
# right now it's being overridden by the below call -> it's not a problem yet because this collision object is only used further via the normal_x
# property, which should stay the same
self.collide_in_one_direction(pushee, "x", self.vx, (orig_x, pushee.rect.y))
# re-align pusher with edge of pushee
if self.vx < 0:
x_delta = pushee.rect.right - pusher.rect.left
else:
x_delta = pushee.rect.left - pusher.rect.right
# and we are done
pusher.move(x_delta, 0)
评论列表
文章目录