summaryrefslogtreecommitdiffstats
path: root/PhysicsBody.py
blob: c4fa02f377fe41d59a96b7b4069b4f58a84f4da5 (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
from Setup import *
from Block import Block
from Function.createText import createText
from Actors import Actor


class PhysicsBody:
    gravity = Actor.gravity
    friction = Actor.friction
    invincibility_time = 150

    def __init__(self, pos, vel, width, height, colour, collision_layer, collision_mask, goon_skin=True, is_jeff = False):
        self.position = pg.Vector2(pos)
        self.velocity = pg.Vector2(vel)
        self.width, self.height = height, width
        self.colour = colour

        self.on_ground = False

        self.dead = False
        self.health = 0
        self.movable = True
        self.attacked = False
        self.invincibility_frames = self.invincibility_time
        self.was_beaten = False

        collision_layer.add(self)  # the layer the actor is on for collisions
        self.collision_layer = collision_layer
        self.collision_mask = collision_mask  # the layer the actor detects collisions against

        self.display_offsets = {"enemy":pg.Vector2(0,0)}

        self.goon_skin = goon_skin

        if goon_skin:
            self.display = pg.transform.scale(pg.image.load("Assets/enemy/Goon_Death.png"), (200,200))
            self.offset = pg.Vector2(-50,-65)
        elif is_jeff:
            self.display = pg.transform.scale(pg.image.load("Assets/enemy/Jeff_Death.png"), (200,200))
            self.offset = pg.Vector2(-50,-140)
            self.goon_skin = True
        else:
            self.display = pg.transform.scale(pg.image.load("Assets/skeleton/SKELETON.png"), (50,50))
            self.offset = pg.Vector2(0, 10)

    def update(self, delta, test=None, test2=None):
        if self.on_ground:
            self.velocity.x *= self.friction

        self.velocity.y += self.gravity


        self.position, self.velocity = self.move_and_collide(self.position.copy(), self.velocity.copy(), delta)

        if self.on_ground:
            self.attacked = False

    def attack(self, enemy, weapon, direction):
        self.push(direction, 2, -1)
        self.attacked = True
        self.invincibility_frames = self.invincibility_time
        self.was_beaten = True

    def push(self, direction, strength=1, y=-1):
        self.velocity += pg.Vector2(direction * strength, y)

    def move_and_collide(self, pos, vel, delta):
        pos.x += vel.x * delta
        collision_rect = self.get_collision_rect(pos)
        for mask in self.collision_mask:
            for thing in mask:
                if thing == self:
                    continue
                if collision_rect.colliderect(thing.get_collision_rect()):
                    # if thing.movable:
                    #     if vel.x > 0:
                    #         thing.position.x = pos.x + self.width
                    #     elif vel.x < 0:
                    #         thing.position.x = pos.x - thing.width
                    if vel.x >= 0:
                        pos.x = thing.position.x - self.width
                        vel.x = min(vel.x, 0)
                    elif vel.x < 0:
                        pos.x = thing.position.x + thing.width
                        vel.x = max(vel.x, 0)
                    collision_rect = self.get_collision_rect(pos)

        self.on_ground = False
        pos.y += vel.y * delta
        collision_rect = self.get_collision_rect(pos)
        for mask in self.collision_mask:
            for thing in mask:
                if thing == self:
                    continue
                if collision_rect.colliderect(thing.get_collision_rect()):
                    if vel.y >= 0:
                        pos.y = thing.position.y - self.height
                        vel.y = min(vel.y, 0)
                        self.on_ground = True
                    elif vel.y < 0:
                        pos.y = thing.position.y + thing.height
                        vel.y = max(vel.y, 0)
                    # collision_rect = self.get_collision_rect(pos)
        return pos, vel

    def get_collision_rect(self, pos=None):
        if pos is None:
            pos = self.position
        return pg.Rect(pos, (self.width, self.height))

    def draw(self, surf):
        # print(self.position, self.velocity)
        # pg.draw.rect(surf, self.colour, get_display_rect(self.get_collision_rect()), border_radius=8)
        surf.blit(self.display,get_display_rect(self.get_collision_rect()).topleft + self.offset)
        # surf.blit(self.display,get_display_rect(self.get_collision_rect()))