Amélioration du bot pour qu'il tire dans la zone la plus éloigné du joueur #4

Merged
raphael merged 10 commits from smart-bot into main 2024-01-05 17:03:16 +00:00
Showing only changes of commit e7aea2b9b7 - Show all commits

View file

@ -249,11 +249,6 @@ def __bounce_on_player(a: Entity, b: Entity):
return True return True
def __bounce_on_player_simul(a: Entity, b: Entity):
__bounce_on_player(a, b)
return RightWall not in b
def __bounce_on_left_wall(a: Entity, b: Entity): def __bounce_on_left_wall(a: Entity, b: Entity):
if Ball in b: if Ball in b:
world = a.world world = a.world
@ -306,6 +301,28 @@ def __update_move(world: World):
__move_up(world) __move_up(world)
def __simulate_wall_position(entity: Entity, component_type: type):
"""
Simule une entité affin de trouver lorsqu'elle entrera en collision avec une entité contenant un certain composant.
"""
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)
del simulation_entity[physics.Velocity]
return entity
def _update_bot(world: World): def _update_bot(world: World):
""" """
Test. Test.
@ -313,26 +330,16 @@ def _update_bot(world: World):
ball_query = world.query(Position, physics.Velocity, physics.CollisionHandler) ball_query = world.query(Position, physics.Velocity, physics.CollisionHandler)
if ball_query == set(): if ball_query == set():
return None return None
ball = max(ball_query, key=lambda entity: entity[Position].y) ball = max(ball_query, key=lambda entity: entity[Position].y)
for bar in world.query(Player2): for bar in world.query(Player2):
bar.remove(physics.Solid) bar.remove(physics.Solid)
entity = world.new_entity() entity = __simulate_wall_position(ball, RightWall)
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 target = entity[Position].y
for bar in world.query(Player2): for bar in world.query(Player2):
diff = target - bar[Position].y diff = target - bar[Position].y
if abs(diff) > 10: if abs(diff) > 10:
bar[Position].y += (diff / abs(diff)) * 300 * world[Delta] bar[Position].y += (diff / abs(diff)) * 300 * world[Delta]
# bar[Position].y += diff
bar.set(physics.Solid()) bar.set(physics.Solid())
entity.destroy() entity.destroy()