Mercurial > touhou
annotate pytouhou/game/game.py @ 123:d1c82d43bbf3
Various optimizations
author | Thibaut Girka <thib@sitedethib.com> |
---|---|
date | Sat, 10 Sep 2011 11:58:24 +0200 |
parents | 174324a4da51 |
children | 11ab06f4c4c6 |
rev | line source |
---|---|
52
ab826bc29aa2
Add some documentation, GPLv3 headers, README and COPYING file.
Thibaut Girka <thib@sitedethib.com>
parents:
50
diff
changeset
|
1 # -*- encoding: utf-8 -*- |
ab826bc29aa2
Add some documentation, GPLv3 headers, README and COPYING file.
Thibaut Girka <thib@sitedethib.com>
parents:
50
diff
changeset
|
2 ## |
ab826bc29aa2
Add some documentation, GPLv3 headers, README and COPYING file.
Thibaut Girka <thib@sitedethib.com>
parents:
50
diff
changeset
|
3 ## Copyright (C) 2011 Thibaut Girka <thib@sitedethib.com> |
ab826bc29aa2
Add some documentation, GPLv3 headers, README and COPYING file.
Thibaut Girka <thib@sitedethib.com>
parents:
50
diff
changeset
|
4 ## |
ab826bc29aa2
Add some documentation, GPLv3 headers, README and COPYING file.
Thibaut Girka <thib@sitedethib.com>
parents:
50
diff
changeset
|
5 ## This program is free software; you can redistribute it and/or modify |
ab826bc29aa2
Add some documentation, GPLv3 headers, README and COPYING file.
Thibaut Girka <thib@sitedethib.com>
parents:
50
diff
changeset
|
6 ## it under the terms of the GNU General Public License as published |
ab826bc29aa2
Add some documentation, GPLv3 headers, README and COPYING file.
Thibaut Girka <thib@sitedethib.com>
parents:
50
diff
changeset
|
7 ## by the Free Software Foundation; version 3 only. |
ab826bc29aa2
Add some documentation, GPLv3 headers, README and COPYING file.
Thibaut Girka <thib@sitedethib.com>
parents:
50
diff
changeset
|
8 ## |
ab826bc29aa2
Add some documentation, GPLv3 headers, README and COPYING file.
Thibaut Girka <thib@sitedethib.com>
parents:
50
diff
changeset
|
9 ## This program is distributed in the hope that it will be useful, |
ab826bc29aa2
Add some documentation, GPLv3 headers, README and COPYING file.
Thibaut Girka <thib@sitedethib.com>
parents:
50
diff
changeset
|
10 ## but WITHOUT ANY WARRANTY; without even the implied warranty of |
ab826bc29aa2
Add some documentation, GPLv3 headers, README and COPYING file.
Thibaut Girka <thib@sitedethib.com>
parents:
50
diff
changeset
|
11 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
ab826bc29aa2
Add some documentation, GPLv3 headers, README and COPYING file.
Thibaut Girka <thib@sitedethib.com>
parents:
50
diff
changeset
|
12 ## GNU General Public License for more details. |
ab826bc29aa2
Add some documentation, GPLv3 headers, README and COPYING file.
Thibaut Girka <thib@sitedethib.com>
parents:
50
diff
changeset
|
13 ## |
ab826bc29aa2
Add some documentation, GPLv3 headers, README and COPYING file.
Thibaut Girka <thib@sitedethib.com>
parents:
50
diff
changeset
|
14 |
ab826bc29aa2
Add some documentation, GPLv3 headers, README and COPYING file.
Thibaut Girka <thib@sitedethib.com>
parents:
50
diff
changeset
|
15 |
50
811cefefb5c8
Fix a few bugs and add support for a few instructions
Thibaut Girka <thib@sitedethib.com>
parents:
49
diff
changeset
|
16 from pytouhou.utils.random import Random |
811cefefb5c8
Fix a few bugs and add support for a few instructions
Thibaut Girka <thib@sitedethib.com>
parents:
49
diff
changeset
|
17 |
97 | 18 from pytouhou.vm.eclrunner import ECLMainRunner |
19 | |
98
d141c851c598
Rename pytouhou.game.enemymanager to pytouhou.game.enemy
Thibaut Girka <thib@sitedethib.com>
parents:
97
diff
changeset
|
20 from pytouhou.game.enemy import Enemy |
97 | 21 |
22 | |
49
cbe1cb50f2fd
Refactor ECLRunner/EnemyManager so that all VM stuff goes to ECLRunner
Thibaut Girka <thib@sitedethib.com>
parents:
diff
changeset
|
23 class GameState(object): |
106 | 24 __slots__ = ('resource_loader', 'bullets', 'players', 'rank', 'difficulty', 'frame', |
122
174324a4da51
Add support for launch animations! (Warning: slow :()
Thibaut Girka <thib@sitedethib.com>
parents:
106
diff
changeset
|
25 'stage', 'boss', 'prng', 'bullet_types') |
174324a4da51
Add support for launch animations! (Warning: slow :()
Thibaut Girka <thib@sitedethib.com>
parents:
106
diff
changeset
|
26 def __init__(self, resource_loader, players, stage, rank, difficulty, bullet_types): |
97 | 27 self.resource_loader = resource_loader |
83
fc0294c745b6
Basic bullet handling! Clean up as soon as possible :p
Thibaut Girka <thib@sitedethib.com>
parents:
52
diff
changeset
|
28 |
122
174324a4da51
Add support for launch animations! (Warning: slow :()
Thibaut Girka <thib@sitedethib.com>
parents:
106
diff
changeset
|
29 self.bullet_types = bullet_types |
174324a4da51
Add support for launch animations! (Warning: slow :()
Thibaut Girka <thib@sitedethib.com>
parents:
106
diff
changeset
|
30 |
106 | 31 self.bullets = [] |
32 | |
49
cbe1cb50f2fd
Refactor ECLRunner/EnemyManager so that all VM stuff goes to ECLRunner
Thibaut Girka <thib@sitedethib.com>
parents:
diff
changeset
|
33 self.stage = stage |
cbe1cb50f2fd
Refactor ECLRunner/EnemyManager so that all VM stuff goes to ECLRunner
Thibaut Girka <thib@sitedethib.com>
parents:
diff
changeset
|
34 self.players = players |
cbe1cb50f2fd
Refactor ECLRunner/EnemyManager so that all VM stuff goes to ECLRunner
Thibaut Girka <thib@sitedethib.com>
parents:
diff
changeset
|
35 self.rank = rank |
cbe1cb50f2fd
Refactor ECLRunner/EnemyManager so that all VM stuff goes to ECLRunner
Thibaut Girka <thib@sitedethib.com>
parents:
diff
changeset
|
36 self.difficulty = difficulty |
cbe1cb50f2fd
Refactor ECLRunner/EnemyManager so that all VM stuff goes to ECLRunner
Thibaut Girka <thib@sitedethib.com>
parents:
diff
changeset
|
37 self.boss = None |
50
811cefefb5c8
Fix a few bugs and add support for a few instructions
Thibaut Girka <thib@sitedethib.com>
parents:
49
diff
changeset
|
38 self.prng = Random() |
49
cbe1cb50f2fd
Refactor ECLRunner/EnemyManager so that all VM stuff goes to ECLRunner
Thibaut Girka <thib@sitedethib.com>
parents:
diff
changeset
|
39 self.frame = 0 |
83
fc0294c745b6
Basic bullet handling! Clean up as soon as possible :p
Thibaut Girka <thib@sitedethib.com>
parents:
52
diff
changeset
|
40 |
fc0294c745b6
Basic bullet handling! Clean up as soon as possible :p
Thibaut Girka <thib@sitedethib.com>
parents:
52
diff
changeset
|
41 |
97 | 42 |
43 class Game(object): | |
122
174324a4da51
Add support for launch animations! (Warning: slow :()
Thibaut Girka <thib@sitedethib.com>
parents:
106
diff
changeset
|
44 def __init__(self, resource_loader, players, stage, rank, difficulty, bullet_types): |
174324a4da51
Add support for launch animations! (Warning: slow :()
Thibaut Girka <thib@sitedethib.com>
parents:
106
diff
changeset
|
45 self.game_state = GameState(resource_loader, players, stage, rank, difficulty, bullet_types) |
97 | 46 |
47 self.enemies = [] | |
48 | |
49 self.bonuses = [] | |
50 | |
51 self.enm_anm_wrapper = resource_loader.get_anm_wrapper2(('stg%denm.anm' % stage, | |
52 'stg%denm2.anm' % stage)) | |
53 ecl = resource_loader.get_ecl('ecldata%d.ecl' % stage) | |
54 self.ecl_runner = ECLMainRunner(ecl, self.new_enemy, self.game_state) | |
55 | |
56 | |
57 def new_enemy(self, pos, life, instr_type): | |
58 enemy = Enemy(pos, life, instr_type, self.enm_anm_wrapper, self.game_state) | |
59 self.enemies.append(enemy) | |
60 return enemy | |
61 | |
62 | |
63 def run_iter(self, keystate): | |
64 # 1. VMs. | |
65 self.ecl_runner.run_iter() | |
66 | |
67 # 2. Filter out destroyed enemies | |
123 | 68 self.enemies = [enemy for enemy in self.enemies if not enemy._removed] |
83
fc0294c745b6
Basic bullet handling! Clean up as soon as possible :p
Thibaut Girka <thib@sitedethib.com>
parents:
52
diff
changeset
|
69 |
97 | 70 # 3. Let's play! |
71 for enemy in self.enemies: | |
72 enemy.update() | |
106 | 73 |
74 for bullet in self.game_state.bullets: | |
97 | 75 bullet.update() |
76 | |
77 # 4. Cleaning | |
78 self.cleanup() | |
79 | |
80 self.game_state.frame += 1 | |
81 | |
82 | |
83 def cleanup(self): | |
84 # Filter out non-visible enemies | |
85 for enemy in tuple(self.enemies): | |
86 if enemy.is_visible(384, 448): #TODO | |
87 enemy._was_visible = True | |
88 elif enemy._was_visible: | |
89 # Filter out-of-screen enemy | |
90 enemy._removed = True | |
91 self.enemies.remove(enemy) | |
92 | |
93 # Filter out-of-scren bullets | |
94 # TODO: was_visible thing | |
123 | 95 self.game_state.bullets = [bullet for bullet in self.game_state.bullets if bullet.is_visible(384, 448)] |
97 | 96 |
97 # Disable boss mode if it is dead/it has timeout | |
98 if self.game_state.boss and self.game_state.boss._removed: | |
99 self.game_state.boss = None | |
100 |