Web   ·   Wiki   ·   Activities   ·   Blog   ·   Lists   ·   Chat   ·   Meeting   ·   Bugs   ·   Git   ·   Translate   ·   Archive   ·   People   ·   Donate
summaryrefslogtreecommitdiffstats
path: root/Saludame.activity/kid_window.py
blob: 5ba68e5f366dc791dc2c585f9c84dcd1eb92411f (plain)
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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
# -*- coding: utf-8 -*-

# Copyright (C) 2011 ceibalJAM! - ceibaljam.org
# This file is part of Saludame.
#
# Saludame is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# Saludame is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with Saludame. If not, see <http://www.gnu.org/licenses/>.

import os
import pygame

import gui
import menu_creator
import animation
import utilities
from character import *
from gettext import gettext as _

BACKGROUND_PATH = os.path.normpath("assets/background/schoolyard_normal.png")

class KidWindow(gui.Window):

    def __init__(self, container, rect, frame_rate, windows_controller, cha_loader, game_man):
        
        gui.Window.__init__(self, container, rect, frame_rate, windows_controller, "kid")
        
        self.game_man = game_man
        self.cha_loader = cha_loader

        environment = self.game_man.environment
        time = self.game_man.current_time
        self.set_environment(environment, time)
        
        mood = self.game_man.character.mood
        
        kid_rect = pygame.Rect((280, 70), (400, 500))        
        self.kid = animation.Kid(rect, kid_rect, 1, windows_controller, game_man, mood)
        self.add_child(self.kid)
        
        self.balloon = None
        
        ### Events ###
        
        # Socials
        self.social_event = None
        self.external_character = None
        
        # Menu
        self.menu = menu_creator.load_menu(game_man, (480, 250), self.rect, windows_controller)
        self.add_window(self.menu)
        
        self.last_repaint = False
        
        
    ##### Environment #####
    def set_environment(self, environment, time):
      
        sky = pygame.image.load("assets/background/sky/" + time + ".png").convert_alpha()
        
        image = pygame.image.load(environment.background_path).convert_alpha()
        if time == "night":
            #image = image.convert(24)
            _filter = pygame.Surface(image.get_size())
            _filter.fill((30, 30, 100))
            _filter.set_alpha(50)
            image.blit(_filter, (0,0))
        sky.blit(image, (0,0))
        self.set_bg_image(sky.convert())
        
        self.set_dirty_background()
        
    ##### Clothes #####
    def update_clothes(self):
        self.kid.update_clothes()
        
    ##### Moods #####
    def change_mood(self):
        self.kid.change_mood()
        
    def set_mood(self, mood):
        self.kid.set_mood(mood)
        
    ##### Actions #####
    def play_action_animation(self, action):
        self.kid.play_action_animation(action)
    
    def stop_action_animation(self):
        self.kid.stop_action_animation()
        
    ##### Events #####
    def add_social_event(self, event):
        self.remove_social_event()
        self.social_event = event
        self.external_character = ExternalCharacter(self.rect, (30, 609), 1, self.windows_controller, event, self.game_man.character)
        self.add_window(self.external_character)
                
    def remove_social_event(self):
        self.social_event = None
        if self.external_character:
            self.remove_window(self.external_character)
        self.external_character = None
    
    ##### Kid ballon #####
    def show_kid_balloon(self, message, time_span):
        self.remove_kid_balloon()
        self.balloon = MessageBalloon(self.rect, pygame.Rect(580, 80, 1, 1), 1, self.windows_controller, 'A')
        self.balloon.set_text(message)
        self.balloon.set_time_span(time_span)
        self.add_window(self.balloon)
        
    def remove_kid_balloon(self):
        if self.balloon:
            self.remove_window(self.balloon)
        self.balloon = None
    
    def update(self, frames):
        
        gui.Window.update(self, frames)
        
        if self.balloon:
            if not self.balloon.visible:
                self.remove_kid_balloon()
                
        if self.external_character:
            if not self.external_character.visible:
                self.remove_social_event()


class ExternalCharacter(gui.Window):
    
    def __init__(self, container, left_bottom, frame_rate, windows_controller, event, character):

        rect = pygame.Rect((0,0), (300, 559))
        rect.bottomleft = left_bottom
        
        self.visible = True
        
        gui.Window.__init__(self, container, rect, frame_rate, windows_controller, "external_character")
        
        self.time_span = event.message_time_span
        
        self.character = character # Main character (Kid)
        
        self.external_character = gui.Image(self.rect, pygame.Rect((13, 179), (273, 380)), 1, event.person_path, True)
        
        if (event.person_path == "assets/characters/mother.png" or event.person_path == "assets/characters/father.png"):
            self.apply_mappings()
        
        self.external_character.keep_dirty = True
        self.add_child(self.external_character)
        
        message_balloon = MessageBalloon(self.rect, pygame.Rect(0, 0, 1, 1), 1, self.windows_controller, 'B')
        message_balloon.set_text(event.person_message)
        self.add_window(message_balloon)
        
    def apply_mappings(self):
        maps = self.character.mappings
        self.change_color(animation.PARENTS_COLORS_TO_MAP, maps["hair"] + maps["skin"])
        self.set_dirty()
        
    def change_color(self, old, new):
        index = 0
        for old_color in old:
            new_color = new[index]
            utilities.change_color(self.external_character.background, old_color, new_color)
            index += 1
            
    ###########################################
    
    # Override handle_mouse_down
    def handle_mouse_down(self, (x, y)):
        self.hide()
        
    def update(self, frames):
        self.time_span -= 1
        if self.time_span == 0:
            self.hide()
            self.dispose()
        
        if self.visible:
            # This shouldn't be neccesary but it's not working without it.
            self.set_dirty_background()    # Always draws it because it collides with the character rectangle
    
class MessageBalloon(gui.Window):
    
    def __init__(self, container, rect, frame_rate, windows_controller, b_type):
        
        self.b_type = b_type
        
        if b_type == 'A':
            # Thinking balloon
            background = pygame.image.load("assets/events/balloon.png").convert()
        else:
            # Saying balloon
            background = pygame.image.load("assets/events/balloonB.png").convert()
            
        rect.size = background.get_size()
        
        gui.Window.__init__(self, container, rect, frame_rate, windows_controller, "balloon")        
        
        self.set_bg_image(background)
        self.text = None
        self.time_span = None
        
        self.visible = True
    
    # Override handle_mouse_down
    def handle_mouse_down(self, (x, y)):
        if self.visible:
            self.hide()
            self.dispose()
            return True
        else:
            return False
        
    def set_text(self, text):
        if self.b_type == 'A':
            self.text = gui.TextBlock(self.rect, 245, 70, 1, text, 18, pygame.Color("#0f5e65"), "normal", False, anchor_type="center")
        else:
            self.text = gui.TextBlock(self.rect, 160, 80, 1, text, 20, pygame.Color("#0f5e65"), "normal", False, anchor_type="center")
        self.text.keep_dirty = True
        self.add_child(self.text)
        
    def set_time_span(self, time_span):
        self.time_span = time_span
    
    def update(self, frames):
        self.time_span -= 1
        if self.time_span == 0:
            self.hide()
            self.dispose()
        
        if self.visible:
            self.set_dirty_background()    # Always draws it because it collides with the character rectangle