def newgame(self, level): logger.info("Starting new game...") self.window.flush() # for i in range(100): # print(str(i) + ": " + self.generate_name("surname")) dungeon_cfg = load_cfg("Dungeon2") self.dungeon = DungeonGenerator( logger, dungeon_cfg ) self.game_size = ( dungeon_cfg["dungeon_size"][0] * 32, dungeon_cfg["dungeon_size"][1] * 32 ) self.space = pymunk.Space() self.dungeon.generate() self.spatial_hash = spatial.SpatialHash() for w in self.dungeon.walls: self.spatial_hash.insert_object_for_rect( w.p1, w.p2, w ) for c in self.dungeon.collidable: self.spatial_hash.insert_object_for_rect( c.p1, c.p2, c ) logger.info("Generating dungeon graphics...") img_grid = self.dungeon.get_tilemap() grid_to_image( img_grid, "dungeon", self.game_size ) dungeon_texture = self.window.load_single_texture( "dungeon" ) dungeon_overlay_texture = self.window.load_single_texture( "dungeon_overlay" ) self.dungeon_sprite = pyglet.sprite.Sprite( dungeon_texture, x=0, y=0, batch=self.window.batches["dungeon"] ) self.dungeon_overlay_sprite = pyglet.sprite.Sprite( dungeon_overlay_texture, x=0, y=0, batch=self.window.batches["dungeon_overlay"] ) logger.info("Done.") start = self.window.grid_to_window(*self.dungeon.startroom.center) # self.tiles = tiles.TiledRenderer(self.window, level) self.spawn_player(start) # Add a static square for every dungeon wall for w in self.dungeon.walls: body = pymunk.Body() body.position = w.center box_points = [(-16, -16), (-16, 16), (16, 16), (16, -16)] shape = pymunk.Poly(body, box_points, (0, 0)) shape.group = 1 self.space.add(shape) w.body = body for c in self.dungeon.collidable: body = pymunk.Body() body.position = c.center box_points = [(-16, -16), (-16, 16), (16, 16), (16, -16)] shape = pymunk.Poly(body, box_points, (0, 0)) shape.group = 1 self.space.add(shape) c.body = body # self.space.add_collision_handler(1, 1, post_solve=smack) self.space.damping = 0.001 itemlist = items.L1_Sword(), items.L1_Armor(), items.L1_Ring() for i in itemlist: self.player.equip_item(i) self.player.update_stats() self.player.restore_stats() logger.info("Spawning enemies...") self.enemies = [] for r in self.dungeon.enemy_rooms: for spawn in r.spawn_locations: x, y = self.window.grid_to_window(*spawn) self.add_enemy(x, y) i = 0 for e in self.enemies: e.modifiers["str"] += i e.update_stats() e.reset() i += 3 logger.info("Done.") if self.window.ui: self.window.ui.add_stats( self.player, 0, self.window.height, 120, 240 ) logger.info("Game successfully loaded.")
class Game: def __init__(self, window): self.window = window self.cfg = load_cfg("Game") def newgame(self, level): logger.info("Starting new game...") self.window.flush() # for i in range(100): # print(str(i) + ": " + self.generate_name("surname")) dungeon_cfg = load_cfg("Dungeon2") self.dungeon = DungeonGenerator( logger, dungeon_cfg ) self.game_size = ( dungeon_cfg["dungeon_size"][0] * 32, dungeon_cfg["dungeon_size"][1] * 32 ) self.space = pymunk.Space() self.dungeon.generate() self.spatial_hash = spatial.SpatialHash() for w in self.dungeon.walls: self.spatial_hash.insert_object_for_rect( w.p1, w.p2, w ) for c in self.dungeon.collidable: self.spatial_hash.insert_object_for_rect( c.p1, c.p2, c ) logger.info("Generating dungeon graphics...") img_grid = self.dungeon.get_tilemap() grid_to_image( img_grid, "dungeon", self.game_size ) dungeon_texture = self.window.load_single_texture( "dungeon" ) dungeon_overlay_texture = self.window.load_single_texture( "dungeon_overlay" ) self.dungeon_sprite = pyglet.sprite.Sprite( dungeon_texture, x=0, y=0, batch=self.window.batches["dungeon"] ) self.dungeon_overlay_sprite = pyglet.sprite.Sprite( dungeon_overlay_texture, x=0, y=0, batch=self.window.batches["dungeon_overlay"] ) logger.info("Done.") start = self.window.grid_to_window(*self.dungeon.startroom.center) # self.tiles = tiles.TiledRenderer(self.window, level) self.spawn_player(start) # Add a static square for every dungeon wall for w in self.dungeon.walls: body = pymunk.Body() body.position = w.center box_points = [(-16, -16), (-16, 16), (16, 16), (16, -16)] shape = pymunk.Poly(body, box_points, (0, 0)) shape.group = 1 self.space.add(shape) w.body = body for c in self.dungeon.collidable: body = pymunk.Body() body.position = c.center box_points = [(-16, -16), (-16, 16), (16, 16), (16, -16)] shape = pymunk.Poly(body, box_points, (0, 0)) shape.group = 1 self.space.add(shape) c.body = body # self.space.add_collision_handler(1, 1, post_solve=smack) self.space.damping = 0.001 itemlist = items.L1_Sword(), items.L1_Armor(), items.L1_Ring() for i in itemlist: self.player.equip_item(i) self.player.update_stats() self.player.restore_stats() logger.info("Spawning enemies...") self.enemies = [] for r in self.dungeon.enemy_rooms: for spawn in r.spawn_locations: x, y = self.window.grid_to_window(*spawn) self.add_enemy(x, y) i = 0 for e in self.enemies: e.modifiers["str"] += i e.update_stats() e.reset() i += 3 logger.info("Done.") if self.window.ui: self.window.ui.add_stats( self.player, 0, self.window.height, 120, 240 ) logger.info("Game successfully loaded.") def add_enemy(self, x, y): inertia = pymunk.moment_for_circle(20, 0, 12, (0, 0)) body = pymunk.Body(20, inertia) body.position = (x, y) shape = pymunk.Circle(body, 12, (0, 0)) shape.group = 0 shape.elasticity = 0.2 self.space.add(body, shape) e = Enemy(self, window=self.window, x=x, y=y) e.name = self.generate_name("surname") e.body = body self.enemies.append(e) def spawn_player(self, point): logger.info("Spawning player...") self.player = Mage( self, window=self.window, x=point[0], y=point[1] ) # Player body for physics inertia = pymunk.moment_for_circle(10, 0, 12, (0, 0)) body = pymunk.Body(10, inertia) body.position = (self.player.x, self.player.y) shape = pymunk.Circle(body, 12, (0, 0)) shape.elasticity = 0.2 # shape.collision_type = 1 shape.group = 0 self.space.add(body, shape) self.player.body = body self.window.set_offset( self.player.windowpos[0] - self.player.x, self.player.windowpos[1] - self.player.y, ) self.window.update_display_dungeon() logger.info("Done.") def generate_name(self, type): syllables = load_cfg("Syllables")[type] syllable_count = random.randrange(2, 4) surname, title_pre, title_post, i = "", "", "", 0 apos_used = False while i < syllable_count: syl = random.choice(syllables) if i and not random.randint(0, 10) and not apos_used: surname += "'" apos_used = True # if i and not random.randint(0, 40): # i -= 1 if syl not in surname: surname += syl i += 1 if not random.randint(0, 4): tp = load_cfg("Syllables")["title_pre"] title_pre = random.choice(tp) tp = load_cfg("Syllables")["title_post"] title_post = random.choice(tp) if random.randint(0, 3): fullname = "{0} the {1}{2}".format( surname.title(), title_pre.title(), title_post, ) else: fullname = "{0} {1}{2}".format( surname.title(), title_pre.title(), title_post, ) elif not random.randint(0, 6): if random.randint(0, 1): tp = load_cfg("Syllables")["title_pre"] else: tp = load_cfg("Syllables")["title_post"] title_pre = random.choice(tp) if title_pre[0] == "-": title_pre = title_pre[1:] fullname = "{0} the {1}".format( surname.title(), title_pre.title(), ) else: fullname = surname.title() if check_consecutive_letters(fullname, 3): # self.generate_name("surname") return "ERRORNAME LOL" else: return fullname def check_enemy_collision(self, x, y): for e in self.enemies: if check_point_rectangle(x, y, e.rectangle): return True else: return False def update_z_index(self): pass # for e in self.enemies: # if e.y > self.player.y: # e.sprite.group = self.window.bg_group # else: # e.sprite.group = self.window.fg_group def update(self, dt): for x in range(10): self.space.step(dt / 10) if self.player: self.player.update(dt) if len(self.enemies) > 0: self.update_z_index() for e in self.enemies: e.update(dt)
class GameWorld(World): def __init__(self): loadstart = time.clock() self.cfg = load_cfg("Game") # Initialize a window self.window = GameWindow() # Forward window events to gameworld self.window.on_mouse_motion = self.on_mouse_motion self.window.on_mouse_press = self.on_mouse_press self.window.on_mouse_scroll = self.on_mouse_scroll self.window.on_key_press = self.on_key_press self.window.on_resize = self.on_resize # Set default zoom self.view_area = ( 0, self.window.width, 0, self.window.height ) self.zoom_factor = 1.5 # Create a keyboard input handler for pyglet window self.input_keys = pyglet.window.key.KeyStateHandler() self.window.push_handlers(self.input_keys) self.new_game(load=True) end = time.clock() logger.info("Game loaded in {:.3f}s.".format(end - loadstart)) def new_game(self, load=False): super().__init__() self.start_systems() # self.components = {} self.window.offset_x, self.window.offset_y = 640, 360 # Create batches and load textures logger.info("Loading textures...") start = time.clock() self.batches = OrderedDict() self.load_textures() end = time.clock() logger.info("Done, {:.3f}s.".format(end - start)) # Setup dungeon configuration logger.info("Building dungeon...") start = time.clock() dungeon_cfg = load_cfg("Dungeon2") self.dungeon = DungeonGenerator( logger, dungeon_cfg ) self.game_size = ( dungeon_cfg["dungeon_size"][0] * 32, dungeon_cfg["dungeon_size"][1] * 32 ) if load: self.dungeon.load("saved.json") else: self.dungeon.generate() end = time.clock() logger.info("Done, {:.3f}s.".format(end - start)) if not load: self.dungeon.save("saved.json") logger.info("Generating dungeon graphics...") start = time.clock() if load: pass else: img_grid = self.dungeon.get_tilemap() grid_to_image( img_grid, "dungeon", self.game_size ) dungeon_texture = self.load_single_texture( "dungeon" ) dungeon_overlay_texture = self.load_single_texture( "dungeon_overlay" ) self.bg = BG(self, dungeon_texture) self.fg = FG(self, dungeon_overlay_texture) self.bg.batch = Batch("dungeon") self.bg.batch.group = 0 self.bg.position.set(0, 0) self.fg.batch = Batch("dungeon") self.fg.batch.group = 1 self.fg.position.set(0, 0) end = time.clock() logger.info("Done, {:.3f}s.".format(end - start)) # Make physical space for physics engine logger.info("Setup physics space...") start = time.clock() self.phys_space = pymunk.Space() self.phys_space.damping = 0.001 end = time.clock() logger.info("Done, {:.3f}s.".format(end - start)) # Add entities to the world logger.info("Spawning enemies...") start = time.clock() self.enemies = [] for r in self.dungeon.enemy_rooms: for spawn in r.spawn_locations: x, y = spawn[0] * 32, spawn[1] * 32 e = Enemy(self) e.position.set(x, y) self.enemies.append(e) end = time.clock() logger.info("Done, {:.3f}s.".format(end - start)) logger.info("Generating wall rectangles...") start = time.clock() wall_rects = self.dungeon.wall_rects self.walls = [] for w in wall_rects: p1, p2 = w x = (p1[0] * 32) y = (p1[1] * 32) w = p2[0] * 32 - p1[0] * 32 + 32 h = p2[1] * 32 - p1[1] * 32 + 32 # print(p1, p2, x, y, w, h) wa = Wall(self) wa.physbody = PhysBody(shape="square", width=w, height=h) wa.position.set(x, y) self.walls.append(wa) end = time.clock() logger.info("Done, {:.3f}s.".format(end - start)) logger.info("Placing collidable items...") # self.collidable = [] # for c in self.dungeon.collidable: # print(c.p1) # co = Wall(self) # co.physbody = PhysBody(shape="square", width=32, height=32) # co.position.set(*c.p1) # self.collidable.append(co) logger.info("Spawning player...") self.spawn_player() # self.e = Enemy(self) # self.e.position.set(self.p.position.x + 32, self.p.position.y) # self.e.followtarget.who = self.p # self.e.lightsource = LightSource() self.viewlines = [] def load_game(self): pass def save_game(self): pass def initialize(self): pass def spawn_player(self, point=(0, 0)): self.p = Player(self) c = self.dungeon.startroom.center x, y = c[0] * 32, c[1] * 32 self.p.position.set(x, y) def load_textures(self): goblin_img = center_image( pyglet.image.load('resources/crappy_goblin.png') ) player_body_img = center_image( pyglet.image.load('resources/player_body.png') ) player_body_glow_img = center_image( pyglet.image.load('resources/player_body_glow.png') ) wall_img = center_image( pyglet.image.load('resources/wall.png') ) bg_img = center_image( pyglet.image.load('resources/bg.png') ) self.textures = dict( enemy=goblin_img, player=player_body_img, player_glow=player_body_glow_img, wall=wall_img, bg=bg_img ) def load_single_texture(self, name, center=False): if center: return center_image( pyglet.image.load(os.path.join(RES_PATH, name + '.png')) ) else: return pyglet.image.load(os.path.join(RES_PATH, name + '.png')) def get_player(self): return self.p def get_gamepos(self, x, y): return int(x - self.window.offset_x), int(y - self.window.offset_y) def on_mouse_motion(self, x, y, dx, dy): pass # c = self.get_components(MouseControlled) # e = None # if c: # try: # e = self.get_entities(c[0])[0] # except KeyError: # pass # if e: # e.mousecontrolled.x, e.mousecontrolled.y = self.get_gamepos(x, y) # print current mouse position # print(x, y) def on_resize(self, width, height): glViewport(0, 0, width, height) self.update_ortho(width, height) # glMatrixMode(gl.GL_MODELVIEW) # glLoadIdentity() return pyglet.event.EVENT_HANDLED def on_mouse_scroll(self, x, y, scroll_x, scroll_y): if scroll_y == 1 and self.zoom_factor < 0.25: self.zoom_factor += 0.05 elif scroll_y == 1 and self.zoom_factor < 0.5: self.zoom_factor += 0.125 elif scroll_y == 1 and self.zoom_factor < 1.5: self.zoom_factor += 0.25 elif scroll_y == 1 and self.zoom_factor < 5: self.zoom_factor += 0.5 elif scroll_y == -1 and self.zoom_factor > 1.5: self.zoom_factor -= 0.5 elif scroll_y == -1 and self.zoom_factor > 0.5: self.zoom_factor -= 0.25 elif scroll_y == -1 and self.zoom_factor > 0.25: self.zoom_factor -= 0.125 elif scroll_y == -1 and self.zoom_factor > 0.06: self.zoom_factor -= 0.05 else: return None print("Zoom: ", self.zoom_factor, "x") self.update_ortho(self.window.width, self.window.height) def update_ortho(self, w, h): lessen_w = int((w - (w / self.zoom_factor)) / 2) lessen_h = int(lessen_w * (h / w)) self.view_area = lessen_w, w - lessen_w, lessen_h, h - lessen_h glMatrixMode(gl.GL_PROJECTION) glLoadIdentity() glOrtho(*self.view_area, -1, 1) glMatrixMode(gl.GL_MODELVIEW) glLoadIdentity() def on_mouse_press(self, x, y, button, modifiers): # pass # when a user presses a mouse button, print which one and mousepos print("{0} pressed at: {1},{2}.".format(button, x, y)) p = self.get_player() p.mouseclicked = MouseClicked(button, *self.get_gamepos(x, y)) def on_key_press(self, k, modifiers): if k == key.ESCAPE: # logger.info("Exiting...") pyglet.app.exit() if k == key._1: self.zoom_factor = 1 if k == key._2: self.zoom_factor = 2 if k == key._3: self.zoom_factor = 3 if k == key.F2: e = [None] c = self.get_components(Input) for comp in c: e = self.get_entities(comp) if e[0] == self.p: delattr(self.p, "input") # self.e.input = Input() else: delattr(e[0], "input") self.p.input = Input() if k == key.F4: if getattr(self.p, "staticposition"): delattr(self.p, "staticposition") self.p.windowposition = WindowPosition() else: self.p.staticposition = StaticPosition(x=640, y=360) self.window.offset_x = 640 - self.p.position.x self.window.offset_y = 360 - self.p.position.y delattr(self.p, "windowposition") if k == key.F5: if getattr(self.p, "lightsource"): delattr(self.p, "lightsource") else: self.p.lightsource = LightSource() if k == key.F8: self.new_game(load=False) if k == key.F9: self.new_game(load=True) if k == key.F11: self.cfg["lighting_enabled"] = not self.cfg["lighting_enabled"] if k == key.F12: self.cfg["show_rays"] = not self.cfg["show_rays"] c = self.get_components(KeyboardControlled) e = None if c: try: e = self.get_entities(c[0])[0] except KeyError: pass if e: if getattr(e, "activeabilities"): try: print(e.activeabilities.slots[keymap[k]]) except KeyError: # print("No ability defined for that key.") pass def start_systems(self): self.add_system(InitializeEffectiveStatsSystem(self)) self.add_system(ApplyAttributeStatsSystem(self)) self.add_system(ApplyHPSystem(self)) self.add_system(ApplyStaminaSystem(self)) self.add_system(ApplyManaSystem(self)) self.add_system(ApplyBasicAttackSystem(self)) self.add_system(ApplyMovementSpeedSystem(self)) self.add_system(AutoAttackSystem(self)) self.add_system(CheckDeadSystem(self)) self.add_system(CheckAttackTargetSystem(self)) self.add_system(CheckAutoAttackTargetSystem(self)) self.add_system(CheckFollowTargetSystem(self)) self.add_system(SearchTargetSystem(self)) self.add_system(AutoAttackInRangeSystem(self)) self.add_system(LevelUpSystem(self)) self.add_system(MobNamingSystem(self)) self.add_system(GlowBatchSystem(self)) self.add_system(SpriteBatchSystem(self)) self.add_system(InputMovementSystem(self)) self.add_system(HeadBobbingSystem(self)) self.add_system(AIBehaviorSystem(self)) self.add_system(MoveSystem(self)) self.add_system(FollowSystem(self)) self.add_system(PhysicsSystem(self)) self.add_system(TargetMobSystem(self)) self.add_system(StaticSpritePosSystem(self)) self.add_system(StaticGlowEffectPosSystem(self)) self.add_system(WindowPosSystem(self)) self.add_system(SpritePosSystem(self)) self.add_system(GlowPosSystem(self)) self.add_system(HideSpriteSystem(self)) self.add_system(PulseAnimationSystem(self)) self.add_system(CleanupClickSystem(self)) self.add_system(LightingSystem(self)) self.add_system(RenderSystem(self))
def new_game(self, load=False): super().__init__() self.start_systems() # self.components = {} self.window.offset_x, self.window.offset_y = 640, 360 # Create batches and load textures logger.info("Loading textures...") start = time.clock() self.batches = OrderedDict() self.load_textures() end = time.clock() logger.info("Done, {:.3f}s.".format(end - start)) # Setup dungeon configuration logger.info("Building dungeon...") start = time.clock() dungeon_cfg = load_cfg("Dungeon2") self.dungeon = DungeonGenerator( logger, dungeon_cfg ) self.game_size = ( dungeon_cfg["dungeon_size"][0] * 32, dungeon_cfg["dungeon_size"][1] * 32 ) if load: self.dungeon.load("saved.json") else: self.dungeon.generate() end = time.clock() logger.info("Done, {:.3f}s.".format(end - start)) if not load: self.dungeon.save("saved.json") logger.info("Generating dungeon graphics...") start = time.clock() if load: pass else: img_grid = self.dungeon.get_tilemap() grid_to_image( img_grid, "dungeon", self.game_size ) dungeon_texture = self.load_single_texture( "dungeon" ) dungeon_overlay_texture = self.load_single_texture( "dungeon_overlay" ) self.bg = BG(self, dungeon_texture) self.fg = FG(self, dungeon_overlay_texture) self.bg.batch = Batch("dungeon") self.bg.batch.group = 0 self.bg.position.set(0, 0) self.fg.batch = Batch("dungeon") self.fg.batch.group = 1 self.fg.position.set(0, 0) end = time.clock() logger.info("Done, {:.3f}s.".format(end - start)) # Make physical space for physics engine logger.info("Setup physics space...") start = time.clock() self.phys_space = pymunk.Space() self.phys_space.damping = 0.001 end = time.clock() logger.info("Done, {:.3f}s.".format(end - start)) # Add entities to the world logger.info("Spawning enemies...") start = time.clock() self.enemies = [] for r in self.dungeon.enemy_rooms: for spawn in r.spawn_locations: x, y = spawn[0] * 32, spawn[1] * 32 e = Enemy(self) e.position.set(x, y) self.enemies.append(e) end = time.clock() logger.info("Done, {:.3f}s.".format(end - start)) logger.info("Generating wall rectangles...") start = time.clock() wall_rects = self.dungeon.wall_rects self.walls = [] for w in wall_rects: p1, p2 = w x = (p1[0] * 32) y = (p1[1] * 32) w = p2[0] * 32 - p1[0] * 32 + 32 h = p2[1] * 32 - p1[1] * 32 + 32 # print(p1, p2, x, y, w, h) wa = Wall(self) wa.physbody = PhysBody(shape="square", width=w, height=h) wa.position.set(x, y) self.walls.append(wa) end = time.clock() logger.info("Done, {:.3f}s.".format(end - start)) logger.info("Placing collidable items...") # self.collidable = [] # for c in self.dungeon.collidable: # print(c.p1) # co = Wall(self) # co.physbody = PhysBody(shape="square", width=32, height=32) # co.position.set(*c.p1) # self.collidable.append(co) logger.info("Spawning player...") self.spawn_player() # self.e = Enemy(self) # self.e.position.set(self.p.position.x + 32, self.p.position.y) # self.e.followtarget.who = self.p # self.e.lightsource = LightSource() self.viewlines = []
def make_map(self): return DungeonGenerator(map_width=settings.MAP_WIDTH, map_height=settings.MAP_HEIGHT, max_rooms=settings.MAX_ROOMS, max_room_size=10, min_room_size=5).generate()