game: add: goal work
This commit is contained in:
@ -136,19 +136,22 @@ def get_impact_point(segments: list[Segment], ball: Ball) -> dict:
|
||||
if (get_sign(diff_y) != get_sign(sin) and sin != 0):
|
||||
continue
|
||||
|
||||
impact.x += (ball.size / 2) * get_sign(cos) * (-1)
|
||||
impact.y += (ball.size / 2) * get_sign(sin)
|
||||
impact_with_padding: Point = impact.copy()
|
||||
|
||||
if (closest is None or impact.distance(ball.position) < closest.get("distance")):
|
||||
impact_with_padding.x += (ball.size / 2) * get_sign(cos) * (-1)
|
||||
impact_with_padding.y += (ball.size / 2) * get_sign(sin)
|
||||
|
||||
if (closest is None or impact_with_padding.distance(ball.position) < closest.get("distance")):
|
||||
closest = {
|
||||
"impact_with_padding": impact_with_padding,
|
||||
"impact": impact,
|
||||
"segment": segment,
|
||||
"distance": impact.distance(ball.position),
|
||||
"distance": impact_with_padding.distance(ball.position),
|
||||
}
|
||||
|
||||
return closest
|
||||
|
||||
def wall_colision(ball_angle: float, wall_angle: float) -> float:
|
||||
def wall_collision(ball_angle: float, wall_angle: float) -> float:
|
||||
|
||||
ball_cos: float = math.cos(ball_angle)
|
||||
ball_sin: float = math.sin(ball_angle)
|
||||
@ -164,21 +167,83 @@ def wall_colision(ball_angle: float, wall_angle: float) -> float:
|
||||
|
||||
return new_angle
|
||||
|
||||
async def update_ball(game: Game, impact: dict):
|
||||
def paddle_collision(ball: Ball, impact: Point, player: Player):
|
||||
|
||||
diff_x: float = player.rail.stop.x - player.rail.start.x
|
||||
diff_y: float = player.rail.stop.y - player.rail.start.y
|
||||
|
||||
distance: float = impact.get("distance")
|
||||
paddle_center_x: float = player.rail.start.x + diff_x * player.position.position
|
||||
paddle_center_y: float = player.rail.start.y + diff_y * player.position.position
|
||||
|
||||
paddle_center: Point = Point(paddle_center_x, paddle_center_y)
|
||||
|
||||
rail_length: float = player.rail.length()
|
||||
paddle_length: float = rail_length * config.PADDLE_RATIO;
|
||||
|
||||
print(paddle_length)
|
||||
print(paddle_center)
|
||||
|
||||
start_x: float = paddle_center.x - (diff_x * (paddle_length / 2 / rail_length))
|
||||
start_y: float = paddle_center.y - (diff_y * (paddle_length / 2 / rail_length))
|
||||
stop_x: float = paddle_center.x + (diff_x * (paddle_length / 2 / rail_length))
|
||||
stop_y: float = paddle_center.y + (diff_y * (paddle_length / 2 / rail_length))
|
||||
|
||||
start: Point = Point(start_x, start_y)
|
||||
stop: Point = Point(stop_x, stop_y)
|
||||
|
||||
paddle: Segment = Segment(start, stop)
|
||||
|
||||
if (not paddle.is_on(impact)):
|
||||
print(not paddle.is_on(impact))
|
||||
return None
|
||||
|
||||
return ball.angle + math.pi
|
||||
|
||||
def collision(game: Game, impact_data: dict) -> bool:
|
||||
|
||||
segment: Segment = impact_data.get("segment")
|
||||
|
||||
player_hitted = None
|
||||
for player in game.players:
|
||||
if (not player.is_connected()):
|
||||
continue
|
||||
if (player.rail is segment):
|
||||
player_hitted = player
|
||||
break
|
||||
|
||||
surface_angle: float = math.atan2(segment.start.y - segment.stop.y, segment.start.x - segment.stop.y)
|
||||
|
||||
if (player_hitted is None):
|
||||
|
||||
game.ball.angle = wall_collision(game.ball.angle, surface_angle)
|
||||
|
||||
return True
|
||||
else:
|
||||
angle = paddle_collision(game.ball, impact_data.get("impact"), player_hitted)
|
||||
|
||||
if (angle is None):
|
||||
return False
|
||||
|
||||
game.ball.angle = angle
|
||||
|
||||
return True
|
||||
|
||||
async def update_ball(game: Game, impact_data: dict) -> None:
|
||||
|
||||
distance: float = impact_data.get("distance")
|
||||
|
||||
time_before_impact: float = distance / game.ball.speed
|
||||
|
||||
await asyncio.sleep(time_before_impact)
|
||||
|
||||
segment: Segment = impact.get("segment")
|
||||
|
||||
wall_angle: float = math.atan2(segment.stop.y - segment.start.y, segment.stop.x - segment.start.x)
|
||||
|
||||
game.ball.angle = wall_colision(game.ball.angle, wall_angle)
|
||||
hit: bool = collision(game, impact_data)
|
||||
|
||||
game.ball.position = impact.get("impact")
|
||||
if (hit == False):
|
||||
await asyncio.sleep(0.1)
|
||||
print("Goal")
|
||||
game.ball.reset()
|
||||
else:
|
||||
game.ball.position = impact_data.get("impact_with_padding")
|
||||
|
||||
await SyncToAsync(game.broadcast)("update_ball", game.ball.to_dict())
|
||||
|
||||
@ -188,9 +253,9 @@ async def render(game: Game):
|
||||
|
||||
segments: list[Segment] = [player.rail for player in game.players] + [wall.rail for wall in game.walls]
|
||||
|
||||
impact: dict = get_impact_point(segments, game.ball, )
|
||||
impact_data: dict = get_impact_point(segments, game.ball)
|
||||
|
||||
await update_ball(game, impact)
|
||||
await update_ball(game, impact_data)
|
||||
|
||||
|
||||
def routine(game: Game):
|
||||
|
Reference in New Issue
Block a user