annotate pytouhou/game/enemymanager.py @ 94:ca571697ec83

Various minor optimisations and refactoring
author Thibaut Girka <thib@sitedethib.com>
date Sun, 04 Sep 2011 20:04:00 +0200
parents d167280a82fc
children ac2e5e1c2c3c
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
18
ca26a84916cb Add preliminary ECL viewer/interpreter.
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
16 from itertools import chain
ca26a84916cb Add preliminary ECL viewer/interpreter.
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
17 from io import BytesIO
ca26a84916cb Add preliminary ECL viewer/interpreter.
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
18 import os
ca26a84916cb Add preliminary ECL viewer/interpreter.
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
19 from struct import unpack, pack
20
6ebf9539c077 Handle more enemies types and movements
Thibaut Girka <thib@sitedethib.com>
parents: 18
diff changeset
20 from pytouhou.utils.interpolator import Interpolator
69
a142e57218a0 Refactor. Move VMs to pytouhou.vm.
Thibaut Girka <thib@sitedethib.com>
parents: 68
diff changeset
21 from pytouhou.vm.eclrunner import ECLRunner
a142e57218a0 Refactor. Move VMs to pytouhou.vm.
Thibaut Girka <thib@sitedethib.com>
parents: 68
diff changeset
22 from pytouhou.vm.anmrunner import ANMRunner
18
ca26a84916cb Add preliminary ECL viewer/interpreter.
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
23 from pytouhou.game.sprite import Sprite
83
fc0294c745b6 Basic bullet handling! Clean up as soon as possible :p
Thibaut Girka <thib@sitedethib.com>
parents: 79
diff changeset
24 from pytouhou.game.bullet import Bullet
79
ffe2c2b9912c Handle a few more ECL instructions. Prepare for bullet handling \o/
Thibaut Girka <thib@sitedethib.com>
parents: 75
diff changeset
25 from math import cos, sin, atan2, pi
18
ca26a84916cb Add preliminary ECL viewer/interpreter.
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
26
ca26a84916cb Add preliminary ECL viewer/interpreter.
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
27
ca26a84916cb Add preliminary ECL viewer/interpreter.
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
28 class Enemy(object):
79
ffe2c2b9912c Handle a few more ECL instructions. Prepare for bullet handling \o/
Thibaut Girka <thib@sitedethib.com>
parents: 75
diff changeset
29 def __init__(self, pos, life, _type, anm_wrapper, game_state):
ffe2c2b9912c Handle a few more ECL instructions. Prepare for bullet handling \o/
Thibaut Girka <thib@sitedethib.com>
parents: 75
diff changeset
30 self._game_state = game_state
49
cbe1cb50f2fd Refactor ECLRunner/EnemyManager so that all VM stuff goes to ECLRunner
Thibaut Girka <thib@sitedethib.com>
parents: 48
diff changeset
31 self._anm_wrapper = anm_wrapper
cbe1cb50f2fd Refactor ECLRunner/EnemyManager so that all VM stuff goes to ECLRunner
Thibaut Girka <thib@sitedethib.com>
parents: 48
diff changeset
32 self._sprite = None
69
a142e57218a0 Refactor. Move VMs to pytouhou.vm.
Thibaut Girka <thib@sitedethib.com>
parents: 68
diff changeset
33 self._anmrunner = None
49
cbe1cb50f2fd Refactor ECLRunner/EnemyManager so that all VM stuff goes to ECLRunner
Thibaut Girka <thib@sitedethib.com>
parents: 48
diff changeset
34 self._removed = False
cbe1cb50f2fd Refactor ECLRunner/EnemyManager so that all VM stuff goes to ECLRunner
Thibaut Girka <thib@sitedethib.com>
parents: 48
diff changeset
35 self._type = _type
56
299de3a9b69f Filter out off-screen enemies (the same way the official game does)
Thibaut Girka <thib@sitedethib.com>
parents: 52
diff changeset
36 self._was_visible = False
49
cbe1cb50f2fd Refactor ECLRunner/EnemyManager so that all VM stuff goes to ECLRunner
Thibaut Girka <thib@sitedethib.com>
parents: 48
diff changeset
37
cbe1cb50f2fd Refactor ECLRunner/EnemyManager so that all VM stuff goes to ECLRunner
Thibaut Girka <thib@sitedethib.com>
parents: 48
diff changeset
38 self.frame = 0
cbe1cb50f2fd Refactor ECLRunner/EnemyManager so that all VM stuff goes to ECLRunner
Thibaut Girka <thib@sitedethib.com>
parents: 48
diff changeset
39
18
ca26a84916cb Add preliminary ECL viewer/interpreter.
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
40 self.x, self.y = pos
ca26a84916cb Add preliminary ECL viewer/interpreter.
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
41 self.life = life
36
f46c18872796 Minor refactoring
Thibaut Girka <thib@sitedethib.com>
parents: 34
diff changeset
42 self.max_life = life
67
e2cb9d434dc2 Add support for a few opcodes.
Thibaut Girka <thib@sitedethib.com>
parents: 62
diff changeset
43 self.touchable = True
e2cb9d434dc2 Add support for a few opcodes.
Thibaut Girka <thib@sitedethib.com>
parents: 62
diff changeset
44 self.damageable = True
e2cb9d434dc2 Add support for a few opcodes.
Thibaut Girka <thib@sitedethib.com>
parents: 62
diff changeset
45 self.death_flags = 0
83
fc0294c745b6 Basic bullet handling! Clean up as soon as possible :p
Thibaut Girka <thib@sitedethib.com>
parents: 79
diff changeset
46 self.bullets = []
79
ffe2c2b9912c Handle a few more ECL instructions. Prepare for bullet handling \o/
Thibaut Girka <thib@sitedethib.com>
parents: 75
diff changeset
47 self.extended_bullet_attributes = (0, 0, 0, 0, 0., 0., 0., 0.)
48
8353c33d53d4 Support a few more instructions
Thibaut Girka <thib@sitedethib.com>
parents: 46
diff changeset
48 self.bullet_attributes = None
36
f46c18872796 Minor refactoring
Thibaut Girka <thib@sitedethib.com>
parents: 34
diff changeset
49 self.bullet_launch_offset = (0, 0)
48
8353c33d53d4 Support a few more instructions
Thibaut Girka <thib@sitedethib.com>
parents: 46
diff changeset
50 self.death_callback = None
8353c33d53d4 Support a few more instructions
Thibaut Girka <thib@sitedethib.com>
parents: 46
diff changeset
51 self.low_life_callback = None
8353c33d53d4 Support a few more instructions
Thibaut Girka <thib@sitedethib.com>
parents: 46
diff changeset
52 self.low_life_trigger = None
8353c33d53d4 Support a few more instructions
Thibaut Girka <thib@sitedethib.com>
parents: 46
diff changeset
53 self.timeout = None
67
e2cb9d434dc2 Add support for a few opcodes.
Thibaut Girka <thib@sitedethib.com>
parents: 62
diff changeset
54 self.timeout_callback = None
48
8353c33d53d4 Support a few more instructions
Thibaut Girka <thib@sitedethib.com>
parents: 46
diff changeset
55 self.remaining_lives = -1
8353c33d53d4 Support a few more instructions
Thibaut Girka <thib@sitedethib.com>
parents: 46
diff changeset
56
75
b3bd421bb895 Handle a few more ECL instructions
Thibaut Girka <thib@sitedethib.com>
parents: 73
diff changeset
57 self.automatic_orientation = False
b3bd421bb895 Handle a few more ECL instructions
Thibaut Girka <thib@sitedethib.com>
parents: 73
diff changeset
58
48
8353c33d53d4 Support a few more instructions
Thibaut Girka <thib@sitedethib.com>
parents: 46
diff changeset
59 self.bullet_launch_interval = 0
83
fc0294c745b6 Basic bullet handling! Clean up as soon as possible :p
Thibaut Girka <thib@sitedethib.com>
parents: 79
diff changeset
60 self.bullet_launch_timer = 0
48
8353c33d53d4 Support a few more instructions
Thibaut Girka <thib@sitedethib.com>
parents: 46
diff changeset
61 self.delay_attack = False
18
ca26a84916cb Add preliminary ECL viewer/interpreter.
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
62
49
cbe1cb50f2fd Refactor ECLRunner/EnemyManager so that all VM stuff goes to ECLRunner
Thibaut Girka <thib@sitedethib.com>
parents: 48
diff changeset
63 self.death_anim = None
21
bf225780973f Small refactoring, and Rumia \o/
Thibaut Girka <thib@sitedethib.com>
parents: 20
diff changeset
64 self.movement_dependant_sprites = None
22
fa87db09fc3a Fix Rumia's animations
Thibaut Girka <thib@sitedethib.com>
parents: 21
diff changeset
65 self.direction = None
20
6ebf9539c077 Handle more enemies types and movements
Thibaut Girka <thib@sitedethib.com>
parents: 18
diff changeset
66 self.interpolator = None #TODO
57
694f25881d0f Fix move_to interpolation, add support for a few ANM and ECL instructions
Thibaut Girka <thib@sitedethib.com>
parents: 56
diff changeset
67 self.speed_interpolator = None
18
ca26a84916cb Add preliminary ECL viewer/interpreter.
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
68 self.angle = 0.
ca26a84916cb Add preliminary ECL viewer/interpreter.
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
69 self.speed = 0.
ca26a84916cb Add preliminary ECL viewer/interpreter.
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
70 self.rotation_speed = 0.
ca26a84916cb Add preliminary ECL viewer/interpreter.
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
71 self.acceleration = 0.
ca26a84916cb Add preliminary ECL viewer/interpreter.
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
72
23
444ac7bca7bc Refacto ECL stuff, add support for a few instructions, and add some culling
Thibaut Girka <thib@sitedethib.com>
parents: 22
diff changeset
73 self.hitbox = (0, 0)
50
811cefefb5c8 Fix a few bugs and add support for a few instructions
Thibaut Girka <thib@sitedethib.com>
parents: 49
diff changeset
74 self.screen_box = None
23
444ac7bca7bc Refacto ECL stuff, add support for a few instructions, and add some culling
Thibaut Girka <thib@sitedethib.com>
parents: 22
diff changeset
75
48
8353c33d53d4 Support a few more instructions
Thibaut Girka <thib@sitedethib.com>
parents: 46
diff changeset
76
83
fc0294c745b6 Basic bullet handling! Clean up as soon as possible :p
Thibaut Girka <thib@sitedethib.com>
parents: 79
diff changeset
77 def set_bullet_attributes(self, type_, anim, sprite_idx_offset,
79
ffe2c2b9912c Handle a few more ECL instructions. Prepare for bullet handling \o/
Thibaut Girka <thib@sitedethib.com>
parents: 75
diff changeset
78 bullets_per_shot, number_of_shots, speed, speed2,
57
694f25881d0f Fix move_to interpolation, add support for a few ANM and ECL instructions
Thibaut Girka <thib@sitedethib.com>
parents: 56
diff changeset
79 launch_angle, angle, flags):
83
fc0294c745b6 Basic bullet handling! Clean up as soon as possible :p
Thibaut Girka <thib@sitedethib.com>
parents: 79
diff changeset
80 self.bullet_attributes = (type_, anim, sprite_idx_offset, bullets_per_shot,
79
ffe2c2b9912c Handle a few more ECL instructions. Prepare for bullet handling \o/
Thibaut Girka <thib@sitedethib.com>
parents: 75
diff changeset
81 number_of_shots, speed, speed2, launch_angle,
48
8353c33d53d4 Support a few more instructions
Thibaut Girka <thib@sitedethib.com>
parents: 46
diff changeset
82 angle, flags)
8353c33d53d4 Support a few more instructions
Thibaut Girka <thib@sitedethib.com>
parents: 46
diff changeset
83 if not self.delay_attack:
57
694f25881d0f Fix move_to interpolation, add support for a few ANM and ECL instructions
Thibaut Girka <thib@sitedethib.com>
parents: 56
diff changeset
84 self.fire()
694f25881d0f Fix move_to interpolation, add support for a few ANM and ECL instructions
Thibaut Girka <thib@sitedethib.com>
parents: 56
diff changeset
85
694f25881d0f Fix move_to interpolation, add support for a few ANM and ECL instructions
Thibaut Girka <thib@sitedethib.com>
parents: 56
diff changeset
86
694f25881d0f Fix move_to interpolation, add support for a few ANM and ECL instructions
Thibaut Girka <thib@sitedethib.com>
parents: 56
diff changeset
87 def fire(self):
83
fc0294c745b6 Basic bullet handling! Clean up as soon as possible :p
Thibaut Girka <thib@sitedethib.com>
parents: 79
diff changeset
88 (type_, anim, sprite_idx_offset, bullets_per_shot, number_of_shots,
79
ffe2c2b9912c Handle a few more ECL instructions. Prepare for bullet handling \o/
Thibaut Girka <thib@sitedethib.com>
parents: 75
diff changeset
89 speed, speed2, launch_angle, angle, flags) = self.bullet_attributes
83
fc0294c745b6 Basic bullet handling! Clean up as soon as possible :p
Thibaut Girka <thib@sitedethib.com>
parents: 79
diff changeset
90
fc0294c745b6 Basic bullet handling! Clean up as soon as possible :p
Thibaut Girka <thib@sitedethib.com>
parents: 79
diff changeset
91 self.bullet_launch_timer = 0
fc0294c745b6 Basic bullet handling! Clean up as soon as possible :p
Thibaut Girka <thib@sitedethib.com>
parents: 79
diff changeset
92
fc0294c745b6 Basic bullet handling! Clean up as soon as possible :p
Thibaut Girka <thib@sitedethib.com>
parents: 79
diff changeset
93 player = self.select_player()
fc0294c745b6 Basic bullet handling! Clean up as soon as possible :p
Thibaut Girka <thib@sitedethib.com>
parents: 79
diff changeset
94
79
ffe2c2b9912c Handle a few more ECL instructions. Prepare for bullet handling \o/
Thibaut Girka <thib@sitedethib.com>
parents: 75
diff changeset
95 if type_ in (67, 69, 71):
83
fc0294c745b6 Basic bullet handling! Clean up as soon as possible :p
Thibaut Girka <thib@sitedethib.com>
parents: 79
diff changeset
96 launch_angle += self.get_player_angle(player)
79
ffe2c2b9912c Handle a few more ECL instructions. Prepare for bullet handling \o/
Thibaut Girka <thib@sitedethib.com>
parents: 75
diff changeset
97 if type_ in (69, 70, 71):
ffe2c2b9912c Handle a few more ECL instructions. Prepare for bullet handling \o/
Thibaut Girka <thib@sitedethib.com>
parents: 75
diff changeset
98 angle = 2. * pi / bullets_per_shot
ffe2c2b9912c Handle a few more ECL instructions. Prepare for bullet handling \o/
Thibaut Girka <thib@sitedethib.com>
parents: 75
diff changeset
99 if type_ == 71:
ffe2c2b9912c Handle a few more ECL instructions. Prepare for bullet handling \o/
Thibaut Girka <thib@sitedethib.com>
parents: 75
diff changeset
100 launch_angle += pi / bullets_per_shot
83
fc0294c745b6 Basic bullet handling! Clean up as soon as possible :p
Thibaut Girka <thib@sitedethib.com>
parents: 79
diff changeset
101
fc0294c745b6 Basic bullet handling! Clean up as soon as possible :p
Thibaut Girka <thib@sitedethib.com>
parents: 79
diff changeset
102 launch_angle -= angle * (bullets_per_shot - 1) / 2.
fc0294c745b6 Basic bullet handling! Clean up as soon as possible :p
Thibaut Girka <thib@sitedethib.com>
parents: 79
diff changeset
103
fc0294c745b6 Basic bullet handling! Clean up as soon as possible :p
Thibaut Girka <thib@sitedethib.com>
parents: 79
diff changeset
104 for shot_nb in range(number_of_shots):
fc0294c745b6 Basic bullet handling! Clean up as soon as possible :p
Thibaut Girka <thib@sitedethib.com>
parents: 79
diff changeset
105 shot_speed = speed if shot_nb == 0 else speed + (speed2 - speed) * float(shot_nb) / float(number_of_shots)
fc0294c745b6 Basic bullet handling! Clean up as soon as possible :p
Thibaut Girka <thib@sitedethib.com>
parents: 79
diff changeset
106 bullet_angle = launch_angle
fc0294c745b6 Basic bullet handling! Clean up as soon as possible :p
Thibaut Girka <thib@sitedethib.com>
parents: 79
diff changeset
107 for bullet_nb in range(bullets_per_shot):
fc0294c745b6 Basic bullet handling! Clean up as soon as possible :p
Thibaut Girka <thib@sitedethib.com>
parents: 79
diff changeset
108 self.bullets.append(Bullet((self.x, self.y),
fc0294c745b6 Basic bullet handling! Clean up as soon as possible :p
Thibaut Girka <thib@sitedethib.com>
parents: 79
diff changeset
109 anim, sprite_idx_offset,
fc0294c745b6 Basic bullet handling! Clean up as soon as possible :p
Thibaut Girka <thib@sitedethib.com>
parents: 79
diff changeset
110 bullet_angle, shot_speed,
fc0294c745b6 Basic bullet handling! Clean up as soon as possible :p
Thibaut Girka <thib@sitedethib.com>
parents: 79
diff changeset
111 self.extended_bullet_attributes,
fc0294c745b6 Basic bullet handling! Clean up as soon as possible :p
Thibaut Girka <thib@sitedethib.com>
parents: 79
diff changeset
112 flags, player, self._game_state))
fc0294c745b6 Basic bullet handling! Clean up as soon as possible :p
Thibaut Girka <thib@sitedethib.com>
parents: 79
diff changeset
113 bullet_angle += angle
48
8353c33d53d4 Support a few more instructions
Thibaut Girka <thib@sitedethib.com>
parents: 46
diff changeset
114
8353c33d53d4 Support a few more instructions
Thibaut Girka <thib@sitedethib.com>
parents: 46
diff changeset
115
79
ffe2c2b9912c Handle a few more ECL instructions. Prepare for bullet handling \o/
Thibaut Girka <thib@sitedethib.com>
parents: 75
diff changeset
116 def select_player(self, players=None):
ffe2c2b9912c Handle a few more ECL instructions. Prepare for bullet handling \o/
Thibaut Girka <thib@sitedethib.com>
parents: 75
diff changeset
117 return (players or self._game_state.players)[0] #TODO
50
811cefefb5c8 Fix a few bugs and add support for a few instructions
Thibaut Girka <thib@sitedethib.com>
parents: 49
diff changeset
118
811cefefb5c8 Fix a few bugs and add support for a few instructions
Thibaut Girka <thib@sitedethib.com>
parents: 49
diff changeset
119
79
ffe2c2b9912c Handle a few more ECL instructions. Prepare for bullet handling \o/
Thibaut Girka <thib@sitedethib.com>
parents: 75
diff changeset
120 def get_player_angle(self, player=None):
ffe2c2b9912c Handle a few more ECL instructions. Prepare for bullet handling \o/
Thibaut Girka <thib@sitedethib.com>
parents: 75
diff changeset
121 player = player or self.select_player()
50
811cefefb5c8 Fix a few bugs and add support for a few instructions
Thibaut Girka <thib@sitedethib.com>
parents: 49
diff changeset
122 return atan2(player.y - self.y, player.x - self.x)
811cefefb5c8 Fix a few bugs and add support for a few instructions
Thibaut Girka <thib@sitedethib.com>
parents: 49
diff changeset
123
811cefefb5c8 Fix a few bugs and add support for a few instructions
Thibaut Girka <thib@sitedethib.com>
parents: 49
diff changeset
124
49
cbe1cb50f2fd Refactor ECLRunner/EnemyManager so that all VM stuff goes to ECLRunner
Thibaut Girka <thib@sitedethib.com>
parents: 48
diff changeset
125 def set_anim(self, index):
69
a142e57218a0 Refactor. Move VMs to pytouhou.vm.
Thibaut Girka <thib@sitedethib.com>
parents: 68
diff changeset
126 self._sprite = Sprite()
a142e57218a0 Refactor. Move VMs to pytouhou.vm.
Thibaut Girka <thib@sitedethib.com>
parents: 68
diff changeset
127 self._anmrunner = ANMRunner(self._anm_wrapper, index, self._sprite)
23
444ac7bca7bc Refacto ECL stuff, add support for a few instructions, and add some culling
Thibaut Girka <thib@sitedethib.com>
parents: 22
diff changeset
128
444ac7bca7bc Refacto ECL stuff, add support for a few instructions, and add some culling
Thibaut Girka <thib@sitedethib.com>
parents: 22
diff changeset
129
444ac7bca7bc Refacto ECL stuff, add support for a few instructions, and add some culling
Thibaut Girka <thib@sitedethib.com>
parents: 22
diff changeset
130 def set_pos(self, x, y, z):
444ac7bca7bc Refacto ECL stuff, add support for a few instructions, and add some culling
Thibaut Girka <thib@sitedethib.com>
parents: 22
diff changeset
131 self.x, self.y = x, y
444ac7bca7bc Refacto ECL stuff, add support for a few instructions, and add some culling
Thibaut Girka <thib@sitedethib.com>
parents: 22
diff changeset
132 self.interpolator = Interpolator((x, y))
444ac7bca7bc Refacto ECL stuff, add support for a few instructions, and add some culling
Thibaut Girka <thib@sitedethib.com>
parents: 22
diff changeset
133 self.interpolator.set_interpolation_start(self.frame, (x, y))
444ac7bca7bc Refacto ECL stuff, add support for a few instructions, and add some culling
Thibaut Girka <thib@sitedethib.com>
parents: 22
diff changeset
134
444ac7bca7bc Refacto ECL stuff, add support for a few instructions, and add some culling
Thibaut Girka <thib@sitedethib.com>
parents: 22
diff changeset
135
62
1f591adcea04 Fix animation determination (ins_98 stuff) and some interpolation functions
Thibaut Girka <thib@sitedethib.com>
parents: 57
diff changeset
136 def move_to(self, duration, x, y, z, formula):
57
694f25881d0f Fix move_to interpolation, add support for a few ANM and ECL instructions
Thibaut Girka <thib@sitedethib.com>
parents: 56
diff changeset
137 if not self.interpolator:
62
1f591adcea04 Fix animation determination (ins_98 stuff) and some interpolation functions
Thibaut Girka <thib@sitedethib.com>
parents: 57
diff changeset
138 self.interpolator = Interpolator((self.x, self.y), formula)
57
694f25881d0f Fix move_to interpolation, add support for a few ANM and ECL instructions
Thibaut Girka <thib@sitedethib.com>
parents: 56
diff changeset
139 self.interpolator.set_interpolation_start(self.frame, (self.x, self.y))
62
1f591adcea04 Fix animation determination (ins_98 stuff) and some interpolation functions
Thibaut Girka <thib@sitedethib.com>
parents: 57
diff changeset
140 self.interpolator.set_interpolation_end(self.frame + duration - 1, (x, y))
23
444ac7bca7bc Refacto ECL stuff, add support for a few instructions, and add some culling
Thibaut Girka <thib@sitedethib.com>
parents: 22
diff changeset
141
75
b3bd421bb895 Handle a few more ECL instructions
Thibaut Girka <thib@sitedethib.com>
parents: 73
diff changeset
142 self.speed = 0.
b3bd421bb895 Handle a few more ECL instructions
Thibaut Girka <thib@sitedethib.com>
parents: 73
diff changeset
143 self.angle = atan2(y - self.y, x - self.x)
23
444ac7bca7bc Refacto ECL stuff, add support for a few instructions, and add some culling
Thibaut Girka <thib@sitedethib.com>
parents: 22
diff changeset
144
75
b3bd421bb895 Handle a few more ECL instructions
Thibaut Girka <thib@sitedethib.com>
parents: 73
diff changeset
145
b3bd421bb895 Handle a few more ECL instructions
Thibaut Girka <thib@sitedethib.com>
parents: 73
diff changeset
146 def stop_in(self, duration, formula):
57
694f25881d0f Fix move_to interpolation, add support for a few ANM and ECL instructions
Thibaut Girka <thib@sitedethib.com>
parents: 56
diff changeset
147 if not self.speed_interpolator:
75
b3bd421bb895 Handle a few more ECL instructions
Thibaut Girka <thib@sitedethib.com>
parents: 73
diff changeset
148 self.speed_interpolator = Interpolator((self.speed,), formula)
57
694f25881d0f Fix move_to interpolation, add support for a few ANM and ECL instructions
Thibaut Girka <thib@sitedethib.com>
parents: 56
diff changeset
149 self.speed_interpolator.set_interpolation_start(self.frame, (self.speed,))
85
3804f07d3b0e Various optimizations
Thibaut Girka <thib@sitedethib.com>
parents: 83
diff changeset
150 self.speed_interpolator.set_interpolation_end(self.frame + duration - 1, (0.,))
57
694f25881d0f Fix move_to interpolation, add support for a few ANM and ECL instructions
Thibaut Girka <thib@sitedethib.com>
parents: 56
diff changeset
151
75
b3bd421bb895 Handle a few more ECL instructions
Thibaut Girka <thib@sitedethib.com>
parents: 73
diff changeset
152 self.speed = 0.
b3bd421bb895 Handle a few more ECL instructions
Thibaut Girka <thib@sitedethib.com>
parents: 73
diff changeset
153
57
694f25881d0f Fix move_to interpolation, add support for a few ANM and ECL instructions
Thibaut Girka <thib@sitedethib.com>
parents: 56
diff changeset
154
23
444ac7bca7bc Refacto ECL stuff, add support for a few instructions, and add some culling
Thibaut Girka <thib@sitedethib.com>
parents: 22
diff changeset
155 def is_visible(self, screen_width, screen_height):
73
e4af16a019d3 Do not remove enemies as soon as they lose their animation (Daiyousei...)
Thibaut Girka <thib@sitedethib.com>
parents: 72
diff changeset
156 if self._sprite:
e4af16a019d3 Do not remove enemies as soon as they lose their animation (Daiyousei...)
Thibaut Girka <thib@sitedethib.com>
parents: 72
diff changeset
157 tx, ty, tw, th = self._sprite.texcoords
e4af16a019d3 Do not remove enemies as soon as they lose their animation (Daiyousei...)
Thibaut Girka <thib@sitedethib.com>
parents: 72
diff changeset
158 if self._sprite.corner_relative_placement:
e4af16a019d3 Do not remove enemies as soon as they lose their animation (Daiyousei...)
Thibaut Girka <thib@sitedethib.com>
parents: 72
diff changeset
159 raise Exception #TODO
e4af16a019d3 Do not remove enemies as soon as they lose their animation (Daiyousei...)
Thibaut Girka <thib@sitedethib.com>
parents: 72
diff changeset
160 else:
e4af16a019d3 Do not remove enemies as soon as they lose their animation (Daiyousei...)
Thibaut Girka <thib@sitedethib.com>
parents: 72
diff changeset
161 tx, ty, tw, th = 0., 0., 0., 0.
29
afa91be769ae Don't lose time updating off-screen enemies' sprites
Thibaut Girka <thib@sitedethib.com>
parents: 23
diff changeset
162
73
e4af16a019d3 Do not remove enemies as soon as they lose their animation (Daiyousei...)
Thibaut Girka <thib@sitedethib.com>
parents: 72
diff changeset
163 max_x = tw / 2.
e4af16a019d3 Do not remove enemies as soon as they lose their animation (Daiyousei...)
Thibaut Girka <thib@sitedethib.com>
parents: 72
diff changeset
164 max_y = th / 2.
e4af16a019d3 Do not remove enemies as soon as they lose their animation (Daiyousei...)
Thibaut Girka <thib@sitedethib.com>
parents: 72
diff changeset
165 min_x = -max_x
e4af16a019d3 Do not remove enemies as soon as they lose their animation (Daiyousei...)
Thibaut Girka <thib@sitedethib.com>
parents: 72
diff changeset
166 min_y = -max_y
29
afa91be769ae Don't lose time updating off-screen enemies' sprites
Thibaut Girka <thib@sitedethib.com>
parents: 23
diff changeset
167
56
299de3a9b69f Filter out off-screen enemies (the same way the official game does)
Thibaut Girka <thib@sitedethib.com>
parents: 52
diff changeset
168 if any((min_x > screen_width - self.x,
299de3a9b69f Filter out off-screen enemies (the same way the official game does)
Thibaut Girka <thib@sitedethib.com>
parents: 52
diff changeset
169 max_x < -self.x,
299de3a9b69f Filter out off-screen enemies (the same way the official game does)
Thibaut Girka <thib@sitedethib.com>
parents: 52
diff changeset
170 min_y > screen_height - self.y,
299de3a9b69f Filter out off-screen enemies (the same way the official game does)
Thibaut Girka <thib@sitedethib.com>
parents: 52
diff changeset
171 max_y < -self.y)):
23
444ac7bca7bc Refacto ECL stuff, add support for a few instructions, and add some culling
Thibaut Girka <thib@sitedethib.com>
parents: 22
diff changeset
172 return False
444ac7bca7bc Refacto ECL stuff, add support for a few instructions, and add some culling
Thibaut Girka <thib@sitedethib.com>
parents: 22
diff changeset
173 return True
444ac7bca7bc Refacto ECL stuff, add support for a few instructions, and add some culling
Thibaut Girka <thib@sitedethib.com>
parents: 22
diff changeset
174
18
ca26a84916cb Add preliminary ECL viewer/interpreter.
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
175
85
3804f07d3b0e Various optimizations
Thibaut Girka <thib@sitedethib.com>
parents: 83
diff changeset
176 def get_objects_by_texture(self, objects_by_texture):
83
fc0294c745b6 Basic bullet handling! Clean up as soon as possible :p
Thibaut Girka <thib@sitedethib.com>
parents: 79
diff changeset
177 for bullet in self.bullets:
85
3804f07d3b0e Various optimizations
Thibaut Girka <thib@sitedethib.com>
parents: 83
diff changeset
178 bullet.get_objects_by_texture(objects_by_texture)
83
fc0294c745b6 Basic bullet handling! Clean up as soon as possible :p
Thibaut Girka <thib@sitedethib.com>
parents: 79
diff changeset
179
73
e4af16a019d3 Do not remove enemies as soon as they lose their animation (Daiyousei...)
Thibaut Girka <thib@sitedethib.com>
parents: 72
diff changeset
180 if not self._sprite:
85
3804f07d3b0e Various optimizations
Thibaut Girka <thib@sitedethib.com>
parents: 83
diff changeset
181 return
73
e4af16a019d3 Do not remove enemies as soon as they lose their animation (Daiyousei...)
Thibaut Girka <thib@sitedethib.com>
parents: 72
diff changeset
182
94
ca571697ec83 Various minor optimisations and refactoring
Thibaut Girka <thib@sitedethib.com>
parents: 93
diff changeset
183 sprite = self._sprite
ca571697ec83 Various minor optimisations and refactoring
Thibaut Girka <thib@sitedethib.com>
parents: 93
diff changeset
184 sprite.update_vertices_uvs_colors()
90
630e9045e851 Minor refactoring
Thibaut Girka <thib@sitedethib.com>
parents: 85
diff changeset
185
94
ca571697ec83 Various minor optimisations and refactoring
Thibaut Girka <thib@sitedethib.com>
parents: 93
diff changeset
186 key = sprite.anm.first_name, sprite.anm.secondary_name
ca571697ec83 Various minor optimisations and refactoring
Thibaut Girka <thib@sitedethib.com>
parents: 93
diff changeset
187 key = (key, sprite.blendfunc)
ca571697ec83 Various minor optimisations and refactoring
Thibaut Girka <thib@sitedethib.com>
parents: 93
diff changeset
188 rec = objects_by_texture.setdefault(key, ([], [], []))
ca571697ec83 Various minor optimisations and refactoring
Thibaut Girka <thib@sitedethib.com>
parents: 93
diff changeset
189 vertices = ((x + self.x, y + self.y, z) for x, y, z in sprite._vertices)
ca571697ec83 Various minor optimisations and refactoring
Thibaut Girka <thib@sitedethib.com>
parents: 93
diff changeset
190 rec[0].extend(vertices)
ca571697ec83 Various minor optimisations and refactoring
Thibaut Girka <thib@sitedethib.com>
parents: 93
diff changeset
191 rec[1].extend(sprite._uvs)
ca571697ec83 Various minor optimisations and refactoring
Thibaut Girka <thib@sitedethib.com>
parents: 93
diff changeset
192 rec[2].extend(sprite._colors)
36
f46c18872796 Minor refactoring
Thibaut Girka <thib@sitedethib.com>
parents: 34
diff changeset
193
f46c18872796 Minor refactoring
Thibaut Girka <thib@sitedethib.com>
parents: 34
diff changeset
194
69
a142e57218a0 Refactor. Move VMs to pytouhou.vm.
Thibaut Girka <thib@sitedethib.com>
parents: 68
diff changeset
195 def update(self):
21
bf225780973f Small refactoring, and Rumia \o/
Thibaut Girka <thib@sitedethib.com>
parents: 20
diff changeset
196 x, y = self.x, self.y
57
694f25881d0f Fix move_to interpolation, add support for a few ANM and ECL instructions
Thibaut Girka <thib@sitedethib.com>
parents: 56
diff changeset
197 if self.interpolator:
694f25881d0f Fix move_to interpolation, add support for a few ANM and ECL instructions
Thibaut Girka <thib@sitedethib.com>
parents: 56
diff changeset
198 self.interpolator.update(self.frame)
20
6ebf9539c077 Handle more enemies types and movements
Thibaut Girka <thib@sitedethib.com>
parents: 18
diff changeset
199 x, y = self.interpolator.values
21
bf225780973f Small refactoring, and Rumia \o/
Thibaut Girka <thib@sitedethib.com>
parents: 20
diff changeset
200
18
ca26a84916cb Add preliminary ECL viewer/interpreter.
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
201 self.speed += self.acceleration #TODO: units? Execution order?
ca26a84916cb Add preliminary ECL viewer/interpreter.
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
202 self.angle += self.rotation_speed #TODO: units? Execution order?
ca26a84916cb Add preliminary ECL viewer/interpreter.
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
203
57
694f25881d0f Fix move_to interpolation, add support for a few ANM and ECL instructions
Thibaut Girka <thib@sitedethib.com>
parents: 56
diff changeset
204
694f25881d0f Fix move_to interpolation, add support for a few ANM and ECL instructions
Thibaut Girka <thib@sitedethib.com>
parents: 56
diff changeset
205 if self.speed_interpolator:
694f25881d0f Fix move_to interpolation, add support for a few ANM and ECL instructions
Thibaut Girka <thib@sitedethib.com>
parents: 56
diff changeset
206 self.speed_interpolator.update(self.frame)
694f25881d0f Fix move_to interpolation, add support for a few ANM and ECL instructions
Thibaut Girka <thib@sitedethib.com>
parents: 56
diff changeset
207 self.speed, = self.speed_interpolator.values
694f25881d0f Fix move_to interpolation, add support for a few ANM and ECL instructions
Thibaut Girka <thib@sitedethib.com>
parents: 56
diff changeset
208
694f25881d0f Fix move_to interpolation, add support for a few ANM and ECL instructions
Thibaut Girka <thib@sitedethib.com>
parents: 56
diff changeset
209
18
ca26a84916cb Add preliminary ECL viewer/interpreter.
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
210 dx, dy = cos(self.angle) * self.speed, sin(self.angle) * self.speed
49
cbe1cb50f2fd Refactor ECLRunner/EnemyManager so that all VM stuff goes to ECLRunner
Thibaut Girka <thib@sitedethib.com>
parents: 48
diff changeset
211 if self._type & 2:
21
bf225780973f Small refactoring, and Rumia \o/
Thibaut Girka <thib@sitedethib.com>
parents: 20
diff changeset
212 x -= dx
18
ca26a84916cb Add preliminary ECL viewer/interpreter.
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
213 else:
21
bf225780973f Small refactoring, and Rumia \o/
Thibaut Girka <thib@sitedethib.com>
parents: 20
diff changeset
214 x += dx
bf225780973f Small refactoring, and Rumia \o/
Thibaut Girka <thib@sitedethib.com>
parents: 20
diff changeset
215 y += dy
bf225780973f Small refactoring, and Rumia \o/
Thibaut Girka <thib@sitedethib.com>
parents: 20
diff changeset
216
bf225780973f Small refactoring, and Rumia \o/
Thibaut Girka <thib@sitedethib.com>
parents: 20
diff changeset
217 if self.movement_dependant_sprites:
57
694f25881d0f Fix move_to interpolation, add support for a few ANM and ECL instructions
Thibaut Girka <thib@sitedethib.com>
parents: 56
diff changeset
218 #TODO: is that really how it works? Almost.
694f25881d0f Fix move_to interpolation, add support for a few ANM and ECL instructions
Thibaut Girka <thib@sitedethib.com>
parents: 56
diff changeset
219 # Sprite determination is done only once per changement, and is
694f25881d0f Fix move_to interpolation, add support for a few ANM and ECL instructions
Thibaut Girka <thib@sitedethib.com>
parents: 56
diff changeset
220 # superseeded by ins_97.
62
1f591adcea04 Fix animation determination (ins_98 stuff) and some interpolation functions
Thibaut Girka <thib@sitedethib.com>
parents: 57
diff changeset
221 end_left, end_right, left, right = self.movement_dependant_sprites
1f591adcea04 Fix animation determination (ins_98 stuff) and some interpolation functions
Thibaut Girka <thib@sitedethib.com>
parents: 57
diff changeset
222 if x < self.x and self.direction != -1:
1f591adcea04 Fix animation determination (ins_98 stuff) and some interpolation functions
Thibaut Girka <thib@sitedethib.com>
parents: 57
diff changeset
223 self.set_anim(left)
22
fa87db09fc3a Fix Rumia's animations
Thibaut Girka <thib@sitedethib.com>
parents: 21
diff changeset
224 self.direction = -1
62
1f591adcea04 Fix animation determination (ins_98 stuff) and some interpolation functions
Thibaut Girka <thib@sitedethib.com>
parents: 57
diff changeset
225 elif x > self.x and self.direction != +1:
1f591adcea04 Fix animation determination (ins_98 stuff) and some interpolation functions
Thibaut Girka <thib@sitedethib.com>
parents: 57
diff changeset
226 self.set_anim(right)
22
fa87db09fc3a Fix Rumia's animations
Thibaut Girka <thib@sitedethib.com>
parents: 21
diff changeset
227 self.direction = +1
62
1f591adcea04 Fix animation determination (ins_98 stuff) and some interpolation functions
Thibaut Girka <thib@sitedethib.com>
parents: 57
diff changeset
228 elif x == self.x and self.direction is not None:
1f591adcea04 Fix animation determination (ins_98 stuff) and some interpolation functions
Thibaut Girka <thib@sitedethib.com>
parents: 57
diff changeset
229 self.set_anim({-1: end_left, +1: end_right}[self.direction])
22
fa87db09fc3a Fix Rumia's animations
Thibaut Girka <thib@sitedethib.com>
parents: 21
diff changeset
230 self.direction = None
21
bf225780973f Small refactoring, and Rumia \o/
Thibaut Girka <thib@sitedethib.com>
parents: 20
diff changeset
231
50
811cefefb5c8 Fix a few bugs and add support for a few instructions
Thibaut Girka <thib@sitedethib.com>
parents: 49
diff changeset
232
811cefefb5c8 Fix a few bugs and add support for a few instructions
Thibaut Girka <thib@sitedethib.com>
parents: 49
diff changeset
233 if self.screen_box:
811cefefb5c8 Fix a few bugs and add support for a few instructions
Thibaut Girka <thib@sitedethib.com>
parents: 49
diff changeset
234 xmin, ymin, xmax, ymax = self.screen_box
811cefefb5c8 Fix a few bugs and add support for a few instructions
Thibaut Girka <thib@sitedethib.com>
parents: 49
diff changeset
235 x = max(xmin, min(x, xmax))
811cefefb5c8 Fix a few bugs and add support for a few instructions
Thibaut Girka <thib@sitedethib.com>
parents: 49
diff changeset
236 y = max(ymin, min(y, ymax))
811cefefb5c8 Fix a few bugs and add support for a few instructions
Thibaut Girka <thib@sitedethib.com>
parents: 49
diff changeset
237
811cefefb5c8 Fix a few bugs and add support for a few instructions
Thibaut Girka <thib@sitedethib.com>
parents: 49
diff changeset
238
21
bf225780973f Small refactoring, and Rumia \o/
Thibaut Girka <thib@sitedethib.com>
parents: 20
diff changeset
239 self.x, self.y = x, y
69
a142e57218a0 Refactor. Move VMs to pytouhou.vm.
Thibaut Girka <thib@sitedethib.com>
parents: 68
diff changeset
240
a142e57218a0 Refactor. Move VMs to pytouhou.vm.
Thibaut Girka <thib@sitedethib.com>
parents: 68
diff changeset
241 #TODO
a142e57218a0 Refactor. Move VMs to pytouhou.vm.
Thibaut Girka <thib@sitedethib.com>
parents: 68
diff changeset
242 if self._anmrunner and not self._anmrunner.run_frame():
a142e57218a0 Refactor. Move VMs to pytouhou.vm.
Thibaut Girka <thib@sitedethib.com>
parents: 68
diff changeset
243 self._anmrunner = None
a142e57218a0 Refactor. Move VMs to pytouhou.vm.
Thibaut Girka <thib@sitedethib.com>
parents: 68
diff changeset
244
49
cbe1cb50f2fd Refactor ECLRunner/EnemyManager so that all VM stuff goes to ECLRunner
Thibaut Girka <thib@sitedethib.com>
parents: 48
diff changeset
245 if self._sprite:
69
a142e57218a0 Refactor. Move VMs to pytouhou.vm.
Thibaut Girka <thib@sitedethib.com>
parents: 68
diff changeset
246 if self._sprite._removed:
49
cbe1cb50f2fd Refactor ECLRunner/EnemyManager so that all VM stuff goes to ECLRunner
Thibaut Girka <thib@sitedethib.com>
parents: 48
diff changeset
247 self._sprite = None
69
a142e57218a0 Refactor. Move VMs to pytouhou.vm.
Thibaut Girka <thib@sitedethib.com>
parents: 68
diff changeset
248 else:
90
630e9045e851 Minor refactoring
Thibaut Girka <thib@sitedethib.com>
parents: 85
diff changeset
249 self._sprite.update(angle_base=self.angle,
630e9045e851 Minor refactoring
Thibaut Girka <thib@sitedethib.com>
parents: 85
diff changeset
250 force_rotation=self.automatic_orientation)
36
f46c18872796 Minor refactoring
Thibaut Girka <thib@sitedethib.com>
parents: 34
diff changeset
251
83
fc0294c745b6 Basic bullet handling! Clean up as soon as possible :p
Thibaut Girka <thib@sitedethib.com>
parents: 79
diff changeset
252
fc0294c745b6 Basic bullet handling! Clean up as soon as possible :p
Thibaut Girka <thib@sitedethib.com>
parents: 79
diff changeset
253 if self.bullet_launch_interval != 0:
fc0294c745b6 Basic bullet handling! Clean up as soon as possible :p
Thibaut Girka <thib@sitedethib.com>
parents: 79
diff changeset
254 self.bullet_launch_timer += 1
fc0294c745b6 Basic bullet handling! Clean up as soon as possible :p
Thibaut Girka <thib@sitedethib.com>
parents: 79
diff changeset
255 if self.bullet_launch_timer == self.bullet_launch_interval:
fc0294c745b6 Basic bullet handling! Clean up as soon as possible :p
Thibaut Girka <thib@sitedethib.com>
parents: 79
diff changeset
256 self.fire()
fc0294c745b6 Basic bullet handling! Clean up as soon as possible :p
Thibaut Girka <thib@sitedethib.com>
parents: 79
diff changeset
257
fc0294c745b6 Basic bullet handling! Clean up as soon as possible :p
Thibaut Girka <thib@sitedethib.com>
parents: 79
diff changeset
258
fc0294c745b6 Basic bullet handling! Clean up as soon as possible :p
Thibaut Girka <thib@sitedethib.com>
parents: 79
diff changeset
259 for bullet in self.bullets:
fc0294c745b6 Basic bullet handling! Clean up as soon as possible :p
Thibaut Girka <thib@sitedethib.com>
parents: 79
diff changeset
260 bullet.update()
fc0294c745b6 Basic bullet handling! Clean up as soon as possible :p
Thibaut Girka <thib@sitedethib.com>
parents: 79
diff changeset
261
fc0294c745b6 Basic bullet handling! Clean up as soon as possible :p
Thibaut Girka <thib@sitedethib.com>
parents: 79
diff changeset
262
18
ca26a84916cb Add preliminary ECL viewer/interpreter.
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
263 self.frame += 1
ca26a84916cb Add preliminary ECL viewer/interpreter.
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
264
ca26a84916cb Add preliminary ECL viewer/interpreter.
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
265
ca26a84916cb Add preliminary ECL viewer/interpreter.
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
266
ca26a84916cb Add preliminary ECL viewer/interpreter.
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
267 class EnemyManager(object):
49
cbe1cb50f2fd Refactor ECLRunner/EnemyManager so that all VM stuff goes to ECLRunner
Thibaut Girka <thib@sitedethib.com>
parents: 48
diff changeset
268 def __init__(self, stage, anm_wrapper, ecl, game_state):
cbe1cb50f2fd Refactor ECLRunner/EnemyManager so that all VM stuff goes to ECLRunner
Thibaut Girka <thib@sitedethib.com>
parents: 48
diff changeset
269 self._game_state = game_state
18
ca26a84916cb Add preliminary ECL viewer/interpreter.
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
270 self.stage = stage
21
bf225780973f Small refactoring, and Rumia \o/
Thibaut Girka <thib@sitedethib.com>
parents: 20
diff changeset
271 self.anm_wrapper = anm_wrapper
18
ca26a84916cb Add preliminary ECL viewer/interpreter.
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
272 self.main = []
23
444ac7bca7bc Refacto ECL stuff, add support for a few instructions, and add some culling
Thibaut Girka <thib@sitedethib.com>
parents: 22
diff changeset
273 self.ecl = ecl
18
ca26a84916cb Add preliminary ECL viewer/interpreter.
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
274 self.enemies = []
49
cbe1cb50f2fd Refactor ECLRunner/EnemyManager so that all VM stuff goes to ECLRunner
Thibaut Girka <thib@sitedethib.com>
parents: 48
diff changeset
275 self.processes = []
83
fc0294c745b6 Basic bullet handling! Clean up as soon as possible :p
Thibaut Girka <thib@sitedethib.com>
parents: 79
diff changeset
276 self.bullets = []
18
ca26a84916cb Add preliminary ECL viewer/interpreter.
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
277
ca26a84916cb Add preliminary ECL viewer/interpreter.
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
278 # Populate main
ca26a84916cb Add preliminary ECL viewer/interpreter.
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
279 for frame, sub, instr_type, args in ecl.main:
ca26a84916cb Add preliminary ECL viewer/interpreter.
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
280 if not self.main or self.main[-1][0] < frame:
ca26a84916cb Add preliminary ECL viewer/interpreter.
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
281 self.main.append((frame, [(sub, instr_type, args)]))
ca26a84916cb Add preliminary ECL viewer/interpreter.
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
282 elif self.main[-1][0] == frame:
ca26a84916cb Add preliminary ECL viewer/interpreter.
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
283 self.main[-1][1].append((sub, instr_type, args))
ca26a84916cb Add preliminary ECL viewer/interpreter.
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
284
ca26a84916cb Add preliminary ECL viewer/interpreter.
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
285
85
3804f07d3b0e Various optimizations
Thibaut Girka <thib@sitedethib.com>
parents: 83
diff changeset
286 def get_objects_by_texture(self, objects_by_texture):
3804f07d3b0e Various optimizations
Thibaut Girka <thib@sitedethib.com>
parents: 83
diff changeset
287 # Add enemies to vertices/uvs
3804f07d3b0e Various optimizations
Thibaut Girka <thib@sitedethib.com>
parents: 83
diff changeset
288 for enemy in self.enemies:
3804f07d3b0e Various optimizations
Thibaut Girka <thib@sitedethib.com>
parents: 83
diff changeset
289 enemy.get_objects_by_texture(objects_by_texture)
3804f07d3b0e Various optimizations
Thibaut Girka <thib@sitedethib.com>
parents: 83
diff changeset
290
3804f07d3b0e Various optimizations
Thibaut Girka <thib@sitedethib.com>
parents: 83
diff changeset
291 # Add bullets to vertices/uvs
3804f07d3b0e Various optimizations
Thibaut Girka <thib@sitedethib.com>
parents: 83
diff changeset
292 for bullet in self.bullets:
3804f07d3b0e Various optimizations
Thibaut Girka <thib@sitedethib.com>
parents: 83
diff changeset
293 bullet.get_objects_by_texture(objects_by_texture)
3804f07d3b0e Various optimizations
Thibaut Girka <thib@sitedethib.com>
parents: 83
diff changeset
294
3804f07d3b0e Various optimizations
Thibaut Girka <thib@sitedethib.com>
parents: 83
diff changeset
295
18
ca26a84916cb Add preliminary ECL viewer/interpreter.
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
296 def update(self, frame):
ca26a84916cb Add preliminary ECL viewer/interpreter.
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
297 if self.main and self.main[0][0] == frame:
ca26a84916cb Add preliminary ECL viewer/interpreter.
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
298 for sub, instr_type, args in self.main.pop(0)[1]:
93
d167280a82fc Handle timeout callbacks, and clean up unneeded things
Thibaut Girka <thib@sitedethib.com>
parents: 90
diff changeset
299 if instr_type in (0, 2, 4, 6) and not self._game_state.boss:
18
ca26a84916cb Add preliminary ECL viewer/interpreter.
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
300 x, y, z, life, unknown1, unknown2, unknown3 = args
40
ce662b372ee0 HANDLE RANDOM PLACEMENT (main flag 4) with results identical to 102h.exe \o/ \o/ \o/
Thibaut Girka <thib@sitedethib.com>
parents: 37
diff changeset
301 if instr_type & 4:
50
811cefefb5c8 Fix a few bugs and add support for a few instructions
Thibaut Girka <thib@sitedethib.com>
parents: 49
diff changeset
302 if x < -990: #102h.exe@0x411820
811cefefb5c8 Fix a few bugs and add support for a few instructions
Thibaut Girka <thib@sitedethib.com>
parents: 49
diff changeset
303 x = self._game_state.prng.rand_double() * 368
811cefefb5c8 Fix a few bugs and add support for a few instructions
Thibaut Girka <thib@sitedethib.com>
parents: 49
diff changeset
304 if y < -990: #102h.exe@0x41184b
811cefefb5c8 Fix a few bugs and add support for a few instructions
Thibaut Girka <thib@sitedethib.com>
parents: 49
diff changeset
305 y = self._game_state.prng.rand_double() * 416
811cefefb5c8 Fix a few bugs and add support for a few instructions
Thibaut Girka <thib@sitedethib.com>
parents: 49
diff changeset
306 if z < -990: #102h.exe@0x411881
811cefefb5c8 Fix a few bugs and add support for a few instructions
Thibaut Girka <thib@sitedethib.com>
parents: 49
diff changeset
307 y = self._game_state.prng.rand_double() * 800
79
ffe2c2b9912c Handle a few more ECL instructions. Prepare for bullet handling \o/
Thibaut Girka <thib@sitedethib.com>
parents: 75
diff changeset
308 enemy = Enemy((x, y), life, instr_type, self.anm_wrapper, self._game_state)
49
cbe1cb50f2fd Refactor ECLRunner/EnemyManager so that all VM stuff goes to ECLRunner
Thibaut Girka <thib@sitedethib.com>
parents: 48
diff changeset
309 self.enemies.append(enemy)
cbe1cb50f2fd Refactor ECLRunner/EnemyManager so that all VM stuff goes to ECLRunner
Thibaut Girka <thib@sitedethib.com>
parents: 48
diff changeset
310 self.processes.append(ECLRunner(self.ecl, sub, enemy, self._game_state))
cbe1cb50f2fd Refactor ECLRunner/EnemyManager so that all VM stuff goes to ECLRunner
Thibaut Girka <thib@sitedethib.com>
parents: 48
diff changeset
311
23
444ac7bca7bc Refacto ECL stuff, add support for a few instructions, and add some culling
Thibaut Girka <thib@sitedethib.com>
parents: 22
diff changeset
312
49
cbe1cb50f2fd Refactor ECLRunner/EnemyManager so that all VM stuff goes to ECLRunner
Thibaut Girka <thib@sitedethib.com>
parents: 48
diff changeset
313 # Run processes
cbe1cb50f2fd Refactor ECLRunner/EnemyManager so that all VM stuff goes to ECLRunner
Thibaut Girka <thib@sitedethib.com>
parents: 48
diff changeset
314 self.processes[:] = (process for process in self.processes if process.run_iteration())
cbe1cb50f2fd Refactor ECLRunner/EnemyManager so that all VM stuff goes to ECLRunner
Thibaut Girka <thib@sitedethib.com>
parents: 48
diff changeset
315
cbe1cb50f2fd Refactor ECLRunner/EnemyManager so that all VM stuff goes to ECLRunner
Thibaut Girka <thib@sitedethib.com>
parents: 48
diff changeset
316 # Filter of destroyed enemies
cbe1cb50f2fd Refactor ECLRunner/EnemyManager so that all VM stuff goes to ECLRunner
Thibaut Girka <thib@sitedethib.com>
parents: 48
diff changeset
317 self.enemies[:] = (enemy for enemy in self.enemies if not enemy._removed)
18
ca26a84916cb Add preliminary ECL viewer/interpreter.
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
318
ca26a84916cb Add preliminary ECL viewer/interpreter.
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
319 # Update enemies
69
a142e57218a0 Refactor. Move VMs to pytouhou.vm.
Thibaut Girka <thib@sitedethib.com>
parents: 68
diff changeset
320 for enemy in self.enemies:
a142e57218a0 Refactor. Move VMs to pytouhou.vm.
Thibaut Girka <thib@sitedethib.com>
parents: 68
diff changeset
321 enemy.update()
94
ca571697ec83 Various minor optimisations and refactoring
Thibaut Girka <thib@sitedethib.com>
parents: 93
diff changeset
322 for bullet in tuple(enemy.bullets):
83
fc0294c745b6 Basic bullet handling! Clean up as soon as possible :p
Thibaut Girka <thib@sitedethib.com>
parents: 79
diff changeset
323 if bullet._launched:
fc0294c745b6 Basic bullet handling! Clean up as soon as possible :p
Thibaut Girka <thib@sitedethib.com>
parents: 79
diff changeset
324 enemy.bullets.remove(bullet)
fc0294c745b6 Basic bullet handling! Clean up as soon as possible :p
Thibaut Girka <thib@sitedethib.com>
parents: 79
diff changeset
325 self.bullets.append(bullet)
fc0294c745b6 Basic bullet handling! Clean up as soon as possible :p
Thibaut Girka <thib@sitedethib.com>
parents: 79
diff changeset
326
fc0294c745b6 Basic bullet handling! Clean up as soon as possible :p
Thibaut Girka <thib@sitedethib.com>
parents: 79
diff changeset
327 # Update bullets
fc0294c745b6 Basic bullet handling! Clean up as soon as possible :p
Thibaut Girka <thib@sitedethib.com>
parents: 79
diff changeset
328 for bullet in self.bullets:
fc0294c745b6 Basic bullet handling! Clean up as soon as possible :p
Thibaut Girka <thib@sitedethib.com>
parents: 79
diff changeset
329 bullet.update()
69
a142e57218a0 Refactor. Move VMs to pytouhou.vm.
Thibaut Girka <thib@sitedethib.com>
parents: 68
diff changeset
330
a142e57218a0 Refactor. Move VMs to pytouhou.vm.
Thibaut Girka <thib@sitedethib.com>
parents: 68
diff changeset
331 # Filter out non-visible enemies
a142e57218a0 Refactor. Move VMs to pytouhou.vm.
Thibaut Girka <thib@sitedethib.com>
parents: 68
diff changeset
332 visible_enemies = [enemy for enemy in self.enemies if enemy.is_visible(384, 448)] #TODO
56
299de3a9b69f Filter out off-screen enemies (the same way the official game does)
Thibaut Girka <thib@sitedethib.com>
parents: 52
diff changeset
333 for enemy in visible_enemies:
299de3a9b69f Filter out off-screen enemies (the same way the official game does)
Thibaut Girka <thib@sitedethib.com>
parents: 52
diff changeset
334 enemy._was_visible = True
299de3a9b69f Filter out off-screen enemies (the same way the official game does)
Thibaut Girka <thib@sitedethib.com>
parents: 52
diff changeset
335
299de3a9b69f Filter out off-screen enemies (the same way the official game does)
Thibaut Girka <thib@sitedethib.com>
parents: 52
diff changeset
336 # Filter out-of-screen enemies
299de3a9b69f Filter out off-screen enemies (the same way the official game does)
Thibaut Girka <thib@sitedethib.com>
parents: 52
diff changeset
337 for enemy in tuple(self.enemies):
299de3a9b69f Filter out off-screen enemies (the same way the official game does)
Thibaut Girka <thib@sitedethib.com>
parents: 52
diff changeset
338 if enemy._was_visible and not enemy in visible_enemies:
299de3a9b69f Filter out off-screen enemies (the same way the official game does)
Thibaut Girka <thib@sitedethib.com>
parents: 52
diff changeset
339 enemy._removed = True
299de3a9b69f Filter out off-screen enemies (the same way the official game does)
Thibaut Girka <thib@sitedethib.com>
parents: 52
diff changeset
340 self.enemies.remove(enemy)
18
ca26a84916cb Add preliminary ECL viewer/interpreter.
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
341
83
fc0294c745b6 Basic bullet handling! Clean up as soon as possible :p
Thibaut Girka <thib@sitedethib.com>
parents: 79
diff changeset
342 # Filter out-of-scren bullets
94
ca571697ec83 Various minor optimisations and refactoring
Thibaut Girka <thib@sitedethib.com>
parents: 93
diff changeset
343 for bullet in tuple(self.bullets):
83
fc0294c745b6 Basic bullet handling! Clean up as soon as possible :p
Thibaut Girka <thib@sitedethib.com>
parents: 79
diff changeset
344 if not bullet.is_visible(384, 448):
fc0294c745b6 Basic bullet handling! Clean up as soon as possible :p
Thibaut Girka <thib@sitedethib.com>
parents: 79
diff changeset
345 self.bullets.remove(bullet)
fc0294c745b6 Basic bullet handling! Clean up as soon as possible :p
Thibaut Girka <thib@sitedethib.com>
parents: 79
diff changeset
346
fc0294c745b6 Basic bullet handling! Clean up as soon as possible :p
Thibaut Girka <thib@sitedethib.com>
parents: 79
diff changeset
347
57
694f25881d0f Fix move_to interpolation, add support for a few ANM and ECL instructions
Thibaut Girka <thib@sitedethib.com>
parents: 56
diff changeset
348 #TODO: disable boss mode if it is dead/it has timeout
694f25881d0f Fix move_to interpolation, add support for a few ANM and ECL instructions
Thibaut Girka <thib@sitedethib.com>
parents: 56
diff changeset
349 if self._game_state.boss and self._game_state.boss._removed:
694f25881d0f Fix move_to interpolation, add support for a few ANM and ECL instructions
Thibaut Girka <thib@sitedethib.com>
parents: 56
diff changeset
350 self._game_state.boss = None
694f25881d0f Fix move_to interpolation, add support for a few ANM and ECL instructions
Thibaut Girka <thib@sitedethib.com>
parents: 56
diff changeset
351