Mercurial > touhou
annotate pytouhou/game/game.py @ 134:e9ac3640280b
Add support for enemy spawnling enemies.
author | Emmanuel Gil Peyrot <linkmauve@linkmauve.fr> |
---|---|
date | Fri, 16 Sep 2011 11:21:33 -0700 |
parents | 11ab06f4c4c6 |
children | c7f0fd9d2145 |
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 | |
130 | 20 from pytouhou.game.player import Player |
98
d141c851c598
Rename pytouhou.game.enemymanager to pytouhou.game.enemy
Thibaut Girka <thib@sitedethib.com>
parents:
97
diff
changeset
|
21 from pytouhou.game.enemy import Enemy |
97 | 22 |
23 | |
49
cbe1cb50f2fd
Refactor ECLRunner/EnemyManager so that all VM stuff goes to ECLRunner
Thibaut Girka <thib@sitedethib.com>
parents:
diff
changeset
|
24 class GameState(object): |
106 | 25 __slots__ = ('resource_loader', 'bullets', 'players', 'rank', 'difficulty', 'frame', |
130 | 26 'stage', 'boss', 'prng', 'bullet_types', 'characters') |
27 def __init__(self, resource_loader, players, stage, rank, difficulty, | |
28 bullet_types, characters): | |
97 | 29 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
|
30 |
122
174324a4da51
Add support for launch animations! (Warning: slow :()
Thibaut Girka <thib@sitedethib.com>
parents:
106
diff
changeset
|
31 self.bullet_types = bullet_types |
130 | 32 self.characters = characters |
122
174324a4da51
Add support for launch animations! (Warning: slow :()
Thibaut Girka <thib@sitedethib.com>
parents:
106
diff
changeset
|
33 |
106 | 34 self.bullets = [] |
35 | |
49
cbe1cb50f2fd
Refactor ECLRunner/EnemyManager so that all VM stuff goes to ECLRunner
Thibaut Girka <thib@sitedethib.com>
parents:
diff
changeset
|
36 self.stage = stage |
cbe1cb50f2fd
Refactor ECLRunner/EnemyManager so that all VM stuff goes to ECLRunner
Thibaut Girka <thib@sitedethib.com>
parents:
diff
changeset
|
37 self.players = players |
cbe1cb50f2fd
Refactor ECLRunner/EnemyManager so that all VM stuff goes to ECLRunner
Thibaut Girka <thib@sitedethib.com>
parents:
diff
changeset
|
38 self.rank = rank |
cbe1cb50f2fd
Refactor ECLRunner/EnemyManager so that all VM stuff goes to ECLRunner
Thibaut Girka <thib@sitedethib.com>
parents:
diff
changeset
|
39 self.difficulty = difficulty |
cbe1cb50f2fd
Refactor ECLRunner/EnemyManager so that all VM stuff goes to ECLRunner
Thibaut Girka <thib@sitedethib.com>
parents:
diff
changeset
|
40 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
|
41 self.prng = Random() |
49
cbe1cb50f2fd
Refactor ECLRunner/EnemyManager so that all VM stuff goes to ECLRunner
Thibaut Girka <thib@sitedethib.com>
parents:
diff
changeset
|
42 self.frame = 0 |
83
fc0294c745b6
Basic bullet handling! Clean up as soon as possible :p
Thibaut Girka <thib@sitedethib.com>
parents:
52
diff
changeset
|
43 |
fc0294c745b6
Basic bullet handling! Clean up as soon as possible :p
Thibaut Girka <thib@sitedethib.com>
parents:
52
diff
changeset
|
44 |
97 | 45 |
46 class Game(object): | |
130 | 47 def __init__(self, resource_loader, player_states, stage, rank, difficulty, |
48 bullet_types, characters): | |
49 self.game_state = GameState(resource_loader, player_states, stage, | |
50 rank, difficulty, | |
51 bullet_types, characters) | |
97 | 52 |
130 | 53 self.players = [Player(player_state, characters[player_state.character]) for player_state in player_states] |
97 | 54 self.enemies = [] |
55 | |
56 self.bonuses = [] | |
57 | |
58 self.enm_anm_wrapper = resource_loader.get_anm_wrapper2(('stg%denm.anm' % stage, | |
59 'stg%denm2.anm' % stage)) | |
60 ecl = resource_loader.get_ecl('ecldata%d.ecl' % stage) | |
61 self.ecl_runner = ECLMainRunner(ecl, self.new_enemy, self.game_state) | |
62 | |
63 | |
134
e9ac3640280b
Add support for enemy spawnling enemies.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
130
diff
changeset
|
64 def new_enemy(self, pos, life, instr_type, pop_enemy): |
e9ac3640280b
Add support for enemy spawnling enemies.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
130
diff
changeset
|
65 enemy = Enemy(pos, life, instr_type, self.enm_anm_wrapper, self.game_state, pop_enemy) |
97 | 66 self.enemies.append(enemy) |
67 return enemy | |
68 | |
69 | |
70 def run_iter(self, keystate): | |
71 # 1. VMs. | |
72 self.ecl_runner.run_iter() | |
73 | |
74 # 2. Filter out destroyed enemies | |
123 | 75 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
|
76 |
97 | 77 # 3. Let's play! |
130 | 78 for player in self.players: |
79 player.update(keystate) #TODO: differentiate keystates | |
80 if player.state.x < 8.: | |
81 player.state.x = 8. | |
82 if player.state.x > 384.-8: #TODO | |
83 player.state.x = 384.-8 | |
84 if player.state.y < 16.: | |
85 player.state.y = 16. | |
86 if player.state.y > 448.-16: #TODO | |
87 player.state.y = 448.-16 | |
88 | |
97 | 89 for enemy in self.enemies: |
90 enemy.update() | |
106 | 91 |
92 for bullet in self.game_state.bullets: | |
97 | 93 bullet.update() |
94 | |
95 # 4. Cleaning | |
96 self.cleanup() | |
97 | |
98 self.game_state.frame += 1 | |
99 | |
100 | |
101 def cleanup(self): | |
102 # Filter out non-visible enemies | |
103 for enemy in tuple(self.enemies): | |
104 if enemy.is_visible(384, 448): #TODO | |
105 enemy._was_visible = True | |
106 elif enemy._was_visible: | |
107 # Filter out-of-screen enemy | |
108 enemy._removed = True | |
109 self.enemies.remove(enemy) | |
110 | |
111 # Filter out-of-scren bullets | |
112 # TODO: was_visible thing | |
123 | 113 self.game_state.bullets = [bullet for bullet in self.game_state.bullets if bullet.is_visible(384, 448)] |
97 | 114 |
115 # Disable boss mode if it is dead/it has timeout | |
116 if self.game_state.boss and self.game_state.boss._removed: | |
117 self.game_state.boss = None | |
118 |