Repository navigation
Expand file tree
/
Copy pathentities.py
More file actions
197 lines (154 loc) · 5.71 KB
/
Copy pathentities.py
File metadata and controls
197 lines (154 loc) · 5.71 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
import os
import random
from functools import lru_cache
import pygame
from graphalama.colors import mix
import logging
from constants import LEVELS_GRAPHICAL_FOLDER, DEFAULT_BLOCK_SIZE, BROCHETTE_VELOCITY, FRAME_BEFORE_DESPAWN
from config import get_available_blocks
from helper import classproperty
from physics import AABB, Pos, Projectile, CollisionType
LOGGER = logging.getLogger(__name__)
SPAWN = "Spawn"
class Object:
_img = None # type: pygame.Surface
img = _img
type: str
pos: (int, int)
def __init__(self, pos=(0, 0), **kwargs):
self.type = self.__class__.__name__
self.pos = pos
def __str__(self):
return f"<{self.__class__}({self.pos})>"
@classmethod
def from_json(cls, d):
LOGGER.info("Loading object from %s", d)
type = d["type"]
return OBJECTS[type](**d)
def save(self):
LOGGER.info("Saving object %s to dict", self)
d = {
"type": self.type,
"pos": tuple(self.pos)
}
return d
class Spawn(Object):
@classproperty
def img(cls):
if cls._img is None:
LOGGER.info("Brochette - Setting sheet")
img = pygame.Surface((DEFAULT_BLOCK_SIZE, DEFAULT_BLOCK_SIZE))
img.set_colorkey((0, 0, 0))
# red circle
pygame.draw.circle(img, (255, 0, 0), (DEFAULT_BLOCK_SIZE // 2,) * 2,
(DEFAULT_BLOCK_SIZE // 4))
cls._img = img
return cls._img
class AK47(Object, Projectile):
character = "K"
deadly = False
rotation = None
def __init__(self, **kwargs):
Object.__init__(self, **kwargs)
shape = AABB(self.pos[0] * DEFAULT_BLOCK_SIZE, self.pos[1] * DEFAULT_BLOCK_SIZE,
*self.img.get_size())
Projectile.__init__(self, shape, mass=0)
@classproperty
def img(cls):
if cls._img is None:
LOGGER.info("AK47 - Setting sheet")
# size = (32, 10)
sheet = pygame.image.load(os.path.join(LEVELS_GRAPHICAL_FOLDER, "ak47.png")).convert()
sheet.set_colorkey((255, 0, 255))
# sheet = pygame.transform.scale(sheet, (DEFAULT_BLOCK_SIZE, DEFAULT_BLOCK_SIZE))
cls._img = sheet
return cls._img
@classmethod
@lru_cache(maxsize=2)
def get_img(cls, flipped=False):
if flipped:
return pygame.transform.flip(cls.img, True, False)
return cls.img
def render(self, surf, offset=(0, 0)):
surf.blit(self.img, self.topleft + offset)
class Brochette(Projectile):
deadly = True
_img = None
@classproperty
def img(cls):
if cls._img is None:
LOGGER.info("Brochette - Setting sheet")
cls._img = [
pygame.transform.scale(pygame.image.load(os.path.join(LEVELS_GRAPHICAL_FOLDER,
brochette.lower())).convert(),
(DEFAULT_BLOCK_SIZE, 14))
for brochette in get_available_blocks("brochette")]
return cls._img
@classmethod
@lru_cache()
def get_image(cls, rot, alpha, variant):
img = pygame.transform.rotate(variant, rot)
img.set_colorkey((255, 0, 255))
img.set_alpha(round(alpha))
return img
def __init__(self, start_pos, physics=(0, Pos(0, 0))):
if abs(physics[0]) == 90: # horizontal
hitbox = Pos(DEFAULT_BLOCK_SIZE - 2,
DEFAULT_BLOCK_SIZE * 14 / 32)
else: # vertical
hitbox = Pos(DEFAULT_BLOCK_SIZE * 14 / 32,
DEFAULT_BLOCK_SIZE - 2)
shape = AABB(start_pos - hitbox / 2, hitbox)
super().__init__(shape, mass=0)
self.velocity = physics[1] * BROCHETTE_VELOCITY
self.rotation = physics[0] - 90
self.variant = random.choice(self.img)
self.ttl = FRAME_BEFORE_DESPAWN
def render(self, surf, offset=(0, 0)):
image = self.get_image(self.rotation, 255 * self.ttl / FRAME_BEFORE_DESPAWN, self.variant)
surf.blit(image, self.shape.topleft + offset)
def internal_logic(self):
for colli in self.collisions:
if colli.type == CollisionType.BLOCK and not self.sleep:
self.sleep = True
if self.sleep:
self.ttl -= 1
if self.ttl <= 0:
self.dead = True
class Particle:
def __init__(self, pos, velocity=(0, 0), acceleration=(0, 1), friction=0.1, life_time: int=60, size=3,
color=(255, 165, 0), color_shift=(80, 00, 0)):
"""Create a particle with a constant acceleration"""
self.pos = Pos(pos)
self.velocity = Pos(velocity)
self.acceleration = Pos(acceleration)
self.friction = friction
self.life_time = life_time
self.age = 0
self.color = color
self.color_shift = color_shift
self.size = size
self.dead = False
def internal_logic(self):
self.velocity += self.acceleration - self.friction * self.velocity
self.pos += self.velocity
self.age += 1
if self.age > self.life_time:
self.dead = True
def render(self, surf: pygame.Surface):
life_prop = 1 - self.age / self.life_time
color = mix(self.color, self.color_shift, life_prop)
rect = (self.pos, (self.size, self.size))
surf.fill(color, rect)
# img = self.get_img_surf(self.size, color, 255 * life_prop)
# surf.blit(img, self.pos)
@lru_cache()
def get_img_surf(self, size, color, alpha):
img = pygame.Surface((size, size))
img.fill(color)
# img.set_alpha(round(alpha))
return img
OBJECTS = {
SPAWN: Spawn,
"AK47": AK47
}