def place(self): cox, coy = 13, 4 cw, ch = 6, 6 center_positions = [] for x, y in product(range(cw), range(ch)): centerp = cox + x, coy + y center_positions.append(centerp) shuffle(center_positions) # place units assigned_positions = [] room = self.room #print(sroom.grid_pos) gx, gy = room.grid_pos ox, oy = gx * self.rw, gy * self.rh for i in range(len(center_positions)): randw, randh = center_positions.pop(0) new_pos = ox + randw, oy + randh walkable = room.walkable_map[randw][randh] if walkable and (self.pos == (None, None) or in_range(new_pos, self.pos, 3)): assigned_positions.append(new_pos) self.pos = assigned_positions[0] self.pos = assigned_positions.pop(0) self.available_spots = assigned_positions
def get_in_range_atks(unit, epos): ranges = [] weps = [] atkw = unit.attack_weapon unit.attack_weapon = None u_range = unit.get_range() weapons = unit.equipment.hand_one, unit.equipment.hand_two for w in weapons: if w == None: continue r = get_weapon_range(w) + u_range weps.append(w) ranges.append(r) can_swing_with = [] for w, r in zip(weps, ranges): if w.weapon_class == "Brawler": real_range = one_tile_range else: real_range = r if in_range(unit.pos, epos, real_range): can_swing_with.append(w) unit.attack_weapon = atkw return can_swing_with
def get_units_list_at(units_, at): units = [] for rad in range(1, 8): units.append([]) for u in units_: if in_range(u.pos, at, rad): units[-1].append(u) return units
def should_be_open_tbt(self, lout, bx, by, roomx, roomy, ox, oy, tsw, tsh, m, direction): if direction == "east": range_mod = 2 else: range_mod = 2 tw, th = self.tw, self.th cam = self.cam cx, cy = cam.get() # print(cx, cy) rw, rh = self.rw, self.rh rx = tw * roomx * rw ry = th * roomy * rh rsize = tw, th u = self.mc x, y = u.anim_pos rux, ruy = ox + x * tw, oy + y * th aox, aoy = 0, 0 if (u.state == "attacking" and u.attack_weapon.attack_type == "melee") or u.dashing: aox, aoy = u.projectile_pos mc_pos = rux + aox + cx + tw / 2, ruy + aoy + cy + th / 2 mpos = m mover = [] inrange = [] for dx, dy in product(range(tsw), range(tsh)): at = rx + (bx - ox + dx) * tw + cx, ry + (by - oy + dy) * th + cy # at = (bx+dx) * tw, (by+dy) * th # self.swin.blit(img, at) r = Rect(at, rsize) if r.collidepoint(mpos): mover.append(True) else: mover.append(False) if in_range(mc_pos, r.center, 3 + tw * range_mod): inrange.append(True) else: inrange.append(False) if any(inrange): # and any(mover): return True else: return False
def update(self, mc, mpos, mpress, ui, fighting): if fighting: return rad = 2 if self.state == "closed": if in_range(mc.pos, self.pos, rad): self.loot_spawner = self.lootmgr.new_spawner(self.pos, self.available_spots, \ self.rating, self.sound, self.weapons, use_cost = True, discount = self.discount) self.state = "opening" self.sound.play_sound_now("blahblah") elif self.state == "opening": if self.loot_spawner.done_spawning: self.state = "opened" elif self.state == "opened": if not in_range(mc.pos, self.pos, rad): if self.sound.is_sound_playing("blahblah"): self.sound.stop_sound_now("blahblah") else: if not self.sound.is_sound_playing("blahblah"): self.sound.play_sound_now("blahblah")
def update(self, g_obj, mpos, mpress, ui): self.bubble_timer.update() if in_range(self.pos, g_obj.mc.pos, 2): if self.should_draw_bub == False: self.bubble_timer.reset() self.bubble_frame = 0 self.should_draw_bub = True else: self.should_draw_bub = False if self.bubble_timer.ticked: self.bubble_timer.reset() self.bubble_frame += 1 if self.bubble_frame >= len(self.bubbles_imgs.keys()): self.bubble_frame = 0
def get_in_range_abis(a_abis, unit, epos): abis = [] atkw = unit.attack_weapon unit.attack_weapon = None u_range = unit.get_range() for a in unit.memory.get_abilities(): r = a.ability_range + u_range if in_range(unit.pos, epos, r): abis.append(a) unit.attack_weapon = atkw #return abis return []
def update(self, mc, mpos, mpress, ui, fighting): if fighting: return if self.state == "closed": self.frame = self.frames[self.rating]["closed"] rad = 2 if in_range(mc.pos, self.pos, rad): self.loot_spawner = self.lootmgr.new_spawner(self.pos, self.available_spots, \ self.rating, self.sound, self.weapons) self.state = "opening" self.sound.play_sound_now("chest open") elif self.state == "opening": self.frame = self.frames[self.rating]["spawning"] if self.loot_spawner.done_spawning: self.state = "opened" elif self.state == "opened": self.frame = self.frames[self.rating]["spawned"]
def prerender_torch(g_obj, actual_intensity): t = 3, 3 tw, th = g_obj.tw, g_obj.th new_surf = pygame.Surface((t[0] * 2 * tw, t[1] * 2 * th), flags=pygame.HWSURFACE) new_surf.fill((0, 0, 0)) new_surf.set_colorkey((0, 0, 0)) td = 5 wtr = td htr = td tcx, tcy = 2.5, 2.5 color = 255, 200, 200 for xtr, ytr in product(range(wtr), range(htr)): if not in_range((tcx, tcy), (xtr, ytr), int(td / 2)): continue dist = get_distance((tcx, tcy), (xtr, ytr)) xt, yt = t[0] + (xtr) * tw, t[1] + (ytr) * th intensity = 0 for i in range(4): if i > dist: break intensity += 1 tintensity = (1 - actual_intensity * (intensity / 3)) * 0.7 + 0.3 # print(tintensity) col = list(map(lambda x: x * tintensity, color)) torch_img = g_obj.lighttile torch_img.fill(col) torch_img.set_alpha(255) new_surf.blit(torch_img, (xt, yt)) # new_surf.set_alpha(actual_intensity) new_surf.set_alpha(80) return new_surf
def get_mouse_hover(g_obj, mpos): at_mouse = {} # map the mouse position to a tile # and see if it's inside the room mpos_mapped = map_mpos(g_obj, mpos) at_mouse["mapped"] = mpos_mapped at_mouse["room"] = get_room_at_mouse(g_obj, mpos_mapped) # get unit at the tile at_mouse["unit"] = None for u in g_obj.units: if u.pos == mpos_mapped: at_mouse["unit"] = u # create a list of units within various ranges # from the tile units = [] for rad in range(1, 8): units.append([]) for u in g_obj.units: if in_range(u.pos, mpos_mapped, rad): units[-1].append(u) at_mouse["units"] = units # get the tile index, from each layer # and whether it is walkable or not tiles, walkable = get_tiles_at_and_walkable(g_obj, at_mouse) # if there is a unit occupying the tile # then it is not walkable if at_mouse["unit"] != None: walkable = False at_mouse["tiles"] = tiles at_mouse["walkable"] = walkable at_mouse["loot"] = get_loot_at_mouse(g_obj, at_mouse) at_mouse["mouse ui item"] = get_mouse_ui_item(g_obj, at_mouse) return at_mouse
def lighting_pass_old(g_obj): dung = g_obj.dungeon for room in dung.get_rooms(): gx, gy = room.grid_pos tw, th = room.tilesets.tw, room.tilesets.th rw, rh = g_obj.rw, g_obj.rh raw, rah = rw * tw, rh * th rx, ry = gx * raw, gy * rah sw, sh = g_obj.w, g_obj.h buffer = max((2 * tw, 2 * th)) screen_rect = Rect(-buffer, -buffer, sw + buffer * 2, sh + buffer * 2) ox, oy = g_obj.cam.get() room_rect = Rect(rx + ox, ry + oy, raw, rah) # print(room_rect.topleft) if not (screen_rect.colliderect(room_rect)): continue in_this_room = dung.get_room().room_id == room.room_id x, y = ox, oy nx, ny = 0, 0 mw, mh = room.layout.w, room.layout.h tindx_x_l = [8, 9, 10, 11, 12, 13, 14, 15] tindx_y = 9 torches = [] for tx, ty, n in product(range(mw), range(mh), room.layout.layers.keys()): add_torch = False if n == "Torches": tile_index = room.layout.get_tile_index(n, tx, ty) if tile_index != None: # transparent tile tile = room.tilesets.get_tile(*tile_index) tilex, tiley = tile_index[1:] # print(tilex, tiley) if tindx_y == tiley and tilex in tindx_x_l: add_torch = True x, y = tx * tw + ox + rx, ty * th + oy + ry cr = Rect(x, y, tw, th) if not screen_rect.colliderect(cr): continue if not in_this_room: g_obj.swin.blit(g_obj.darktile, (x, y)) if add_torch: torches.append((x, y)) td = 6 wtr = td htr = td tcx, tcy = 2.5, 2.5 for t in torches: for xtr, ytr in product(range(wtr), range(htr)): if not in_range((tcx, tcy), (xtr, ytr), int(td / 2)): continue dist = get_distance((tcx, tcy), (xtr, ytr)) xt, yt = t[0] + (xtr - tcx) * tw, t[1] + (ytr - tcy) * th intensity = 0 for i in range(3): if i > dist: break intensity += 1 tseed = (t[0], t[1]) tintensity = g_obj.renderlogic.torches.get_torch_intensity( tseed) g_obj.lighttile.set_alpha( int((60 - intensity * 20) * tintensity)) g_obj.swin.blit(g_obj.lighttile, (xt, yt))
def action(self, c_ap, c_hp_self, m_hp_self, c_pos_enemy, c_hp_enemy, u_state, c_state, l_action, c_type): if u_state in ("dying", "dead") or u_state != "idle": return "wait", c_state, {} flee_per = 0.3 if c_hp_self / m_hp_self < flee_per: c_state = "fleeing" else: pass if c_ap < 1 and c_state != "fleeing": return "pass", c_state, {} if c_state == "fleeing": available_abis = get_available_abis( self.unit, c_ap, ["heal", "buff"]) # returns a list of cast-able abilities if len(available_abis) == 0: return "walk", c_state, {"walk_command": "away_from_enemy"} else: abi_choice = choice(available_abis) return "cast", c_state, {"cast_ability": abi_choice} pass # check if we have heal/buff available, then go into that state # otherwise stay in this state and keep running away. elif c_state == "idling": if in_range(self.unit.pos, c_pos_enemy, self.spot_range): c_state = "approaching" return "wait", c_state, {} else: return "pass", c_state, {} pass # check if enemy is within range, if so- go out of idle state. elif c_state == "approaching": cur_max_range_self = get_max_range_of(self.unit) if in_range(self.unit.pos, c_pos_enemy, cur_max_range_self): c_state = "attacking" return "wait", c_state, {} else: return "walk", c_state, {"walk_command": "toward_enemy"} pass # if you're too far away from enemy to attack - approach; walk toward # otherwise; go into attack state. elif c_state == "kiting": pass # if you're ranged and your range is greater than the enemies, then # move a little back, otherwise go into a state of attacking. elif c_state == "attacking": available_abis = get_available_abis(self.unit, c_ap, ["ranged", "melee"]) in_range_abis = get_in_range_abis(available_abis, self.unit, c_pos_enemy) in_range_attacks = get_in_range_atks(self.unit, c_pos_enemy) if len(in_range_abis) != 0 and False: # ignore abilites for now use_abi = choice(in_range_abis) return "cast", c_state, {"use_ability": use_abi} elif len(in_range_attacks) != 0: use_weapon = choice(in_range_attacks) return "smack", c_state, {"use_weapon": use_weapon} else: c_state = "approaching" return "wait", c_state, {} pass # here we can use a ranged or melee ability, or ranged or melee basic attack. return "wait", c_state, {}
def close_to_bed(self, g_obj, mpos): return in_range(self.pos, g_obj.mc.pos, 2)