def __init__(self, filename): super(GameEntity, self).__init__() self.gravity = True self.timers = pygame.sprite.Group() self.position = Vector3(-100, -100, 0) self.acceleration = Vector3(0, 0, 0) self.velocity = Vector3(0, 0, 0) self.original_image = resources.tiles[filename] self.anchor = None self.radius = .4 self._layer = 1 self.max_velocity = [.15, .15, 100] self.scale = 1 self._flipped = False self.image = None self.rect = self.original_image.get_rect() self.original_anchor = Vector2(*self.rect.midbottom) self.update_image() self.dirty = 1 self.move_sound = None self.carried = None self._playing_move_sound = False self.pickup_item_sound = resources.sounds['woosh1.ogg'] self.drop_item_sound = resources.sounds['woosh2.ogg'] self.bounce_sound = resources.sounds['boing-slow.ogg'] self.bounce_sound.set_volume(.2) # self.drop_sound = resources.sounds[''] # self.injure = resources.sounds[''] # self.surprise_sound = resources.sounds[''] # self.chase_sound = resources.sounds[''] self._collided = set() self._pickup_cooldown = 0 self._attached = None
def add_entity(self, entity, position): sx, sy = axial_to_sprites(evenr_to_axial(position)) entity.position = Vector3(sx, sy, 900) if hasattr(entity, 'home'): entity.home = Vector3(entity.position) self.view.add(entity) self.internal_event_group.add(entity) self.velocity_updates.add(entity)
def __init__(self, data): super(PhysicsGroup, self).__init__() self.data = data self.gravity = Vector3(0, 0, config.getfloat('world', 'gravity')) self.timestep = config.getfloat('world', 'physics_tick') self.gravity_delta = None self.ground_friction = None self.sleeping = set() self.wake = set() self.stale = set() self.collide_walls = set()
def build_button(self, door_key, door_sprite_file_name, position, anchor=None): # send position in even r coordinates if anchor is None: anchor = Point2(30, 60) sx, sy = axial_to_sprites(evenr_to_axial(position)) button = Button(door_sprite_file_name, door_key) button.position = Vector3(sx, sy, 0) button.anchor = anchor button.update_image() self.view.add(button, layer=0) self.internal_event_group.add(button) self.velocity_updates.add(button) return button
def move_hero(self, position): sx, sy = axial_to_sprites(evenr_to_axial(position)) self.hero.position = Vector3(sx, sy, 900)
def draw(self, surface): if self.needs_cache: buffer_size = surface.get_size() self.map_buffer = pygame.Surface(buffer_size, pygame.SRCALPHA) self.map_buffer.blit(self.background, (0, 0)) self.rect = self.map_buffer.get_rect() self.project = self.get_projection() self._hex_draw = self.get_hex_draw() self._hex_tile = self.get_hex_tile() self.needs_cache = False self.needs_refresh = True self.rect = self.map_buffer.get_rect() dirty = self.lostsprites project = self.project draw_tile = self._hex_tile draw_hex = self._hex_draw get_cell = self.data.get_cell surface_blit = surface.blit surface_rect = surface.get_rect() buffer_blit = self.map_buffer.blit dirty_append = dirty.append spritedict = self.spritedict self.lostsprites = list() refreshed = False # draw the cell tiles, a thread will blit the tiles in the background # this is rendering the background and all hex tiles if self.needs_refresh: for sprite in spritedict.keys(): try: sprite.dirty = 1 except: pass upper_buffer = pygame.Surface(surface.get_size(), pygame.SRCALPHA) buffer2_blit = upper_buffer.blit upper_cells = list() # get in draw order ww, hh = self.data.size for rr, qq in product(range(hh), range(ww)): pos = evenr_to_axial((qq, rr)) cell = get_cell(pos) pos = Vector2(*project(pos, cell)) # draw tall columns if cell.height > 0: rects = list() draw_tile(buffer_blit, self.default_cell, pos) for i in range(int(ceil(cell.height))): # pos.y -= self.hex_radius / 2 * float(cell.height) pos.y -= self.voff / 2 draw_tile(buffer_blit, cell, pos) rect = draw_tile(buffer2_blit, cell, pos) rects.append(rect) rect = rects[0].unionall(rects[:1]) surf = pygame.Surface(rect.size, pygame.SRCALPHA) surf.blit(upper_buffer, (0, 0), rect) upper_cells.append(UpperLayerRect(surf, rect, 1)) else: draw_tile(buffer_blit, cell, pos) self.layer_quadtree = quadtree.FastQuadTree(upper_cells, 4) rect = surface_blit(self.map_buffer, self.rect) dirty_append(rect) self.needs_refresh = False refreshed = True # draw cell lines (outlines and highlights) to the surface surface.lock() for pos, cell in self.data.cells: if cell in self._selected: fill = self.select_color elif cell is self._hovered: fill = self.hover_color else: continue old_rect = spritedict.get("hover", None) pos = Vector3(*project(pos, cell)) rect = draw_hex(surface, pos, self.border_color, fill) if not refreshed: if old_rect: if rect.colliderect(rect): dirty_append(rect.union(old_rect)) else: dirty_append(rect) dirty_append(rect) else: dirty_append(rect) spritedict["hover"] = rect surface.unlock() overlap_limit = self.overlap_limit for sprite in [s for s in self.sprites() if s.visible & s.dirty]: # hover is the internal name for the tile cursor if sprite == "hover": continue pos = sprites_to_axial(sprite.position) x, y = project(pos, use_cache=False) pos = (int(round(x - sprite.anchor.x, 0)), int(round(y - sprite.anchor.y - sprite.position.z, 0))) if not sprite.dirty == 2: sprite.dirty -= 1 rect = surface_blit(sprite.image, pos) clipped_rect = rect.clip(surface_rect) # this will be true if the sprite was blit off the surface if not clipped_rect: continue old_rect = spritedict[sprite] spritedict[sprite] = rect for _sprite, _rect in spritedict.items(): if _sprite == "hover": continue if _sprite is sprite or not _rect: continue if rect.colliderect(_rect): sprite.dirty = 1 _sprite.dirty = 1 if not refreshed: if old_rect: if rect.colliderect(rect): dirty_append(rect.union(old_rect)) else: dirty_append(rect) dirty_append(rect) else: dirty_append(rect) sprite_layer = sprite._layer for up in self.layer_quadtree.hit(rect): if sprite_layer <= up.layer + 1: if rect.bottom < up.bottom - overlap_limit: overlap = rect.clip(up.rect) if overlap: surface.set_clip(overlap) surface_blit(up.surface, up.rect) surface.set_clip(None) return dirty
def stop(self): self.velocity = Vector3(0, 0, 0) self.acceleration = Vector3(0, 0, 0) self.wake()
def attach(self, other, anchor=None): if anchor is None: anchor = (0, 0, 0) anchor = Vector3(*anchor) self.wake() self._attached = other, anchor