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
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
|
# -*- coding: utf-8 -*-
INSTANCE_FILE_PATH = "game.save"
GAME_VERSION = "1.0"
CONTROL_INTERVAL = 16 # Qty of signal calls until a new control is performed (actions, events, weather, etc.)
EVENTS_OCCURRENCE_INTERVAL = 5 #per control interval after an event
HOUR_COUNT_CYCLE = 10 #control intevals that have to pass to management the time of day
import random
import effects
import character
import events
import sound_manager
instance = None
class GameManager:
"""
Clase gestora del sistema. Se encarga del control de las acciones
y los eventos del juego.
"""
def __init__(self, character, bars_controller, actions_list, events_list, places_dictionary, weathers, environments_dictionary, weather_effects, moods_list, windows_controller):
"""
Constructor de la clase
"""
global instance
instance = self
self.character = character
self.bars_controller = bars_controller
self.windows_controller = windows_controller
#management
self.count = 0 #sirve como 'clock' interno, para mantener un orden de tiempo dentro de la clase.
self.pause = False
#events, actions, moods
self.personal_events_list = self.__get_personal_events(events_list)
self.social_events_list = self.__get_social_events(events_list)
self.actions_list = actions_list
self.moods_list = moods_list
self.background_actions = []
#character states
self.active_char_action = None #Active character action, Action instance
self.active_events = []
self.active_social_events = []
self.active_mood = None
#self.__check_active_mood() # sets active_mood -> doesn't work because status bars aren't ready
self.places_dictionary = places_dictionary
self.weathers = weathers
#for events handling:
self.events_interval = EVENTS_OCCURRENCE_INTERVAL
#environment
self.environments_dictionary = environments_dictionary
self.current_weather = "sunny" # default weather
self.environment = None
# time of day
self.hour = 2 # value between 0 and 3
# 0 night, 1 morning, 2 noon, 3 afternoon
self.hour_count = HOUR_COUNT_CYCLE # managment cycles that have to pass for handling the hour
self.day_dic = {0 : "night", 1 : "morning", 2 : "noon", 3 : "afternoon"}
self.current_time = self.day_dic[self.hour] #current time of day
# menu handling
self.menu_active = False
# weather
self.weather_effects = weather_effects
self.environment_effect = None # this is an Effect that represents the effect on the character by the environment: weather + place + clothes
self.update_environment_effect()
# management
def pause_game(self):
self.pause = True
def continue_game(self):
self.pause = False
def signal(self):
"""
Increment signal, it means that a main iteration has been completed
"""
if not self.pause:
self.count += 1
if self.count >= CONTROL_INTERVAL:
self.__control_active_actions() # handle active character actions
self.bars_controller.calculate_score() # calculates the score of the score_bar
self.__control_level() # Checks if level must be changed
self.__control_active_events() # handle active events
self.__check_active_mood() # check if the active character mood
self.__handle_time()
if self.environment_effect:
self.environment_effect.activate()
else:
self.update_environment_effect()
self.count = 0
## Environment handling
def update_environment(self):
"""
Sets the character environment and send a message to the
windows_controller
"""
environment_id = (self.character.current_place, self.current_weather)
self.environment = self.environments_dictionary[environment_id]
self.windows_controller.set_environment(self.environment, self.current_time)
def update_environment_effect(self):
"""
Create and action with the effect on the character by the environment, taking current_place +
clothes + current_weather.
"""
outdoor = self.places_dictionary[self.character.current_place]["outdoor"]
affected_bars = self.weather_effects[(self.character.clothes, self.current_weather, outdoor)]
effect = effects.Effect(self.bars_controller, affected_bars)
self.environment_effect = effect
print "environment effect updated: ", affected_bars
### time of day
def __handle_time(self):
if not self.hour_count:
sound_manager.instance.play_time_change()
self.hour_count = HOUR_COUNT_CYCLE
self.hour += 1
if self.hour > 3:
self.hour = 0 #night
self.current_time = self.day_dic[self.hour]
print "cambio el momento del día a: ", self.current_time
if self.hour == 1: #temporal para cambiar el clima
self.set_current_weather(self.get_random_weather())
self.update_environment()
else:
self.hour_count -= 1
### weather
def set_current_weather(self, weather):
"""
Set the current weather.
"""
self.current_weather = weather
self.update_environment_effect()
def get_random_weather(self):
"""
Returns a random weather, never returns the previous weather.
"""
random_weather = self.current_weather
while random_weather == self.current_weather:
random_weather = random.choice(self.weathers)
print "se genero el clima: ", random_weather
return random_weather
### location
def set_character_location(self, place_id):
"""
Set the character location.
"""
print "character went to: ", place_id
self.character.current_place = place_id
self.update_environment()
self.update_environment_effect()
def get_place(self, place_id):
"""
(not implemented) Returns the place asociated to the place_id
"""
None
### Clothes
def set_character_clothes(self, clothes_id):
"""
Set the character clothes.
"""
self.character.set_clothes(clothes_id)
print "character's clothes: ", clothes_id
self.update_environment_effect()
self.windows_controller.update_clothes()
## Actions handling
def execute_action(self, action_id):
action = self.get_action(action_id)
if action:
if isinstance(action.effect, effects.Effect): #this action affects status bars
self.set_active_action(action_id)
elif isinstance(action.effect, effects.LocationEffect): #this action affects character location
if self.active_char_action:
self.interrupt_active_action(None)
action.perform()
action.reset()
elif isinstance(action.effect, effects.ClothesEffect): #this action affects character clothes
if self.active_char_action:
self.interrupt_active_action(None)
action.perform()
action.reset()
def interrupt_active_action(self, action_id):
"""
Stops the active action if exist, and set as active the
action with the 'action_id'. If the action_id is 'None', just
stops the active action.
"""
if self.active_char_action:
self.active_char_action.reset()
self.active_char_action = None
self.windows_controller.stop_current_action_animation()
if action_id:
action = self.get_action(action_id)
if action:
self.active_char_action = action
def add_background_action(self, action_id):
"""
Add a background action.
"""
action = self.get_action(action_id)
if action:
self.background_actions.append(action)
def get_active_action(self):
"""
Return the character active action
"""
return self.active_char_action
def set_active_action(self, action_id):
"""
Set the active char actions
"""
#place = get_place(self.character.current_place)
#if place.allowed_action(action_id): #continúa con la acción, solo si es permitida en el lugar
if not self.active_char_action: #Si existe una accion activa no la interrumpe
if True: #dont check char's place yet
action = self.get_action(action_id)
if action:
action.perform()
self.windows_controller.show_action_animation(action)
self.active_char_action = action
def get_action(self, action_id):
"""
Returns the action asociated to the id_action
"""
for action in self.actions_list:
if action.id == action_id:
return action
def get_lowest_bar(self):
return self.bars_controller.get_lowest_bar()
def __control_active_actions(self):
"""
Controls active game actions.
"""
for action in self.background_actions:
action.perform()
action.time_span = -1 #that means background actions never stop
if self.active_char_action: #if the character is performing an action:
if self.active_char_action.time_left > 0:
self.active_char_action.perform()
else: #if the action was completed:
self.active_char_action.reset()
self.active_char_action = None
self.windows_controller.stop_current_action_animation()
## Moods handling
def __check_active_mood(self):
"""
Check the active mood, and set it according to the character state.
"""
mood = None
event_preferred_mood = 12 # set in highest mood rank (happy 1)
overall_bar_percent = self.bars_controller.get_overall_percent()
overall_bar_mood = 9 # set in normal mood
if overall_bar_percent < 0.33:
overall_bar_mood = 5 #set mood in sad grade 1
elif overall_bar_percent > 0.66:
overall_bar_mood = 10 #set mood in happy 3
mood = self.moods_list[overall_bar_mood]
event_preferred_moods = [event.preferred_mood for event in self.active_events]
#event_preferred_moods += [event.preferred_mood for event in self.active_social_events]
if event_preferred_moods:
event_preferred_mood = min(event_preferred_moods)
if event_preferred_mood <= overall_bar_mood: # choose the lowest value
mood = self.moods_list[event_preferred_mood]
if mood <> self.active_mood:
self.active_mood = mood
self.windows_controller.set_mood(mood)
self.character.mood = mood
print "cambio estado de animo a: ", self.active_mood.name
sound_manager.instance.set_music(mood.music)
## Events handling
def __control_active_events(self):
"""
Active events handler
"""
self.__handle_personal_events()
self.__handle_social_events()
if self.events_interval == 0 and not self.menu_active and not self.active_char_action:
if random.randint(0, 1):
# add personal
if len(self.active_events) <= 1:
event = self.__get_random_event(self.personal_events_list) #get a new random event
if event and not (event in self.active_events):
self.active_events.append(event)
self.windows_controller.add_personal_event(event) #notify windows controller
print "se disparó el evento: ", event.name
else:
# add social
if len(self.active_social_events) <= 1:
event = self.__get_random_event(self.social_events_list)
if event and not (event in self.active_social_events):
self.active_social_events.append(event)
self.windows_controller.add_social_event(event)
print "se disparó el evento: ", event.name
self.events_interval = EVENTS_OCCURRENCE_INTERVAL
elif self.events_interval > 0:
self.events_interval -= 1
def __handle_social_events(self):
"""
Handle social events
"""
for event in self.active_social_events:
if event.time_left:
event.perform()
else:
self.windows_controller.remove_social_event(event)
event.reset()
self.active_social_events.remove(event)
def __handle_personal_events(self):
"""
Handle personal events
"""
for event in self.active_events:
if event.time_left:
event.perform()
else:
self.windows_controller.remove_personal_event(event)
event.reset()
self.active_events.remove(event)
def __get_random_event(self, events_list):
"""
Get a random event
"""
self.__update_events_probability(events_list) # it updates the probabilities of the list's events
#max_rand = self.__calculate_max_rand(events_list) # get the max_rand for the events_list
probability_ranges = self.__calculate_ranges(events_list) # calculate the ranges for these events
max_rand = probability_ranges[-1][1] # Second member of last event
print probability_ranges
if max_rand == 0:
# There aren't events with probability
return None
else:
rand = random.random()*max_rand
for i in range(0, len(probability_ranges)):
if rand >= probability_ranges[i][0] and rand <= probability_ranges[i][1]:
return events_list[i]
def __update_events_probability(self, events_list):
"""
Updates events probability
"""
#updates events probability
for evt in events_list:
print evt.name, " prob: ", evt.update_probability(self.bars_controller.get_bars_status())
def __calculate_max_rand(self, events_list):
"""
Calculates the max random number
"""
max_rand = 0
for event in events_list:
max_rand += event.get_probability()
return max_rand
def __calculate_ranges(self, events_list):
"""
Calculate a probability range for each event and returns
a list of ranges
"""
previous = 0
ranges = []
for event in events_list:
ranges += [(previous, previous + event.get_probability())]
previous += event.get_probability()
return ranges
def __get_social_events(self, events_list):
social_events = []
for evt in events_list:
if isinstance(evt, events.SocialEvent):
social_events.append(evt)
return social_events
def __get_personal_events(self, events_list):
personal_events = []
for evt in events_list:
if isinstance(evt, events.PersonalEvent):
personal_events.append(evt)
return personal_events
# Score handling
def add_points(self, points):
score_bar = self.bars_controller.score_bar
score_bar.value += points
self.character.score = score_bar.value
def __control_level(self):
score_bar = self.bars_controller.score_bar
if score_bar.value == 100:
# sets master challenge
self.character.level += 1
score_bar.value = 1
self.set_master_challenge()
if score_bar.value == 0:
# falls back to previous level
if self.character.level > 1:
self.character.level -= 1
score_bar.value = 99
def set_master_challenge(self):
# The master challenge occurs when the player completed a level.
# If it is answered correctly the player wins some points, so he starts the new level with these points
# Otherwise it loses some points, and continues in the same level, so he has to continue playing to
# reach the master challenge again.
#self.windows_controller.show_master_challenge_intro()
pass
# Save, load and reset game
def reset_game(self):
"""
Reset game properties
"""
# actions
self.interrupt_active_action(None)
# events
for personal_event in self.active_events:
self.windows_controller.remove_personal_event(personal_event)
for social_event in self.active_social_events:
self.windows_controller.remove_social_event(social_event)
self.active_events = []
self.active_social_events = []
self.events_interval = EVENTS_OCCURRENCE_INTERVAL
# character
self.character.reset()
# bars
self.bars_controller.reset()
# weather
self.current_weather = "sunny" # default weather
self.update_environment()
self.update_environment_effect()
self.windows_controller.update_clothes()
# hour
self.hour = 2
self.hour_count = HOUR_COUNT_CYCLE
self.current_time = self.day_dic[self.hour]
print "game reseted successfully... "
def save_game(self):
"""
Save the game instance
"""
game_status = {}
##save bars status
bars_status_dic = self.bars_controller.get_bars_status()
##save character properties
char_properties = self.character.get_status()
##
game_status.update(bars_status_dic)
game_status.update(char_properties)
game_status.update({"version" : GAME_VERSION})
try:
f = open(INSTANCE_FILE_PATH, 'w')
f.write(game_status.__str__())
f.close()
print "se guardo la partida con exito. Version ", GAME_VERSION
except:
print "no se pudo guardar la partida."
raise
def load_game(self):
try:
f = open(INSTANCE_FILE_PATH)
str = f.read()
game_status = eval(str)
f.close()
#load bars status
self.bars_controller.load_bars_status(game_status)
print "status bars loaded..."
#character properties
self.character.load_properties(game_status)
print "character properties loaded..."
self.update_environment()
self.update_environment_effect()
self.windows_controller.update_clothes()
print "se cargo la partida con exito. Version ", game_status["version"]
except:
print "no se pudo cargar la partida."
##
def get_level(self):
"""
returns the character's current level
"""
return self.character.level
def get_active_events(self):
"""
returns current events
"""
events = self.active_events + self.active_social_events
return events
def get_current_place(self):
"""
returns character's current location.
"""
return self.character.current_place
def get_current_hour(self):
"""
returns current momento of day.
"""
return self.current_time
|