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
15 changes: 15 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,5 +37,20 @@ 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)

# Potion

1) Know the attributes of your potion

2) Go to **builtin.py** -> **Potion** and find **Potion** class. This is where you initialize the potion. **NOTE:** You don't assume the player is going to use an object. This is merely initialization. Tip: Initialize the logic of your object here.

3) Do not forget to go to **crpg.py** -> **generate()** iff you are:
- creating a new potion type.
- go to the first for loop and look for the first if statement.
- remember to include your new object type in n_types
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I still think this documentation is a bit out of place here.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Deleted.


Potion encoding:
> nodeID | potion | po(-type of potion -operation) | Description
> Example: 3 | potion | po-health-collect-[options] | Pick up potion

Health:
nodeID | potion | po(-health-collect-[amount]) | Description
19 changes: 16 additions & 3 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,24 @@ 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-collect" in title:
self.amount_health = int(title[19:21])
self.potionType = "Health potion"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could we have classes that inherit the potion class for Health, Speed, etc, instead of this?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Got it. Check out the recent push. I made it as generic as possible, but notice that it is impossible to encode "this is a health potion worth 25 hp" without actually calling it out and subsequently using conditionals to decipher it. However, the implementation of Potion is certainly cleaner now.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you add is_health_potion as an argument? Another approach would be to have an action method with different implementations for each class

def on_select(self):
self.player.bag.append(self)
for i in self.player.bag:
print(i.potionType)

# 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 Down
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[:2] == "po":
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You should be able to specify n_type == "potion" here. n_type stands for node type, and is the second item in the .dl file

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.

args.append(game.player)

obj = n_types[n_type](*args)
Expand Down
24 changes: 6 additions & 18 deletions game-2-layout.dl
Original file line number Diff line number Diff line change
@@ -1,22 +1,10 @@
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 | po-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.
---
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