def update(self, items_dict): self.tiles = [] yn = 0 n = 1 for item, number in items_dict.items(): point = Point(self.x, self.y - yn * self.dy) tilename = self.tilenames[item] width = self.surface.tiledict[tilename].width height = self.surface.tiledict[tilename].height shift = Point(width, height / 2) sprite = create_tile(point - shift, tilename) label = create_label("F-%s [%s]" % (n, number), point) self.tiles.append(label) self.tiles.append(sprite) self.slots[n] = item yn += 1 n += 1
def choice_position(self, player, radius=7, start=False, ask_player=False): "выбирает случайную позицию, доступную для объекта" if not start: start = Point(self.size / 2, self.size / 2) else: start = start / TILESIZE lim = 1000 counter = 0 cords = set() timeout = self.size ** 2 while len(cords) < timeout: position = start + Point(randrange(-radius, radius), randrange(-radius, radius)) cord = position if cord not in cords: cords.add(cord) i, j = cord.get() if 1 < i < self.size - 1 and 1 < j < self.size - 1: if not self.map[i][j] in player.BLOCKTILES: #проверяем, подходит ли клетка объекту position = position * TILESIZE location = self.get_location(position) exp = ask_player and player.choice_position(self, location, i ,j) if counter > 1000 or exp: shift = Point(randrange(TILESIZE-1), randrange(TILESIZE-1)) return position + shift counter += 1 if counter > lim: lim *= 2 if radius < self.size / 2: radius = int(radius * 1.5) raise BaseException('world[%s].choice_position: no place for %s' % (self.name, player))
def __init__(self, world, i, j): self.world = world self.i, self.j = i, j self.cord = Point(i, j) self.nears = [] ActivityContainer.__init__(self) LocationObjects.__init__(self) LocationEvents.__init__(self)
def choice_position(cls, world, location, i, j): if len(world.tiles[Point(i, j)]): return False for ij in world.get_near_cords(i, j): for player in world.tiles[Point(*ij)]: if isinstance(player, AloneTree): return True if chance(98): return False else: return True
class Location(ActivityContainer, LocationEvents, LocationObjects): "небольшие локаци на карте, содержат ссылки на соседние локации и хранят ссылки на объекты и события" def __init__(self, world, i, j): self.world = world self.i, self.j = i, j self.cord = Point(i, j) self.nears = [] ActivityContainer.__init__(self) LocationObjects.__init__(self) LocationEvents.__init__(self) def create_links(self): "создает сслыки на соседние локации" for i, j in near_cords: try: near_location = self.world.locations[self.i + i][self.j + j] except IndexError: pass else: self.nears.append(near_location) def update(self): LocationObjects.update(self) def complete_round(self): LocationObjects.complete_round(self) LocationEvents.complete_round(self) def set_activity(self): self.world.active_locations[self.cord.get()] = self def unset_activity(self): key = self.cord.get() if key in self.world.active_locations: del self.world.active_locations[key] else: print_log("key error", key)
class Location(ActivityContainer, LocationEvents, LocationObjects): "небольшие локаци на карте, содержат ссылки на соседние локации и хранят ссылки на объекты и события" def __init__(self, world, i, j): self.world = world self.i, self.j = i, j self.cord = Point(i, j) self.nears = [] ActivityContainer.__init__(self) LocationObjects.__init__(self) LocationEvents.__init__(self) def create_links(self): "создает сслыки на соседние локации" for i, j in near_cords: try: near_location = self.world.locations[self.i + i][self.j + j] except IndexError: pass else: self.nears.append(near_location) def update(self): LocationObjects.update(self) def complete_round(self): LocationObjects.complete_round(self) LocationEvents.complete_round(self) def set_activity(self): self.world.active_locations[self.cord.get()] = self def unset_activity(self): key = self.cord.get() if key in self.world.active_locations: del self.world.active_locations[key] else: print_log('key error', key)
def __init__(self, game, name): PersistentWorld.__init__(self, name) self.game = game self.name = name self.teleports = [Point(self.size / 2, self.size / 2)] self.location_size = self.size / LOCATIONSIZE + 1 self.tiles = defaultdict(lambda: set()) self.players = {} self.static_objects = {} self.locations = [] self.active_locations = {} self.create_locations() self.create_links() init = imp.load_source('init', WORLD_PATH % name + 'init.py') self.generate_func = init.generate self.init_func = init.init
def unpack(cls,land,observed): land = [(Point(x,y), str(tilename)) for x,y, tilename in land] observed = [(i,j) for (i,j) in observed] return land, observed
def __init__(self): self.shift = Point(0, 0) self.tiles = []
def unpack(cls, wold_name, world_size, xy, background): position = Point(*xy) return wold_name, world_size, position, background
def unpack(cls, x,y): move_vector = Point(x,y) return move_vector
def unpack(cls, x_y, destination): x,y = x_y return (Point(x,y), destination)
def unpack(cls, x,y): return Point(x,y)
#!/usr/bin/env python # -*- coding: utf-8 -*- from config import * from weakref import proxy, ProxyType from engine.enginelib.meta import ActiveState from share.mathlib import Point #список инкременаторов к индексу на соседние локации nears = ((-1, 1), (0, 1), (1, 1), (-1, 0), (1, 0), (-1, -1), (0, -1), (1, -1)) near_cords = [Point(i, j) for i, j in nears] class ObjectContainer: proxy_list = () def __init__(self): for p_type in self.proxy_list: assert isinstance(p_type, type) self.__forproxy__ = { p_type: p_type.__name__ for p_type in self.proxy_list } self.proxy_dicts = { type_name: {} for type_name in self.__forproxy__.values() } self.new_players = False
def on_mouse_release(self, x, y, button, modifiers): for surface in self.surfaces: if Point(x, y) in surface: surface.on_mouse_release(x, y, button, modifiers)
def __init__(self, height, width): Window.__init__(self, height, width) self.clock_setted = False self.complete = 0 self.center = Point(height / 2, width / 2)
def on_mouse_press(self, x, y, button, modifiers): "перехватывавем нажатие левой кнопки мышки" for surface in self.surfaces: if Point(x, y) in surface: surface.on_mouse_press(x, y, button, modifiers)
def force_complete(self): if self.vector: self.position += self.vector self.vector = Point(0, 0)
def move(self, xy): vector = Point(*xy) self.vector += vector if self.vector: self.moving = True
def __init__(self, frames=1): Animated.__init__(self) self.moving = False self.vector = Point(0, 0) self.create_animation('moving', 'move', frames, 2)
def choice_position(cls, world, location, i, j): if len(world.tiles[Point(i, j)]): return False else: return True
def unpack(cls, players): players = dict([(gid, (name, o_type, Point(x,y), args)) for gid, (name, o_type, (x,y), args) in players.items()]) return players
def on_mouse_drag(self, x, y, dx, dy, button, modifiers): for surface in self.surfaces: if Point(x, y) in surface: surface.on_mouse_drag(x, y, dx, dy, button, modifiers)
def unpack_events(cls, events): return [(name, object_type, Point(x,y), timeout, action, args) for (name, object_type, (x,y), timeout, action, args) in events]
#!/usr/bin/env python # -*- coding: utf-8 -*- from config import * from weakref import proxy, ProxyType from share.mathlib import Point from engine.enginelib.meta import DynamicObject, StaticObject, ActiveState, Solid from engine.world.objects_containers import ActivityContainer from share.logger import print_log #список инкременаторов к индексу на соседние локации near_cords = [ cord.get() for cord in (Point(-1, 1), Point(0, 1), Point(1, 1), Point(-1, 0), Point(1, 0), Point(-1, -1), Point(0, -1), Point(1, -1)) ] class LocationEvents: "функционал локации для работы с событиями" def __init__(self): self.events = [] self.static_events = [] self.new_events = False self.new_static_events = False self.timeouted_events = []
def draw_label(self): label = create_label('%d/%d' % (self.hp, self.hp_value), self.position+Point(0, self.sprite.height)) return [label]
def __init__(self, x, y, width, height): self.x, self.y = x, y self.width = width self.height = height self.center = Point(width / 2, height / 2) self.elements = []
def on_mouse_motion(self, x, y, dx, dy): for surface in self.surfaces: if Point(x, y) in surface: surface.on_mouse_motion(x, y, dx, dy)