Commits

dr0id committed c31f153

faster fire heat source

Comments (0)

Files changed (4)

pyweek15/gamelib/level01.py

         firesprite.HeatSource.__init__(self)
         self.model = model
         
+        self.sources = []
+        # todo: density!!
+        for i in range(10):
+            rsurf = pygame.Surface((self.model.cell_size_x, self.model.cell_size_y), pygame.SRCALPHA)
+            self.sources.append(rsurf)
+            rsurf.fill((0,0,0,0))
+            rw = self.model.cell_size_x
+            y = self.model.cell_size_y // 2
+            for x in range(rw):
+                # color = (max(0, min(255, int(self.model.cells[(x, y)].temp * 10))), )*3
+                color = self.palette.get_at((random.randint(0, self.palette.get_size()[0] - 1), 0))
+                # base = 150
+                # alpha = int((255-base) * ratio) + base
+                # # alpha = 255
+                # color = (color[0], color[1], color[2], alpha)
+                # surf.set_at((sx, sy + random.randint(-rh//2, rh//2)), color)
+                rsurf.set_at((x, y + random.randint(-y, y)), color)
+            
     def apply(self, surf):
+        for pos, cell in self.model.cells.items():
+            if cell.temp > cell.ignition and cell.ignition > 0:
+                cx, cy = pos
+                src = pygame.transform.flip(self.sources[random.randint(0, 9)], random.randint(0,1), random.randint(0,1))
+                # surf.blit(src, (cx * self.model.cell_size_x, cy * self.model.cell_size_y), None, pygame.BLEND_RGBA_ADD))
+                surf.blit(src, (cx * self.model.cell_size_x, cy * self.model.cell_size_y))
+    
+    
+    def __apply(self, surf):
         # todo
-
         rw = rh = 10
         rect = pygame.Rect(0, 0, rw, rh)
-        for cy in range(self.model.cells_count_y):
-            for cx in range(self.model.cells_count_x):
-                cell = self.model.cells[(cx, cy)]
-                if cell.has_fire():
-                    ratio = 2 * cell.temp / self.model.max_temp
+        # surf.lock()
+        for pos, cell in self.model.cells.items():
+                cx, cy = pos
+        # for cy in range(self.model.cells_count_y):
+            # for cx in range(self.model.cells_count_x):
+                # cell = self.model.cells[(cx, cy)]
+                # if cell.has_fire():
+                if cell.temp > cell.ignition and cell.ignition > 0:
+                    # return
+                    ratio = cell.temp / self.model.max_temp
                     for i in range(rw):
                         sx = cx * rw + i
                         sy = cy * rh - rh//2
                         
                         # color = (max(0, min(255, int(self.model.cells[(x, y)].temp * 10))), )*3
                         color = self.palette.get_at((random.randint(0, self.palette.get_size()[0] - 1), 0))
-                        alpha = int(255 * ratio)
-                        color = (color[0], color[1], color[2], alpha if alpha <= 255 else 255)
-                        surf.set_at((sx, sy + random.randint(-rh//2, rh//2)), color)
+                        base = 150
+                        alpha = int((255-base) * ratio) + base
+                        # alpha = 255
+                        color = (color[0], color[1], color[2], alpha)
+                        # surf.set_at((sx, sy + random.randint(-rh//2, rh//2)), color)
+                        surf.set_at((sx, sy + random.randint(-2, 2)), color)
+        # surf.unlock()
                 
 
         
         
         self.win_cond_id = self.scheduler.schedule(self.check_win_condition, 1.0)
         
-        self.firesprite = firesprite.FireSprite(settings.resolution, (0, 0))
+        # self.firesprite = firesprite.FireSprite(settings.resolution, (0, 0))
+        self.firesprite = firesprite.FireSprite((200, 200), (0, 0))
         self.firesprite.add_heat_source(GridHeatSource(self.model))
         
     def check_win_condition(self):
         print('     fires : ', len(self.model.fires))
         if self.player.health <= 0:
             print('LOOOSEEE!!')
-        if len(self.model.fires) == 0:
+        if not self.model.is_burning():
             print('WWIIIIIINN!')
         return 1.0
         

pyweek15/gamelib/levels.py

 
             
                                     0, 0, 0, 0, 0, 0, 0, 0, 0, 0,     0, 0, 0, 0, 0, 0, 0, 0, 0, 0,   
-                                    0, 0, 0, 0, 0, 0, 0, 0, 0, 0,     0, 0, 0, 0, 0, 0, 0, 0, 0, 0,   
+                                    0, 5, 0, 0, 0, 0, 0, 0, 0, 0,     0, 0, 0, 0, 0, 0, 0, 0, 0, 0,   
                                     0, 0, 0, 0, 0, 0, 0, 0, 0, 0,     0, 0, 0, 0, 0, 0, 0, 0, 0, 0,   
                                     0, 0, 0, 0, 0, 0, 0, 0, 0, 0,     0, 0, 0, 0, 0, 0, 0, 0, 0, 0,   
                                     0, 0, 0, 0, 0, 0, 0, 0, 0, 0,     0, 0, 0, 0, 0, 0, 0, 0, 0, 0,   
 
                                     ],
                     # x, y, size, max_size, burn_rate, update_interval
-            "fires"         : [ (1, 1, 2, 20000, 1, 3),]
+            "fires"         : [ (10, 10, 2, 20000, 1, 1),]
             }
             

pyweek15/gamelib/main.py

         context_top().draw(screen)
         alpha = lock_stepper.update(dt, timestep_seconds=settings.sim_time_step)
         if __debug__:
-            print('fps: ', str(clock.get_fps()), alpha)
+            fps = clock.get_fps()
+            # if fps < 15 or fps > 500:
+            print('fps: ', str(fps), alpha)
         
     pygame.quit()
     

pyweek15/gamelib/model.py

         self.cell_size_y = 0
         self.fires = []
         
-
+    def is_burning(self):
+        for cell in self.cells.values():
+            if cell.temp > cell.ignition:
+                return True
+        return False
+        
+        
     def update(self, dt):
         # self.scheduler.update(dt)
         pass
         fire.id = self.scheduler.schedule(self._update_fire, update_interval, random.random() * update_interval, fire)
         cell = self.cells.get((x, y), Cell())
         cell.fire = fire
+        cell.temp = cell.ignition
         self.fires.append(fire)
 
     def _update_fire(self, fire):
                     cell.temp = cell.temp if cell.temp < self.max_temp else self.max_temp
                     # ignite additional fires
                     if cell.temp > cell.ignition:
-                        if not cell.has_fire():
+                        if not cell.has_fire() and (x + y) % 2 == 0:
                             self.create_fire(x, y, 1, fire.max_size, fire.burn_rate, fire.update_interval)
+                            # pass
         
         # todo: fire size growth independent of burn_rate?
         # todo: fire size growth depending on cell temp?
         if fire.size < fire.max_size:
             fire.size += int(1.5 - self.cells[(fire.x, fire.y)].temp / self.max_temp)
         
-        if self.cells[(fire.x, fire.y)].temp > 0:
+        if self.cells[(fire.x, fire.y)].has_fire() > 0:
             return fire.update_interval
         return Scheduler.STOPREPEAT
         
         self.cells_count_y = data["cells_count_y"]
         self.cell_size_x = data["cell_size_x"]
         self.cell_size_y = data["cell_size_y"]
+        self.max_temp = data["max_temp"]
         for y in range(self.cells_count_y):
             for x in range(self.cells_count_x):
                 cell = self.cells.get((x, y), Cell())