comparison pytouhou/game/game.py @ 151:5cf927cbd9c5

Merge GameState into Game. TODO: Merge PlayerState into Player
author Thibaut Girka <thib@sitedethib.com>
date Tue, 04 Oct 2011 23:32:02 +0200
parents 4f46717390aa
children 86807b8a63bd
comparison
equal deleted inserted replaced
150:4f46717390aa 151:5cf927cbd9c5
20 from pytouhou.game.player import Player 20 from pytouhou.game.player import Player
21 from pytouhou.game.enemy import Enemy 21 from pytouhou.game.enemy import Enemy
22 from pytouhou.game.item import Item 22 from pytouhou.game.item import Item
23 23
24 24
25 class GameState(object): 25
26 __slots__ = ('resource_loader', 'bullets', 'items', 'players', 'rank', 'difficulty', 'frame', 26 class Game(object):
27 'stage', 'boss', 'prng', 'bullet_types', 'item_types', 'characters', 'nb_bullets_max') 27 def __init__(self, resource_loader, player_states, stage, rank, difficulty,
28 def __init__(self, resource_loader, players, stage, rank, difficulty, 28 bullet_types, item_types, characters, nb_bullets_max=None):
29 bullet_types, item_types, characters, nb_bullets_max):
30 self.resource_loader = resource_loader 29 self.resource_loader = resource_loader
31 30
31 self.nb_bullets_max = nb_bullets_max
32 self.bullet_types = bullet_types 32 self.bullet_types = bullet_types
33 self.item_types = item_types 33 self.item_types = item_types
34 self.characters = characters 34 self.characters = characters
35 35
36 self.players = [Player(player_state, characters[player_state.character]) for player_state in player_states]
37 self.enemies = []
36 self.bullets = [] 38 self.bullets = []
37 self.items = [] 39 self.items = []
38 self.nb_bullets_max = nb_bullets_max
39 40
40 self.stage = stage 41 self.stage = stage
41 self.players = players
42 self.rank = rank 42 self.rank = rank
43 self.difficulty = difficulty 43 self.difficulty = difficulty
44 self.boss = None 44 self.boss = None
45 self.prng = Random() 45 self.prng = Random()
46 self.frame = 0 46 self.frame = 0
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)
47 52
48 53
49 def change_bullets_into_star_items(self): 54 def change_bullets_into_star_items(self):
50 player = self.players[0] #TODO 55 player = self.players[0] #TODO
51 item_type = self.item_types[6] 56 item_type = self.item_types[6]
52 self.items.extend(Item((bullet.x, bullet.y), item_type, 0.0, item_type.speed, player, self) for bullet in self.bullets) 57 self.items.extend(Item((bullet.x, bullet.y), item_type, 0.0, item_type.speed, player, self) for bullet in self.bullets)
53 self.bullets = [] 58 self.bullets = []
54 59
55 60
56
57 class Game(object):
58 def __init__(self, resource_loader, player_states, stage, rank, difficulty,
59 bullet_types, item_types, characters, nb_bullets_max=None):
60 self.game_state = GameState(resource_loader, player_states, stage,
61 rank, difficulty,
62 bullet_types, item_types, characters, nb_bullets_max)
63
64 self.players = [Player(player_state, characters[player_state.character]) for player_state in player_states]
65 self.enemies = []
66
67 self.bonuses = []
68
69 self.enm_anm_wrapper = resource_loader.get_anm_wrapper2(('stg%denm.anm' % stage,
70 'stg%denm2.anm' % stage))
71 ecl = resource_loader.get_ecl('ecldata%d.ecl' % stage)
72 self.ecl_runner = ECLMainRunner(ecl, self.new_enemy, self.game_state)
73
74
75 def new_enemy(self, pos, life, instr_type, pop_enemy): 61 def new_enemy(self, pos, life, instr_type, pop_enemy):
76 enemy = Enemy(pos, life, instr_type, self.enm_anm_wrapper, self.game_state, pop_enemy) 62 enemy = Enemy(pos, life, instr_type, self.enm_anm_wrapper, self, pop_enemy)
77 self.enemies.append(enemy) 63 self.enemies.append(enemy)
78 return enemy 64 return enemy
79 65
80 66
81 def run_iter(self, keystate): 67 def run_iter(self, keystate):
99 player.state.y = 448.-16 85 player.state.y = 448.-16
100 86
101 for enemy in self.enemies: 87 for enemy in self.enemies:
102 enemy.update() 88 enemy.update()
103 89
104 for bullet in self.game_state.bullets: 90 for bullet in self.bullets:
105 bullet.update() 91 bullet.update()
106 92
107 for item in self.game_state.items: 93 for item in self.items:
108 item.update() 94 item.update()
109 95
110 # 4. Check for collisions! 96 # 4. Check for collisions!
111 #TODO 97 #TODO
112 for player in self.players: 98 for player in self.players:
113 px, py = player.x, player.y 99 px, py = player.x, player.y
114 phalf_size = player.hitbox_half_size 100 phalf_size = player.hitbox_half_size
115 px1, px2 = px - phalf_size, px + phalf_size 101 px1, px2 = px - phalf_size, px + phalf_size
116 py1, py2 = py - phalf_size, py + phalf_size 102 py1, py2 = py - phalf_size, py + phalf_size
117 for bullet in self.game_state.bullets: 103 for bullet in self.bullets:
118 half_size = bullet.hitbox_half_size 104 half_size = bullet.hitbox_half_size
119 bx, by = bullet.x, bullet.y 105 bx, by = bullet.x, bullet.y
120 bx1, bx2 = bx - half_size, bx + half_size 106 bx1, bx2 = bx - half_size, bx + half_size
121 by1, by2 = by - half_size, by + half_size 107 by1, by2 = by - half_size, by + half_size
122 108
128 #TODO: item-player collision 114 #TODO: item-player collision
129 115
130 # 5. Cleaning 116 # 5. Cleaning
131 self.cleanup() 117 self.cleanup()
132 118
133 self.game_state.frame += 1 119 self.frame += 1
134 120
135 121
136 def cleanup(self): 122 def cleanup(self):
137 # Filter out non-visible enemies 123 # Filter out non-visible enemies
138 for enemy in tuple(self.enemies): 124 for enemy in tuple(self.enemies):
143 enemy._removed = True 129 enemy._removed = True
144 self.enemies.remove(enemy) 130 self.enemies.remove(enemy)
145 131
146 # Filter out-of-scren bullets 132 # Filter out-of-scren bullets
147 # TODO: was_visible thing 133 # TODO: was_visible thing
148 self.game_state.bullets = [bullet for bullet in self.game_state.bullets if bullet.is_visible(384, 448)] 134 self.bullets = [bullet for bullet in self.bullets if bullet.is_visible(384, 448)]
149 135
150 # Disable boss mode if it is dead/it has timeout 136 # Disable boss mode if it is dead/it has timeout
151 if self.game_state.boss and self.game_state.boss._removed: 137 if self.boss and self.boss._removed:
152 self.game_state.boss = None 138 self.boss = None
153 139