c58fea548eb8cc1dbcf77f6d17770ca6c12c140d
[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             HelpItem(("64", "mouldA.png"), (
65                 "This is Boyd the space mould. He loves turnips. You've"
66                 " disinfected your ship a thousand times, but somehow he shows"
67                 " up on every planet at night to devour your crop. He is"
68                 " vulnerable to light, but beware -- he builds up a resistance"
69                 " if you overuse the same colour.")),
70             HelpItem(("32", "seed.png"), (
71                 "This is a turnip seed. You get a limited number of these at"
72                 " the start of a level. You can plant them, and you can use"
73                 " them to buy lights to protect your crop. Turnips which"
74                 " survive to harvest yield more seeds.")),
75             HelpItem(("48", "lamp.png"), (
76                 "This is a lamp. It comes in many colours, and has some other"
77                 " varying properties. It is powered by your farm's battery"
78                 " during the night. The battery recharges during the day."),
79                 transform=Multiply(colour=COLOURS["yellow"])),
80             HelpItem(("48", "spotlight.png"), (
81                 "This is a spotlight. Unlike a lamp, it has a rotating beam."
82                 " Lights can be multicoloured, like this one. You can toggle"
83                 " lights on and off at night to conserve power, and toggle the"
84                 " colour of multicoloured lights."),
85                 transform=ColourWedges(colours=("red", "green"))),
86             HelpItem(("32", "night.png"), (
87                 "When you have finished planting seeds and placing lights, you"
88                 " can prepare for Boyd's night-time onslaught by clicking this"
89                 " button.")),
90             HelpItem(("32", "pause.png"), (
91                 "You can pause the game during the night if you need to make a"
92                 " more detailed analysis of why you're losing horribly.")),
93             HelpItem(("32", "day.png"), (
94                 "If you have run out of turnips, you can click this button to"
95                 " skip to the next day. Or you can keep playing with your lights."
96                 " If you have no power left either, or you are completely"
97                 " bankrupt, the night will end automatically.")),
98         ]
99
100         # Special mould assembly
101         items[1]._img.blit(
102             loader.load_image("32", "mouldB.png").convert_alpha(),
103             (30, 30), None)
104         items[1]._img.blit(
105             loader.load_image("32", "eyeballA.png").convert_alpha(),
106             (10, 10), None)
107         items[1]._img.blit(
108             loader.load_image("32", "eyelid.png").convert_alpha(),
109             (10, 10), None)
110
111         return items
112
113     def render(self, surface, gamestate):
114         surface.fill(COLOURS["blue"])
115
116         pos = ((surface.get_width() - self._title.get_width()) / 2, 5)
117         surface.blit(self._title, pos, None)
118
119         height = 50
120         x_offset = 0
121
122         for item in self._items:
123             item.render(surface, height, x_offset)
124             height += max(
125                 item._img_size, item.FONT.get_height() * len(item._text))
126             height += 5
127             if height > SCREEN_SIZE[1]:
128                 height = 50
129                 x_offset = SCREEN_SIZE[0] / 2
130
131     def event(self, ev, gamestate):
132         if ev.type == pgl.KEYDOWN:
133             if ev.key in (pgl.K_q, pgl.K_ESCAPE):
134                 from .menu import MenuScene
135                 SceneChangeEvent.post(scene=MenuScene())
136         elif ev.type == pgl.MOUSEBUTTONDOWN:
137             if ev.button == 1:
138                 # Check tools
139                 for tool in self._tools:
140                     if tool.pressed(ev):
141                         if tool.name == 'exit':
142                             from .menu import MenuScene
143                             SceneChangeEvent.post(scene=MenuScene())