Mercurial > sypikslang
comparison gamelib/lab.py @ 6:826b44731323
Start of basic lab implementation.
author | Jeremy Thurgood <firxen@gmail.com> |
---|---|
date | Sun, 06 May 2012 12:48:12 +0200 |
parents | |
children | b132a56ae4d4 |
comparison
equal
deleted
inserted
replaced
5:dd9046d0680c | 6:826b44731323 |
---|---|
1 from gamelib import research, products | |
2 | |
3 | |
4 def is_subclass(item, superclass): | |
5 return ( | |
6 isinstance(item, type) | |
7 and issubclass(item, superclass) | |
8 and not item is superclass) | |
9 | |
10 | |
11 def list_products(): | |
12 for item in dir(products): | |
13 # Ugh! | |
14 item = getattr(products, item) | |
15 if is_subclass(item, products.Product): | |
16 yield item | |
17 | |
18 | |
19 def list_research(): | |
20 for item in dir(research): | |
21 # Ugh! | |
22 item = getattr(research, item) | |
23 if is_subclass(item, research.ResearchArea): | |
24 yield item | |
25 | |
26 | |
27 class Lab(object): | |
28 def __init__(self): | |
29 self.science = [] | |
30 | |
31 def spend_points(self, things, basic_research): | |
32 for thing in things: | |
33 assert thing in self.science | |
34 thing.spend_points(1) | |
35 if isinstance(thing, research.ResearchArea): | |
36 self.find_new_product(thing) | |
37 self.try_basic_research(basic_research) | |
38 | |
39 def _get_science(self, science_class): | |
40 for science in self.science: | |
41 if isinstance(science, science_class): | |
42 return science | |
43 return None | |
44 | |
45 def _meet_requirements(self, science_class): | |
46 for science, level in science_class.PREREQUISITES: | |
47 my_science = self._get_science(science) | |
48 if my_science is None: | |
49 return False | |
50 if my_science.points < level: | |
51 return False | |
52 return True | |
53 | |
54 def find_new_products(self, research_area): | |
55 available_products = [] | |
56 for product_class in list_products(): | |
57 if self._get_science(product_class): | |
58 continue | |
59 if self._meet_requirements(product_class): | |
60 available_products.append(product_class) | |
61 return available_products | |
62 | |
63 def find_new_research(self, basic_research): | |
64 available_research = [] | |
65 for research_class in list_research(): | |
66 if self._get_science(research_class): | |
67 continue | |
68 if self._meet_requirements(research_class): | |
69 available_research.append(research_class) | |
70 return available_research |