costs
[tabakrolletjie.git] / tabakrolletjie / scenes / help.py
1 """ What's going on?! """
2
3 from textwrap import wrap
4
5 import pymunk
6 import pygame.locals as pgl
7
8 from .base import BaseScene
9 from ..widgets import ImageButton
10 from ..constants import SCREEN_SIZE, FONTS, COLOURS
11 from ..loader import loader
12 from ..events import SceneChangeEvent
13 from ..transforms import Multiply, NullTransform, ColourWedges
14
15
16 class HelpItem(object):
17     FONT = loader.load_font(FONTS['sans'], size=14)
18
19     def __init__(self, imgparts, label, transform=NullTransform()):
20         self._img_size = int(imgparts[0])
21         self._img = loader.load_image(
22             *imgparts, transform=transform).convert_alpha()
23
24         maxwidth = SCREEN_SIZE[0] / 2 - self._img_size
25
26         wrap_width = int(2.25 * maxwidth / self.FONT.get_height())
27
28         self._text = [
29             self.FONT.render(text, True, COLOURS["white"])
30             for text in wrap(label, wrap_width)]
31
32     def render(self, surface, height, x_offset=0):
33         surface.blit(self._img, (x_offset, height), None)
34         for t in self._text:
35             surface.blit(t, (x_offset + self._img_size + 5, height), None)
36             height += self.FONT.get_height()
37
38
39 class HelpScene(BaseScene):
40
41     def enter(self, gamestate):
42         self._space = pymunk.Space()
43
44         font_title = loader.load_font(FONTS['bold'], size=32)
45         self._title = font_title.render(
46             "Help! What's going on?!", True, COLOURS["white"])
47
48         self._tools = self.create_tools(gamestate)
49         self._items = self.create_items(gamestate)
50
51     def create_tools(self, gamestate):
52         tools = []
53         tools.append(ImageButton(
54             '32', 'exit.png', name='exit',
55             pos=(SCREEN_SIZE[0] - 50, SCREEN_SIZE[1] - 40)))
56         return tools
57
58     def create_items(self, gamestate):
59         items = [
60             HelpItem(("48", "turnip3.png"), (
61                 "This is a space turnip, the most valuable vegetable in the"
62                 " universe. A crucial ingredient of longevity serum, space"
63                 " navigator tonic and a pink sweet that everyone loves.")),
64         ]
65
66         mould = HelpItem(("64", "mouldA.png"), (
67             "This is Boyd the space mould. He loves turnips. You've"
68             " disinfected your ship a thousand times, but somehow he shows"
69             " up on every planet at night to devour your crop. He is"
70             " vulnerable to light, but beware -- he builds up a resistance"
71             " if you overuse the same colour."))
72
73         mould._img.blit(
74             loader.load_image("32", "mouldB.png").convert_alpha(),
75             (30, 30), None)
76         mould._img.blit(
77             loader.load_image("32", "eyeballA.png").convert_alpha(),
78             (10, 10), None)
79         mould._img.blit(
80             loader.load_image("32", "eyelid.png").convert_alpha(),
81             (10, 10), None)
82
83         items.append(mould)
84
85         items.extend([
86             HelpItem(("32", "seed.png"), (
87                 "This is a turnip seed. You get a limited number of these at"
88                 " the start of a level. You can plant them, and you can use"
89                 " them to buy lights to protect your crop. Turnips which"
90                 " survive to harvest yield more seeds. Click on the icon that"
91                 " looks like this to start planting seeds. It takes three nights"
92                 " for a turnip to mature.")),
93             HelpItem(("48", "lamp.png"), (
94                 "This is a lamp. It comes in many colours, and has some other"
95                 " varying properties. It is powered by your farm's battery"
96                 " during the night. The battery recharges during the day."),
97                 transform=Multiply(colour=COLOURS["yellow"])),
98             HelpItem(("48", "pulsatinglamp.png"), (
99                 "This is a pulsating lamp. Its light radius increases and"
100                 " decreases."),
101                 transform=Multiply(colour=COLOURS["cyan"])),
102             HelpItem(("48", "spotlight.png"), (
103                 "This is a spotlight. Unlike a lamp, it has a beam which is"
104                 " pointed in a particular direction. To change the direction of"
105                 " a beam, right-click near the spotlight and drag. You can only"
106                 " do this during the day. Some spotlights rotate."),
107                 transform=Multiply(colour=COLOURS["red"])),
108             HelpItem(("48", "lamp.png"), (
109                 " Lights can be multicoloured, like this one. You can toggle"
110                 " lights on and off at night to conserve power, and toggle the"
111                 " colour of multicoloured lights."),
112                 transform=ColourWedges(colours=("red", "green"))),
113         ])   
114
115         soil = HelpItem(("48", "square.png"), (
116             "This is soil. You can plant seeds in it and position lights on"
117             " it. Lights and plants can't overlap with each other."))
118         soil._img.blit(
119             loader.load_image("textures", "soil.png").convert_alpha(),
120             (0, 0), None, pgl.BLEND_RGBA_MULT)
121         items.append(soil)   
122
123         stone = HelpItem(("48", "square.png"), (
124             "This is a stone wall. It gets in your way. On the other hand, it"
125             " also gets in Boyd's way."))
126         stone._img.blit(
127             loader.load_image("textures", "stone.png").convert_alpha(),
128             (0, 0), None, pgl.BLEND_RGBA_MULT)
129         items.append(stone)  
130
131         shrub = HelpItem(("48", "circle.png"), (
132             "This is a shrub. It works exactly like a wall, except that it's"
133             " round."))
134         shrub._img.blit(
135             loader.load_image("textures", "shrub.png").convert_alpha(),
136             (0, 0), None, pgl.BLEND_RGBA_MULT)
137         items.append(shrub)
138
139         items.extend([
140             HelpItem(("32", "spotlight.png"), (
141                 "Click on a light icon like this to get a menu of colour options"
142                 " available on your level.")),
143             HelpItem(("32", "spotlight.png"), (
144                 "Once you have selected a colour option, you will be able to"
145                 " place the light, as long as this does not reduce your seed"
146                 " count to zero. The cost will be deducted from your seed"
147                 " total."),
148                 transform=Multiply(colour=COLOURS["magenta"])),
149             HelpItem(("32", "remove.png"), (
150                 "Buyer's remorse? Use this icon to blow up unwanted lights. You"
151                 " don't get your money back. Sorry.")),
152             HelpItem(("32", "default_cursor.png"), (
153                 "You can use this to change your cursor back to an arrow.")),
154             HelpItem(("32", "night.png"), (
155                 "When you have finished planting seeds and placing lights, you"
156                 " can prepare for Boyd's night-time onslaught by clicking this"
157                 " button.")),
158             HelpItem(("32", "pause.png"), (
159                 "You can pause the game during the night if you need to make a"
160                 " more detailed analysis of why you're losing horribly.")),
161             HelpItem(("32", "day.png"), (
162                 "If you have run out of turnips, you can click this button to"
163                 " skip to the next day. Or you can keep playing with your lights."
164                 " If you have no power left either, or you are completely"
165                 " bankrupt, the night will end automatically.")),
166             HelpItem(("32", "exit.png"), (
167                 "Click this icon to quit the level in disgust and return to the"
168                 " menu.")),
169         ])
170
171         return items
172
173     def render(self, surface, gamestate):
174         surface.fill(COLOURS["blue"])
175
176         pos = ((surface.get_width() - self._title.get_width()) / 2, 5)
177         surface.blit(self._title, pos, None)
178
179         height = 50
180         x_offset = 0
181
182         for item in self._items:
183             item.render(surface, height, x_offset)
184             height += max(
185                 item._img_size, item.FONT.get_height() * len(item._text))
186             height += 5
187             if height > SCREEN_SIZE[1] - 50:
188                 height = 50
189                 x_offset = SCREEN_SIZE[0] / 2
190
191         for tool in self._tools:
192             tool.render(surface)
193
194     def event(self, ev, gamestate):
195         if ev.type == pgl.KEYDOWN:
196             if ev.key in (pgl.K_q, pgl.K_ESCAPE):
197                 from .menu import MenuScene
198                 SceneChangeEvent.post(scene=MenuScene())
199         elif ev.type == pgl.MOUSEBUTTONDOWN:
200             if ev.button == 1:
201                 # Check tools
202                 for tool in self._tools:
203                     if tool.pressed(ev):
204                         if tool.name == 'exit':
205                             from .menu import MenuScene
206                             SceneChangeEvent.post(scene=MenuScene())