comparison pytouhou/game/game.py @ 106:c7847bfed427

Minor refactoring
author Thibaut Girka <thib@sitedethib.com>
date Mon, 05 Sep 2011 22:40:46 +0200
parents d141c851c598
children 174324a4da51
comparison
equal deleted inserted replaced
105:572740acdb25 106:c7847bfed427
19 19
20 from pytouhou.game.enemy import Enemy 20 from pytouhou.game.enemy import Enemy
21 21
22 22
23 class GameState(object): 23 class GameState(object):
24 __slots__ = ('resource_loader', 'players', 'rank', 'difficulty', 'frame', 24 __slots__ = ('resource_loader', 'bullets', 'players', 'rank', 'difficulty', 'frame',
25 'stage', 'boss', 'prng') 25 'stage', 'boss', 'prng')
26 def __init__(self, resource_loader, players, stage, rank, difficulty): 26 def __init__(self, resource_loader, players, stage, rank, difficulty):
27 self.resource_loader = resource_loader 27 self.resource_loader = resource_loader
28
29 self.bullets = []
28 30
29 self.stage = stage 31 self.stage = stage
30 self.players = players 32 self.players = players
31 self.rank = rank 33 self.rank = rank
32 self.difficulty = difficulty 34 self.difficulty = difficulty
40 def __init__(self, resource_loader, players, stage, rank, difficulty): 42 def __init__(self, resource_loader, players, stage, rank, difficulty):
41 self.game_state = GameState(resource_loader, players, stage, rank, difficulty) 43 self.game_state = GameState(resource_loader, players, stage, rank, difficulty)
42 44
43 self.enemies = [] 45 self.enemies = []
44 46
45 self.bullets = []
46 self.bonuses = [] 47 self.bonuses = []
47 48
48 self.enm_anm_wrapper = resource_loader.get_anm_wrapper2(('stg%denm.anm' % stage, 49 self.enm_anm_wrapper = resource_loader.get_anm_wrapper2(('stg%denm.anm' % stage,
49 'stg%denm2.anm' % stage)) 50 'stg%denm2.anm' % stage))
50 ecl = resource_loader.get_ecl('ecldata%d.ecl' % stage) 51 ecl = resource_loader.get_ecl('ecldata%d.ecl' % stage)
51 self.ecl_runner = ECLMainRunner(ecl, self.new_enemy, self.game_state) 52 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 53
62 54
63 def new_enemy(self, pos, life, instr_type): 55 def new_enemy(self, pos, life, instr_type):
64 enemy = Enemy(pos, life, instr_type, self.enm_anm_wrapper, self.game_state) 56 enemy = Enemy(pos, life, instr_type, self.enm_anm_wrapper, self.game_state)
65 self.enemies.append(enemy) 57 self.enemies.append(enemy)
74 self.enemies[:] = (enemy for enemy in self.enemies if not enemy._removed) 66 self.enemies[:] = (enemy for enemy in self.enemies if not enemy._removed)
75 67
76 # 3. Let's play! 68 # 3. Let's play!
77 for enemy in self.enemies: 69 for enemy in self.enemies:
78 enemy.update() 70 enemy.update()
79 for bullet in tuple(enemy.bullets): 71
80 if bullet._launched: 72 for bullet in self.game_state.bullets:
81 enemy.bullets.remove(bullet)
82 self.bullets.append(bullet)
83 for bullet in self.bullets:
84 bullet.update() 73 bullet.update()
85
86 74
87 # 4. Cleaning 75 # 4. Cleaning
88 self.cleanup() 76 self.cleanup()
89 77
90 self.game_state.frame += 1 78 self.game_state.frame += 1
100 enemy._removed = True 88 enemy._removed = True
101 self.enemies.remove(enemy) 89 self.enemies.remove(enemy)
102 90
103 # Filter out-of-scren bullets 91 # Filter out-of-scren bullets
104 # TODO: was_visible thing 92 # TODO: was_visible thing
105 for bullet in tuple(self.bullets): 93 bullets = self.game_state.bullets
94 for bullet in tuple(bullets):
106 if not bullet.is_visible(384, 448): 95 if not bullet.is_visible(384, 448):
107 self.bullets.remove(bullet) 96 bullets.remove(bullet)
108 97
109 # Disable boss mode if it is dead/it has timeout 98 # Disable boss mode if it is dead/it has timeout
110 if self.game_state.boss and self.game_state.boss._removed: 99 if self.game_state.boss and self.game_state.boss._removed:
111 self.game_state.boss = None 100 self.game_state.boss = None
112 101