comparison pytouhou/game/game.py @ 97:ac2e5e1c2c3c

Refactor \o/
author Thibaut Girka <thib@sitedethib.com>
date Sun, 04 Sep 2011 23:50:00 +0200
parents fc0294c745b6
children d141c851c598
comparison
equal deleted inserted replaced
96:54929d495654 97:ac2e5e1c2c3c
13 ## 13 ##
14 14
15 15
16 from pytouhou.utils.random import Random 16 from pytouhou.utils.random import Random
17 17
18 from pytouhou.vm.eclrunner import ECLMainRunner
19
20 from pytouhou.game.enemymanager import Enemy #TODO: enemymanager -> enemy
21
22
18 class GameState(object): 23 class GameState(object):
19 __slots__ = ('resources', 'players', 'rank', 'difficulty', 'frame', 'stage', 'boss', 'prng') 24 __slots__ = ('resource_loader', 'players', 'rank', 'difficulty', 'frame',
20 def __init__(self, resources, players, stage, rank, difficulty): 25 'stage', 'boss', 'prng')
21 self.resources = resources 26 def __init__(self, resource_loader, players, stage, rank, difficulty):
27 self.resource_loader = resource_loader
22 28
23 self.stage = stage 29 self.stage = stage
24 self.players = players 30 self.players = players
25 self.rank = rank 31 self.rank = rank
26 self.difficulty = difficulty 32 self.difficulty = difficulty
27 self.boss = None 33 self.boss = None
28 self.prng = Random() 34 self.prng = Random()
29 self.frame = 0 35 self.frame = 0
30 36
31 37
32 class Resources(object):
33 def __init__(self, etama_anm_wrappers, players_anm_wrappers, effects_anm_wrapper):
34 self.etama_anm_wrappers = etama_anm_wrappers
35 self.players_anm_wrappers = players_anm_wrappers
36 self.effects_anm_wrapper = effects_anm_wrapper
37 38
39 class Game(object):
40 def __init__(self, resource_loader, players, stage, rank, difficulty):
41 self.game_state = GameState(resource_loader, players, stage, rank, difficulty)
42
43 self.enemies = []
44
45 self.bullets = []
46 self.bonuses = []
47
48 self.enm_anm_wrapper = resource_loader.get_anm_wrapper2(('stg%denm.anm' % stage,
49 'stg%denm2.anm' % stage))
50 ecl = resource_loader.get_ecl('ecldata%d.ecl' % stage)
51 self.ecl_runner = ECLMainRunner(ecl, self.new_enemy, self.game_state)
52
53
54 def get_objects_by_texture(self, objects_by_texture):
55 #TODO: move elsewhere
56 for enemy in self.enemies:
57 enemy.get_objects_by_texture(objects_by_texture)
58
59 for bullet in self.bullets:
60 bullet.get_objects_by_texture(objects_by_texture)
61
62
63 def new_enemy(self, pos, life, instr_type):
64 enemy = Enemy(pos, life, instr_type, self.enm_anm_wrapper, self.game_state)
65 self.enemies.append(enemy)
66 return enemy
67
68
69 def run_iter(self, keystate):
70 # 1. VMs.
71 self.ecl_runner.run_iter()
72
73 # 2. Filter out destroyed enemies
74 self.enemies[:] = (enemy for enemy in self.enemies if not enemy._removed)
75
76 # 3. Let's play!
77 for enemy in self.enemies:
78 enemy.update()
79 for bullet in tuple(enemy.bullets):
80 if bullet._launched:
81 enemy.bullets.remove(bullet)
82 self.bullets.append(bullet)
83 for bullet in self.bullets:
84 bullet.update()
85
86
87 # 4. Cleaning
88 self.cleanup()
89
90 self.game_state.frame += 1
91
92
93 def cleanup(self):
94 # Filter out non-visible enemies
95 for enemy in tuple(self.enemies):
96 if enemy.is_visible(384, 448): #TODO
97 enemy._was_visible = True
98 elif enemy._was_visible:
99 # Filter out-of-screen enemy
100 enemy._removed = True
101 self.enemies.remove(enemy)
102
103 # Filter out-of-scren bullets
104 # TODO: was_visible thing
105 for bullet in tuple(self.bullets):
106 if not bullet.is_visible(384, 448):
107 self.bullets.remove(bullet)
108
109 # Disable boss mode if it is dead/it has timeout
110 if self.game_state.boss and self.game_state.boss._removed:
111 self.game_state.boss = None
112