Commits

Pierre Surply  committed b21d645

Added light effects

  • Participants
  • Parent commits 18af75e

Comments (0)

Files changed (5)

File res/img/robots.png

Old
Old image
New
New image
 ##    along with BASIC-RoBots.  If not, see <http://www.gnu.org/licenses/>. 
 ##
 ## Started on  Thu Jun 28 13:22:05 2012 Pierre Surply
-## Last update Mon Aug 27 19:37:59 2012 Pierre Surply
+## Last update Wed Aug 29 17:45:40 2012 Pierre Surply
 ##
 
 import os
         self.sprite_floor_mini = surface.cut_surface(pygame.image.load("res/img/floor_mini.png").convert_alpha(), (2, 2))
         self.sprite_elts = surface.cut_surface(pygame.image.load("res/img/lvl_elts.png").convert_alpha(), (16, 16))
         self.sprite_robots = surface.cut_surface(pygame.image.load("res/img/robots.png").convert_alpha(), (32, 32))
+        self.tps_anim_sprite = 0
+        self.id_sprite_water = 0
+        self.sprite_water = surface.cut_surface(pygame.image.load("res/img/water.png").convert_alpha(), (16, 16))
         self.cursor = surface.surface2texture(pygame.image.load("res/img/cursor.png").convert_alpha())
 
 
     def init_tile(self):
         self.tile = [[0] * self.size for i in range(self.size)]
         self.tile_elts = [[None] * self.size for i in range(self.size)]
+        self.light = [[0] * self.size for i in range(self.size)]
 
     def load(self):
         self.load_img(self.path_img, self.size, self.tile, self.color2tile)
         else:
             return None
 
+    def calculate_light(self):
+        self.light = [[0] * self.size for i in range(self.size)]
+        for i in self.robots:
+            self.rec_calculate_light((i.pos_x, i.pos_y), 0, 0xEE)
+
+    def rec_calculate_light(self, (org_x, org_y), dist, intens):
+        if intens > 0:
+            for x in range(max(org_x, 0), min(org_x+dist+1, self.size)):
+                self.set_light((x, org_y), intens)
+                if org_y+dist < self.size:
+                    self.set_light((x, org_y+dist), intens)
+            for y in range(max(org_y+1, 0), min(org_y+dist, self.size)):
+                self.set_light((org_x, y), intens)
+                if org_x+dist < self.size:
+                    self.set_light((org_x+dist, y), intens)
+            self.rec_calculate_light((org_x-1, org_y-1), dist+2, intens-0x33)
+
+    def set_light(self, (x, y), intens):
+        org = self.light[x][y]
+        light = org + intens
+        if light > 0xFF:
+            light = 0xFF
+        self.light[x][y] = light
+
+    def set_color(self, (x, y)):
+        l = 0
+        if x >= 0 and x < self.size and\
+                y >=0 and y < self.size:
+            l = self.light[x][y]
+        glColor3ub(l, l, l)
+
+    def set_color_corner(self, (x, y)):
+        ltl = 0
+        ltr = 0
+        lbl = 0
+        lbr = 0
+        if x > 0 and y > 0:
+            ltl = self.light[x-1][y-1]
+        if x < self.size and y > 0:
+            ltr = self.light[x][y-1]
+        if x > 0 and y < self.size:
+            lbl = self.light[x-1][y]
+        if x < self.size and y < self.size:
+            lbl = self.light[x][y]
+        l = (ltl + ltr + lbl + ltr) / 4
+        if l > 0xFF:
+            glColor3ub(0xFF, 0xFF, 0xFF)
+        else:
+            glColor3ub(l, l, l)
+            
     def render_case(self, x, y):
-        glBindTexture(GL_TEXTURE_2D, self.sprite_floor[self.tile[x][y]])
+        tile = self.tile[x][y]
+        if tile == 2:
+            glBindTexture(GL_TEXTURE_2D, self.sprite_water[self.id_sprite_water])
+        else:
+            glBindTexture(GL_TEXTURE_2D, self.sprite_floor[tile])
         z = self.temp_tiles[self.tile[x][y]][3]
         glPushMatrix()
         glTranslated(x, y, z)
-        glCallList(self.list_floor)
+        glBegin(GL_QUADS)
+        self.set_color_corner((x, y+1))
+        glTexCoord2d(0,0); glVertex3d(0, 1, 0)
+        self.set_color_corner((x, y))
+        glTexCoord2d(0,1); glVertex3d(0, 0, 0)
+        self.set_color_corner((x+1, y))
+        glTexCoord2d(1,1); glVertex3d(1, 0, 0)
+        self.set_color_corner((x+1, y+1))
+        glTexCoord2d(1,0); glVertex3d(1, 1, 0)
+        glEnd()
+        self.set_color((x, y+1))
+        #glCallList(self.list_floor)
         if (not self.tile[x][y] in [2, 4]):
             if (y >= self.size-1 or self.temp_tiles[self.tile[x][y+1]][3] < z):
                 glPushMatrix()
         glMatrixMode(GL_MODELVIEW)
         glLoadIdentity()
 
-        gluLookAt(-2, 6, 10,\
+        gluLookAt(-2, 6, 16,\
                       0, 0, 0, \
                       0, 0, 1)
         glRotated(self.angle, 0, 0, 1)
         glPopMatrix()
         glTranslated(-select_x-0.5-dx, -select_y-0.5-dy, 0);
 
-        for y in range(max(select_y-7, 0), min(select_y+8, self.size)):
-            for x in range(max(select_x-6, 0), min(select_x+10, self.size)):
+        for y in range(max(select_y-8, 0), min(select_y+9, self.size)):
+            for x in range(max(select_x-7, 0), min(select_x+11, self.size)):
                 self.render_case(x, y)
                 if self.tile_elts[x][y] != None:
+                    light = self.light[x][y]
+                    glColor3ub(light, light, light)
                     self.render_elt(x, y)
+        glColor3ub(0xFF, 0xFF, 0xFF)
         for i in self.robots:
             x = i.pos_x
             y = i.pos_y
                 return i[1]
         return None
 
-
     def land_mothership(self):
         r = mothership.Mothership(True,\
                                       self.path,\
         #self.running_robot += 1
         #if self.running_robot >= len(self.robots):
         #    self.running_robot = 0
+
+    def update_sprite(self):
+        if self.tps_anim_sprite <= 0:
+            self.tps_anim_sprite = 25
+            self.id_sprite_water += 1
+            if self.id_sprite_water > 3:
+                self.id_sprite_water = 0
+        else:
+            self.tps_anim_sprite -= 1
 ##    along with BASIC-RoBots.  If not, see <http://www.gnu.org/licenses/>.
 ##  
 ## Started on  Thu Jun 28 15:31:47 2012 Pierre Surply
-## Last update Mon Aug 27 18:44:15 2012 Pierre Surply
+## Last update Wed Aug 29 12:19:00 2012 Pierre Surply
 ##
 
 import pygame
             elif events.get_key_once(K_n):
                 self.incr_select_robot(1)
         self.env.update_robots(self.select_robot, display, events)
+        self.env.update_sprite()
         if self.select_robot != None:
             self.select_x, self.select_y = self.env.get_pos_robot(self.select_robot)
             self.env.render((self.select_x, self.select_y, \

File src/robot/robot.py

 ##    along with BASIC-RoBots.  If not, see <http://www.gnu.org/licenses/>.
 ## 
 ## Started on  Sun Jul  1 15:34:10 2012 Pierre Surply
-## Last update Mon Aug 27 15:10:00 2012 Pierre Surply
+## Last update Wed Aug 29 15:10:39 2012 Pierre Surply
 ##
 
 import os
             self.pos_y = y
             self.save_pos()
             self.os.terminal.info.write_info("Moved to (" + str(x) + ", " + str(y) + ")", 7)
+            self.env.calculate_light()
             return 1
         else:
             self.os.terminal.info.write_info("Can't move to (" + str(x) + ", " + str(y) + ")", 8)

File src/worldmap.py

 ##    along with BASIC-RoBots.  If not, see <http://www.gnu.org/licenses/>.
 ## 
 ## Started on  Thu Jun 28 20:42:21 2012 Pierre Surply
-## Last update Mon Aug 27 16:06:29 2012 Pierre Surply
+## Last update Wed Aug 29 15:15:47 2012 Pierre Surply
 ##
 
 import os
                                                                 (0, 0),\
                                                                 events)
             self.env[coord[0]][coord[1]].robots.append(r)
+            self.env[coord[0]][coord[1]].calculate_light()
 
     def save(self):
         self.save_img("saves/" + self.path + "/env/worldmap.bmp",\