summaryrefslogtreecommitdiffstats
path: root/Player.py
blob: 7789f50432727085794cdc4d0165d42c9acf90bc (plain)
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
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
import pygame.key

import Setup
from Setup import *
import time
from Actors import Actor
from datetime import datetime, timedelta
from Potion import Potion
from Item import PotionItem
from Spike import Spike
from Weapon import Sword
from Particle import Dust


class Player(Actor):
    friction = 0.2
    scale = 100
    factor = 640 / scale
    crop = pg.Rect(179, 169, 170, 401)
    idle_frames = [
        pg.transform.scale(pg.image.load(path.join("Assets/player/idle", file)).subsurface(pg.Rect(179, 169, 170, 401)),
                           (50, 100)) for file in listdir("Assets/player/idle")]

    attack_gif = Image.open("Assets/player/Sword_Slash.gif")
    attack_frames = []
    for i in range(attack_gif.n_frames):
        attack_frames.append(pg.transform.scale(pil_to_game(get_gif_frame(attack_gif, i)), (200, 200)))
    run_gif = Image.open("Assets/player/Running_animation.gif")
    run_frames = []
    for i in range(run_gif.n_frames):
        run_frames.append(pg.transform.scale(pil_to_game(get_gif_frame(run_gif, i)), (155, 155)))

    # Player SFX
    running_sound = pg.mixer.Sound("Assets/SFX/Running_Sound_Effect.wav")
    running_sound_channel = pg.mixer.Channel(1)
    sword_swing_sound = pg.mixer.Sound("Assets/SFX/Sword_Swing.wav")
    sword_swing_channel = pg.mixer.Channel(2)
    landing_sound = pg.mixer.Sound("Assets/SFX/Jump_Landing.wav")
    landing_sound_channel = pg.mixer.Channel(3)
    potion_drink_sound = pg.mixer.Sound("Assets/SFX/Potion Drink.wav")

    running_sound.set_volume(0.5)
    sword_swing_sound.set_volume(0.5)
    landing_sound.set_volume(0.5)
    potion_drink_sound.set_volume(0.25)
    # Enemy SFX
    grunt_sound = pg.mixer.Sound("Assets/SFX/Grunt Sound.wav")
    skeleton_damaged_sound = pg.mixer.Sound("Assets/SFX/Skeleton_Damaged.wav")
    player_grunt = pg.mixer.Sound("Assets/SFX/Player Grunt.wav")
    player_grunt_channel = pg.mixer.Channel(4)

    grunt_sound.set_volume(0.5)
    skeleton_damaged_sound.set_volume(0.5)
    player_grunt.set_volume(0.5)

    width, height = idle_frames[0].get_size()

    colour = (52, 94, 235)

    def __init__(self, pos, collision_layer, collision_mask, can_hurt, heals, spikes, arrows):
        super().__init__(pos, collision_layer, collision_mask)
        # self.initial_position = pg.Vector2(pos)
        # self.dashCooldown = timedelta(seconds=2, microseconds=500000)
        # self.timeBetweenDoublePress = timedelta(seconds=0, microseconds=500000)
        # self.dashSpeed = 5

        # self.dashPossible = False
        # self.lastDash = datetime.utcnow()
        # self.lastPressedLeft = datetime.utcnow()
        # self.lastValueL = False
        # self.lastPressedRight = datetime.utcnow()
        # self.lastValueR = False
        self.areas = {"head": Area(self.position, pg.Vector2(0, self.height / 2), self.width, self.height / 2, Actor),
                      "body": Area(self.position, (0, 0), self.width, self.height, PotionItem, self.add_potion),
                      "body2": Area(self.position, (0, 0), self.width, self.height, Spike, self.die)}

        self.spike_layer = spikes

        self.heal_layer = heals
        self.arrows = arrows

        self.potion_cooldown = 0
        self.starting_potions = 1
        self.potion_bag = [Potion(self)]
        for i in range(self.starting_potions - 1):
            self.potion_bag.append(Potion(self))  # use one liner

        self.weapon = Sword(self.position, (0, 0), self.width, -1)
        self.targets = can_hurt

        self.direction = -1

        self.state = "IDLE"
        self.previous_state = "IDLE"
        self.previous_ground_state = self.on_ground
        self.current_frame = 0
        self.display = self.idle_frames[0]
        self.display_offsets = {"weapon": pg.Vector2(0, 0), "player": pg.Vector2(0, 0)}

        self.buffer = []

        #easter egg stuff
        self.has_pet = False

    def die(self):
        self.dead = True

    def add_potion(self):
        if len(self.potion_bag) < 3:
            self.potion_bag.append(Potion(self))

    # def attack(self, enemy, weapon):
    #     super(Player, self).attack(enemy, weapon)
    #     self.current_frame = 0
    #     self.state = "ATTACK"
    #     print('a')

    def attack(self, enemy, weapon, direction):
        self.friction = 0.9
        self.player_grunt_channel.play(self.player_grunt)

        super().attack(enemy, weapon, direction)

    def update(self, delta):

        if self.attacked:
            self.friction = 0.9
        else:
            self.friction = 0.2

        super().update(delta)

        # Get and handle input
        self.handle_input()

        for i, heal in enumerate(self.heal_layer):
            if len(self.potion_bag) < 3 and self.areas["body"].is_colliding(heal):
                self.add_potion()
                self.heal_layer.remove(heal)
                break

        for i in self.spike_layer:
            if self.areas["body2"].is_colliding(i):
                self.die()

        if len(self.potion_bag) > 0:
            self.potion_bag[0].get_input(self)

        # Deals with collision and applying velocity
        self.position, self.velocity = self.move_and_collide(self.position.copy(), self.velocity.copy(), delta)

        prev_direction = self.direction
        # if self.velocity.x == 0:
        #     self.direction = 0
        # else:
        #     self.direction = math.copysign(1, self.velocity.x)
        self.direction = math.copysign(1, pg.mouse.get_pos()[0] - get_display_point(self.position).x)
        self.weapon.update(delta, self.position, self.direction)
        for arrow in self.arrows:
            if (not arrow.used) and (not arrow.in_ground) and (not self.attacked) and (self.invincibility_frames == 0) and self.get_collision_rect().colliderect(self.get_collision_rect()):
                self.modify_health(-1, "arrow")
                arrow.used = True
                self.attacked = True
                self.invincibility_frames = self.invincibility_time
        if self.velocity.x == 0 and self.state == "RUN":
            self.state = "IDLE"

        if self.on_ground == True and self.previous_ground_state == False:
            self.landing_sound_channel.play(self.landing_sound)

        if self.state == "IDLE":
            self.display_offsets["player"] = pg.Vector2(0, 0)

            frame = math.floor(self.current_frame)
            if self.direction == 1:
                self.display = self.idle_frames[math.floor(frame)]
            elif self.direction == -1:
                self.display = pg.transform.flip(self.idle_frames[math.floor(frame)], True, False)
            else:
                self.direction = prev_direction
                if prev_direction == 1:
                    self.display = self.idle_frames[math.floor(frame)]
                elif prev_direction == -1:
                    self.display = pg.transform.flip(self.idle_frames[math.floor(frame)], True, False)
            # 1 - 10 up, 11 down by 1, 12 - 18 down by 2, 19-20 down 1 FIX THIS
            frame += 1
            if 3 < frame < 12:
                self.display_offsets["weapon"] = pg.Vector2(3, -2)
            else:
                self.display_offsets["weapon"] = pg.Vector2(3, -5)
            self.current_frame = (self.current_frame + 0.1) % len(self.idle_frames)
        elif self.state == "ATTACK":
            frame = math.floor(self.current_frame)
            if self.direction == 1:
                self.display = self.attack_frames[math.floor(frame)]
                self.display_offsets["player"] = pg.Vector2(-50, -50)

            elif self.direction == -1:
                self.display = pg.transform.flip(self.attack_frames[math.floor(frame)], True, False)
                self.display_offsets["player"] = pg.Vector2(-100, -50)

            else:
                self.direction = prev_direction
                if prev_direction == 1:
                    self.display_offsets["player"] = pg.Vector2(-50, -50)

                    self.display = self.attack_frames[math.floor(frame)]
                elif prev_direction == -1:
                    self.display = pg.transform.flip(self.attack_frames[math.floor(frame)], True, False)
                    self.display_offsets["player"] = pg.Vector2(-100, -50)

            self.current_frame += 0.4
            if math.floor(self.current_frame) == self.attack_gif.n_frames:
                self.state = "IDLE"
        elif self.state == "RUN":
            frame = math.floor(self.current_frame)
            if self.velocity.x > 0:
                self.display = self.run_frames[math.floor(frame)]
                self.display_offsets["player"] = pg.Vector2(-40, -35)
            elif self.velocity.x < 0:
                self.display = pg.transform.flip(self.run_frames[math.floor(frame)], True, False)
                self.display_offsets["player"] = pg.Vector2(-65, -35)

            if frame % 2 == 0 and self.on_ground:
                Dust(pg.Vector2(self.get_collision_rect().midbottom) + pg.Vector2(
                    math.copysign(1, self.velocity.x) * -self.width / 2, -15), 16, self.direction)

            self.current_frame = (self.current_frame + 0.3) % self.run_gif.n_frames

        if self.state == "RUN":
            if self.previous_state != "RUN":
                self.running_sound_channel.play(self.running_sound, -1)

            if self.on_ground == True and self.previous_ground_state == False:
                self.running_sound_channel.play(self.running_sound, -1)

        self.previous_state = self.state
        self.previous_ground_state = self.on_ground
        return self.position - center

    def handle_input(self):
        if self.stun_time > 0:
            return

        pressed = pygame.key.get_pressed()
        if pressed[pg.K_w] or pressed[pg.K_UP] or pressed[pg.K_SPACE]:
            self.jump()
            self.running_sound_channel.stop()
        if pressed[pg.K_a] or pressed[pg.K_LEFT]:
            if self.state != "ATTACK":
                if self.state != "RUN":
                    self.current_frame = 0

                self.state = "RUN"
            self.move_left()
            # if (self.lastValueL == False):
            #     timeSincePressed = datetime.utcnow() - self.lastPressedLeft
            #     timeSinceLastDash = datetime.utcnow() - self.lastDash
            #     if (timeSinceLastDash >= self.dashCooldown):
            #         self.dashPossible = False # True to enable dash
            #     else:
            #         self.dashPossible = False
            #     if (timeSincePressed < self.timeBetweenDoublePress and self.dashPossible == True):
            #         self.move_left(
            #             self.dashSpeed)  # change this to change how the player dashes (maybe replace with a custom dash function)
            #         self.dashPossible = False
            #         self.lastDash = datetime.utcnow()
            #     self.lastPressedLeft = datetime.utcnow()

        elif pressed[pg.K_d] or pressed[pg.K_RIGHT]:
            if self.state != "ATTACK":
                if self.state != "RUN":
                    self.current_frame = 0

                self.state = "RUN"

            self.move_right()
            # if (self.lastValueR == False):
            #     timeSincePressed = datetime.utcnow() - self.lastPressedRight
            #     timeSinceLastDash = datetime.utcnow() - self.lastDash
            #     if (timeSinceLastDash >= self.dashCooldown):
            #         self.dashPossible = False # True to enable dash
            #     else:
            #         self.dashPossible = False
            #     if (timeSincePressed < self.timeBetweenDoublePress and self.dashPossible == True):
            #         self.lastDash = datetime.utcnow()
            #         self.dashPossible = False
            #         self.move_right(
            #             self.dashSpeed)  # change this to change how the player dashes (maybe replace with a custom dash function)
            #     self.lastPressedRight = datetime.utcnow()
        else:
            if self.state == "RUN":
                self.state = "IDLE"
            self.running_sound_channel.stop()

        # self.lastValueL = pressed[pg.K_a] or pressed[pg.K_LEFT]
        # self.lastValueR = pressed[pg.K_d] or pressed[pg.K_RIGHT]

        mouse_pressed = pg.mouse.get_pressed(3)
        if mouse_pressed[0]:  # LMB
            if self.state != "ATTACK":
                self.state = "ATTACK"
                self.current_frame = 0
                # self.sword_swing_channel.stop()
                self.sword_swing_channel.play(self.sword_swing_sound)
        if self.state == "ATTACK":
            if 12 > math.floor(self.current_frame) > 6:
                for mask in self.targets:
                    for enemy in mask:
                        # if not enemy.attacked and self.weapon.get_collision_rect().colliderect(
                        #         enemy.get_collision_rect()):

                        if not enemy.attacked and get_display_rect(self.weapon.get_collision_rect()).colliderect(
                                get_display_rect(enemy.get_collision_rect())):
                            enemy_type = str(type(enemy))
                            # print(enemy_type)
                            if enemy.health > 0:
                                if enemy_type == "<class 'Enemy.Enemy'>":
                                    pg.mixer.Sound.play(self.grunt_sound)
                                if enemy_type == "<class 'Enemy.Skeleton'>":
                                    pg.mixer.Sound.play(self.skeleton_damaged_sound)
                            if enemy_type == "<class 'PhysicsBody.PhysicsBody'>" and enemy.goon_skin == False:
                                    pg.mixer.Sound.play(self.skeleton_damaged_sound)
                            enemy.attack(self, self.weapon, self.direction)
                for arrow in self.arrows:
                    if not arrow.attacked and get_display_rect(self.weapon.get_collision_rect()).colliderect(
                            get_display_rect(arrow.get_collision_rect())):
                        arrow.attack(self, self.weapon, self.direction)

    def draw(self, surf):
        # super().draw(surf)
        if self.state == "IDLE":
            # self.weapon.draw(surf, self.display_offsets["weapon"])
            if self.direction == 1:
                surf.blit(pg.transform.flip(self.weapon.img, True, True),
                          get_display_rect(self.get_collision_rect()).topleft + pg.Vector2(-25, 55) +
                          self.display_offsets["weapon"])
            elif self.direction == -1:
                surf.blit(pg.transform.flip(self.weapon.img, False, True),
                          get_display_rect(self.get_collision_rect()).topleft + pg.Vector2(0, 55) +
                          self.display_offsets["weapon"])
        elif self.state == "RUN":
            # self.weapon.draw(surf, self.display_offsets["weapon"])
            if self.velocity.x > 0:
                surf.blit(pg.transform.flip(self.weapon.img2, True, True),
                          get_display_rect(self.get_collision_rect()).topleft + pg.Vector2(-25, 25) +
                          self.display_offsets["weapon"])
            else:
                surf.blit(pg.transform.flip(self.weapon.img2, False, True),
                          get_display_rect(self.get_collision_rect()).topleft + pg.Vector2(0, 25) +
                          self.display_offsets["weapon"])

        a = get_display_rect(self.get_collision_rect())
        b = a.topleft + self.display_offsets["player"]
        if pg.Rect(b, a.size).colliderect(screen_rect):
            surf.blit(self.display, b)
        #
        # for b in self.buffer:
        #     pg.draw.rect(surf,(0,0,255),get_display_rect(b),3)
        # self.buffer.append(self.get_collision_rect())

        # print(self, self.position)
        # super().draw(surf)
        # print(self.position, self.velocity, get_display_rect(self.get_collision_rect()).topleft, Setup.camera_offset)
        # pg.draw.rect(surf, self.colour, get_display_rect(self.get_collision_rect()), border_radius=8)
        # pg.draw.rect(surf, (255, 0, 0), get_display_rect(self.weapon.get_collision_rect()), width=3)

        # pg.draw.rect(surf, (0, 255, 0), get_display_rect(self.get_collision_rect()), 2)

        # pg.draw.rect(surf,self.colour,pg.Rect(center, (self.width, self.height)),border_radius=8)

        # # Healthbar Stuff
        # # bar is made of 2 rectanges, background which is just a simple rectange and foreground which goes on top and has a bit of math involved
        # background_rect = pg.Rect(20, 20, 1080 * 0.2, 640 * 0.08)

        # # idea is that 1080*0.185 = size of bar at 100% hp, at lower hp you want to get a fraction of that which is why we multiply by (health*0.01) example: 70 hp * 0.01 = 0.7
        # foreground_rect = pg.Rect(0, 0, 1080 * 0.185 * (self.health * 0.01), 640 * 0.06)
        # # make sure the red part health bar always sits on the left
        # # sets bar to center of background bar, then subtracts 1/2 of blank space to put it on the left
        # foreground_rect.center = (
        #     background_rect.centerx - 1080 * 0.185 * ((1 - self.health * 0.01) / 2), background_rect.centery)
        # pg.draw.rect(surf, (54, 54, 54), background_rect)
        # pg.draw.rect(surf, red, foreground_rect)

        # # text
        # current_health_display = createText(0, 0, 30, white, "Regular", str(self.health) + "/100")[0]
        # text_rect = current_health_display.get_rect(center=background_rect.center)
        # surf.blit(current_health_display, text_rect)

    def potion_cooldown_timer(self):
        while self.potion_cooldown > 0:
            self.potion_cooldown -= 1
            # print(self.potion_cooldown)
            time.sleep(1)