Better simulation

This commit is contained in:
Timéo Cézard 2024-01-04 17:57:14 +01:00
parent d8db3b6de9
commit e7aea2b9b7

View file

@ -249,11 +249,6 @@ def __bounce_on_player(a: Entity, b: Entity):
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):
if Ball in b:
world = a.world
@ -306,6 +301,28 @@ def __update_move(world: 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):
"""
Test.
@ -313,26 +330,16 @@ def _update_bot(world: World):
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)
entity = __simulate_wall_position(ball, RightWall)
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()