-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathplayers.py
348 lines (280 loc) · 13.1 KB
/
players.py
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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
import global_values as g
import graphics as gfx
import utilities as util
import creatures
import particles
import items
import actions
import sounds
import pygame as p
import math as m
class Inventory:
"""
Class for storing player inventory items
"""
def __init__(self, slots):
self.slots = [None for slot in range(slots)]
self.selected_item = None
self.selected_index = None
#a way for ammunition to be collected before a gun is picked up
self.ammunition_reserves = {}
def add_item(self, item):
"""
Attempt to add an item to inventory
"""
#add to existing slot:
for i,slot in enumerate(self.slots):
if slot and slot.name == item.name:
slot.amount += 1
return True
#add to new slot
for i,slot in enumerate(self.slots):
if not slot:
self.slots[i] = item
if isinstance(item, items.Gun):
item.change_ammunition(self.ammunition_reserves.get(item.name, 0))
self.ammunition_reserves[item.name] = 0
if not self.selected_item and not isinstance(item, items.Consumable):
self.select_index(i)
return True
return False
def check_for_named_item(self, name):
"""
Attempt to find a named item within the inventory
Returns the item if it exists, False otherwise
"""
for slot in self.slots:
if slot and slot.name == name:
return slot
return False
def select_index(self, index):
"""
Select an item at an index
"""
if index is not None and 0 <= index < len(self.slots):
old_index = self.selected_index
self.selected_index = index
self.selected_item = self.slots[self.selected_index]
if isinstance(self.selected_item, items.Consumable):
self.selected_item.consume()
self.remove_index(self.selected_index)
self.select_index(old_index)
sounds.play_sound("inventory_select")
return self.selected_item
else:
return None
def remove_index(self, index, full=False):
"""
Delete an item at an index
"""
item = self.slots[index]
if item.amount > 1 and not full:
item.amount -= 1
else:
self.slots[index] = None
if not self.slots[index] and index == self.selected_index:
self.selected_item = None
self.selected_index = None
def remove(self, item):
"""
Delete an item
"""
for i, slot in enumerate(self.slots):
if slot == item:
self.remove_index(i)
def clear(self):
"""
Clear inventory
"""
for slot in self.slots:
if slot:
self.remove(slot)
class Player(creatures.Creature):
def __init__(self):
self.target_x = None
rect = p.Rect(0,32,16,32)
super().__init__(rect, None, "player", solid=True)
self.gfx = g.spritesheets[f"{self.name}_ss"].create_animation_system({"static":0, "moving":1, "hurt_static":2, "hurt_move":3}, 0.25)
self.z_index = 1
self.collision_dict = {"class_Entity":False, "class_LargeEnemy":False}
self.speed = 42 # units per second
self.inventory = Inventory(8)
#handgun = items.Gun("handgun", self, 1, 0.5, 200)
#shotgun = items.Gun("shotgun", self, 0.8, 1, 100, projectiles=5, spread=0.5, max_ammunition=25, recharge=0)
#stungun = items.Gun("stungun", self, 0.0, 1.5, 64, projectiles=1, spread=0, max_ammunition=3, recharge=0.4, stun=5, fire_effect=2)
#revolver = items.Gun("revolver", self, 5, 1.5, 300, projectiles=1, spread=0, max_ammunition=15)
self.angle = 0
self.respawn_time = None
self.arm = gfx.load_image("player_arm", alpha=True)
self.hand = gfx.load_image("player_hand", alpha=True)
self.smoke_effect = None
self.flash_effect = None
self.punching = False
self.punch_damage = 0.5
self.control_locks = 0
self.hurt = 0
self.fully_dead = False
def debug_set_inventory(self):
"""
Debug method for setting up player inventory
"""
self.inventory.add_item(items.Handgun())
self.inventory.add_item(items.Shotgun())
self.inventory.add_item(items.Stungun())
self.inventory.add_item(items.Revolver())
self.inventory.select_index(0)
def set_target_x(self, x):
"""
Set the point the player tries to walk through
"""
if x is not None:
self.target_x = x-(self.rect.w/2)
else:
self.target_x = None
def set_animation(self):
if self.health < self.max_health/2:
if self.change_x:
self.gfx.set_anim("hurt_move")
else:
self.gfx.set_anim("hurt_static")
elif self.change_x:
self.gfx.set_anim("moving")
else:
self.gfx.set_anim("static")
def update(self):
super().update()
#kind of a hack, for when the player is damaged through doors
if not self.dead and not self.health:
self.die()
if self.target_x is not None and abs(self.target_x - self.x) > 4:
result = self.move_towards(self.target_x, self.y, self.speed * g.dt)
if result:
self.target_x = None
elif abs(self.target_x - self.x) <= 4:
self.target_x = None
self.angle = util.get_angle(self.rect.centerx, self.rect.centery, g.tmx, g.tmy)
item = self.inventory.selected_item
#position arm
self.shoulder_pos = self.rect.move(0, -4).center
if item:
if True:#item.name == "handgun" or item.name == "stungun" or item.name == "revolver" or item.name == "shotgun"
if g.tmx > self.rect.centerx:
self.arm_angle = util.get_angle(self.shoulder_pos[0], self.shoulder_pos[1], g.tmx, g.tmy) + (m.pi/4)
self.elbow_angle = self.arm_angle - (m.pi/2)
else:
self.arm_angle = util.get_angle(self.shoulder_pos[0], self.shoulder_pos[1], g.tmx, g.tmy) - (m.pi/4)
self.elbow_angle = self.arm_angle + (m.pi/2)
else:
self.arm_angle = util.get_angle(self.shoulder_pos[0], self.shoulder_pos[1], g.tmx, g.tmy) + (m.pi/4)
self.elbow_angle = self.arm_angle - (m.pi/2)
else:
if self.punching:
progress = -m.cos(min(self.punching.progress*2,1) * 2 * m.pi)
self.arm_angle = util.get_angle(self.shoulder_pos[0], self.shoulder_pos[1], g.tmx, g.tmy)
self.elbow_angle = self.arm_angle
if g.tmx > self.rect.centerx:
self.arm_angle += (m.pi * 0.175)*(1-progress)
self.elbow_angle += (m.pi*0)*(progress)
else:
self.arm_angle -= (m.pi * 0.175)*(1-progress)
self.elbow_angle -= (m.pi*0)*(progress)
else:
if g.tmx > self.rect.centerx:
self.arm_angle = util.get_angle(self.shoulder_pos[0], self.shoulder_pos[1], g.tmx, g.tmy) + (m.pi/4)
self.elbow_angle = self.arm_angle - (m.pi*.25)
else:
self.arm_angle = util.get_angle(self.shoulder_pos[0], self.shoulder_pos[1], g.tmx, g.tmy) - (m.pi/4)
self.elbow_angle = self.arm_angle + (m.pi*0.25)
if item:
if isinstance(item, items.Gun) and item.recharge:
item.change_ammunition(item.recharge * g.dt)
#split_size = (m.pi*2/8)
#self.arm_angle = arm_angle - ( arm_angle % split_size)
def attack(self):
item = self.inventory.selected_item
if item:
if isinstance(item, items.Gun) and not g.elements.get("class_Popup", False):
res = item.attempt_fire()
#show effect
if res:
if item.fire_effect == 1:
self.smoke_effect = particles.create_smoke(self.level, (0,0))
self.flash_effect = particles.create_flash(self.level, (0,0), self.angle)
elif item.fire_effect == 2:
self.flash_effect = particles.create_stun_flash(self.level, (0,0), self.angle)
#recoil
recoil_magnitude = min(item.damage*item.projectiles, 4)
if g.tmx < self.rect.centerx:
self.move(recoil_magnitude, 0)
self.direction = "left"
else:
self.move(-recoil_magnitude, 0)
self.direction = "right"
else:
if not self.punching:
punch_time = 1
self.punching = actions.VarChangeAction(self.pipe, punch_time, self, "punching", False, change_type=2, blocking=False, blockable=False)
actions.FuncCallAction(self.pipe, punch_time/2 /2, self, "punch", change_type=1, blocking=False, blockable=False)
def punch(self):
"""
Trigger punch
"""
for entity in self.level.entities:
if isinstance(entity, creatures.Enemy):
dist = util.get_distance(self.rect.centerx, self.rect.centery, entity.rect.centerx, entity.rect.centery)
if dist <= (self.rect.w/2)+12:
entity.take_damage(self.punch_damage)
sounds.play_sound("player_punch", volume=0.5)
break
sounds.play_sound("player_punch_swoosh", volume=0.5)
def get_direction_for_rendering(self):
# player should always appear to face the mouse, regardless of their true direction.
return "right" if g.tmx > self.rect.centerx else "left"
def take_damage(self, amount):
if self.health:
super().take_damage(amount)
overlay_strength = min(96*amount, 255)
actions.OverlayAction(self.pipe, 1, g.colour_remaps["red"], fade_type=1, full_alpha=overlay_strength, blockable=False, blocking=False)
#self.hurt += 1
#actions.VarChangeAction(self.pipe, 1, self, "hurt", self.hurt-1, change_type=2, revert=False, blocking=False, blockable=False, force=False)
def die(self):
super().die()
self.visible_override = False
timer = 1
actions.OverlayAction(self.pipe, timer, g.colour_remaps["darkred"], blocking=False, blockable=False)
actions.FuncCallAction(self.pipe, timer, self, "set_gameover", change_type=1, blocking=False, blockable=False)
g.player.control_locks += 1
creatures.Corpse(self, self.x, self.y, self.level)
def set_gameover(self):
"""
Actually end the game after player death
"""
self.fully_dead = True
def draw(self):
super().draw()
#arm
g.camera.draw_rotated_gfx(self.arm, self.arm_angle, self.shoulder_pos, ox=0, oy=0.5)
#hand
elbow_x = self.shoulder_pos[0] + m.cos(self.arm_angle)*self.arm.get_width()
elbow_y = self.shoulder_pos[1] + m.sin(self.arm_angle)*self.arm.get_width()
g.camera.draw_rotated_gfx(self.hand, self.elbow_angle, (elbow_x,elbow_y), ox=0, oy=0.5, yflip=g.tmx > self.rect.centerx)
#weapon
hand_x = elbow_x + m.cos(self.elbow_angle)*self.hand.get_width()
hand_y = elbow_y + m.sin(self.elbow_angle)*self.hand.get_width()
if self.inventory.selected_item:
if g.tmx > self.rect.centerx:
item_surf = p.transform.flip(self.inventory.selected_item.surface, False, True)
item_angle = self.arm_angle - (m.pi/4)
else:
item_surf = p.transform.flip(self.inventory.selected_item.surface, False, True)
item_angle = self.arm_angle + (m.pi/4)
g.camera.draw_rotated_gfx(item_surf, item_angle, (hand_x, hand_y), ox=0.5, oy=0.5, yflip=g.tmx > self.rect.centerx)
#weapon end
weapon_x = hand_x + m.cos(item_angle)*(item_surf.get_width()/2)
weapon_y = hand_y + m.sin(item_angle)*(item_surf.get_width()/2)
if self.smoke_effect and not self.smoke_effect.x and not self.smoke_effect.y:
self.smoke_effect.x = weapon_x
self.smoke_effect.y = weapon_y-2
if self.flash_effect and not self.flash_effect.x and not self.flash_effect.y:
self.flash_effect.x = weapon_x
self.flash_effect.y = weapon_y-2