Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Potion class, and how it is encoded #86

Open
wants to merge 13 commits into
base: v2.0
Choose a base branch
from
42 changes: 31 additions & 11 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -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
!.gitignore
=======
/__pycache__
*.pyc
venv/
ENV/
env/
build/

# JetBrains
.idea/
null/
out/
target/
.bsp/

#dev
LICENSE
.github
game-2-layout.dl
2 changes: 0 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)



66 changes: 62 additions & 4 deletions builtin.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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):
Expand All @@ -86,4 +116,32 @@ class Run(Action):
def on_select(self):
self.player.xp = max(self.player.xp - 10, 0)

super().on_select()
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
7 changes: 4 additions & 3 deletions crpg.py
Original file line number Diff line number Diff line change
@@ -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

Expand Down Expand Up @@ -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]] = {
Expand All @@ -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)
Expand Down
26 changes: 8 additions & 18 deletions game-2-layout.dl
Original file line number Diff line number Diff line change
@@ -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