diff --git a/.gitignore b/.gitignore index d06749a..4fa7dcf 100644 --- a/.gitignore +++ b/.gitignore @@ -1,20 +1,40 @@ # Python -**/__pycache__/ -**/*.pyc -**/venv/ -**/ENV/ -**/env/ -**/build/ + +/__pycache__ +/*.pyc +/venv/ +/ENV/ +/env/ +/build/ # JetBrains -**/null/ -**/out/ -**/target/ +/null/ +/out/ +/target/ # Windows -**/Thumbs.db +/Thumbs.db # Hidden Files and Exceptions **/.* !.github -!.gitignore \ No newline at end of file +!.gitignore +======= +/__pycache__ +*.pyc +venv/ +ENV/ +env/ +build/ + +# JetBrains +.idea/ +null/ +out/ +target/ +.bsp/ + +#dev +LICENSE +.github +game-2-layout.dl diff --git a/README.md b/README.md index 5ac3e0e..80b7f66 100644 --- a/README.md +++ b/README.md @@ -37,5 +37,3 @@ If we want to expand the API for connections, we can always add more symbols for parsing. One I was thinking about is `\->`, which would denote an "ending" connection (to signal destroying the path to reach the current action) - - diff --git a/builtin.py b/builtin.py index ab35f8e..bf72dbd 100644 --- a/builtin.py +++ b/builtin.py @@ -5,10 +5,11 @@ class Player: def __init__(self): self.hp = 100 self.xp = 0 + self.bag = [] + self.money = 0 def summary(self): - return f'hp: {self.hp} xp: {self.xp}' - + return f'hp: {self.hp} xp: {self.xp} money: {self.money}' # Base connection classes @@ -58,12 +59,41 @@ def advance(self, index: int) -> Node: def __str__(self): return self.title +#To initialize another potion type, create another if statement in init. Define what happens on select in on_select. +class Potion(Node): + def __init__(self, title:str, desc:str, player:Player): + super().__init__(title, desc) + self.desc = desc + self.player = player + if "health" in title: + Health.init_health(self,title) + if "speed" in title: + Speed.init_speed(self,title) + + def on_select(self): + self.player.bag.append(self) + showBag(self) + +class Health(): + def init_health(self,title): + self.amount_health = extract_number(title) + if "potion" in title: + self.objectType = "Health Potion" + self.category = "Health" + self.title = self.objectType + " " + str(self.amount_health)+ " awarded" + +class Speed(): + def init_speed(self,title): + self.amount_speed = extract_number(title) + if "potion" in title: + self.objectType = "Speed Potion" + self.category = "Speed" + self.title = self.objectType + " " + str(self.amount_speed )+ " awarded" # Location behaves exactly like a Node class Location(Node): pass - # Actions include the player for reference (augment player attributes) class Action(Node): def __init__(self, title: str, desc: str, player: Player): @@ -86,4 +116,32 @@ class Run(Action): def on_select(self): self.player.xp = max(self.player.xp - 10, 0) - super().on_select() \ No newline at end of file + super().on_select() + +def showBag(self): + objTypeToCount = {} + for obj in self.player.bag: + if obj.objectType not in objTypeToCount: + objTypeToCount[obj.objectType]=1 + else: + objTypeToCount[obj.objectType]+=1 + print() + print("Inventory") + print(objTypeToCount) + print() + +def extract_number(title): + c = 0 + h,t = 0,0 + ret = 0 + while c < len(title): + if title[c] == "[": + h = c + while c < len(title): + if title[c] == "]": + t = c + ret = int(title[h+1:t]) + break + c += 1 + c += 1 + return ret \ No newline at end of file diff --git a/crpg.py b/crpg.py index 2fa46fe..85c6730 100644 --- a/crpg.py +++ b/crpg.py @@ -1,7 +1,7 @@ from typing import Callable # for connection function type hints from setup import start import re -from builtin import Connection, BreakingConnection, Node, Location, Fight, Run, Player +from builtin import Connection, Potion, BreakingConnection, Node, Location, Fight, Run, Player # the base crpg game @@ -84,7 +84,8 @@ def generate(filename): "node": Node, "location": Location, "fight": Fight, - "run": Run + "run": Run, + "potion" : Potion, } c_funcs: dict[str, Callable[[Game, Node, Node], None]] = { @@ -99,7 +100,7 @@ def generate(filename): for node in nodes.split("\n"): n, n_type, *args = re.split(r"\s*\|\s*", node) - if n_type == "fight" or n_type == "run": + if n_type == "fight" or n_type == "run" or n_type == "potion": args.append(game.player) obj = n_types[n_type](*args) diff --git a/game-2-layout.dl b/game-2-layout.dl index 4aee0e5..2fb0d74 100644 --- a/game-2-layout.dl +++ b/game-2-layout.dl @@ -1,22 +1,12 @@ 1 | starting | castle | You wake up in the prison cell of an abandoned castle. 2 | node | examine skeleton | You examine the skeleton of a corpse. -3 | node | open cell gate | The cell gate is unlocked. You push it open. +3 | potion | potion-health-collect-[25] | Pick up potion 4 | node | left corridor | The corridor leads to an open door to the castle's courtyard. -9 | location | courtyard | You enter the castle's courtyard. -5 | node | right corridor | The corridor leads to a large flight of stairs to the dungeon. -10 | location | dungeon | You enter the castle's dungeon. -6 | node | scale stairs | A dragon swoops down from the sky, blocking your path. -7 | fight | fight dragon | You vanquish the dragon. -8 | run | run from dragon | You run from the dragon. +5 | potion | potion-speed-collect-[25] | Pick up speed --- -1 <-> 2 -1 -> 3 -3 -> 4 -3 -> 5 -4 -> 9 -5 -> 10 -9 -> 6 -6 -> 7 -6 -> 8 -7 -> 4 -8 -> 4 +1 <-> 3 +1 -> 2 +2 -> 4 +4 <-> 5 + +