Web   ·   Wiki   ·   Activities   ·   Blog   ·   Lists   ·   Chat   ·   Meeting   ·   Bugs   ·   Git   ·   Translate   ·   Archive   ·   People   ·   Donate
summaryrefslogtreecommitdiffstats
path: root/Saludame.activity/animation.py
blob: 69f27ca8c05847786309291987be28c5c07985ef (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
# -*- coding: utf-8 -*-

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_SOCKS = (pygame.Color("#fd8255"), pygame.Color("#db601f"))
COLORS_SHOES = (pygame.Color("#eeea00"), pygame.Color("#938200"))

COLORS_TO_MAP = map(utilities.get_color_tuple, COLORS_HAIR + COLORS_SKIN + COLORS_SOCKS + COLORS_SHOES)

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()
            
    ##### 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.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)
    
    
    ##### Draw #####
    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["socks"] + maps["shoes"])
        
        self.index = (self.index + 1) % len(self.file_list)
        
        self.set_dirty()
        
    ###### Draw #####
    #def pre_draw(self, screen):
        #filename = self.file_list[self.index]
        #self.sprite = load_animation(self.sprite, filename)
        
        #maps = self.character.mappings
        #self.change_color(COLORS_TO_MAP, maps["hair"] + maps["skin"] + maps["socks"] + maps["shoes"])
        
        ##screen.blit(self.bg_image, self.rect)
        #screen.blit(self.sprite, self.rect)
        
        #self.index = (self.index + 1) % len(self.file_list)
        
        #return [self.rect]
    
    ##### 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.Widget):
    """
    An action animation to show at panel
    """
    def __init__(self, container, rect, frame_rate, animation_path, sound_path):
        gui.Widget.__init__(self, container, rect, frame_rate)
        
        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