From: Skullheadx <94652084+Skullheadx@users.noreply.github.com> Date: Sun, 10 Jul 2022 04:54:21 +0000 (-0400) Subject: Fixed sword hitbox X-Git-Url: http://git.skullheadx.com/nixos/static/gitweb.css?a=commitdiff_plain;h=42100e9a4f724c25883fe92599bd21a1547e5cab;p=Pygame-Jam.git Fixed sword hitbox --- diff --git a/Actors.py b/Actors.py index 87d3783..ddc158e 100644 --- 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: diff --git a/Enemy.py b/Enemy.py index 6564b2c..6dc3c98 100644 --- 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: diff --git a/PhysicsBody.py b/PhysicsBody.py index e3b14f9..eef9d33 100644 --- a/PhysicsBody.py +++ b/PhysicsBody.py @@ -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): diff --git a/Player.py b/Player.py index 2848799..3709651 100644 --- 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) diff --git a/Weapon.py b/Weapon.py index d56a9ba..c1c3105 100644 --- 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)