Skip to content

Commit

Permalink
Merge pull request #124 from HagenSR/diagonal-hitboxes
Browse files Browse the repository at this point in the history
Add diagonal hitbox feature
  • Loading branch information
erickbickler authored Sep 21, 2021
2 parents f82ac03 + 1ec1586 commit d5a21ba
Showing 1 changed file with 12 additions and 4 deletions.
16 changes: 12 additions & 4 deletions game/common/hitbox.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,21 @@
import math

from game.common.game_object import GameObject
from game.common.enums import *
import game.common.stats as stats


class Hitbox(GameObject):
def __init__(self, width, height, xy_tuple):
def __init__(self, width, height, xy_tuple, rotation=0):
super().__init__()
self.object_type = ObjectType.hitbox
self.width = width
self.height = height
# (x,y) tuple, where [0] is the x position and y is [1] of the top left corner
self.position = xy_tuple
# added rotation to allow for diagonal hitboxes while keeping backwards
# compatibility
self.rotation = math.radians(rotation)

@property
def width(self):
Expand All @@ -30,15 +35,18 @@ def topLeft(self):

@property
def topRight(self):
return (self.position[0] + self.width, self.position[1])
return (self.position[0] + (self.width * math.cos(self.rotation)),
self.position[1] + (self.width * math.sin(self.rotation)))

@property
def bottomLeft(self):
return (self.position[0], self.position[1] + self.height)
return (self.position[0] + (self.height * math.sin(self.rotation)),
self.position[1] + (self.height * math.cos(self.rotation)))

@property
def bottomRight(self):
return (self.position[0] + self.width, self.position[1] + self.height)
return (self.bottomLeft[0] + (self.width * math.cos(self.rotation)),
self.bottomLeft[1] + (self.width * math.sin(self.rotation)))

@property
def middle(self):
Expand Down

0 comments on commit d5a21ba

Please sign in to comment.