Web   ·   Wiki   ·   Activities   ·   Blog   ·   Lists   ·   Chat   ·   Meeting   ·   Bugs   ·   Git   ·   Translate   ·   Archive   ·   People   ·   Donate
summaryrefslogtreecommitdiffstats
path: root/Saludame.activity/animation.py
blob: d3a36e5a43554b84bd565a843c5466af5823888a (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
# -*- 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 pygame
import os
import utilities
import gui

COLORS_HAIR = (pygame.Color("#00ffff"), pygame.Color("#009f9f"))
COLORS_SKIN = (pygame.Color("#ffccc7"), pygame.Color("#cba5a0"))
COLORS_SWEATER = (pygame.Color("#00d69f"), pygame.Color("#00b07e"))
COLORS_PANTS = (pygame.Color("#ff9900"), pygame.Color("#d37e00"), pygame.Color("#b06800"), pygame.Color("#d47f00"))
COLORS_SHOES = (pygame.Color("#eeea00"), pygame.Color("#98a600"))

COLORS_TO_MAP = map(utilities.get_color_tuple, COLORS_HAIR + COLORS_SKIN + COLORS_SWEATER + COLORS_PANTS + COLORS_SHOES)
PARENTS_COLORS_TO_MAP = map(utilities.get_color_tuple, COLORS_HAIR + COLORS_SKIN)

GRAY = pygame.Color("gray")
BLACK = pygame.Color("black")
BLUE = pygame.Color("blue")
WHITE = pygame.Color("white")

class Kid(gui.Widget):
    
    def __init__(self, container, rect, frame_rate, windows_controller, game_man, mood):
        gui.Widget.__init__(self, container, rect, frame_rate, windows_controller)
        
        self.character = game_man.character
        
        self.moods = game_man.moods_list
        
        self.mood_index = 9 # Default mood_index (normal)
        self.mood = self.moods[9] # Default mood (normal)
        
        self.action_index = -1 # Default action_index (no-action)
        self.action = None
        
        self.sprite = None
        self.set_animation()
        
        self.visible = True
        
    ##### Moods #####
    def change_mood(self):
        self.mood_index += 1
        if self.mood_index == len(self.moods):
            self.mood_index = 0
        self.mood = self.moods[self.mood_index]
        self.index = 0
        
    def set_mood(self, mood):
        self.mood_index = self.moods.index(mood)
        self.mood = self.moods[self.mood_index]
        self.set_animation()
        
    ##### Clothes ####
    def update_clothes(self):
        self.set_animation()
        
    ##### Actions #####
    def play_action_animation(self, action):
        self.action = action
        self.set_animation()
        
    def stop_action_animation(self):
        self.action = None
        self.set_animation()
    
    def set_animation(self):
        sex = self.character.sex
        clothes = self.character.clothes
        
        self.loops = 0
        self.sound_loops = 0
        self.visible = True
        self.index = 0 # Sequence number of the current animation
        if self.action and self.action.kid_animation_path: # An action with animation is enabled
            directory = "%s/%s/%s" % (self.action.kid_animation_path, sex, clothes)
            self.file_list = get_image_list(directory)
        else:
            directory = "%s/%s/%s" % (self.mood.kid_animation_path, sex, clothes)
            self.file_list = get_image_list(directory)

    ##### update #####
    def update(self, frames):
        filename = self.file_list[self.index]
        self.sprite = load_animation(self.sprite, filename)
        
        self.background = self.sprite.copy()
        maps = self.character.mappings
        self.change_color(COLORS_TO_MAP, maps["hair"] + maps["skin"] + maps["sweater"] + maps["pants"] + maps["shoes"])
        
        self.index += 1
        if self.index >= len(self.file_list):
            self.index = 0
            self.loops += 1
            if self.action and self.action.kid_loop_times > 0 and self.loops == self.action.kid_loop_times:
                self.visible = False
        
        if self.action and self.action.sound_path:
            self.play_sound()

        self.set_dirty()
    
    def play_sound(self):
        if self.sound_loops == self.loops:
            if isinstance(self.action.sound_path, list):
                if len(self.action.sound_path) > self.sound_loops:
                    sound_file = self.action.sound_path[self.sound_loops]
                    if sound_file:
                        pygame.mixer.Sound(self.action.sound_path[self.sound_loops]).play()
            else:
                if self.sound_loops < self.action.sound_loop_times:
                    pygame.mixer.Sound(self.action.sound_path).play()
            self.sound_loops += 1
        
    def draw(self, frames):
        if self.visible:
            return gui.Widget.draw(self, frames)
        else:
            return self.rect_absolute
        
    ##### Colors #####
    def change_color(self, old, new):
        index = 0
        for old_color in old:
            new_color = new[index]
            utilities.change_color(self.background, old_color, new_color)
            
            index += 1
            
class ActionAnimation(gui.Button):
    """
    An action animation to show at panel
    """
    def __init__(self, container, rect, frame_rate, animation_path):
        gui.Button.__init__(self, container, rect, frame_rate, None)
        
        self.center_in_rect = True
        
        self.path = animation_path
        self.frame_rate = frame_rate
        
        dirList = os.listdir(animation_path)
        dirList.sort()
        self.file_list = [os.path.join(animation_path, fname) for fname in dirList if '.png' in fname]
        
        self.index = 0
        
    def update(self, frames):
        if frames % self.frame_rate == 0:
            filename = self.file_list[self.index]
            self.background = pygame.image.load(filename)
            self.index = (self.index + 1) % len(self.file_list)
            self.set_dirty()
            
class FPS(gui.Widget):
    def __init__(self, container, rect, frame_rate, clock):
        
        gui.Widget.__init__(self, container, rect, frame_rate)
        
        self.clock = clock
        
        self.font = pygame.font.Font(None, 16)
    
    def draw(self, screen):
        screen.fill(BLACK, self.rect_absolute)
        text = str(round(self.clock.get_fps()))
        text_surf = self.font.render(text, False, (255, 255, 255))
        screen.blit(text_surf, self.rect_absolute)
        return self.rect_absolute

def get_image_list(directory):
    dirList = os.listdir(directory)
    dirList.sort()
    return [os.path.join(directory, fname) for fname in dirList if fname.endswith('.png') or fname.endswith('.diff.gz') or fname.endswith('.diff.zlib')]


import zlib
import imagepatch
def load_animation(last_image, new_filename):
    if new_filename.endswith('.png'):
        new = pygame.image.load(new_filename)
        
    elif new_filename.endswith('.diff.zlib'):
        f = open(new_filename, 'r')
        diff = zlib.decompress(f.read())
        f.close()
        
        new_buffer = imagepatch.patch(last_image.get_buffer().raw, diff)
        
        new = last_image                        # both point to the same surface
        new.get_buffer().write(new_buffer, 0)   # Instead of using a copy modifies the same surface
        
    return new