Skip to content

Commit

Permalink
simpler and actually works properly! fix #95
Browse files Browse the repository at this point in the history
fix #95
  • Loading branch information
kawa-kokosowa committed Aug 18, 2016
1 parent 166aa4f commit 8235496
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 47 deletions.
5 changes: 3 additions & 2 deletions demo/demo.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,15 +68,16 @@ def update(self, camera, wall_collision_group, layer_size, timedelta):
return None

# move
new_coord = [self.x_speed + self.collider.rect.topleft[0],
self.y_speed + self.collider.rect.topleft[1]]
new_coord = (self.x_speed + self.collider.rect.topleft[0],
self.y_speed + self.collider.rect.topleft[1])

dummy_future_rect = self.collider.rect.copy()
dummy_future_rect.topleft = new_coord
wrapped_coord = wrap_logic(dummy_future_rect, layer_size).topleft

if wrapped_coord != new_coord:
# we wrapped around the screen pacman style
print([wrapped_coord, new_coord])
oldie = self.collider.rect.topleft
self.collider.rect.topleft = wrapped_coord
collided_with = self.collider.collides_rect_mask(wall_collision_group)
Expand Down
76 changes: 31 additions & 45 deletions sappho/collide.py
Original file line number Diff line number Diff line change
Expand Up @@ -102,11 +102,9 @@ def collides_rect_mask(self, sprite_group):
and (pygame.sprite
.collide_mask(sprite_whose_rect_collides, self))):

print("mask collision")
return sprite_whose_rect_collides

elif (not hasattr(self, 'mask')) and (not hasattr(self, 'mask')):
print("rect collision")
return sprite_whose_rect_collides

else:
Expand Down Expand Up @@ -146,60 +144,48 @@ def move_as_close_as_possible(self, destination, sprite_group):
"""

position_difference_x = self.rect.topleft[0] - destination[0]
position_difference_y = self.rect.topleft[1] - destination[1]
# first figure out the x and y increments
goal_x, goal_y = destination
current_x, current_y = self.rect.topleft

if position_difference_y > 0:
y_modifier = -1
elif position_difference_y < 0:
y_modifier = 1
elif position_difference_y == 0:
y_modifer = 0
if goal_x > self.rect.left:
x_increment = 1
elif goal_x < self.rect.left:
x_increment = -1
else:
x_increment = 0

if goal_y > self.rect.top:
y_increment = 1
elif goal_y < self.rect.top:
y_increment = -1
else:
y_increment = 0

if position_difference_x > 0:
x_modifier = -1
elif position_difference_x < 0:
x_modifier = 1
elif position_difference_x == 0:
x_modifer = 0
# ...
colliding_with = None
distance_to_goal_x = goal_x - current_x
distance_to_goal_y = goal_y - current_y

# will not check the last position
while not (position_difference_x == 0 and position_difference_y == 0):
while True:
last_safe_topleft = self.rect.topleft

if position_difference_x != 0:
position_difference_x += x_modifier
if not self.rect.top == goal_y:
self.rect.top += y_increment

if position_difference_y != 0:
position_difference_y += y_modifier
if not self.rect.left == goal_x:
self.rect.left += x_increment

old_coord = self.rect.topleft
new_coord = [position_difference_x + self.rect.topleft[0],
position_difference_y + self.rect.topleft[1]]
self.rect.topleft = new_coord
colliding_with = self.collides_rect_mask(sprite_group)

if colliding_with:
self.rect.topleft = old_coord
print("is colliding somehow")
self.rect.topleft = last_safe_topleft
return colliding_with
elif self.rect.topleft == (goal_x, goal_y):
break

# one last check for the last point
if position_difference_x != 0:
position_difference_x += x_modifier

if position_difference_y != 0:
position_difference_y += y_modifier

old_topleft = self.rect.topleft
new_coord = [position_difference_x + self.rect.topleft[0],
position_difference_y + self.rect.topleft[1]]
self.rect.topleft = new_coord
colliding_with = self.collides_rect_mask(sprite_group)

if colliding_with:
self.rect.topleft = old_coord
return colliding_with
else:
return None
return None

# TODO: what if I want diagonal!?
def sprites_in_path(self, new_coord, sprite_group):
Expand Down

0 comments on commit 8235496

Please sign in to comment.