f9b35e5af35916c3ccac28895aa6e6640531fd7e
[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(*imgparts, transform=transform).convert_alpha()
22
23         maxwidth = SCREEN_SIZE[0] / 2 - self._img_size
24         
25         wrap_width = int(2.25 * maxwidth / self.FONT.get_height())
26
27         self._text = [self.FONT.render(text, True, COLOURS["white"]) for text in wrap(label, wrap_width)]
28
29     def render(self, surface, height, x_offset=0):
30         surface.blit(self._img, (x_offset, height), None)
31         for t in self._text:
32             surface.blit(t, (x_offset + self._img_size + 5, height), None)
33             height += self.FONT.get_height()
34
35
36 class HelpScene(BaseScene):
37
38     def enter(self, gamestate):
39         self._space = pymunk.Space()
40         
41         font_title = loader.load_font(FONTS['bold'], size=32)
42         self._title = font_title.render("Help! What's going on?!", True, COLOURS["white"])
43         
44         self._tools = self.create_tools(gamestate)
45         self._items = self.create_items(gamestate)
46
47     def create_tools(self, gamestate):
48         tools = []
49         tools.append(ImageButton(
50             '32', 'exit.png', name='exit', pos=(SCREEN_SIZE[0] - 50, SCREEN_SIZE[1] - 40)))
51         return tools
52
53     def create_items(self, gamestate):
54         items = [
55             HelpItem(("48", "turnip3.png"), "This is a space turnip, the most valuable vegetable in the universe. A crucial ingredient of longevity serum, space navigator tonic and a pink sweet that everyone loves."),
56             HelpItem(("64", "mouldA.png"), "This is Boyd the space mould. He loves turnips. You've disinfected your ship a thousand times, but somehow he shows up on every planet at night to devour your crop. He is vulnerable to light, but beware -- he builds up a resistance if you overuse the same colour."),
57             HelpItem(("32", "seed.png"), "This is a turnip seed. You get a limited number of these at the start of a level. You can plant them, and you can use them to buy lights to protect your crop. Turnips which survive to harvest yield more seeds."),
58             HelpItem(("48", "lamp.png"), "This is a lamp. It comes in many colours, and has some other varying properties. It is powered by your farm's battery during the night. The battery recharges during the day.", transform=Multiply(colour=COLOURS["yellow"])),
59             HelpItem(("48", "spotlight.png"), "This is a spotlight. Unlike a lamp, it has a rotating beam. Lights can be multicoloured, like this one. You can toggle lights on and off at night to conserve power, and toggle the colour of multicoloured lights.", transform=ColourWedges(colours=("red", "green"))),
60             HelpItem(("32", "night.png"), "When you have finished planting seeds and placing lights, you can prepare for Boyd's night-time onslaught by clicking this button."),
61             HelpItem(("32", "pause.png"), "You can pause the game during the night if you need to make a more detailed analysis of why you're losing horribly."),
62             #HelpItem(("", ""), ""),
63             #HelpItem(("", ""), ""),
64         ]
65
66         # Special mould assembly
67         items[1]._img.blit(loader.load_image("32", "mouldB.png").convert_alpha(), (30, 30), None)
68         items[1]._img.blit(loader.load_image("32", "eyeballA.png").convert_alpha(), (10, 10), None)
69
70         return items
71
72     def render(self, surface, gamestate):
73         surface.fill(COLOURS["blue"])
74         
75         pos = ((surface.get_width() - self._title.get_width()) / 2, 5)
76         surface.blit(self._title, pos, None)
77
78         height = 50
79         x_offset = 0
80
81         for item in self._items:
82             item.render(surface, height, x_offset)
83             height += max(item._img_size, item.FONT.get_height() * len(item._text))
84             height += 5
85             if height > SCREEN_SIZE[1]:
86                 height = 50
87                 x_offset = SCREEN_SIZE[0] / 2
88
89     def event(self, ev, gamestate):
90         if ev.type == pgl.KEYDOWN:
91             if ev.key in (pgl.K_q, pgl.K_ESCAPE):
92                 from .menu import MenuScene
93                 SceneChangeEvent.post(scene=MenuScene())
94         elif ev.type == pgl.MOUSEBUTTONDOWN:
95             if ev.button == 1:
96                 # Check tools
97                 for tool in self._tools:
98                     if tool.pressed(ev):
99                         if tool.name == 'exit':
100                             from .menu import MenuScene
101                             SceneChangeEvent.post(scene=MenuScene())