git.fiddlerwoaroof.com
Browse code

combat mostly implemented

edwlan authored on 29/07/2013 14:35:00
Showing 2 changed files
... ...
@@ -44,8 +44,8 @@ class Adventurer(object):
44 44
 		return self.attributes.state
45 45
 	@state.setter
46 46
 	def state(self, val):
47
-		if val not in {const.HEALTHY, const.WOUNDED, const.KNOCKOUT}:
48
-			raise ValueError('Value for state invalid')
47
+		if val < 0: val == 0
48
+		if val > const.KNOCKOUT: val = const.KNOCKOUT
49 49
 		self.attributes.state = val
50 50
 
51 51
 	@classmethod
... ...
@@ -1,4 +1,5 @@
1 1
 import abc
2
+import collections
2 3
 from . import combat
3 4
 
4 5
 class Overlay(object):
... ...
@@ -12,12 +13,40 @@ class Overlay(object):
12 13
 	def pos(self):
13 14
 		return self.x, self.y
14 15
 	def __init__(self, x,y, map):
16
+		self.events = collections.OrderedDict()
15 17
 		self.x = x
16 18
 		self.y = y
17 19
 		self.map = map
20
+		for key, value in self.handled_events.viewitems():
21
+			self.events.setdefault(key, []).extend(value)
22
+
18 23
 	def draw(self):
19 24
 		self.map.add(self)
20 25
 
26
+	def register_event(self, event, cb):
27
+		'''register a callback for an event
28
+
29
+		if the callback returns a false value besides None, execution will end after that event'''
30
+		self.events.setdefault(event, []).append(cb)
31
+
32
+	def trigger_event(self, event, *args, **kw):
33
+		if event not in self.events:
34
+			raise ValueError('%r has no event %r' % (self, event))
35
+
36
+		for cb in self.events[event]:
37
+			result = cb(self, *args, **kw)
38
+			result = result is None or result # if the event returns a false value besides None, break
39
+			if result == False: break
40
+
41
+	handled_events = collections.OrderedDict()
42
+	@classmethod
43
+	def handle_event(cls, event):
44
+		def _inner(cb):
45
+			cls.handled_events.setdefault(event, []).append(cb)
46
+			return cb
47
+		return _inner
48
+
49
+
21 50
 
22 51
 class Actor(Overlay):
23 52
 	char = ord('@')
... ...
@@ -29,9 +58,8 @@ class Actor(Overlay):
29 58
 		return self.x, self.y
30 59
 
31 60
 	def __init__(self, x,y, map, adventurer=None):
32
-		self.x = x
33
-		self.y = y
34
-		self.map = map
61
+		super(Actor, self).__init__(x,y, map)
62
+		self.inventory = []
35 63
 		if adventurer is None:
36 64
 			adventurer = combat.Adventurer.randomize()
37 65
 		self.adventurer = adventurer
... ...
@@ -50,27 +78,42 @@ class Actor(Overlay):
50 78
 		return result
51 79
 
52 80
 	def ishostile(self, other):
53
-		return True #TODO: implement factions
81
+		return self.adventurer.state < 2 #TODO: implement factions
54 82
 
55 83
 	def bump(self, other):
56 84
 		print '%s bumped %s' % (type(self).__name__, type(other).__name__)
57 85
 		if isinstance(other, Actor) and other.ishostile(self):
58 86
 			self.adventurer.attack(other.adventurer)
59
-			other.attacked_by(self)
60
-		other.bumped_by(self)
87
+			other.trigger_event('attacked', self)
88
+		elif isinstance(other, Object):
89
+			self.pickup(other)
90
+			other.trigger_event('picked_up', self)
61 91
 
92
+		other.trigger_event('bumped', self)
93
+
94
+	@Overlay.handle_event('attacked')
62 95
 	def attacked_by(self, other):
63 96
 		if self.adventurer.skills.check('agility'):
64 97
 			self.adventurer.attack(other.adventurer)
65 98
 
99
+	@Overlay.handle_event('bumped')
66 100
 	def bumped_by(self, other):
67 101
 		print '%s was bumped by %s' % (type(self).__name__, type(other).__name__)
68 102
 
69 103
 class Object(Overlay):
70
-	pass
104
+	item = None
105
+	@Overlay.handle_event('picked_up')
106
+	def picked_up_by(self, other):
107
+		self.map.remove(self)
71 108
 
109
+class Weapon(Object):
110
+	item = None
72 111
 
73 112
 class Potion(Object):
74 113
 	char = ord('!')
114
+
75 115
 class Scroll(Object):
76 116
 	char = ord('!')
117
+
118
+class Equipment(Object):
119
+	pass