Skip to content

Commit

Permalink
Merge pull request #10 from 20akshay00/main
Browse files Browse the repository at this point in the history
added item visibility and dynamic placement
  • Loading branch information
DhruvaSambrani authored Mar 20, 2022
2 parents fdc3bb5 + fe5b583 commit cdfb4b0
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 8 deletions.
1 change: 1 addition & 0 deletions tetra/items.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ def __init__(self, itempath):
jsonobj = json.loads(open(itempath, encoding="utf-8").read())
self.name = jsonobj["name"]
self.desc = jsonobj["desc"]
self.char = jsonobj.get("char", "?")
self.media_path = jsonobj.get("media_path", None)
self.need_input = jsonobj["need_input"]
self.answerhash = jsonobj.get("answerhash", None)
Expand Down
31 changes: 25 additions & 6 deletions tetra/maps.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,17 +24,24 @@ def clamp(p, r, c):
return [min(max(0, p[0]), r-1), min(max(0, p[1]), c-1)]

class Map:
def __init__(self, filepath, settings, pos = None):
def __init__(self, filepath, settings, game, pos = None):
with open(filepath, encoding = 'utf-8') as fh:
jsonobj = json.load(fh, strict=False)
self.name = jsonobj["name"]
self.fmt = np.stack([list(elt) for elt in jsonobj["raw"].splitlines()]) #formatted map as np grid
self.r, self.c = self.fmt.shape #map size
self.fmt_ref = np.stack([list(elt) for elt in jsonobj["raw"].splitlines()]) #formatted map as np grid
self.r, self.c = self.fmt_ref.shape #map size
self.pos = np.array(jsonobj["init_pos"]) if (pos is None) else pos #player pos
self.items = {tuple(jsonobj["items"][it_name]): it_name for it_name in jsonobj["items"].keys()}
self.items_ref = {tuple(jsonobj["items"][it_name]): it_name for it_name in jsonobj["items"].keys()}
self.exits = jsonobj["exits"] #dict of exit coords and new map
self.exit_coords = [[int(x) for x in elt.split(",")] for elt in jsonobj["exits"].keys()] #list of exit points

self.fmt = np.stack([list(elt) for elt in jsonobj["raw"].splitlines()])
self.items = {}

items_on_startup = jsonobj.get("items_on_startup", self.items_ref.values())
for item_name in items_on_startup:
self.place_item(item_name, game)

def __eq__(self, t):
return t == self.name

Expand All @@ -52,14 +59,26 @@ def render(self, settings):
block[r_rel : (r_rel + mini_r), c_rel : (c_rel + mini_c)] = mini
block[vr//2, vc//2] = settings.player
return "\n".join(["".join(elt) for elt in block.tolist()])


def place_item(self, item_name, game):
if not (item_name in self.items.values()):
coord = list(self.items_ref.keys())[list(self.items_ref.values()).index(item_name)]
self.fmt[coord] = game.item(item_name).char
self.items[tuple(coord)] = self.items_ref[tuple(coord)]

def remove_item(self, item_name, game):
if(item_name in self.items.values()):
coord = list(self.items_ref.keys())[list(self.items_ref.values()).index(item_name)]
self.fmt[coord] = self.fmt_ref[coord]
self.items.pop(tuple(coord))

def activate_item_here(self, game):
nowpos = [self.pos[0], self.pos[1]]
i = self.items.get(tuple(nowpos), None)
if not (i is None):
to_remove = game.item(i).render(game)
if to_remove:
self.items.pop(tuple(nowpos))
self.remove_item(i, game)

def iswalkable(self, new_pos, game):
return game.surface(self.fmt[new_pos[0]][new_pos[1]]).walkable
Expand Down
3 changes: 1 addition & 2 deletions tetra/terminal.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,11 @@ def __init__(self, title, settingsfile, gadgets_list, theme="Dark"):
Surface(self.settings.surfacefile(i))
for i in os.listdir(self.settings.surfacespath)
]
self.maps = [Map(self.settings.mapfile(i), self.settings) for i in os.listdir(self.settings.mapspath)]
self.items = [
Item(self.settings.itemfile(i))
for i in os.listdir(self.settings.itemspath) if ".item" in i
]

self.maps = [Map(self.settings.mapfile(i), self.settings, self) for i in os.listdir(self.settings.mapspath)]
self.active_map = self.map("Home")
self.pocket = Pocket([])
self.gadgets = [G(self) for G in gadgets_list]
Expand Down

0 comments on commit cdfb4b0

Please sign in to comment.