fix: game: routine
This commit is contained in:
parent
90d179eb72
commit
fd3e807136
@ -3,7 +3,6 @@ from __future__ import annotations
|
||||
from typing import TYPE_CHECKING
|
||||
|
||||
if TYPE_CHECKING:
|
||||
from .objects.pong.PongSpectator import PongSpectator
|
||||
from .objects.pong.PongPlayer import PongPlayer
|
||||
from .objects.pong.PongGame import PongGame
|
||||
from .objects.pong.Ball import Ball
|
||||
@ -13,15 +12,11 @@ from .objects.pong.Segment import Segment
|
||||
|
||||
from . import config
|
||||
|
||||
from . import config
|
||||
|
||||
import math
|
||||
import asyncio
|
||||
|
||||
from asgiref.sync import SyncToAsync
|
||||
|
||||
from time import sleep
|
||||
|
||||
VERTICALLY = 1
|
||||
NORMAL = 2
|
||||
|
||||
@ -116,9 +111,9 @@ def get_impact_data(segments: list[Segment], ball: Ball) -> dict:
|
||||
inc_x: float = (-1) * get_sign(cos) * (config.STROKE_THICKNESS + config.BALL_SIZE / 2)
|
||||
inc_y: float = get_sign(sin) * (config.STROKE_THICKNESS + config.BALL_SIZE / 2)
|
||||
|
||||
point: Point = Point(ball.position.x + cos, ball.position.y - sin)
|
||||
point: Point = Point(ball.position.location.x + cos, ball.position.location.y - sin)
|
||||
|
||||
ball_segment = Segment(ball.position, point)
|
||||
ball_segment = Segment(ball.position.location, point)
|
||||
|
||||
closest: dict = None
|
||||
|
||||
@ -137,21 +132,23 @@ def get_impact_data(segments: list[Segment], ball: Ball) -> dict:
|
||||
if (impact is None):
|
||||
continue
|
||||
|
||||
diff_x: float = ball.position.x - impact.x
|
||||
diff_x: float = ball.position.location.x - impact.x
|
||||
if (get_sign(diff_x) == get_sign(cos) and cos != 0):
|
||||
continue
|
||||
|
||||
diff_y: float = (ball.position.y - impact.y)
|
||||
diff_y: float = (ball.position.location.y - impact.y)
|
||||
if (get_sign(diff_y) != get_sign(sin) and sin != 0):
|
||||
continue
|
||||
|
||||
if (closest is None or impact.distance(ball.position) < closest.get("distance")):
|
||||
distance: float = impact.distance(ball.position.location)
|
||||
|
||||
if (closest is None or distance < closest.get("distance")):
|
||||
closest = {
|
||||
"inc_x": inc_x,
|
||||
"inc_y": inc_y,
|
||||
"impact": impact,
|
||||
"segment": segment,
|
||||
"distance": impact.distance(ball.position),
|
||||
"distance": distance,
|
||||
}
|
||||
|
||||
return closest
|
||||
@ -171,18 +168,18 @@ def wall_collision(ball_angle: float, wall: Segment) -> float:
|
||||
|
||||
return reflection_angle
|
||||
|
||||
async def paddle_collision(game: Game, impact: Point, player: Player, inc_x: float, inc_y: float):
|
||||
async def paddle_collision(game: PongGame, impact: Point, player: PongPlayer, inc_x: float, inc_y: float):
|
||||
|
||||
diff_x: float = player.rail.stop.x - player.rail.start.x
|
||||
diff_y: float = player.rail.stop.y - player.rail.start.y
|
||||
|
||||
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_x: float = player.rail.start.x + diff_x * player.position.location
|
||||
paddle_center_y: float = player.rail.start.y + diff_y * player.position.location
|
||||
|
||||
paddle_center: Point = Point(paddle_center_x, paddle_center_y)
|
||||
|
||||
rail_length: float = player.rail.length()
|
||||
paddle_length: float = rail_length * config.PADDLE_RATIO;
|
||||
paddle_length: float = rail_length * config.PADDLE_RATIO
|
||||
|
||||
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))
|
||||
@ -215,7 +212,7 @@ async def paddle_collision(game: Game, impact: Point, player: Player, inc_x: flo
|
||||
|
||||
return new_angle
|
||||
|
||||
async def collision(game: Game, impact_data: dict) -> bool:
|
||||
async def collision(game: PongGame, impact_data: dict) -> bool:
|
||||
|
||||
segment: Segment = impact_data.get("segment")
|
||||
|
||||
@ -242,7 +239,7 @@ async def collision(game: Game, impact_data: dict) -> bool:
|
||||
|
||||
return True
|
||||
|
||||
async def update_ball(game: Game, impact_data: dict) -> None:
|
||||
async def update_ball(game: PongGame, impact_data: dict) -> None:
|
||||
|
||||
distance: float = impact_data.get("distance")
|
||||
|
||||
@ -260,7 +257,7 @@ async def update_ball(game: Game, impact_data: dict) -> None:
|
||||
|
||||
await SyncToAsync(game.broadcast)("update_ball", game.ball.to_dict())
|
||||
|
||||
async def render_ball(game: Game):
|
||||
async def render_ball(game: PongGame):
|
||||
|
||||
while True:
|
||||
|
||||
@ -270,7 +267,7 @@ async def render_ball(game: Game):
|
||||
|
||||
await update_ball(game, impact_data)
|
||||
|
||||
async def render_players(game: Game):
|
||||
async def render_players(game: PongGame):
|
||||
|
||||
while True:
|
||||
|
||||
@ -281,7 +278,7 @@ async def render_players(game: Game):
|
||||
|
||||
await asyncio.sleep(1 / config.SERVER_TPS)
|
||||
|
||||
async def render(game: Game):
|
||||
async def render(game: PongGame):
|
||||
|
||||
routine_ball = asyncio.create_task(render_ball(game))
|
||||
routine_players = asyncio.create_task(render_players(game))
|
||||
@ -293,6 +290,6 @@ async def render(game: Game):
|
||||
return
|
||||
await asyncio.sleep(0.3)
|
||||
|
||||
def routine(game: Game):
|
||||
def routine(game: PongGame):
|
||||
|
||||
asyncio.run(render(game))
|
||||
|
Loading…
Reference in New Issue
Block a user