annotate pytouhou/game/game.py @ 171:2f3665a77f11

Add support for the last unknown value of the enemy spawning.
author Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
date Thu, 20 Oct 2011 06:34:35 -0700
parents dcf32488a2c9
children ea2ad94c33a0
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
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
ac2e5e1c2c3c Refactor \o/
Thibaut Girka <thib@sitedethib.com>
parents: 83
diff changeset
18 from pytouhou.vm.eclrunner import ECLMainRunner
ac2e5e1c2c3c Refactor \o/
Thibaut Girka <thib@sitedethib.com>
parents: 83
diff changeset
19
130
11ab06f4c4c6 Introduce characters!
Thibaut Girka <thib@sitedethib.com>
parents: 123
diff changeset
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
150
4f46717390aa Introduce items, implement ECL instruction 83
Thibaut Girka <thib@sitedethib.com>
parents: 143
diff changeset
22 from pytouhou.game.item import Item
97
ac2e5e1c2c3c Refactor \o/
Thibaut Girka <thib@sitedethib.com>
parents: 83
diff changeset
23
ac2e5e1c2c3c Refactor \o/
Thibaut Girka <thib@sitedethib.com>
parents: 83
diff changeset
24
151
5cf927cbd9c5 Merge GameState into Game. TODO: Merge PlayerState into Player
Thibaut Girka <thib@sitedethib.com>
parents: 150
diff changeset
25
5cf927cbd9c5 Merge GameState into Game. TODO: Merge PlayerState into Player
Thibaut Girka <thib@sitedethib.com>
parents: 150
diff changeset
26 class Game(object):
5cf927cbd9c5 Merge GameState into Game. TODO: Merge PlayerState into Player
Thibaut Girka <thib@sitedethib.com>
parents: 150
diff changeset
27 def __init__(self, resource_loader, player_states, stage, rank, difficulty,
5cf927cbd9c5 Merge GameState into Game. TODO: Merge PlayerState into Player
Thibaut Girka <thib@sitedethib.com>
parents: 150
diff changeset
28 bullet_types, item_types, characters, nb_bullets_max=None):
97
ac2e5e1c2c3c Refactor \o/
Thibaut Girka <thib@sitedethib.com>
parents: 83
diff changeset
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
151
5cf927cbd9c5 Merge GameState into Game. TODO: Merge PlayerState into Player
Thibaut Girka <thib@sitedethib.com>
parents: 150
diff changeset
31 self.nb_bullets_max = nb_bullets_max
122
174324a4da51 Add support for launch animations! (Warning: slow :()
Thibaut Girka <thib@sitedethib.com>
parents: 106
diff changeset
32 self.bullet_types = bullet_types
150
4f46717390aa Introduce items, implement ECL instruction 83
Thibaut Girka <thib@sitedethib.com>
parents: 143
diff changeset
33 self.item_types = item_types
130
11ab06f4c4c6 Introduce characters!
Thibaut Girka <thib@sitedethib.com>
parents: 123
diff changeset
34 self.characters = characters
122
174324a4da51 Add support for launch animations! (Warning: slow :()
Thibaut Girka <thib@sitedethib.com>
parents: 106
diff changeset
35
151
5cf927cbd9c5 Merge GameState into Game. TODO: Merge PlayerState into Player
Thibaut Girka <thib@sitedethib.com>
parents: 150
diff changeset
36 self.players = [Player(player_state, characters[player_state.character]) for player_state in player_states]
5cf927cbd9c5 Merge GameState into Game. TODO: Merge PlayerState into Player
Thibaut Girka <thib@sitedethib.com>
parents: 150
diff changeset
37 self.enemies = []
166
dcf32488a2c9 Better enemy death, with animation and (hopefully) correct flags handling.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 165
diff changeset
38 self.effects = []
106
c7847bfed427 Minor refactoring
Thibaut Girka <thib@sitedethib.com>
parents: 98
diff changeset
39 self.bullets = []
161
7e7368356445 Add bullet cancel anim support
Thibaut Girka <thib@sitedethib.com>
parents: 159
diff changeset
40 self.cancelled_bullets = []
164
5271789c067d Implement player bullets rendering and updating.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 161
diff changeset
41 self.players_bullets = []
150
4f46717390aa Introduce items, implement ECL instruction 83
Thibaut Girka <thib@sitedethib.com>
parents: 143
diff changeset
42 self.items = []
106
c7847bfed427 Minor refactoring
Thibaut Girka <thib@sitedethib.com>
parents: 98
diff changeset
43
49
cbe1cb50f2fd Refactor ECLRunner/EnemyManager so that all VM stuff goes to ECLRunner
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
44 self.stage = stage
cbe1cb50f2fd Refactor ECLRunner/EnemyManager so that all VM stuff goes to ECLRunner
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
45 self.rank = rank
cbe1cb50f2fd Refactor ECLRunner/EnemyManager so that all VM stuff goes to ECLRunner
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
46 self.difficulty = difficulty
cbe1cb50f2fd Refactor ECLRunner/EnemyManager so that all VM stuff goes to ECLRunner
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
47 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
48 self.prng = Random()
49
cbe1cb50f2fd Refactor ECLRunner/EnemyManager so that all VM stuff goes to ECLRunner
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
49 self.frame = 0
83
fc0294c745b6 Basic bullet handling! Clean up as soon as possible :p
Thibaut Girka <thib@sitedethib.com>
parents: 52
diff changeset
50
151
5cf927cbd9c5 Merge GameState into Game. TODO: Merge PlayerState into Player
Thibaut Girka <thib@sitedethib.com>
parents: 150
diff changeset
51 self.enm_anm_wrapper = resource_loader.get_anm_wrapper2(('stg%denm.anm' % stage,
5cf927cbd9c5 Merge GameState into Game. TODO: Merge PlayerState into Player
Thibaut Girka <thib@sitedethib.com>
parents: 150
diff changeset
52 'stg%denm2.anm' % stage))
5cf927cbd9c5 Merge GameState into Game. TODO: Merge PlayerState into Player
Thibaut Girka <thib@sitedethib.com>
parents: 150
diff changeset
53 ecl = resource_loader.get_ecl('ecldata%d.ecl' % stage)
157
ca6f8b3f739d Remove half of the new_enemy/pop_enemy mess.
Thibaut Girka <thib@sitedethib.com>
parents: 156
diff changeset
54 self.ecl_runner = ECLMainRunner(ecl, self)
151
5cf927cbd9c5 Merge GameState into Game. TODO: Merge PlayerState into Player
Thibaut Girka <thib@sitedethib.com>
parents: 150
diff changeset
55
83
fc0294c745b6 Basic bullet handling! Clean up as soon as possible :p
Thibaut Girka <thib@sitedethib.com>
parents: 52
diff changeset
56
153
37df8c618c2e Add falling items.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 152
diff changeset
57 def drop_bonus(self, x, y, _type):
37df8c618c2e Add falling items.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 152
diff changeset
58 player = self.players[0] #TODO
165
c8c60291c56f Implement item dropping by enemies.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 164
diff changeset
59 if _type > 6:
c8c60291c56f Implement item dropping by enemies.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 164
diff changeset
60 return
153
37df8c618c2e Add falling items.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 152
diff changeset
61 item_type = self.item_types[_type]
37df8c618c2e Add falling items.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 152
diff changeset
62 item = Item((x, y), item_type, self)
37df8c618c2e Add falling items.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 152
diff changeset
63 self.items.append(item)
37df8c618c2e Add falling items.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 152
diff changeset
64
37df8c618c2e Add falling items.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 152
diff changeset
65
150
4f46717390aa Introduce items, implement ECL instruction 83
Thibaut Girka <thib@sitedethib.com>
parents: 143
diff changeset
66 def change_bullets_into_star_items(self):
4f46717390aa Introduce items, implement ECL instruction 83
Thibaut Girka <thib@sitedethib.com>
parents: 143
diff changeset
67 player = self.players[0] #TODO
4f46717390aa Introduce items, implement ECL instruction 83
Thibaut Girka <thib@sitedethib.com>
parents: 143
diff changeset
68 item_type = self.item_types[6]
153
37df8c618c2e Add falling items.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 152
diff changeset
69 self.items.extend(Item((bullet.x, bullet.y), item_type, self, player=player) for bullet in self.bullets)
150
4f46717390aa Introduce items, implement ECL instruction 83
Thibaut Girka <thib@sitedethib.com>
parents: 143
diff changeset
70 self.bullets = []
4f46717390aa Introduce items, implement ECL instruction 83
Thibaut Girka <thib@sitedethib.com>
parents: 143
diff changeset
71
4f46717390aa Introduce items, implement ECL instruction 83
Thibaut Girka <thib@sitedethib.com>
parents: 143
diff changeset
72
171
2f3665a77f11 Add support for the last unknown value of the enemy spawning.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 166
diff changeset
73 def new_enemy(self, pos, life, instr_type, bonus_dropped, die_score):
2f3665a77f11 Add support for the last unknown value of the enemy spawning.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 166
diff changeset
74 enemy = Enemy(pos, life, instr_type, bonus_dropped, die_score, self.enm_anm_wrapper, self)
97
ac2e5e1c2c3c Refactor \o/
Thibaut Girka <thib@sitedethib.com>
parents: 83
diff changeset
75 self.enemies.append(enemy)
ac2e5e1c2c3c Refactor \o/
Thibaut Girka <thib@sitedethib.com>
parents: 83
diff changeset
76 return enemy
ac2e5e1c2c3c Refactor \o/
Thibaut Girka <thib@sitedethib.com>
parents: 83
diff changeset
77
ac2e5e1c2c3c Refactor \o/
Thibaut Girka <thib@sitedethib.com>
parents: 83
diff changeset
78
ac2e5e1c2c3c Refactor \o/
Thibaut Girka <thib@sitedethib.com>
parents: 83
diff changeset
79 def run_iter(self, keystate):
ac2e5e1c2c3c Refactor \o/
Thibaut Girka <thib@sitedethib.com>
parents: 83
diff changeset
80 # 1. VMs.
ac2e5e1c2c3c Refactor \o/
Thibaut Girka <thib@sitedethib.com>
parents: 83
diff changeset
81 self.ecl_runner.run_iter()
ac2e5e1c2c3c Refactor \o/
Thibaut Girka <thib@sitedethib.com>
parents: 83
diff changeset
82
ac2e5e1c2c3c Refactor \o/
Thibaut Girka <thib@sitedethib.com>
parents: 83
diff changeset
83 # 2. Filter out destroyed enemies
123
d1c82d43bbf3 Various optimizations
Thibaut Girka <thib@sitedethib.com>
parents: 122
diff changeset
84 self.enemies = [enemy for enemy in self.enemies if not enemy._removed]
166
dcf32488a2c9 Better enemy death, with animation and (hopefully) correct flags handling.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 165
diff changeset
85 self.effects = [enemy for enemy in self.effects if not enemy._removed]
152
86807b8a63bd Add collisions with enemies and items.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 151
diff changeset
86 self.bullets = [bullet for bullet in self.bullets if not bullet._removed]
161
7e7368356445 Add bullet cancel anim support
Thibaut Girka <thib@sitedethib.com>
parents: 159
diff changeset
87 self.cancelled_bullets = [bullet for bullet in self.cancelled_bullets if not bullet._removed]
152
86807b8a63bd Add collisions with enemies and items.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 151
diff changeset
88 self.items = [item for item in self.items if not item._removed]
83
fc0294c745b6 Basic bullet handling! Clean up as soon as possible :p
Thibaut Girka <thib@sitedethib.com>
parents: 52
diff changeset
89
97
ac2e5e1c2c3c Refactor \o/
Thibaut Girka <thib@sitedethib.com>
parents: 83
diff changeset
90 # 3. Let's play!
142
c7f0fd9d2145 Simple collision detection
Thibaut Girka <thib@sitedethib.com>
parents: 134
diff changeset
91 #TODO: check update orders
130
11ab06f4c4c6 Introduce characters!
Thibaut Girka <thib@sitedethib.com>
parents: 123
diff changeset
92 for player in self.players:
142
c7f0fd9d2145 Simple collision detection
Thibaut Girka <thib@sitedethib.com>
parents: 134
diff changeset
93 player.update(keystate) #TODO: differentiate keystates (multiplayer mode)
130
11ab06f4c4c6 Introduce characters!
Thibaut Girka <thib@sitedethib.com>
parents: 123
diff changeset
94 if player.state.x < 8.:
11ab06f4c4c6 Introduce characters!
Thibaut Girka <thib@sitedethib.com>
parents: 123
diff changeset
95 player.state.x = 8.
11ab06f4c4c6 Introduce characters!
Thibaut Girka <thib@sitedethib.com>
parents: 123
diff changeset
96 if player.state.x > 384.-8: #TODO
11ab06f4c4c6 Introduce characters!
Thibaut Girka <thib@sitedethib.com>
parents: 123
diff changeset
97 player.state.x = 384.-8
11ab06f4c4c6 Introduce characters!
Thibaut Girka <thib@sitedethib.com>
parents: 123
diff changeset
98 if player.state.y < 16.:
11ab06f4c4c6 Introduce characters!
Thibaut Girka <thib@sitedethib.com>
parents: 123
diff changeset
99 player.state.y = 16.
11ab06f4c4c6 Introduce characters!
Thibaut Girka <thib@sitedethib.com>
parents: 123
diff changeset
100 if player.state.y > 448.-16: #TODO
11ab06f4c4c6 Introduce characters!
Thibaut Girka <thib@sitedethib.com>
parents: 123
diff changeset
101 player.state.y = 448.-16
11ab06f4c4c6 Introduce characters!
Thibaut Girka <thib@sitedethib.com>
parents: 123
diff changeset
102
97
ac2e5e1c2c3c Refactor \o/
Thibaut Girka <thib@sitedethib.com>
parents: 83
diff changeset
103 for enemy in self.enemies:
ac2e5e1c2c3c Refactor \o/
Thibaut Girka <thib@sitedethib.com>
parents: 83
diff changeset
104 enemy.update()
106
c7847bfed427 Minor refactoring
Thibaut Girka <thib@sitedethib.com>
parents: 98
diff changeset
105
166
dcf32488a2c9 Better enemy death, with animation and (hopefully) correct flags handling.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 165
diff changeset
106 for enemy in self.effects:
dcf32488a2c9 Better enemy death, with animation and (hopefully) correct flags handling.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 165
diff changeset
107 enemy.update()
dcf32488a2c9 Better enemy death, with animation and (hopefully) correct flags handling.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 165
diff changeset
108
151
5cf927cbd9c5 Merge GameState into Game. TODO: Merge PlayerState into Player
Thibaut Girka <thib@sitedethib.com>
parents: 150
diff changeset
109 for bullet in self.bullets:
97
ac2e5e1c2c3c Refactor \o/
Thibaut Girka <thib@sitedethib.com>
parents: 83
diff changeset
110 bullet.update()
ac2e5e1c2c3c Refactor \o/
Thibaut Girka <thib@sitedethib.com>
parents: 83
diff changeset
111
161
7e7368356445 Add bullet cancel anim support
Thibaut Girka <thib@sitedethib.com>
parents: 159
diff changeset
112 for bullet in self.cancelled_bullets:
7e7368356445 Add bullet cancel anim support
Thibaut Girka <thib@sitedethib.com>
parents: 159
diff changeset
113 bullet.update()
7e7368356445 Add bullet cancel anim support
Thibaut Girka <thib@sitedethib.com>
parents: 159
diff changeset
114
164
5271789c067d Implement player bullets rendering and updating.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 161
diff changeset
115 for bullet in self.players_bullets:
5271789c067d Implement player bullets rendering and updating.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 161
diff changeset
116 bullet.update()
5271789c067d Implement player bullets rendering and updating.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 161
diff changeset
117
151
5cf927cbd9c5 Merge GameState into Game. TODO: Merge PlayerState into Player
Thibaut Girka <thib@sitedethib.com>
parents: 150
diff changeset
118 for item in self.items:
150
4f46717390aa Introduce items, implement ECL instruction 83
Thibaut Girka <thib@sitedethib.com>
parents: 143
diff changeset
119 item.update()
4f46717390aa Introduce items, implement ECL instruction 83
Thibaut Girka <thib@sitedethib.com>
parents: 143
diff changeset
120
142
c7f0fd9d2145 Simple collision detection
Thibaut Girka <thib@sitedethib.com>
parents: 134
diff changeset
121 # 4. Check for collisions!
c7f0fd9d2145 Simple collision detection
Thibaut Girka <thib@sitedethib.com>
parents: 134
diff changeset
122 #TODO
c7f0fd9d2145 Simple collision detection
Thibaut Girka <thib@sitedethib.com>
parents: 134
diff changeset
123 for player in self.players:
c7f0fd9d2145 Simple collision detection
Thibaut Girka <thib@sitedethib.com>
parents: 134
diff changeset
124 px, py = player.x, player.y
c7f0fd9d2145 Simple collision detection
Thibaut Girka <thib@sitedethib.com>
parents: 134
diff changeset
125 phalf_size = player.hitbox_half_size
c7f0fd9d2145 Simple collision detection
Thibaut Girka <thib@sitedethib.com>
parents: 134
diff changeset
126 px1, px2 = px - phalf_size, px + phalf_size
c7f0fd9d2145 Simple collision detection
Thibaut Girka <thib@sitedethib.com>
parents: 134
diff changeset
127 py1, py2 = py - phalf_size, py + phalf_size
152
86807b8a63bd Add collisions with enemies and items.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 151
diff changeset
128
151
5cf927cbd9c5 Merge GameState into Game. TODO: Merge PlayerState into Player
Thibaut Girka <thib@sitedethib.com>
parents: 150
diff changeset
129 for bullet in self.bullets:
142
c7f0fd9d2145 Simple collision detection
Thibaut Girka <thib@sitedethib.com>
parents: 134
diff changeset
130 half_size = bullet.hitbox_half_size
c7f0fd9d2145 Simple collision detection
Thibaut Girka <thib@sitedethib.com>
parents: 134
diff changeset
131 bx, by = bullet.x, bullet.y
c7f0fd9d2145 Simple collision detection
Thibaut Girka <thib@sitedethib.com>
parents: 134
diff changeset
132 bx1, bx2 = bx - half_size, bx + half_size
c7f0fd9d2145 Simple collision detection
Thibaut Girka <thib@sitedethib.com>
parents: 134
diff changeset
133 by1, by2 = by - half_size, by + half_size
c7f0fd9d2145 Simple collision detection
Thibaut Girka <thib@sitedethib.com>
parents: 134
diff changeset
134
c7f0fd9d2145 Simple collision detection
Thibaut Girka <thib@sitedethib.com>
parents: 134
diff changeset
135 if not (bx2 < px1 or bx1 > px2
c7f0fd9d2145 Simple collision detection
Thibaut Girka <thib@sitedethib.com>
parents: 134
diff changeset
136 or by2 < py1 or by1 > py2):
156
ebfd328e700c Rename a few functions, move a few things around...
Thibaut Girka <thib@sitedethib.com>
parents: 154
diff changeset
137 bullet.collide()
ebfd328e700c Rename a few functions, move a few things around...
Thibaut Girka <thib@sitedethib.com>
parents: 154
diff changeset
138 player.collide()
152
86807b8a63bd Add collisions with enemies and items.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 151
diff changeset
139
86807b8a63bd Add collisions with enemies and items.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 151
diff changeset
140 for enemy in self.enemies:
86807b8a63bd Add collisions with enemies and items.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 151
diff changeset
141 half_size_x, half_size_y = enemy.hitbox_half_size
86807b8a63bd Add collisions with enemies and items.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 151
diff changeset
142 bx, by = enemy.x, enemy.y
86807b8a63bd Add collisions with enemies and items.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 151
diff changeset
143 bx1, bx2 = bx - half_size_x, bx + half_size_x
86807b8a63bd Add collisions with enemies and items.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 151
diff changeset
144 by1, by2 = by - half_size_y, by + half_size_y
142
c7f0fd9d2145 Simple collision detection
Thibaut Girka <thib@sitedethib.com>
parents: 134
diff changeset
145
156
ebfd328e700c Rename a few functions, move a few things around...
Thibaut Girka <thib@sitedethib.com>
parents: 154
diff changeset
146 if enemy.touchable and not (bx2 < px1 or bx1 > px2
ebfd328e700c Rename a few functions, move a few things around...
Thibaut Girka <thib@sitedethib.com>
parents: 154
diff changeset
147 or by2 < py1 or by1 > py2):
166
dcf32488a2c9 Better enemy death, with animation and (hopefully) correct flags handling.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 165
diff changeset
148 enemy.on_collide()
156
ebfd328e700c Rename a few functions, move a few things around...
Thibaut Girka <thib@sitedethib.com>
parents: 154
diff changeset
149 player.collide()
152
86807b8a63bd Add collisions with enemies and items.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 151
diff changeset
150
86807b8a63bd Add collisions with enemies and items.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 151
diff changeset
151 for item in self.items:
86807b8a63bd Add collisions with enemies and items.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 151
diff changeset
152 half_size = item.hitbox_half_size
86807b8a63bd Add collisions with enemies and items.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 151
diff changeset
153 bx, by = item.x, item.y
86807b8a63bd Add collisions with enemies and items.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 151
diff changeset
154 bx1, bx2 = bx - half_size, bx + half_size
86807b8a63bd Add collisions with enemies and items.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 151
diff changeset
155 by1, by2 = by - half_size, by + half_size
86807b8a63bd Add collisions with enemies and items.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 151
diff changeset
156
86807b8a63bd Add collisions with enemies and items.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 151
diff changeset
157 if not (bx2 < px1 or bx1 > px2
86807b8a63bd Add collisions with enemies and items.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 151
diff changeset
158 or by2 < py1 or by1 > py2):
156
ebfd328e700c Rename a few functions, move a few things around...
Thibaut Girka <thib@sitedethib.com>
parents: 154
diff changeset
159 player.collect(item)
150
4f46717390aa Introduce items, implement ECL instruction 83
Thibaut Girka <thib@sitedethib.com>
parents: 143
diff changeset
160
164
5271789c067d Implement player bullets rendering and updating.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 161
diff changeset
161 for enemy in self.enemies:
5271789c067d Implement player bullets rendering and updating.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 161
diff changeset
162 ex, ey = enemy.x, enemy.y
5271789c067d Implement player bullets rendering and updating.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 161
diff changeset
163 ehalf_size_x, ehalf_size_y = enemy.hitbox_half_size
5271789c067d Implement player bullets rendering and updating.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 161
diff changeset
164 ex1, ex2 = ex - ehalf_size_x, ex + ehalf_size_x
5271789c067d Implement player bullets rendering and updating.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 161
diff changeset
165 ey1, ey2 = ey - ehalf_size_y, ey + ehalf_size_y
5271789c067d Implement player bullets rendering and updating.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 161
diff changeset
166
5271789c067d Implement player bullets rendering and updating.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 161
diff changeset
167 for bullet in self.players_bullets:
5271789c067d Implement player bullets rendering and updating.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 161
diff changeset
168 half_size = bullet.hitbox_half_size
5271789c067d Implement player bullets rendering and updating.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 161
diff changeset
169 bx, by = bullet.x, bullet.y
5271789c067d Implement player bullets rendering and updating.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 161
diff changeset
170 bx1, bx2 = bx - half_size, bx + half_size
5271789c067d Implement player bullets rendering and updating.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 161
diff changeset
171 by1, by2 = by - half_size, by + half_size
5271789c067d Implement player bullets rendering and updating.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 161
diff changeset
172
5271789c067d Implement player bullets rendering and updating.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 161
diff changeset
173 if not (bx2 < ex1 or bx1 > ex2
5271789c067d Implement player bullets rendering and updating.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 161
diff changeset
174 or by2 < ey1 or by1 > ey2):
5271789c067d Implement player bullets rendering and updating.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 161
diff changeset
175 bullet.collide()
166
dcf32488a2c9 Better enemy death, with animation and (hopefully) correct flags handling.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 165
diff changeset
176 enemy.on_attack(bullet)
164
5271789c067d Implement player bullets rendering and updating.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 161
diff changeset
177
142
c7f0fd9d2145 Simple collision detection
Thibaut Girka <thib@sitedethib.com>
parents: 134
diff changeset
178 # 5. Cleaning
97
ac2e5e1c2c3c Refactor \o/
Thibaut Girka <thib@sitedethib.com>
parents: 83
diff changeset
179 self.cleanup()
ac2e5e1c2c3c Refactor \o/
Thibaut Girka <thib@sitedethib.com>
parents: 83
diff changeset
180
151
5cf927cbd9c5 Merge GameState into Game. TODO: Merge PlayerState into Player
Thibaut Girka <thib@sitedethib.com>
parents: 150
diff changeset
181 self.frame += 1
97
ac2e5e1c2c3c Refactor \o/
Thibaut Girka <thib@sitedethib.com>
parents: 83
diff changeset
182
ac2e5e1c2c3c Refactor \o/
Thibaut Girka <thib@sitedethib.com>
parents: 83
diff changeset
183
ac2e5e1c2c3c Refactor \o/
Thibaut Girka <thib@sitedethib.com>
parents: 83
diff changeset
184 def cleanup(self):
ac2e5e1c2c3c Refactor \o/
Thibaut Girka <thib@sitedethib.com>
parents: 83
diff changeset
185 # Filter out non-visible enemies
ac2e5e1c2c3c Refactor \o/
Thibaut Girka <thib@sitedethib.com>
parents: 83
diff changeset
186 for enemy in tuple(self.enemies):
ac2e5e1c2c3c Refactor \o/
Thibaut Girka <thib@sitedethib.com>
parents: 83
diff changeset
187 if enemy.is_visible(384, 448): #TODO
ac2e5e1c2c3c Refactor \o/
Thibaut Girka <thib@sitedethib.com>
parents: 83
diff changeset
188 enemy._was_visible = True
ac2e5e1c2c3c Refactor \o/
Thibaut Girka <thib@sitedethib.com>
parents: 83
diff changeset
189 elif enemy._was_visible:
ac2e5e1c2c3c Refactor \o/
Thibaut Girka <thib@sitedethib.com>
parents: 83
diff changeset
190 # Filter out-of-screen enemy
ac2e5e1c2c3c Refactor \o/
Thibaut Girka <thib@sitedethib.com>
parents: 83
diff changeset
191 enemy._removed = True
ac2e5e1c2c3c Refactor \o/
Thibaut Girka <thib@sitedethib.com>
parents: 83
diff changeset
192 self.enemies.remove(enemy)
ac2e5e1c2c3c Refactor \o/
Thibaut Girka <thib@sitedethib.com>
parents: 83
diff changeset
193
ac2e5e1c2c3c Refactor \o/
Thibaut Girka <thib@sitedethib.com>
parents: 83
diff changeset
194 # Filter out-of-scren bullets
ac2e5e1c2c3c Refactor \o/
Thibaut Girka <thib@sitedethib.com>
parents: 83
diff changeset
195 # TODO: was_visible thing
151
5cf927cbd9c5 Merge GameState into Game. TODO: Merge PlayerState into Player
Thibaut Girka <thib@sitedethib.com>
parents: 150
diff changeset
196 self.bullets = [bullet for bullet in self.bullets if bullet.is_visible(384, 448)]
161
7e7368356445 Add bullet cancel anim support
Thibaut Girka <thib@sitedethib.com>
parents: 159
diff changeset
197 self.cancelled_bullets = [bullet for bullet in self.cancelled_bullets if bullet.is_visible(384, 448)]
164
5271789c067d Implement player bullets rendering and updating.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 161
diff changeset
198 self.players_bullets = [bullet for bullet in self.players_bullets if bullet.is_visible(384, 448)]
97
ac2e5e1c2c3c Refactor \o/
Thibaut Girka <thib@sitedethib.com>
parents: 83
diff changeset
199
165
c8c60291c56f Implement item dropping by enemies.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 164
diff changeset
200 # Filter out-of-scren items
c8c60291c56f Implement item dropping by enemies.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 164
diff changeset
201 self.items = [item for item in self.items if item.y < 448]
c8c60291c56f Implement item dropping by enemies.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 164
diff changeset
202
97
ac2e5e1c2c3c Refactor \o/
Thibaut Girka <thib@sitedethib.com>
parents: 83
diff changeset
203 # Disable boss mode if it is dead/it has timeout
151
5cf927cbd9c5 Merge GameState into Game. TODO: Merge PlayerState into Player
Thibaut Girka <thib@sitedethib.com>
parents: 150
diff changeset
204 if self.boss and self.boss._removed:
5cf927cbd9c5 Merge GameState into Game. TODO: Merge PlayerState into Player
Thibaut Girka <thib@sitedethib.com>
parents: 150
diff changeset
205 self.boss = None
97
ac2e5e1c2c3c Refactor \o/
Thibaut Girka <thib@sitedethib.com>
parents: 83
diff changeset
206