diff --git a/src/item.py b/src/item.py index f3b9add..4f937b9 100644 --- a/src/item.py +++ b/src/item.py @@ -29,16 +29,34 @@ class Item: # to check if the time ran out to buy the item def is_expired(self): - return pygame.time.get_ticks() - self.spawn_time >= ITEM_SPAWN_INTERVAL + return pygame.time.get_ticks() - self.spawn_time >= ITEM_SHOPPING_TIMER + # cam offset to convert world coordinates to screen coordinates + def draw(self, screen, cam_x, cam_y): + draw_x = self.x - cam_x + draw_y = self.y - cam_y -class ItemManger: + if self.image is not None: + screen.blit(self.image, (draw_x, draw_y)) + else: + # fallback using settings colors + # coffee brown, beer yellow + if self.item_type == "coffee": + color = (120, 70, 30) + else: + color = COLOR_HP_BAR_YELLOW + + draw_rect = (draw_x, draw_y, ITEM_SIZE, ITEM_SIZE) + pygame.draw.rect(screen, color, draw_rect) + pygame.draw.rect(screen, COLOR_HP_BAR_BG, draw_rect, 2) + +class ItemManager: def __init__(self): self.items = [] self.last_spawn_time = pygame.time.get_ticks() # try 50 times to find a free position to spawn item - def _random_position(room_rect, blocked_rects): + def _random_position(self, room_rect, blocked_rects): attempts = 0 max_attempts = 50 @@ -66,7 +84,7 @@ class ItemManger: return None - # remove expired items + # remove expired items and spawn new ones def update(self, room_rect, blocked_rects): now = pygame.time.get_ticks() @@ -76,5 +94,17 @@ class ItemManger: if item.is_expired() == False: remaining_items.append(item) - # replace the old list with the new temp_list - self.items = remaining_items + # replace the old list with the new temp_list + self.items = remaining_items + + # todo spawn logic for ned ones with random time? (in settings we can set the time range?) + + # draw active items + def draw(self, screen, cam_x, cam_y): + for item in self.items: + item.draw(screen, cam_x, cam_y) + + # clear items when changing room + def clear(self): + self.items = [] + self.last_spawn_time = pygame.time.get_ticks() \ No newline at end of file