Skip to content

Commit

Permalink
fixes and item class
Browse files Browse the repository at this point in the history
  • Loading branch information
dogeek committed Aug 21, 2019
1 parent 7f51d03 commit fd09415
Show file tree
Hide file tree
Showing 6 changed files with 50 additions and 22 deletions.
8 changes: 4 additions & 4 deletions rpglib/combat_system.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,17 +48,17 @@ def start_combat(self, opponent):

self.current_opponent = opponent

while not self.is_combat_finished(opponent):
while not self.is_combat_finished():
self.n_turns += 1
command = sanitized_input("> ", error_msg="Invalid Command!")
print(self.combat_state(opponent))
print(self.combat_state())
while not self.game.command_system.parse_combat(command):
print("Invalid command. Type 'help' for help.")
command = sanitized_input("> ", error_msg="Invalid Command!")
opponent.apply_status_effects()
self.game.player.apply_status_effects()

self.finish_combat(opponent)
self.finish_combat()

def is_combat_finished(self):
return self.current_opponent.is_dead or self.game.player.is_dead or self.fleeing
Expand Down Expand Up @@ -88,8 +88,8 @@ def get_hit(cls, attacker, defender):

@classmethod
def attack(cls, attacker, defender):
attack, damage, status_effects = attacker.damage
if CombatSystem.get_hit(attacker, defender):
attack, damage, status_effects = attacker.damage
defender.take_damage(damage)
if status_effects is None:
print(f"{attacker.name} attacked {defender.name} with {attack} and hit for {damage}!")
Expand Down
1 change: 1 addition & 0 deletions rpglib/game.py
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ def new_game(self):
self.player.stats.recall_stats(saved_stats)
elif p_cmd == 'info':
pass
self.player.health = self.player.max_health

def load_game(self):
clear_screen()
Expand Down
3 changes: 2 additions & 1 deletion rpglib/inventory_system.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
from .item import Item
from collections import defaultdict


class MoneyInventory:
def __init__(self):
self.coins = {
Expand Down Expand Up @@ -209,7 +210,7 @@ def _d(d):
class Inventory:
def __init__(self):
self.items = defaultdict(int)
self.equipped = EquipmentInventory()
self.equipped = EquipmentInventory(self)
self.money = MoneyInventory()

def get_item(self, item):
Expand Down
27 changes: 24 additions & 3 deletions rpglib/item.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,32 @@
import json
from .utils import parse_dice_format


class Item:
def __init__(self, name):
self.name = name
with open("data/items.json") as f:
self.set_attributes(json.load(f))
data = json.load(f).get("name", {})

def set_attributes(self, obj):
pass
self.equippable = data.get("equippable", False)
self.weapon_type = data.get("weapon_type", "melee")
self.effects_on_hit = data.get("effects_on_hit", [])
self.damage_die = data.get("damage", "1d6")
self.damage_modifier = data.get("damage_modifier", 0)
self.hit_modifier = data.get("hit_modifier", 0)
self.slot = data.get("slot", "")
self.price = data.get("price", 0)
self._display_name = data.get("display_name", None)
self.effects_on_equip = data.get("effects_on_equip", [])
self.description = data.get("description", "")

@property
def display_name(self):
if self._display_name is None:
return self.name.replace("_", " ").capitalize()
else:
return self.display_name

@property
def damage(self):
return parse_dice_format(self.damage_die) + self.damage_modifier
31 changes: 18 additions & 13 deletions rpglib/player.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@

class Player(Entity):
def __init__(self, game):
self.health_rolls = []
self.mana_rolls = []
super().__init__()
self.game = game
self.name = None
Expand All @@ -18,9 +20,7 @@ def __init__(self, game):
self.inventory = Inventory()
self.damage = ("hands", 0, None)
self.level = 1
self.health_rolls = []
self.mana_rolls = []
self.quest_log = QuestLog()
self.quest_log = QuestLog(self)

@property
def health(self):
Expand All @@ -34,6 +34,10 @@ def health(self, amount):
def max_health(self):
return sum(self.health_rolls)

@max_health.setter
def max_health(self, value):
pass

@property
def mana(self):
return self._mana
Expand Down Expand Up @@ -83,14 +87,15 @@ def xp_bonus(self):
def __str__(self):
return \
f"""Player {self.name} ; {self.health}/{self.max_health} HP ; {self.mana}/{self.max_mana} MP
Location : {str(self.location)} ; Level {self.level} {str(self.job).capitalize()}"""
Location : {str(self.location)} ; Level {self.level} {str(self.job).capitalize()}
STATS : {str(self.stats)}"""

def melee_attack(self):
default = ["hands", parse_dice_format("1d4"), []]
weapon = self.inventory.equipped.r_hand
if weapon is not None:
default[0] = self.inventory.equipped.r_hand.name
default[1] = parse_dice_format(weapon.damage) + self.stats.str.modifier
default[0] = self.inventory.equipped.r_hand.display_name
default[1] = weapon.damage + self.stats.str.modifier
if weapon.effects_on_hit:
default[2].extend(weapon.effects_on_hit)
self.damage = default
Expand All @@ -100,20 +105,20 @@ def ranged_attack(self):
weapon = self.inventory.equipped.r_hand
ammunition = self.inventory.equipped.l_hand
if weapon is not None and self.inventory.has_item(weapon.ammunition_type):
default[0] = weapon.name
default[1] = parse_dice_format(weapon.damage) + self.stats.dex.modifier + ammunition.damage_modifier
default[0] = weapon.display_name
default[1] = weapon.damage + self.stats.dex.modifier + ammunition.damage_modifier
if weapon.effects_on_hit:
default[2].extend(weapon.effects_on_hit)
if ammunition.effects_on_hit:
default[2].extend(ammunition.effects_on_hit)
elif weapon is not None:
print(f"{self.name} tried to attack with {weapon.name} but did not have enough ammo!")
print(f"{self.name} tried to attack with {weapon.display_name} but did not have enough ammo!")
self.damage = ("", 0, [])
return
elif weapon is None:
print(f"{self.name} has no ranged weapon equipped")
self.damage = ("", 0, [])
return
if weapon.effects_on_hit:
default[2].extend(weapon.effects_on_hit)
if ammunition.effects_on_hit:
default[2].extend(ammunition.effects_on_hit)
self.damage = default

def attack(self):
Expand Down
2 changes: 1 addition & 1 deletion rpglib/stats.py
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ def __getitem__(self, item):
raise ValueError

def __str__(self):
return " ; ".join([f"{stat_name.upper()}: {stat_value}" for stat_name, stat_value in self.as_dict])
return " ; ".join([f"{stat_name.upper()}: {stat_value}" for stat_name, stat_value in self.as_dict.items()])

@property
def as_dict(self):
Expand Down

0 comments on commit fd09415

Please sign in to comment.