]> Skullheadx's Git Forge - Pygame-Jam.git/commitdiff
Fixed sword hitbox
authorSkullheadx <94652084+Skullheadx@users.noreply.github.com>
Sun, 10 Jul 2022 04:54:21 +0000 (00:54 -0400)
committerSkullheadx <94652084+Skullheadx@users.noreply.github.com>
Sun, 10 Jul 2022 04:54:21 +0000 (00:54 -0400)
Actors.py
Enemy.py
PhysicsBody.py
Player.py
Weapon.py

index 87d3783588d3509074dc9fc93a209239feda924b..ddc158e70766db49cf5b2ff988b9c97c870dffdf 100644 (file)
--- a/Actors.py
+++ b/Actors.py
@@ -17,6 +17,7 @@ class Actor:
     jump_buffer = []
     variable_jump_time = timedelta(milliseconds=125)  # how long to hold jump so we go higher
     terminal_velocity = 15
+    invincibility_time = 150
 
     def __init__(self, pos, collision_layer, collision_mask):
         self.position = pg.Vector2(pos)
@@ -39,6 +40,8 @@ class Actor:
         self.coyote_time = datetime.utcnow()
         self.hold_jump = datetime.utcnow()
         self.stun_time = 0
+        self.attacked = False
+        self.invincibility_frames = 0
 
     def update(self, delta):
         if self.jumping:
@@ -47,6 +50,10 @@ class Actor:
 
         self.stun_time -= delta
         self.stun_time = max(self.stun_time, 0)
+        self.invincibility_frames -= delta
+        self.invincibility_frames = max(self.invincibility_frames, 0)
+        if self.invincibility_frames == 0:
+            self.attacked = False
 
         for area in self.areas.values():
             area.update(delta, self.position)
@@ -79,6 +86,8 @@ class Actor:
     def attack(self, enemy, weapon):
         self.modify_health(weapon.damage, "enemy")
         self.push(enemy)
+        self.attacked = True
+        self.invincibility_frames = self.invincibility_time
 
     def follow_target(self, node, follow_range=None, stop_dist=None):
         if stop_dist is None:
index 6564b2c5fbdbafa443a56de8d8d011830ff61aab..6dc3c9863d2b74472fd6071fdd04484212977889 100644 (file)
--- a/Enemy.py
+++ b/Enemy.py
@@ -6,11 +6,9 @@ from Weapon import Melee
 
 class Enemy(Actor):
     width, height = 50, 100
+    speed = Actor.speed * 0.5
+    jump_strength = Actor.jump_strength * 0.5
     colour = (235, 64, 52)
-    speed = 0.2
-    jump_strength = 0.75
-    gravity = 0.098
-    friction = 0.9
     
 
     def __init__(self, pos, collision_layer, collision_mask):
@@ -20,10 +18,11 @@ class Enemy(Actor):
         self.movable = True
         self.dizzy_time = 0
 
-        self.health = 0 # for debugging without getting killed
+        self.health = 0 # for debugging without getting killed
 
         self.weapon = Melee(self.position, (-Melee.width/2 + 7, Melee.height/2 + self.height/3 - 8), (-5,Melee.height), self.width,-1, -10)
 
+
     def update(self, delta, target=None):
         super().update(delta)
         if target is not None and self.dizzy_time == 0:
index e3b14f93ba4108eebe0780bbf9e77f081ec61638..eef9d33e90c727c07627b154a1e1575b6b21c624 100644 (file)
@@ -7,6 +7,7 @@ class PhysicsBody:
     jump_strength = 1
     gravity = 0.098
     friction = 0.9
+    invincibility_time = 150
 
     def __init__(self, pos, vel, width, height, colour, collision_layer, collision_mask):
         self.position = pg.Vector2(pos)
@@ -19,12 +20,18 @@ class PhysicsBody:
         self.dead = False
 
         self.movable =True
+        self.attacked = False
+        self.invincibility_frames = 0
 
         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
 
     def update(self, delta, test=None):
+        self.invincibility_frames -= delta
+        self.invincibility_frames = max(self.invincibility_frames, 0)
+        if self.invincibility_frames == 0:
+            self.attacked = False
         # Apply friction so the enemy isn't walking on ice
         if self.on_ground:
             self.velocity.x *= self.friction
@@ -36,6 +43,9 @@ class PhysicsBody:
         # print(self.position)
     def attack(self, enemy, weapon):
         self.push(enemy)
+        self.attacked = True
+        self.invincibility_frames = self.invincibility_time
+
     def push(self, enemy):
         v = enemy.weapon.direction
         # if enemy.velocity.x != pg.Vector2(0,0):
index 2848799f4d53606e8b8da0ad6aa3e521f11d29dc..37096519e193e98a4f53a9973bed5d912d4c1b38 100644 (file)
--- a/Player.py
+++ b/Player.py
@@ -4,7 +4,7 @@ from Setup import *
 from Actors import Actor
 from datetime import datetime, timedelta
 from Potion import Potion
-from Weapon import Melee
+from Weapon import Sword
 
 
 class Player(Actor):
@@ -47,8 +47,7 @@ class Player(Actor):
         for i in range(self.starting_potions):
             self.potion_bag.append(Potion(self)) # use one liner
 
-        self.weapon = Melee(self.position, (-Melee.width / 2 + 7, Melee.height / 2 + self.height / 3 - 8),
-                            (-5, Melee.height), self.width, -1, -25)
+        self.weapon = Sword(self.position, (0,0), self.width, -1)
         self.targets = can_hurt
 
         self.direction = -1
@@ -86,7 +85,7 @@ class Player(Actor):
         # 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)
+        self.weapon.update(delta, self.position, self.direction)
 
         if self.state == "IDLE":
             self.display_offsets["player"] = pg.Vector2(0,0)
@@ -175,14 +174,15 @@ class Player(Actor):
 
         mouse_pressed = pg.mouse.get_pressed(3)
         if mouse_pressed[0]:  # LMB
-            if not self.weapon.attacking:
-                self.weapon.swing()
+            if self.state != "ATTACK":
                 self.state = "ATTACK"
                 self.current_frame = 0
-                for mask in self.targets:
-                    for enemy in mask:
-                        if self.weapon.get_collision_rect().colliderect(enemy.get_collision_rect()):
-                            enemy.attack(self, self.weapon)
+            elif self.state == "ATTACK":
+                if math.floor(self.current_frame) > 2:
+                    for mask in self.targets:
+                        for enemy in mask:
+                            if not enemy.attacked and self.weapon.get_collision_rect().colliderect(enemy.get_collision_rect()):
+                                enemy.attack(self, self.weapon)
 
 
     def draw(self, surf):
@@ -196,7 +196,7 @@ class Player(Actor):
         # 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,self.colour,pg.Rect(center, (self.width, self.height)),border_radius=8)
 
 
index d56a9baebe286ad50d09ef426ce83f91f4a8fbb0..c1c3105e9cd569308c57df693f6398678a195dd0 100644 (file)
--- a/Weapon.py
+++ b/Weapon.py
@@ -61,58 +61,33 @@ class Melee:
         # pg.draw.circle(surf,(0,255,0),get_display_point(self.position),3)
 
 
+
 class Sword:
-    img = pg.transform.scale(pg.image.load("Assets/SWORD.png"), (40, 40))
+    img = pg.transform.scale(pg.image.load("Assets/SWORD.png"), (50, 50))
+    img,_ = rotate(pg.Vector2(img.get_rect().topright),img, -30,pg.Vector2(img.get_rect().bottomleft))
     flipped_img = pg.transform.flip(img,True,False)
-    width,height = img.get_size()
+    width,height = (75,200)
+    damage = 25
 
-    def __init__(self, pos, offset, pivot, width,direction, damage):
+    def __init__(self, pos, offset, width,direction):
         self.position = pg.Vector2(pos)
         self.offset = pg.Vector2(offset)
-        self.pivot = self.position + pg.Vector2(pivot)
         self.holder_width = width
-
         self.direction = direction
-
         self.display = self.img
         self.display_rect = self.display.get_rect()
-        self.swing_timer = 0
-        self.attacking = False
-
-        self.damage = damage
 
     def update(self, delta, pos, direction):
         self.position = pg.Vector2(pos)
-        self.pivot = self.position + self.offset + pg.Vector2(self.width/2, self.height/2)
         if direction != 0:
             self.direction = direction
 
-        if self.direction == -1:
-            angle = 25 * (math.sin(math.radians(self.swing_timer)))
-            self.display, self.display_rect = rotate(self.position + self.offset, self.img, angle,self.pivot)
-        elif self.direction == 1:
-            angle = -25 * (math.sin(math.radians(self.swing_timer)))
-            self.display, self.display_rect = rotate(self.position+ pg.Vector2(self.holder_width,0) + pg.Vector2(-self.offset.x, self.offset.y), self.flipped_img, angle,self.pivot + pg.Vector2(self.holder_width,0))
-
-        self.swing_timer -= delta
-        self.swing_timer = max(self.swing_timer, 0)
-        if self.swing_timer == 0:
-            self.attacking = False
-        else:
-            self.attacking = True
-
     def get_collision_rect(self):
-        if self.direction == -1:
-            return pg.Rect(self.display_rect.topleft,(self.width, self.height))
-        elif self.direction == 1:
-            return pg.Rect(self.display_rect.topleft,(self.width, self.height))
+        if self.direction == 1:
+            return pg.Rect(self.position + self.offset + pg.Vector2(self.holder_width,-self.height/4),(self.width, self.height))
+        elif self.direction == -1:
+            return pg.Rect(self.position - self.offset - pg.Vector2(self.width,self.height/4),(self.width, self.height))
 
-    def swing(self):
-        if True:
-            if self.swing_timer == 0:
-                self.swing_timer = 360
 
     def draw(self, surf, display_offset = pg.Vector2(0,0)):
         surf.blit(self.display, get_display_rect(self.get_collision_rect()).topleft + display_offset)
-        # pg.draw.circle(surf,(255,0,255),get_display_point(self.pivot),3)
-        # pg.draw.circle(surf,(0,255,0),get_display_point(self.position),3)