Advertisement
Guest User

Untitled

a guest
Feb 18th, 2020
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.92 KB | None | 0 0
  1. from enum import Enum
  2.  
  3. class SIDE(Enum):
  4. LEFT = 1
  5. TOP = 2
  6. RIGHT = 3
  7. BOT = 4
  8.  
  9. class Collider:
  10. #Type = 0 Solid everypixel
  11. #Type = 1 Around
  12. def __init__(self, object_attached, size, type_c=1):
  13. self.size = size
  14. self.type_c = type_c
  15. self.object_attached = object_attached
  16.  
  17. def iscollision(self, other, side=None):
  18. #1 left
  19. #2 top
  20. #3 right
  21. #4 bot
  22. if isinstance(other, Collider):
  23. loc_self = self.object_attached.location
  24. loc_other = other.object_attached.location
  25. if self.object_attached.distance(other.object_attached) > self.size[0] + self.size[1] and self.object_attached.distance(other.object_attached) > other.size[0] + other.size[1]:
  26. #print("time saved by collision")
  27. return None
  28. if side is None:
  29. #left
  30. if self.linecollide(loc_self[0], loc_other[0]+other.size[0], loc_self[1], self.size[1], loc_other[1], other.size[1]):
  31. return SIDE.LEFT.value
  32. #top
  33. if self.linecollide(loc_self[1], loc_other[1]+other.size[1], loc_self[0], self.size[0], loc_other[0], other.size[0]):
  34. return SIDE.TOP.value
  35. #right
  36. if self.linecollide(loc_other[0], loc_self[0]+self.size[0], loc_other[1], other.size[1], loc_self[1], self.size[1]):
  37. return SIDE.RIGHT.value
  38. #bot
  39. if self.linecollide(loc_other[1], loc_self[1]+self.size[1], loc_other[0], other.size[0], loc_self[0], self.size[0]):
  40. return SIDE.BOT.value
  41. elif side is SIDE.LEFT.value:
  42. if self.linecollide(loc_self[0], loc_other[0]+other.size[0], loc_self[1], self.size[1], loc_other[1], other.size[1]):
  43. return True
  44. return False
  45. elif side is SIDE.TOP.value:
  46. if self.linecollide(loc_self[1], loc_other[1]+other.size[1], loc_self[0], self.size[0], loc_other[0], other.size[0]):
  47. return True
  48. return False
  49. elif side is SIDE.RIGHT.value:
  50. if self.linecollide(loc_other[0], loc_self[0]+self.size[0], loc_other[1], other.size[1], loc_self[1], self.size[1]):
  51. return True
  52. return False
  53. elif side is SIDE.BOT.value:
  54. if self.linecollide(loc_other[1], loc_self[1]+self.size[1], loc_other[0], other.size[0], loc_self[0], self.size[0]):
  55. return True
  56. return False
  57. return None
  58.  
  59. #add for more ez call
  60. def linecollide(self, value, valuevalue, mini, endi, minii, endii):
  61. for v2 in range(mini, mini+endi):
  62. for v2v2 in range(minii, minii+endii):
  63. if value == valuevalue and v2 == v2v2:
  64. return True
  65. return False
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement