throwable.py 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  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. return # 目标点与当前点重叠 不能再投掷
  28. cl = level.current_level
  29. real_x = (to_x + cl.map_x) % cl.map_x
  30. real_y = (to_y + cl.map_y) % cl.map_y
  31. if not emit_obstacles and (real_x, real_y) in cl.obstacle_instances:
  32. if direction == "R":
  33. self.throw_to(to_x - 1, to_y, "R")
  34. if direction == "L":
  35. self.throw_to(to_x + 1, to_y, "L")
  36. if direction == "U":
  37. self.throw_to(to_x, to_y + 1, "U")
  38. if direction == "D":
  39. self.throw_to(to_x, to_y - 1, "D")
  40. return
  41. self.throwing = True
  42. self.throwing_time_begin = pygame.time.get_ticks()
  43. self.direction = direction
  44. self.set_restoration(real_x, real_y)
  45. from_x_pos = self.x_pos
  46. from_y_pos = self.y_pos
  47. to_x_pos = to_x * G.GAME_SQUARE + G.HALF_GAME_SQUARE
  48. to_y_pos = to_y * G.GAME_SQUARE + G.HALF_GAME_SQUARE
  49. self.get_points((from_x_pos, from_y_pos), (to_x_pos, to_y_pos))
  50. def get_points(self, p1, p2):
  51. self.points = list()
  52. if p1[0] == p2[0]:
  53. # 竖直抛掷
  54. now_y = p1[1]
  55. diff = abs(now_y - p2[1])
  56. if self.direction == "U":
  57. while diff > 10:
  58. now_y -= 10
  59. if diff < 10:
  60. now_y = p2[1]
  61. if now_y < 0:
  62. self.points.append((p1[0], now_y + level.current_level.map_y_pos))
  63. else:
  64. self.points.append((p1[0], now_y))
  65. diff = abs(now_y - p2[1])
  66. if self.direction == "D":
  67. while diff > 10:
  68. now_y += 10
  69. if diff < 10:
  70. now_y = p2[1]
  71. if now_y > level.current_level.map_y_pos:
  72. self.points.append((p1[0], now_y % level.current_level.map_y_pos))
  73. else:
  74. self.points.append((p1[0], now_y))
  75. diff = abs(now_y - p2[1])
  76. else:
  77. # 非竖直抛掷
  78. # 两点式
  79. a = 0.002 + (0.002 - (1.0 / 280000) * abs(p2[0] - p1[0]))
  80. b = (p1[1] - p2[1]) / (p1[0] - p2[0]) - a * (p1[0] + p2[0])
  81. c = p1[1] - a * p1[0] * p1[0] - b * p1[0]
  82. now_x = p1[0]
  83. diff = abs(now_x - p2[0])
  84. if self.direction == "R":
  85. while diff > 10:
  86. k = 2 * a * now_x + b
  87. now_x += 14 / math.sqrt(k * k + 1)
  88. if diff < 10:
  89. now_x = p2[0]
  90. now_y = cal_y(a, b, c, now_x)
  91. if now_x > level.current_level.map_x_pos:
  92. self.points.append((now_x % level.current_level.map_x_pos, now_y))
  93. else:
  94. self.points.append((now_x, now_y))
  95. diff = abs(now_x - p2[0])
  96. if self.direction == "L":
  97. while diff > 10:
  98. k = 2 * a * now_x + b
  99. now_x -= 14 / math.sqrt(k * k + 1)
  100. if diff < 10:
  101. now_x = p2[0]
  102. now_y = cal_y(a, b, c, now_x)
  103. if now_x < 0:
  104. self.points.append((now_x + level.current_level.map_x_pos, now_y))
  105. else:
  106. self.points.append((now_x, now_y))
  107. diff = abs(now_x - p2[0])
  108. def throw(self):
  109. if self.throwing is False:
  110. return
  111. current_time = pygame.time.get_ticks()
  112. if self.current_point < len(self.points) - 1:
  113. # 正在投掷
  114. self.throwing_time = current_time
  115. self.current_point = min(len(self.points) - 1, (current_time - self.throwing_time_begin) // 20)
  116. self.x_pos = self.points[self.current_point][0]
  117. self.y_pos = self.points[self.current_point][1]
  118. else:
  119. # 投掷完毕
  120. self.throwing = False
  121. self.direction = None
  122. self.points = None
  123. self.current_point = 0
  124. self.x_pos = self.x * G.GAME_SQUARE + G.HALF_GAME_SQUARE
  125. self.y_pos = self.y * G.GAME_SQUARE + G.HALF_GAME_SQUARE
  126. def set_restoration(self, to_x, to_y):
  127. # 子类必须重写 在抛出的一瞬间就要确定xy坐标
  128. pass