changeset 3:32c80cf60826

Tweak the lighting
author Radomir Dopieralski <sheep-devel@sheep.art.pl>
date Sun, 31 Mar 2013 23:52:11 +0200
parents 67eeaa4ef863
children c290a652a783
files rrr/level.py rrr/light.py run.py
diffstat 3 files changed, 58 insertions(+), 13 deletions(-) [+]
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/rrr/level.py	Sun Mar 31 23:52:11 2013 +0200
@@ -0,0 +1,34 @@
+#!/usr/bin/env python
+# -*- coding: utf-8 -*-
+
+class Level(object):
+    def __init__(self):
+        self.map = [
+            '...#############',
+            '...#...........#',
+            '#..#...........#',
+            '#..#...........#',
+            '#..............#',
+            '#..............#',
+            '#....####......#',
+            '#....#.........#',
+            '#....#.........#',
+            '#....######....#',
+            '#..............#',
+            '#..............#',
+            '#..............#',
+            '###########.#..#',
+            '#...........#..#',
+            '################',
+        ]
+
+    def is_opaque(self, (x, y)):
+        if not (0<=x<16 and 0<=y<16):
+            return True
+        return self.map[y][x] == '#'
+
+    def get_char(self, (x, y)):
+        if not (0<=x<16 and 0<=y<16):
+            return 'X'
+        return self.map[y][x]
+
--- a/rrr/light.py	Sun Mar 31 18:12:14 2013 +0200
+++ b/rrr/light.py	Sun Mar 31 23:52:11 2013 +0200
@@ -54,13 +54,8 @@
                 corner -= 1
             for x in xrange(corner):
                 rayspan = ray_max[x][y] - ray_min[x][y]
-                rayweight = max_rays / rayspan
-                shadow = sum(
-                    rayweight
-                    for ray in rays[ray_min[x][y]:ray_max[x][y]]
-                    if ray
-                )
-                if 3 * shadow < 2 * max_rays:
+                shadow = sum(rays[ray_min[x][y]:ray_max[x][y]])
+                if 12 * shadow < 11 * rayspan:
                     lit.add((xpos, ypos))
                 if is_opaque((xpos, ypos)):
                     rays[ray_min[x][y]:ray_max[x][y]] = [1] * rayspan
--- a/run.py	Sun Mar 31 18:12:14 2013 +0200
+++ b/run.py	Sun Mar 31 23:52:11 2013 +0200
@@ -4,20 +4,36 @@
 import curses
 
 from rrr.display_curses import Display
+from rrr.level import Level
 from rrr.light import LightRaycasting as Light
 
+
 def main(stdscr):
     display = Display(stdscr)
     light = Light()
+    level = Level()
+    x, y = 0, 0
     display.stats_draw('HP 100/100')
     display.message('Hello world!')
     display.unselect()
-    display.center_map((0, 0))
-    for x, y in light.cast((0, 0), 12, lambda p: False):
-        display.map_draw((x, y), '.', 'gray')
-    display.map_draw((0, 0), '@', 'white')
-    display.refresh()
-    display.get_command()
+    display.center_map((x, y))
+    lit = set()
+    while True:
+        for p in lit:
+            display.map_draw(p, level.get_char(p), 'magenta')
+        lit = light.cast((x, y), 12, level.is_opaque)
+        for p in lit:
+            display.map_draw(p, level.get_char(p), 'gray')
+        display.map_draw((x, y), '@', 'white')
+        display.refresh()
+        command, params = display.get_command()
+        if command == 'arrow':
+            dx, dy = params
+            x += dx
+            y += dy
+        elif command == 'key' and params == 'q':
+            raise SystemExit()
+
 
 if __name__ == '__main__':
     curses.wrapper(main)