-
Notifications
You must be signed in to change notification settings - Fork 0
/
Player.py
129 lines (104 loc) · 3.63 KB
/
Player.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
from Grimoire import Grimoire
from Card import Card
from Unit import *
from Const import *
import random
import logging
class Player:
STARTING_HEALTH = 100
INVALID_PLAYER = -1
STARTING_GOLD = 20
MAX_HAND_SIZE = 5
def __init__(self, id):
self.id = id
self.reset()
def reset(self):
self.deck = []
self.discard_pile = []
self.hand = []
self.inplay = []
self._health = Player.STARTING_HEALTH
self.set_direction(Const.FACING_RIGHT)
self.gold = Player.STARTING_GOLD
self.grimoire = Grimoire(self, 'play')
self.modifiers = []
def __str__(self):
lines = []
lines.append("Health: {0}".format(self.get_curr_health()))
lines.append("Gold: {0}".format(self.gold))
lines.append("Hand: {0}".format(", ".join([str(x) for x in self.hand])))
return "\n".join(lines)
def buy(self, card_name):
if (self.spend_gold(Card.get_card(card_name).buy_cost)):
self.grimoire.remove_from_grimoire(card_name)
self.discard_pile.append(Card.get_card(card_name))
def play(self, card_name, spend_gold=True):
# find the card in hand
card = self.find_card(card_name, self.hand)
if spend_gold:
if not self.spend_gold(card.cost):
logging.debug("Attempted to play {0} without sufficient funds".format(card_name))
return None
self.inplay.append(card)
self.hand.remove(card)
return card
def draw(self):
if len(self.deck) == 0:
if len(self.discard_pile) == 0:
return
self.deck = self.discard_pile
self.discard_pile = []
random.shuffle(self.deck)
if len(self.hand) < self.MAX_HAND_SIZE:
self.hand.append(self.deck.pop(0))
def discard(self, card):
self.hand.remove(card)
self.discard_pile.append(card)
def unit_died(self, unit):
if unit.card in self.inplay:
if isinstance(unit, Hero):
self.inplay.remove(unit.card)
self.deck.insert(0, unit.card)
else:
self.inplay.remove(unit.card)
self.discard_pile.append(unit.card)
def spell_remove(self, spell):
self.inplay.remove(spell.card)
self.discard_pile.append(spell.card)
def spend_gold(self, amount):
"""
Attempts to spend gold. Returns false if there is not enough gold to
spend.
"""
if amount > self.gold:
return False
self.gold -= amount
return True
def find_card(self, card_name, collec):
# returns the first card object that matches card name from collec
if card_name not in [c.name for c in collec]:
assert False, "{0} not in {1}".format(card_name, collec)
return [c for c in collec if c.name == card_name][0]
## Setters
def set_discard_pile(self, discard_pile):
self.discard_pile = discard_pile
def set_deck(self, deck):
self.deck = deck
def set_hand(self, hand):
self.hand = hand
def set_inplay(self, inplay):
self.inplay = inplay
def set_gold(self, amount):
self.gold = amount
def set_direction(self, direction):
self.direction = direction
def get_direction(self):
return self.direction
def get_curr_health(self):
return self._health
def take_damage(self, damage_amount):
self._health = self._health - damage_amount
logging.info("Player {0} at {1} health".format(
self.id,
self.get_curr_health()
))