game: online: recoded but collision not work

This commit is contained in:
2024-05-14 03:28:05 +02:00
parent 18bc8a0028
commit 7a0ff15cec
16 changed files with 518 additions and 653 deletions

View File

@ -1,25 +1,145 @@
from __future__ import annotations
from .objects.pong.PongGame import PongPlayer
from .objects.pong.Segment import Segment
from .objects.pong.Point import Point
from .objects.pong.Ball import Ball
from typing import TYPE_CHECKING
if TYPE_CHECKING:
from .objects.pong.PongPlayer import PongPlayer
from .objects.pong.PongGame import PongGame
from .objects.pong.Ball import Ball
from .objects.pong.Point import Point
from .objects.pong.Segment import Segment
from . import config
import math
import asyncio
from asgiref.sync import SyncToAsync
import asyncio
VERTICALLY = 1
NORMAL = 2
def get_player_hitted(players: list[PongPlayer], segment: Segment) -> PongPlayer | None:
for player in players:
if (player.rail is segment):
return player
return None
def wall_collision(ball_angle: float, wall: Segment) -> float:
wall_angle: float = wall.angle()
cos: float = math.cos(wall_angle) * -1
sin: float = math.sin(wall_angle)
wall_angle: float = math.atan2(sin, cos)
incident_angle: float = ball_angle - wall_angle
reflection_angle: float = wall_angle - incident_angle
return reflection_angle
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.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
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)
hit_point: Point = Point(impact.x - inc_x, impact.y - inc_y)
if not paddle.is_on(hit_point):
await asyncio.sleep(0.1) # delay to create frontend animation
await SyncToAsync(game.goal)(player)
return None
paddle_angle: float = paddle.angle()
normal: float = paddle_angle - math.pi / 2
start_distance: float = paddle.start.distance(impact)
stop_distance: float = paddle.stop.distance(impact)
hit_percent: float = (start_distance) / (start_distance + stop_distance)
hit_percent = round(hit_percent, 1)
new_angle: float = normal + (math.pi * 0.85) * (hit_percent - 0.5)
return new_angle
async def collision(game: PongGame, 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
angle: float
print(impact_data.get("impact"))
if (player_hitted is None):
print("wall")
angle = wall_collision(game.ball.angle, segment)
else:
print("paddle")
angle = await paddle_collision(game, impact_data.get("impact"), player_hitted, impact_data.get("inc_x"), impact_data.get("inc_y"))
if (angle is None):
return False
print((game.ball.angle % (math.pi * 2)) * 180 / math.pi, (angle % (math.pi * 2)) * 180 / math.pi)
game.ball.speed += config.BALL_SPEED_INC
game.ball.angle = angle
return True
async def update_ball(game: PongGame, impact_data: dict):
distance: float = impact_data.get("distance")
time_before_impact: float = distance / game.ball.speed
print(time_before_impact)
await asyncio.sleep(time_before_impact)
hit: bool = await collision(game, impact_data)
print("HIT" * 10 + str(hit))
if hit:
game.ball.position.location = impact_data.get("impact")
SyncToAsync(game.broadcast)("update_ball", game.ball.to_dict())
def get_sign(num: float) -> int:
if (num == 0):
return 0
@ -153,143 +273,31 @@ def get_impact_data(segments: list[Segment], ball: Ball) -> dict:
return closest
def wall_collision(ball_angle: float, wall: Segment) -> float:
wall_angle: float = wall.angle()
cos: float = math.cos(wall_angle) * -1
sin: float = math.sin(wall_angle)
wall_angle: float = math.atan2(sin, cos)
incident_angle: float = ball_angle - wall_angle
reflection_angle: float = wall_angle - incident_angle
return reflection_angle
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.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
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)
hit_point: Point = Point(impact.x - inc_x, impact.y - inc_y)
if (not paddle.is_on(hit_point)):
await SyncToAsync(game.goal)(player)
return None
paddle_angle: float = paddle.angle()
normal: float = paddle_angle - math.pi / 2
start_distance: float = paddle.start.distance(impact)
stop_distance: float = paddle.stop.distance(impact)
hit_percent: float = (start_distance) / (start_distance + stop_distance)
hit_percent = round(hit_percent, 1)
new_angle: float = normal + (math.pi * 0.85) * (hit_percent - 0.5)
return new_angle
async def collision(game: PongGame, 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
angle: float
if (player_hitted is None):
angle = wall_collision(game.ball.angle, segment)
else:
angle = await paddle_collision(game, impact_data.get("impact"), player_hitted, impact_data.get("inc_x"), impact_data.get("inc_y"))
if (angle is None):
return False
game.ball.speed += config.BALL_SPEED_INC
game.ball.angle = angle
return True
async def update_ball(game: PongGame, impact_data: dict) -> None:
distance: float = impact_data.get("distance")
time_before_impact: float = distance / game.ball.speed
await asyncio.sleep(time_before_impact)
hit: bool = await collision(game, impact_data)
if (hit == False):
await asyncio.sleep(0.1) # delay to create frontend animation
game.ball.reset()
else:
game.ball.position.location = impact_data.get("impact")
await SyncToAsync(game.broadcast)("update_ball", game.ball.to_dict())
async def render_ball(game: PongGame):
segments: list[Segment] = [player.rail for player in game.players] + game.walls
while True:
segments: list[Segment] = [player.rail for player in game.players] + game.walls
impact_data: dict = get_impact_data(segments, game.ball)
await update_ball(game, impact_data)
async def async_routine(game: PongGame):
async def render_players(game: PongGame):
#TODO DEBUG collision
ball_routine = asyncio.create_task(render_ball(game))
while True:
for player in game._updated_players:
await SyncToAsync(game.broadcast)("update_player", player.to_dict(), [player])
game._updated_players.clear()
await asyncio.sleep(1 / config.SERVER_TPS)
async def render(game: PongGame):
routine_ball = asyncio.create_task(render_ball(game))
routine_players = asyncio.create_task(render_players(game))
while(True):
if (game.stopped):
routine_ball.cancel()
routine_players.cancel()
if game.stopped:
ball_routine.cancel()
return
await asyncio.sleep(0.05)
def routine(game: PongGame):
asyncio.run(render(game))
asyncio.run(async_routine(game))