class Entity(object): # pylint: disable=R0902 """The entity class which all entities in the world are subclassed, the only authorized to say "I'm your father" quote to everybody""" def __init__(self, uid, world): self.__uid = uid self.__world = world self.__load() self.init() def __load(self): """Loads data when entity is created, doesn't run when entity is reused, ideal for image loading for example""" pass def init(self): """Values that are used for managing the entity in update(), also they are reset for reusing the object instance""" self.health = 100 #Health of entity self.max_health = 100 #Maximum health level self.nodamage = False #Makes the entity indestructible (sets the health to maximum every update and ignores health < 0) self.team = None #Team of the entity belongs self.delete = False #Deletion flag, when its True, means that it can be reused, also with this true, update and draw is not called #Private or immutable values, self.__pos = Vector(0, 0) #The actual position of the unit self.__prev_pos = Vector(0, 0) #The previous position before a net update, used for interpolation self.__heading = 0 #Heading of the unit self.__prev_heading = 0 #Previous heading of unit, used for interpolation #Values that are not send during serialization self.__image = None #Contains Surface of actual frame self.__net_contact = time() #The last network update, used for interpolation def get_uid(self): """Returns the entity's unique id""" return self.__uid @property def pos(self): """Get the position""" return self.__pos @pos.setter def pos(self, pos, update_prev = True): """Set the position""" if update_prev: self.__prev_pos = pos self.__pos = Vector(pos) @property def heading(self): """Get the heading""" return self.__heading @heading.setter def heading(self, heading, update_prev = True): """Set the heading""" if update_prev: self.__prev_heading = heading self.__heading = heading def update(self): """Called when the entity is updated""" self.health_update() def health_update(self): """Checks health""" if self.nodamage: self.health = self.max_health elif (self.health <= 0): self.delete = True def draw(self): """Called when the entity needs to be drawed""" graphics = self.__world.graphics graphics.draw_circle(COLOR_WHITE, self.__pos.round(), 5) def serialize(self): """Called when needs to serialize this entity""" #Add the positions and heading x, y = self.pos.round() data = encode_word(x) data = data + encode_word(y) data = data + encode_word(self.heading) return data def deserialize(self, data): """Called when needs to deserialize information on this entity""" #Get the x y coordinates and heading in first 3 words bytes index = 0 self.x = decode_word(data[index:index+2]) index = index + 2 self.y = decode_word(data[index:index+2]) index = index + 2 self.heading = decode_word(data[index:index+2]) def net_update(self): """Update the network contact time""" self.__net_contact = time()