Browse code
allow the player to interact with the terrain
edwlan authored on 26/07/2013 19:00:47
Showing 6 changed files
Showing 6 changed files
2 | 2 |
new file mode 100644 |
... | ... |
@@ -0,0 +1,36 @@ |
1 |
+from __future__ import print_function, division |
|
2 |
+ |
|
3 |
+# from: http://roguebasin.roguelikedevelopment.org/index.php?title=Bresenham's_Line_Algorithm#Python |
|
4 |
+ |
|
5 |
+def line(x0, y0, x1, y1, wd): |
|
6 |
+ dx = abs(x1-x0); sx = 1 if x0 < x1 else -1 |
|
7 |
+ dy = abs(y1-y0); sy = 1 if y0 < y1 else -1 |
|
8 |
+ err = dx-dy |
|
9 |
+ ed = 1 if dx + dy == 0 else (dx*dx + dy*dy)**0.5 |
|
10 |
+ |
|
11 |
+ e2 = x2 = y2 = 0 |
|
12 |
+ |
|
13 |
+ wd = (wd + 1) / 2.0 |
|
14 |
+ while True: |
|
15 |
+ yield (x0,y0) |
|
16 |
+ e2 = err; x2 = x0 |
|
17 |
+ if 2*e2 >= -dx: |
|
18 |
+ e2 += dx |
|
19 |
+ y2 = y0 |
|
20 |
+ while e2 < ed*wd and (y1 != y2 or dx > dy): |
|
21 |
+ yield (x0, y2) |
|
22 |
+ y2 += sy |
|
23 |
+ e2 += dx |
|
24 |
+ if x0 == x1: break |
|
25 |
+ e2 = err |
|
26 |
+ err -= dy |
|
27 |
+ x0 += sx |
|
28 |
+ if 2*e2 <= dy: |
|
29 |
+ e2 = dx-e2 |
|
30 |
+ while e2 < ed*wd and (x1 != x2 or dx < dy): |
|
31 |
+ yield (x2, y0) |
|
32 |
+ x2 += sx |
|
33 |
+ e2 += dy |
|
34 |
+ if y0 == y1: break |
|
35 |
+ err += dx |
|
36 |
+ y0 += sy |
... | ... |
@@ -10,10 +10,10 @@ from src import map |
10 | 10 |
|
11 | 11 |
class Application(object): |
12 | 12 |
def __init__(self): |
13 |
- self.screen = console.Screen(200,125) |
|
13 |
+ self.screen = console.Screen(100,63) |
|
14 | 14 |
self.terrain_registry = map.TerrainRegistry() |
15 | 15 |
self.terrain_registry.load_from_file('data/terrain.yml') |
16 |
- self.map = map.Map(200,125, self.terrain_registry) |
|
16 |
+ self.map = map.Map(100,63, self.terrain_registry) |
|
17 | 17 |
self.player = player.Player(4,5, self.map) |
18 | 18 |
self.events = events.EventHandler() |
19 | 19 |
player.ArrowHandler(self.player, self.events) |
... | ... |
@@ -14,14 +14,15 @@ class EventHandler(object): |
14 | 14 |
self.key, self.mouse |
15 | 15 |
) |
16 | 16 |
|
17 |
+ alt, shift, ctrl = self.key.lalt|self.key.ralt, self.key.shift, self.key.lctrl|self.key.rctrl |
|
17 | 18 |
char = chr(self.key.c) |
18 | 19 |
if char != '\x00' and char in self.cbs: |
19 | 20 |
for cb in self.cbs[char]: |
20 |
- cb() |
|
21 |
+ cb(alt,shift,ctrl) |
|
21 | 22 |
|
22 | 23 |
elif self.key.vk in self.cbs: |
23 | 24 |
for cb in self.cbs[self.key.vk]: |
24 |
- cb() |
|
25 |
+ cb(alt,shift,ctrl) |
|
25 | 26 |
|
26 | 27 |
|
27 | 28 |
|
... | ... |
@@ -1,6 +1,7 @@ |
1 | 1 |
import numpy as np |
2 | 2 |
import libtcodpy as tc |
3 | 3 |
import yaml |
4 |
+import libs.bresenham |
|
4 | 5 |
|
5 | 6 |
def squeeze(val, low, high): |
6 | 7 |
return min(max(val, low), high) |
... | ... |
@@ -45,12 +46,25 @@ class Map(object): |
45 | 46 |
|
46 | 47 |
def move(self, object, dx,dy): |
47 | 48 |
print self.overlays, |
48 |
- self.overlays[object.pos].remove(object) |
|
49 |
- ox,oy = object.pos |
|
50 |
- x = squeeze(ox+dx, 0, self.width-1) |
|
51 |
- y = squeeze(oy+dy, 0, self.height-1) |
|
52 |
- self.overlays.setdefault((x,y), []).append(object) |
|
53 |
- self.update_overlay(ox,oy) |
|
49 |
+ if abs(dx) < 2 and abs(dy) < 2: |
|
50 |
+ self.overlays[object.pos].remove(object) |
|
51 |
+ ox,oy = object.pos |
|
52 |
+ x = squeeze(ox+dx, 0, self.width-1) |
|
53 |
+ y = squeeze(oy+dy, 0, self.height-1) |
|
54 |
+ if not self.fov.is_passable((x,y)): |
|
55 |
+ x,y = ox,oy |
|
56 |
+ |
|
57 |
+ self.overlays.setdefault((x,y), []).append(object) |
|
58 |
+ self.update_overlay(ox,oy) |
|
59 |
+ else: |
|
60 |
+ ox,oy = object.pos |
|
61 |
+ tx,ty = ox+dx, oy+dy |
|
62 |
+ gx,gy = ox,oy |
|
63 |
+ for x,y in libs.bresenham.line(ox,oy, tx,ty): |
|
64 |
+ if not self.fov.is_passable((x,y)): break |
|
65 |
+ else: gx,gy = x,y |
|
66 |
+ x,y = gx,gy |
|
67 |
+ |
|
54 | 68 |
return x-ox, y-oy |
55 | 69 |
|
56 | 70 |
def update_overlay(self, x=None, y=None): |
... | ... |
@@ -29,16 +29,28 @@ class ArrowHandler(object): |
29 | 29 |
eh.register(tc.KEY_RIGHT,self.right) |
30 | 30 |
eh.register(tc.KEY_UP,self.up) |
31 | 31 |
eh.register(tc.KEY_DOWN,self.down) |
32 |
- def left(self): |
|
32 |
+ def left(self, alt, shift, ctrl): |
|
33 | 33 |
print 'left' |
34 |
- self.player.move(-1, 0) |
|
35 |
- def right(self): |
|
34 |
+ if alt: |
|
35 |
+ self.player.move(-1, -1) |
|
36 |
+ else: |
|
37 |
+ self.player.move(-1, 0) |
|
38 |
+ def right(self, alt, shift, ctrl): |
|
36 | 39 |
print 'right' |
37 |
- self.player.move(1, 0) |
|
38 |
- def up(self): |
|
40 |
+ if alt: |
|
41 |
+ self.player.move(1, 1) |
|
42 |
+ else: |
|
43 |
+ self.player.move(1, 0) |
|
44 |
+ def up(self, alt, shift, ctrl): |
|
39 | 45 |
print 'up' |
40 |
- self.player.move(0, -1) |
|
41 |
- def down(self): |
|
46 |
+ if alt: |
|
47 |
+ self.player.move(1, -1) |
|
48 |
+ else: |
|
49 |
+ self.player.move(0, -1) |
|
50 |
+ def down(self, alt, shift, ctrl): |
|
42 | 51 |
print 'down' |
43 |
- self.player.move(0, 1) |
|
52 |
+ if alt: |
|
53 |
+ self.player.move(-1, 1) |
|
54 |
+ else: |
|
55 |
+ self.player.move(0, 1) |
|
44 | 56 |
|