Amélioration du bot pour qu'il tire dans la zone la plus éloigné du joueur #4
|
@ -241,14 +241,6 @@ def __bounce_on_player(a: Entity, b: Entity):
|
|||
return True
|
||||
|
||||
|
||||
def __bounce_on_player_simul(a: Entity, b: Entity):
|
||||
"""
|
||||
Fonction qui fait une simulation de la balle
|
||||
"""
|
||||
__bounce_on_player(a, b)
|
||||
return RightWall not in b
|
||||
|
||||
|
||||
def __bounce_on_left_wall(a: Entity, b: Entity):
|
||||
"""
|
||||
Fonction qui decrit se qui se passe lorque la ball entre en collision avec le mur de gauche
|
||||
|
@ -307,35 +299,76 @@ def __update_move(world: World):
|
|||
__move_up(world)
|
||||
|
||||
|
||||
def __simulate_wall_position(entity: Entity, component_type: type):
|
||||
"""
|
||||
Simule une entité afin de trouver lorsqu'elle entrera en collision avec une entité contenant un certain composant.
|
||||
tipragot marked this conversation as resolved
Outdated
|
||||
"""
|
||||
simulation_entity = entity.world.new_entity()
|
||||
|
||||
def __collision_handler(a: Entity, b: Entity):
|
||||
entity[physics.CollisionHandler].callback(a, b)
|
||||
return component_type not in b
|
||||
|
||||
simulation_entity.set(
|
||||
Position(entity[Position]),
|
||||
Scale(entity[Scale]),
|
||||
physics.Velocity(entity[physics.Velocity]),
|
||||
Origin(entity[Origin]),
|
||||
physics.CollisionHandler(__collision_handler),
|
||||
)
|
||||
physics.move_entity(simulation_entity, entity[physics.Velocity] * 500)
|
||||
return simulation_entity
|
||||
|
||||
|
||||
def _update_bot(world: World):
|
||||
"""
|
||||
Fonction qui update les mouvement du bot
|
||||
"""
|
||||
# On récupère la balle la plus proche du bot
|
||||
ball_query = world.query(Position, physics.Velocity, physics.CollisionHandler)
|
||||
if ball_query == set():
|
||||
return None
|
||||
|
||||
ball = max(ball_query, key=lambda entity: entity[Position].y)
|
||||
for bar in world.query(Player2):
|
||||
bar.remove(physics.Solid)
|
||||
entity = world.new_entity()
|
||||
entity.set(
|
||||
Position(ball[Position]),
|
||||
Scale(ball[Scale]),
|
||||
physics.Velocity(ball[physics.Velocity]),
|
||||
Origin(ball[Origin]),
|
||||
physics.CollisionHandler(__bounce_on_player_simul),
|
||||
)
|
||||
physics.move_entity(entity, entity[physics.Velocity] * 500)
|
||||
target = entity[Position].y
|
||||
for bar in world.query(Player2):
|
||||
diff = target - bar[Position].y
|
||||
|
||||
if abs(diff) > 10:
|
||||
bar[Position].y += (diff / abs(diff)) * 300 * world[Delta]
|
||||
# bar[Position].y += diff
|
||||
bar.set(physics.Solid())
|
||||
entity.destroy()
|
||||
# On récupère le bot et le joueur
|
||||
bot = world.query(Player2).pop()
|
||||
player = world.query(Player1).pop()
|
||||
|
||||
# On trouve l'endroit ou la balle va arriver sur le mur de droite
|
||||
bot.remove(physics.Solid)
|
||||
right_wall_ball = __simulate_wall_position(ball, RightWall)
|
||||
right_touch_height = right_wall_ball[Position].y
|
||||
right_wall_ball.destroy()
|
||||
bot.set(physics.Solid())
|
||||
|
||||
# On teste différentes possitions pour voir laquelle la plus éloigné du joueur
|
||||
# Mais seulement si la balle vas vers la droite car sinon elle touchera le mur
|
||||
# de gauche sans intervention du bot
|
||||
if ball[physics.Velocity].x > 0:
|
||||
bot_base_y = bot[Position].y
|
||||
target: float = right_touch_height
|
||||
better_distance = None
|
||||
for offset in [-100, -50, 0, 50, 100]:
|
||||
bot[Position].y = right_touch_height + offset
|
||||
player.remove(physics.Solid)
|
||||
left_wall_ball = __simulate_wall_position(ball, LeftWall)
|
||||
player.set(physics.Solid())
|
||||
left_touch_height = left_wall_ball[Position].y
|
||||
left_wall_ball.destroy()
|
||||
if (
|
||||
better_distance is None
|
||||
or abs(left_touch_height - player[Position].y) > better_distance
|
||||
):
|
||||
better_distance = abs(left_touch_height - player[Position].y)
|
||||
target = right_touch_height + offset
|
||||
bot[Position].y = bot_base_y
|
||||
else:
|
||||
target = right_touch_height
|
||||
|
||||
# On se déplace vers la meilleure option
|
||||
diff = target - bot[Position].y
|
||||
if abs(diff) > 10:
|
||||
bot[Position].y += (diff / abs(diff)) * 300 * world[Delta]
|
||||
|
||||
|
||||
def __update_scores(world: World, ball: Entity):
|
||||
|
|
Loading…
Reference in a new issue
affin ?