Skip to content

Commit

Permalink
partially implemented parts in demo, fix collide
Browse files Browse the repository at this point in the history
  • Loading branch information
kawa-kokosowa committed Dec 10, 2016
1 parent 77b6e2b commit 1d7e8a8
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 23 deletions.
31 changes: 25 additions & 6 deletions demo/demo.py
Expand Up @@ -120,7 +120,7 @@ def __init__(self, center, size, x_speed, y_speed):
super(Asteroid, self).__init__()

@classmethod
def add_if_below_threshold(cls, player, asteroid_list, at_least_x_existing):
def add_if_below_threshold(cls, player, spatial_partition, asteroid_list, at_least_x_existing):

if len(asteroid_list) < at_least_x_existing:
plus_or_minus_x = random.choice([1, -1])
Expand All @@ -141,6 +141,7 @@ def add_if_below_threshold(cls, player, asteroid_list, at_least_x_existing):

another_asteroid = Asteroid((new_asteroid_x, new_asteroid_y), 10, x_speed, y_speed)
asteroid_list.add(another_asteroid)
spatial_partition.add_sprites(another_asteroid)

def new_smaller_asteroid(self):
"""New x and y speed based on player coords
Expand Down Expand Up @@ -290,6 +291,20 @@ def wrap_logic(rect, layer_size):
# Asteroids
asteroid_list = pygame.sprite.Group()

# for right now we're using a fixed z position
tilemap_on_players_index = tilemaps_by_layer[config.ANIMATED_SPRITE_Z_INDEX]
tilemap_collision_group = tilemap_on_players_index.collision_group

# The spatial partition which is used specifically (at least at the
# moment) for asteroids and walls.
spatial_partition = collision.SpatialPartitionGrid.from_dimensions(
*surface_size,
20,
20,
)
spatial_partition.add_sprites(sprite for sprite in tilemap_collision_group)


# Main program loop ###########################################################
while not done:

Expand Down Expand Up @@ -328,14 +343,19 @@ def wrap_logic(rect, layer_size):
#
# We will be resetting player.sprite.rect.topleft
# to old_topleft if there is a collision.
tilemap_on_players_index = tilemaps_by_layer[config.ANIMATED_SPRITE_Z_INDEX]
collision_group_on_player_index = tilemap_on_players_index.collision_group
timedelta = clock.get_time()
player.update(camera, collision_group_on_player_index, layers[0].get_size(), timedelta)
# TODO: why not have player accept spatial partition?
player.update(
camera,
tilemap_collision_group,
layers[0].get_size(),
timedelta,
)

# create some asteroids, hurdled t the player
# we should make these chase the player, actually...
Asteroid.add_if_below_threshold(player, asteroid_list, 10)
# TODO: accept spatial partition???
Asteroid.add_if_below_threshold(player, spatial_partition, asteroid_list, 10)

# DRAWING/RENDER CODE

Expand All @@ -344,7 +364,6 @@ def wrap_logic(rect, layer_size):
layers[i].blit(animated_bg.image, camera.view_rect.topleft)
layers[i].blit(tilemap_layer, (0, 0))


# Finally let's render the animated sprite on some
# arbitrary layer. In the future the TMX will set this.
layers[config.ANIMATED_SPRITE_Z_INDEX].blit(player.sprite.image,
Expand Down
45 changes: 28 additions & 17 deletions sappho/collide.py
Expand Up @@ -339,7 +339,8 @@ def intersecting_partitions(self, pygame_rect):
Warning:
Since this just checks a rectangles's four
corners, it will not work properly if the
rectangle is greater than two partitions in size.
rectangle is equal to or greater than
normal partition size + 2 pixels.
Arguments:
pygame_rect (pygame.Rect): ...
Expand All @@ -363,40 +364,50 @@ def intersecting_partitions(self, pygame_rect):
"""

if ((pygame_rect.width > self.normal_partition_width * 2)
or (pygame_rect.height > self.normal_partition_height * 2)):
if ((pygame_rect.width >= self.normal_partition_width + 2)
or (pygame_rect.height >= self.normal_partition_height + 2)):
# update this part to get the partitions in between, in the future
message = 'Rect must not exceed 2x height nor width. Support soon!'
raise NotImplementedError(message)
else:
# this won't work but the basic idea works, it can be broken down
# into a simple calc (testing if first coordinate is last of a part
# right?
return set([
self.pixel_coordinates_to_partition(*pygame_rect.topleft),
self.pixel_coordinates_to_partition(*pygame_rect.bottomleft),
self.pixel_coordinates_to_partition(*pygame_rect.topright),
self.pixel_coordinates_to_partition(*pygame_rect.bottomright),
])

# FIXME/TODO
def partitions_for_sprite(self, sprite):
def move_sprite(self, some_sprite, x, y):
"""Move some_sprite to a new coordinate on
the SpatialPartitionGrid.
sprite_position
# get the index
asdf
pass
Arguments:
some_sprite (pygame.Sprite): ...
x (int): ...
y (int): ...
"""

# first remove sprite from the partition(s) it's currently on
for partition in self.intersecting_partitions(some_sprite.rect):
partition.sprite_group.remove(some_sprite)

# FIXME/TODO
def move_sprite(self, some_sprite):
pass
# set the new position
some_sprite.rect.topleft = (x, y)

# FIXME/TODO
def update_sprite_location(self, some_sprite):
some_sprite._partitions = asdf
# now add this sprite to the partitions based on the
# sprite's new position
for partition in self.intersecting_partitions(some_sprite.rect):
partition.sprite_group.add(some_sprite)

# FIXME/TODO
# TODO...
def add_sprites(self, *args):

for sprite in args:
sprite._partitions = self.intersecting_partitions(sprite.rect)
self.move_sprite(sprite, *sprite.rect)


# TODO: this is pretty unnecessary now...
Expand Down

0 comments on commit 1d7e8a8

Please sign in to comment.