import logging
from .enums import Zone

class Entity(object):
	def __init__(self):
		self.tags = {}

		# Register the events
		self._registerEvents()

	def _registerEvents(self):
		self._eventListeners = {}
		for name in self.events:
			func = getattr(self, name, None)
			if func:
				self._eventListeners[name] = [func]

	def broadcast(self, event, *args):
		for entity in self.entities:
			for f in entity._eventListeners.get(event, []):
				if getattr(f, "zone", Zone.PLAY) == Zone.PLAY:
					f(*args)
		if event != "UPDATE":
			self.broadcast("UPDATE")

	def setTag(self, tag, value):
		logging.debug("%r::%r %r -> %r" % (self, tag, self.tags.get(tag, None), value))
		self.tags[tag] = value

	def unsetTag(self, tag):
		del self.tags[tag]

	def getIntProperty(self, tag):
		ret = self.tags.get(tag, 0)
		for slot in self.slots:
			ret += slot.getIntProperty(tag)
		return ret

	def getBoolProperty(self, tag):
		if self.tags.get(tag, False):
			return True
		for slot in self.slots:
			if slot.getBoolProperty(tag):
				return True
		return

import logging
from .enums import Zone

class Entity(object):
	def __init__(self):
		self.tags = {}

		# Register the events
		self._registerEvents()

	def _registerEvents(self):
		self._eventListeners = {}
		for name in self.events:
			func = getattr(self, name, None)
			if func:
				self._eventListeners[name] = [func]

	def broadcast(self, event, *args):
		for entity in self.entities:
			for f in entity._eventListeners.get(event, []):
				if getattr(f, "zone", Zone.PLAY) == Zone.PLAY:
					f(*args)
		if event != "UPDATE":
			self.broadcast("UPDATE")

	def setTag(self, tag, value):
		logging.debug("%r::%r %r -> %r" % (self, tag, self.tags.get(tag, None), value))
		self.tags[tag] = value

	def unsetTag(self, tag):
		del self.tags[tag]

	def getIntProperty(self, tag):
		ret = self.tags.get(tag, 0)
		for slot in self.slots:
			_ret = slot.getIntProperty(tag)
			if isinstance(_ret, int):
				ret += _ret
			else:
				ret = _ret(ret)
		return ret

	def getBoolProperty(self, tag):
		if self.tags.get(tag, False):
			return True
		for slot in self.slots:
			if slot.getBoolProperty(tag):
				return True
		return

Allow for callables in getIntProperty()
Allow for callables in getIntProperty()

Some tags can now implicitly be functions, as long as they are called
through getIntProperty().
The function will take the current value as input, and will output the
result of a calculation. This matters for Gahz'rilla and Blessed Champion
where the attack up to the point of the buff is doubled.

Thanks, Xinhuan
