Mercurial > touhou
annotate pytouhou/game/player.py @ 152:86807b8a63bd
Add collisions with enemies and items.
author | Emmanuel Gil Peyrot <linkmauve@linkmauve.fr> |
---|---|
date | Sun, 09 Oct 2011 15:32:43 -0700 |
parents | c7f0fd9d2145 |
children | ebfd328e700c |
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 |
130 | 16 from math import cos, sin, atan2, pi |
17 | |
18 from pytouhou.game.sprite import Sprite | |
19 from pytouhou.vm.anmrunner import ANMRunner | |
20 | |
21 | |
22 SQ2 = 2. ** 0.5 / 2. | |
23 | |
24 | |
25 class PlayerState(object): | |
26 def __init__(self, character=0, score=0, power=0, lives=0, bombs=0): | |
27 self.character = character # ReimuA/ReimuB/MarisaA/MarisaB/... | |
28 | |
29 self.score = score | |
30 self.lives = lives | |
31 self.bombs = bombs | |
32 self.power = power | |
33 | |
34 self.graze = 0 | |
35 self.points = 0 | |
36 | |
50
811cefefb5c8
Fix a few bugs and add support for a few instructions
Thibaut Girka <thib@sitedethib.com>
parents:
diff
changeset
|
37 self.x = 192.0 |
811cefefb5c8
Fix a few bugs and add support for a few instructions
Thibaut Girka <thib@sitedethib.com>
parents:
diff
changeset
|
38 self.y = 384.0 |
130 | 39 |
40 | |
41 class Player(object): | |
42 def __init__(self, state, character): | |
43 self._sprite = None | |
44 self._anmrunner = None | |
45 | |
142
c7f0fd9d2145
Simple collision detection
Thibaut Girka <thib@sitedethib.com>
parents:
130
diff
changeset
|
46 self.hitbox_half_size = character.hitbox_size / 2. |
c7f0fd9d2145
Simple collision detection
Thibaut Girka <thib@sitedethib.com>
parents:
130
diff
changeset
|
47 |
130 | 48 self.state = state |
49 self.character = character | |
50 self.anm_wrapper = character.anm_wrapper | |
51 self.direction = None | |
52 | |
53 self.set_anim(0) | |
54 | |
55 | |
56 @property | |
57 def x(self): | |
58 return self.state.x | |
59 | |
60 | |
61 @property | |
62 def y(self): | |
63 return self.state.y | |
64 | |
65 | |
66 def set_anim(self, index): | |
67 self._sprite = Sprite() | |
68 self._anmrunner = ANMRunner(self.anm_wrapper, index, self._sprite) | |
69 self._anmrunner.run_frame() | |
70 | |
71 | |
152
86807b8a63bd
Add collisions with enemies and items.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
142
diff
changeset
|
72 def die(self): |
86807b8a63bd
Add collisions with enemies and items.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
142
diff
changeset
|
73 self.state.lives -= 1 |
86807b8a63bd
Add collisions with enemies and items.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
142
diff
changeset
|
74 self.state.x = 192.0 |
86807b8a63bd
Add collisions with enemies and items.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
142
diff
changeset
|
75 self.state.y = 384.0 |
86807b8a63bd
Add collisions with enemies and items.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
142
diff
changeset
|
76 #TODO: animation |
86807b8a63bd
Add collisions with enemies and items.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
142
diff
changeset
|
77 #TODO: set invulnerability. |
86807b8a63bd
Add collisions with enemies and items.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
142
diff
changeset
|
78 |
86807b8a63bd
Add collisions with enemies and items.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
142
diff
changeset
|
79 |
130 | 80 def update(self, keystate): |
81 try: | |
82 dx, dy = {16: (0.0, -1.0), 32: (0.0, 1.0), 64: (-1.0, 0.0), 128: (1.0, 0.0), | |
83 16|64: (-SQ2, -SQ2), 16|128: (SQ2, -SQ2), | |
84 32|64: (-SQ2, SQ2), 32|128: (SQ2, SQ2)}[keystate & (16|32|64|128)] | |
85 except KeyError: | |
86 speed = 0.0 | |
87 dx, dy = 0.0, 0.0 | |
88 else: | |
89 speed = self.character.focused_speed if keystate & 4 else self.character.speed | |
90 dx, dy = dx * speed, dy * speed | |
91 | |
92 if dx < 0 and self.direction != -1: | |
93 self.set_anim(1) | |
94 self.direction = -1 | |
95 elif dx > 0 and self.direction != +1: | |
96 self.set_anim(3) | |
97 self.direction = +1 | |
98 elif dx == 0 and self.direction is not None: | |
99 self.set_anim({-1: 2, +1: 4}[self.direction]) | |
100 self.direction = None | |
101 | |
102 self.state.x += dx | |
103 self.state.y += dy | |
104 | |
105 self._anmrunner.run_frame() | |
106 |