git.fiddlerwoaroof.com
edwlan authored on 29/07/2013 04:29:20
Showing 4 changed files
1 1
new file mode 100644
... ...
@@ -0,0 +1,58 @@
1
+import abc
2
+
3
+class Overlay(object):
4
+	__metaclass__ = abs.ABCMeta
5
+
6
+	char = abc.abstractproperty()
7
+	color = (0,0,0)
8
+	blocks = False
9
+
10
+	@property
11
+	def pos(self):
12
+		return self.x, self.y
13
+	def __init__(self, x,y, map):
14
+		self.x = x
15
+		self.y = y
16
+		self.map = map
17
+
18
+
19
+class Actor(Overlay):
20
+	char = ord('@')
21
+	color = (255,0,0)
22
+	blocks = True
23
+
24
+	@property
25
+	def pos(self):
26
+		return self.x, self.y
27
+
28
+	def __init__(self, x,y, map, adventurer=None):
29
+		self.x = x
30
+		self.y = y
31
+		self.map = map
32
+
33
+	def draw(self):
34
+		self.map.add(self)
35
+
36
+	def move(self, dx, dy):
37
+		dx, dy = self.map.move(self, dx,dy)
38
+		self.x += dx
39
+		self.y += dy
40
+
41
+	def tick(self):
42
+		pass
43
+
44
+
45
+	def bump(self, other):
46
+		print '%s bumped %s' % (type(self).__name__, type(other).__name__)
47
+		other.bumped_by(self)
48
+	def bumped_by(self, other):
49
+		print '%s was bumped by %s' % (type(self).__name__, type(other).__name__)
50
+
51
+class Object(Overlay):
52
+	pass
53
+
54
+
55
+class Potion(Object):
56
+	char = ord('!')
57
+class Scroll(Object):
58
+	char = ord('!')
... ...
@@ -7,6 +7,7 @@ import numpy as np
7 7
 
8 8
 from src import events
9 9
 from src import player
10
+from libs import actor
10 11
 from src import console
11 12
 from src import map
12 13
 
... ...
@@ -25,7 +26,11 @@ class Application(object):
25 26
 
26 27
 		self.actors = []
27 28
 		for x in range(40):
28
-			self.actors.append(player.Actor(random.randrange(100), random.randrange(63), self.map))
29
+			self.actors.append(actor.Actor(random.randrange(100), random.randrange(63), self.map))
30
+
31
+		self.objects = []
32
+		for x in range(50):
33
+			self.objects.append(
29 34
 
30 35
 		tc.sys_set_fps(60)
31 36
 
... ...
@@ -46,14 +46,22 @@ class Map(object):
46 46
 	def add(self, object):
47 47
 		self.overlays.setdefault(object.pos,[]).append(object)
48 48
 
49
+	def check_and_execute_bump(self, object, x,y):
50
+		if (x,y) in self.overlays:
51
+			for other in self.overlays[x,y]:
52
+				object.bump(other)
53
+
49 54
 	def move(self, object, dx,dy):
50 55
 		self.overlays[object.pos].remove(object)
51 56
 
57
+		collide_x, collide_y = None, None
58
+
52 59
 		if abs(dx) < 2 and abs(dy) < 2:
53 60
 			ox,oy = object.pos
54 61
 			x = squeeze(ox+dx, 0, self.width-1)
55 62
 			y = squeeze(oy+dy, 0, self.height-1)
56 63
 			if not self.is_passable((x,y)):
64
+				collide_x, collide_y = x,y
57 65
 				x,y = ox,oy
58 66
 
59 67
 		else:
... ...
@@ -61,10 +69,15 @@ class Map(object):
61 69
 			tx,ty = ox+dx, oy+dy
62 70
 			gx,gy = ox,oy
63 71
 			for x,y in libs.bresenham.line(ox,oy, tx,ty, 1):
64
-				if not self.is_passable((x,y)): break
72
+				if not self.is_passable((x,y)):
73
+					collide_x, collide_y = x,y
74
+					break
65 75
 				else: gx,gy = x,y
66 76
 			x,y = gx,gy
67 77
 
78
+		if collide_x is not None:
79
+			self.check_and_execute_bump(object, collide_x, collide_y)
80
+
68 81
 		self.overlays.setdefault((x,y), []).append(object)
69 82
 		self.update_overlay(ox,oy)
70 83
 		return x-ox, y-oy
... ...
@@ -1,40 +1,16 @@
1 1
 import libtcodpy as tc
2 2
 import libs.combat
3
+import libs.actor
3 4
 
4
-class Actor(object):
5
-	char = ord('@')
6
-	color = (255,0,0)
7
-	blocks = True
8
-
9
-	@property
10
-	def pos(self):
11
-		return self.x, self.y
12
-
13
-	def __init__(self, x,y, map):
14
-		self.x = x
15
-		self.y = y
16
-		self.map = map
17
-
18
-	def draw(self):
19
-		self.map.add(self)
20
-
21
-	def move(self, dx, dy):
22
-		dx, dy = self.map.move(self, dx,dy)
23
-		self.x += dx
24
-		self.y += dy
25
-
26
-	def tick(self):
27
-		pass
28
-
29
-class Player(Actor):
5
+class Player(libs.actor.Actor):
30 6
 	char = ord('@')
31 7
 	color = (255,255,255)
32 8
 	light_radius = 10
33 9
 	def __init__(self, x,y, map):
34
-		Actor.__init__(self, x,y, map)
10
+		libs.actor.Actor.__init__(self, x,y, map)
35 11
 		self.map.set_pov((self.pos, self.light_radius))
36 12
 	def move(self, dx, dy):
37
-		Actor.move(self, dx,dy)
13
+		libs.actor.Actor.move(self, dx,dy)
38 14
 		self.map.set_pov((self.pos, self.light_radius))
39 15
 
40 16
 class ArrowHandler(object):