throwable.py 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. import math
  2. import pygame.time
  3. from client_local.const import game as G
  4. from client_local.level import level
  5. from client_local.sprite import updatable
  6. def cal_y(a, b, c, x):
  7. return a * x * x + b * x + c
  8. class Throwable(updatable.Updatable):
  9. def __init__(self, x, y):
  10. super(Throwable, self).__init__(x, y)
  11. self.throwing = False # 正在被投掷
  12. self.throwing_time_begin = 0 # 投掷开始时刻
  13. self.throwing_time = pygame.time.get_ticks() # 上次更新抛掷帧时刻
  14. self.direction = None # 抛掷方向 "R" "L"
  15. self.points = None # 抛物线上的点
  16. self.current_point = 0 # 当前在第几个点
  17. def get_direction(self, to_x, to_y):
  18. # 用于自动获取方向
  19. if self.x == to_x:
  20. return "D" if to_y > self.y else "U"
  21. else:
  22. return "R" if to_x > self.x else "L"
  23. def throw_to(self, to_x, to_y, direction, emit_obstacles=False):
  24. if self.throwing is True:
  25. return # 正在投掷的对象 不能再投掷
  26. if to_x == self.x and to_y == self.y:
  27. self.throwing = False
  28. return # 目标点与当前点重叠 不能再投掷
  29. cl = level.current_level
  30. real_x = (to_x + cl.map_x) % cl.map_x
  31. real_y = (to_y + cl.map_y) % cl.map_y
  32. if not emit_obstacles and (real_x, real_y) in cl.obstacle_instances:
  33. if direction == "R":
  34. self.throw_to(to_x - 1, to_y, "R")
  35. if direction == "L":
  36. self.throw_to(to_x + 1, to_y, "L")
  37. if direction == "U":
  38. self.throw_to(to_x, to_y + 1, "U")
  39. if direction == "D":
  40. self.throw_to(to_x, to_y - 1, "D")
  41. return
  42. self.throwing = True
  43. self.throwing_time_begin = pygame.time.get_ticks()
  44. self.direction = direction
  45. self.set_restoration(real_x, real_y)
  46. from_x_pos = self.x_pos
  47. from_y_pos = self.y_pos
  48. to_x_pos = to_x * G.GAME_SQUARE + G.HALF_GAME_SQUARE
  49. to_y_pos = to_y * G.GAME_SQUARE + G.HALF_GAME_SQUARE
  50. self.get_points((from_x_pos, from_y_pos), (to_x_pos, to_y_pos))
  51. def get_points(self, p1, p2):
  52. self.points = list()
  53. if p1[0] == p2[0]:
  54. # 竖直抛掷
  55. now_y = p1[1]
  56. diff = abs(now_y - p2[1])
  57. if self.direction == "U":
  58. while diff > 10:
  59. now_y -= 10
  60. if diff < 10:
  61. now_y = p2[1]
  62. if now_y < 0:
  63. self.points.append((p1[0], now_y + level.current_level.map_y_pos))
  64. else:
  65. self.points.append((p1[0], now_y))
  66. diff = abs(now_y - p2[1])
  67. if self.direction == "D":
  68. while diff > 10:
  69. now_y += 10
  70. if diff < 10:
  71. now_y = p2[1]
  72. if now_y > level.current_level.map_y_pos:
  73. self.points.append((p1[0], now_y % level.current_level.map_y_pos))
  74. else:
  75. self.points.append((p1[0], now_y))
  76. diff = abs(now_y - p2[1])
  77. else:
  78. # 非竖直抛掷
  79. # 两点式
  80. a = 0.002 + (0.002 - (1.0 / 280000) * abs(p2[0] - p1[0]))
  81. b = (p1[1] - p2[1]) / (p1[0] - p2[0]) - a * (p1[0] + p2[0])
  82. c = p1[1] - a * p1[0] * p1[0] - b * p1[0]
  83. now_x = p1[0]
  84. diff = abs(now_x - p2[0])
  85. if self.direction == "R":
  86. while diff > 10:
  87. k = 2 * a * now_x + b
  88. now_x += 14 / math.sqrt(k * k + 1)
  89. if diff < 10:
  90. now_x = p2[0]
  91. now_y = cal_y(a, b, c, now_x)
  92. if now_x > level.current_level.map_x_pos:
  93. self.points.append((now_x % level.current_level.map_x_pos, now_y))
  94. else:
  95. self.points.append((now_x, now_y))
  96. diff = abs(now_x - p2[0])
  97. if self.direction == "L":
  98. while diff > 10:
  99. k = 2 * a * now_x + b
  100. now_x -= 14 / math.sqrt(k * k + 1)
  101. if diff < 10:
  102. now_x = p2[0]
  103. now_y = cal_y(a, b, c, now_x)
  104. if now_x < 0:
  105. self.points.append((now_x + level.current_level.map_x_pos, now_y))
  106. else:
  107. self.points.append((now_x, now_y))
  108. diff = abs(now_x - p2[0])
  109. def throw(self):
  110. if self.throwing is False:
  111. return
  112. current_time = pygame.time.get_ticks()
  113. if self.current_point < len(self.points) - 1:
  114. # 正在投掷
  115. self.throwing_time = current_time
  116. self.current_point = min(len(self.points) - 1, (current_time - self.throwing_time_begin) // 20)
  117. self.x_pos = self.points[self.current_point][0]
  118. self.y_pos = self.points[self.current_point][1]
  119. else:
  120. # 投掷完毕
  121. self.throwing = False
  122. self.direction = None
  123. self.points = None
  124. self.current_point = 0
  125. self.x, self.y = self._landing_x, self._landing_y
  126. self.x_pos = self.x * G.GAME_SQUARE + G.HALF_GAME_SQUARE
  127. self.y_pos = self.y * G.GAME_SQUARE + G.HALF_GAME_SQUARE
  128. def set_restoration(self, to_x, to_y):
  129. # 子类必须重写 在抛出的一瞬间就要确定xy坐标
  130. pass