def updateEntities(self, time, timePassed): disposables = [] for entity in self.entities: entity.update(time, timePassed) if entity.disposable: disposables.append(entity) for entity in disposables: self.entities.remove(entity)
def draw_entities(self): """ Draw every entities on the map in their current state of animation. """ for i in xrange(HEIGHT): layer = self.l_entities.get_layer(i) for entity in layer.values(): image = entity.update() self.draw_entity_HUD(entity.faction, entity.hp, entity.max_hp, entity.pixel_pos, entity.height, entity.width) self.window.blit(image, entity.pixel_pos)
def updateMicrodom(self): """Write the dynamically changing information back to the loaded microdom tree. The reason I don't create an entirely new tree is to preserve any application extensions that might have been put into the file. """ # First, update the model to make sure that it is internally consistent for (name,i) in self.instances.items(): for parent in filter(lambda ent: isinstance(ent, Entity), i.childOf): # If the object has parent pointers, update them. This is pretty specific to SAFplus data types... fieldName = parent.et.name[0].lower() + parent.et.name[1:] # uncapitalize the first letter to make it use SAFplus bumpycase if i.data.has_key(fieldName): i.data[fieldName] = parent.data["name"] # Locate or create the needed sections in the XML file # find or create the entity area in the microdom entities = self.data.getElementsByTagName("entities") if not entities: entities = microdom.MicroDom({"tag_":"entities"},[],[]) self.data.addChild(entities) else: assert(len(entities)==1) entities = entities[0] # Find or create the GUI area in the microdom. The GUI area is structured like: # ide # ide_entity_info # ide_instance_info ide = self.data.getElementsByTagName("ide") if not ide: ide = microdom.MicroDom({"tag_":"ide"},[],[]) self.data.addChild(ide) else: assert(len(ide)==1) ide = ide[0] ideEntities = ide.getElementsByTagName("ide_entity_info") if not ideEntities: ideEntities = microdom.MicroDom({"tag_":"ide_entity_info"},[],[]) ide.addChild(ideEntities) else: assert(len(ideEntities)==1) ideEntities = ideEntities[0] ideInsts = ide.getElementsByTagName("ide_instance_info") if not ideInsts: ideInsts = microdom.MicroDom({"tag_":"ide_instance_info"},[],[]) ide.addChild(ideInsts) else: assert(len(ideInsts)==1) ideInsts = ideInsts[0] # Write out the entities # iterate through all entities writing them to the microdom, or changing the existing microdom for (name,e) in self.entities.items(): # Find the existing DOM nodes for the entity information, creating the node if it is missing entity = entities.findOneByChild("name",name) if not entity: entity = microdom.MicroDom({"tag_":e.et.name},[],[]) entities.addChild(entity) ideEntity = ideEntities.getElementsByTagName(name) if ideEntity: ideEntity = ideEntity[0] else: ideEntity = microdom.MicroDom({"tag_":name},[],[]) ideEntities.addChild(ideEntity) # Remove all "None", replacing with the default or "" temp = {} for (key,val) in e.data.items(): if val is None: val = e.et.data[key].get("default",None) if val is None: val = "" if val == "None": val = "" temp[key] = val # Write all the data fields into the model's microdom entity.update(temp) # write the IDE specific information to the IDE area of the model xml ideEntity["position"] = str(e.pos) ideEntity["size"] = str(e.size) # Now write all the arrows contains = {} # Create a dictionary to hold all linkages by type for arrow in e.containmentArrows: # Add the contained object to the dictionary keyed off of the object's entitytype tmp = contains.get(arrow.contained.et.name,[]) tmp.append(arrow.contained.data["name"]) contains[arrow.contained.et.name] = tmp # TODO: write the containment arrow IDE specific information to the IDE area of the model xml self.writeContainmentArrow(ideEntity, arrow) # Now erase the missing linkages from the microdom for (key, val) in self.entityTypes.items(): # Look through all the children for a key that corresponds to the name of an entityType (+ s), eg: "ServiceGroups" if not contains.has_key(key): # Element is an entity type but no linkages if entity.child_.has_key(key + 's'): entity.delChild(key + 's') # Ok now write the linkages to the microdom for (key, val) in contains.items(): k = key + "s" if entity.child_.has_key(k): entity.delChild(k) entity.addChild(microdom.MicroDom({"tag_":k},[",".join(val)],"")) # TODO: do we really need to pluralize? Also validate comma separation is ok # Building instance lock fields etType = ide.getElementsByTagName(e.et.name) if not etType: etType = microdom.MicroDom({"tag_":e.et.name},[],[]) ide.addChild(etType) else: assert(len(etType)==1) etType = etType[0] et = etType.getElementsByTagName(name) if not et: et = microdom.MicroDom({"tag_":name},[],[]) etType.addChild(et) else: assert(len(et)==1) et = et[0] et.update(e.instanceLocked) # Find or create the instance area in the microdom instances = self.data.getElementsByTagName("instances") if not instances: instances = microdom.MicroDom({"tag_":"instances"},[],[]) self.data.addChild(instances) else: assert(len(instances)==1) instances = instances[0] # iterate through all instances writing them to the microdom, or changing the existing microdom for (name,e) in self.instances.items(): instance = instances.findOneByChild("name",name) if not instance: instance = microdom.MicroDom({"tag_":e.et.name},[],[]) instances.addChild(instance) # Remove all "None", replacing with the default or "" temp = {} for (key,val) in e.data.items(): if val is None: val = e.et.data[key].get("default",None) if val is None: val = "" if val == "None": val = "" temp[key] = val # Add module and xpath attributes instance.addAttribute("xpath",e.entity.et.data["xpath"] + ("[@name=\"%s\"]" % e.data["name"])) instance.addAttribute("module",e.entity.et.data["module"]) # Write all the data fields into the model's microdom instance.update(temp) # Now write all the arrows contains = {} # Create a dictionary to hold all linkages by type for arrow in e.containmentArrows: # Add the contained object to the dictionary keyed off of the object's entitytype # leaf-list entity type with camelCase(s) key = arrow.contained.et.name[0].lower() + arrow.contained.et.name[1:] + 's' tmp = contains.get(key,[]) tmp.append(arrow.contained.data["name"]) contains[key] = tmp # Now erase the missing linkages from the microdom for (key, val) in self.entityTypes.items(): # Look through all the children for a key that corresponds to the name of an entityType (+ s), eg: "serviceUnits" key = key[0].lower() + key[1:] + 's' if not contains.has_key(key): # Element is an entity type but no linkages if instance.child_.has_key(key): instance.delChild(key) # Ok now write the linkages to the microdom for (key, vals) in contains.items(): if instance.child_.has_key(key): instance.delChild(key) for val in vals: instance.addChild(microdom.MicroDom({"tag_":key},[val],"")) # TODO: do we really need to pluralize? Also validate comma separation is ok # Extra parent entity name entityParentVal = e.entity.data["name"] entityParentKey = "%sType"%e.et.name if instance.child_.has_key(entityParentKey): instance.delChild(entityParentKey) instance.addChild(microdom.MicroDom({"tag_":entityParentKey},[entityParentVal],""))
def update(self, timeSinceLastUpdate, *args, **kwargs): #We start by clearing the collision manager self.timer += timeSinceLastUpdate self.collisionManager.clear() #As required by the Cocos collision API, we now add all the #collider entities into it again for entity in self.entityList: if entity.isCollider: self.collisionManager.add(entity) #Beam is handled in a special way, using a string of circular subcolliders #along its length. self.player.beam.addToCollisionManager(self.collisionManager) #Handle all collisions between entities for firstObject, secondObject in self.collisionManager.iter_all_collisions(): # strId1 = "Beam" # strId2 = "Beam" # if hasattr(firstObject,"entityId"): # strId1 = str(firstObject.entityId) # if hasattr(secondObject,"entityId"): # strId2 = str(secondObject.entityId) # print("Checking between "+strId1+" and "+strId2) if firstObject.isCollider: firstObject.notifyCollision(secondObject) elif firstObject.beamSubCollider: self.player.beam.notifyCollision(secondObject) if secondObject.isCollider: secondObject.notifyCollision(firstObject) elif secondObject.beamSubCollider: self.player.beam.notifyCollision(firstObject) #Once all collisions are over, we remove all the entities #that were marked as killed because collisions with other #entities (like the beam). We can now clear this #queue of zombie actors from game manager self.clearDeadEntities() #Now we update the game logic for the entities for entity in self.entityList: entity.update(timeSinceLastUpdate, args, kwargs) #Finally, we handle the post collision check updates for the #entities for entity in self.entityList: if hasattr(entity,'isCollider') or hasattr(entity,'isBeam'): entity.updateCollision() self.textTimer += timeSinceLastUpdate if self.textTimer > 5.0: self.text.visible = False self.debugText.x = self.player.sprite.x+200 if self.debugText.x<250: self.debugText.x = 250 #Spawn npc if (self.timer-self.lastSpawnAt)>0.5 and self.oneSpawn==False: self.lastSpawnAt = self.timer randLoc = cocos.euclid.Vector2(0.0,0.0) randLoc.x = self.player.currentFocus.x + 550 randLoc.y = random.randint(30,348) charTypeId = random.randint(0,1) charType = self.characterTypes[charTypeId] entityNameIndx = random.randint(0,len(self.npcs[charTypeId])-1) entityName = self.npcs[charTypeId][entityNameIndx] #print("Spawning " + charType + " entity named " + entityName) npchar = npc.NPC(entityName,charType,randLoc) self.addEntity(npchar, self.mainLayer)
def run_level(level): player = level.player triggers = level.triggers entities = level.entities background_entities = level.background_entities camera = level.camera barbers = level.barbers while True: CLOCK.tick(FPS) key_presses = pygame.event.get(pygame.KEYDOWN) key_states = pygame.key.get_pressed() for event in pygame.event.get(): if event.type == pygame.QUIT: return 'quit' if key_states[pygame.K_r]: return 'restart' if key_states[pygame.K_ESCAPE]: return 'quit' if player.life_lost == True: display_life_lost_screen() player.life_lost = False player.update(key_presses, key_states, level) player.mustache.update() camera.update(player) if player.dead == True: display_death_screen() return 'dead' if player.mustache_numb == 4 and player.completed == False: display_sequence_screens(SCREENS_COMPLETED) player.completed = True # draw the background for y in range(26): for x in range(20): SCREEN.blit(IMAGES['bg_wall'], (x * TILESIZE, y * TILESIZE)) for entity in background_entities: entity.update() if entity.image.get_rect().colliderect(SCREEN.get_rect()): SCREEN.blit(entity.image, camera.apply(entity)) for entity in triggers: SCREEN.blit(entity.image, camera.apply(entity)) for barber in barbers: barber.update(level) for entity in entities: if entity.image.get_rect().colliderect(SCREEN.get_rect()): SCREEN.blit(entity.image, camera.apply(entity)) SCREEN.blit(player.mustache.image, camera.apply(player)) pygame.display.update()