Fixes collision code.

This commit is contained in:
William Hubbard
2023-02-05 13:58:32 -07:00
parent b80c215d98
commit 518bf5f5e6
3 changed files with 6 additions and 9 deletions

View File

@@ -116,7 +116,6 @@ class GameLoop(Room):
to_remove = set()
for entity in self.entities:
if not entity.update(tpf, self.player, self.camera) or abs(entity.y - self.player.y) > self.camera.height * 1.5:
print("Removing entity at (%d, %d) (or %d)" % (entity.x, entity.y, abs(entity.y - self.player.y)))
to_remove.add(entity)
elif entity.intersects_pixel(self.player.xsub, self.player.ysub):
if isinstance(entity, Obstacle):
@@ -126,7 +125,7 @@ class GameLoop(Room):
}
self.entities -= to_remove
self.score = self.player.y#int(abs(self.player.y) * 10)
self.score = self.player.x#int(abs(self.player.y) * 10)
thumby.display.fill(0)
# Draw the entities

View File

@@ -92,9 +92,7 @@ class Camera:
self.target = target
def entity_in_camera(self, ent_x, ent_y, ent_width = 0, ent_height = 0) -> bool:
if ent_x + ent_width >= self.x and ent_y + ent_height >= self.y and ent_x < self.x + self.width and ent_y < self.y + self.height:
return True
print("Dropping entity (%d, %d - %d, %d) from (%d, %d - %d, %d)" % (ent_x, ent_y, ent_x + ent_width, ent_y + ent_height, self.x, self.y, self.x + self.width, self.y + self.height))
return ent_x + ent_width >= self.x and ent_y + ent_height >= self.y and ent_x < self.x + self.width and ent_y < self.y + self.height
def relative_to_camera(self, global_x, global_y):
return (global_x - self.x, global_y - self.y)

View File

@@ -37,7 +37,7 @@ class Entity:
return False
rel_x = int(loc_x - self.x)
rel_y = int(loc_y - self.y)
return bool((self.collision[rel_x] >> (self.height - rel_y)) & 1)
return bool((self.collision[rel_x] >> (rel_y)) & 1)
def can_render(self, camera: Camera) -> bool:
"""
@@ -123,8 +123,8 @@ def update_entities(entities, difficulty: int, current_depth: int, prev_depth: i
return set()
spawned = set()
max_spawned = 1#randrange(0, difficulty + 1)
while len(spawned) < max_spawned and random() < (2 * math.pow(difficulty + 0.01, 2)) / (3 * math.pow(difficulty + 0.01, 2) + 20 * (difficulty + 0.01)) + 0.05:
max_spawned = randrange(0, difficulty + 1)
while len(entities) < max_spawned and random() < (2 * math.pow(difficulty + 0.01, 2)) / (3 * math.pow(difficulty + 0.01, 2) + 20 * (difficulty + 0.01)) + 0.05:
sprite_image = OBSTACLES[randrange(0, min((difficulty + 1) * 3, len(OBSTACLES)))]
if sprite_image.width >= thumby.display.width:
x = 0
@@ -141,7 +141,7 @@ def update_entities(entities, difficulty: int, current_depth: int, prev_depth: i
error = True
break
if not error:
print("Creating entity at %d, %d" % (x, y))
sprite = thumby.Sprite(sprite_image.width, sprite_image.height, sprite_image.image, 0, 0, 0, False, False)
print("Adding obstacle of to (%d, %d) of size (%d, %d)" % (x, y, sprite_image.width, sprite_image.height))
spawned.add(Obstacle(x, y, sprite_image.width, sprite_image.height, sprite_image.collision or sprite_image.image, sprite))
return spawned