Skip to content
Snippets Groups Projects
Commit 7b947393 authored by Florian Schröder's avatar Florian Schröder
Browse files

Merge branch '35-can-push-other-players-around' into 'main'

Resolve "Can push other players around"

Closes #35

See merge request scs/cocosy/overcooked-simulator!9
parents 7f9770db d0d73c30
No related branches found
No related tags found
1 merge request!9Resolve "Can push other players around"
Pipeline #41460 passed
class HoldableItem:
"""Base class for game items which can be held by a player."""
pass
def can_combine(self, other):
return False
......
......@@ -158,6 +158,8 @@ class Environment:
The movement action is a unit 2d vector.
Detects collisions with other players and pushes them out of the way.
Args:
player: The player to move.
move_vector: The movement vector which is a unit-2d-vector of the movement direction
......@@ -167,6 +169,18 @@ class Environment:
step = move_vector * player.move_dist
player.move(step)
if self.detect_collision(player):
collided_players = self.get_collided_players(player)
for collided_player in collided_players:
pushing_vector = collided_player.pos - player.pos
if np.linalg.norm(pushing_vector) != 0:
pushing_vector = pushing_vector / np.linalg.norm(pushing_vector)
old_pos_other = collided_player.pos.copy()
collided_player.move(pushing_vector * (collided_player.move_dist / 2))
if self.detect_collision_counters(
collided_player
) or self.detect_collision_world_bounds(player):
collided_player.move_abs(old_pos_other)
player.move_abs(old_pos)
old_pos = player.pos.copy()
......@@ -198,11 +212,29 @@ class Environment:
Returns: True if the player is intersecting with any object in the environment.
"""
return (
self.detect_player_collision(player)
or self.detect_collision_counters(player)
or self.detect_collision_world_bounds(player)
len(self.get_collided_players(player)) != 0
or self.detect_collision_counters(player)
or self.detect_collision_world_bounds(player)
)
def get_collided_players(self, player: Player) -> list[Player]:
"""Detects collisions between the queried player and other players. Returns the list of the collided players.
A player is modelled as a circle. Collision is detected if the distance between the players is smaller
than the sum of the radius's.
Args:
player: The player to check collisions with other players for.
Returns: The list of other players the player collides with.
"""
other_players = filter(lambda p: p.name != player.name, self.players.values())
def collide(p):
return np.linalg.norm(player.pos - p.pos) <= (player.radius + p.radius)
return list(filter(collide, other_players))
def detect_player_collision(self, player: Player):
"""Detects collisions between the queried player and other players.
A player is modelled as a circle. Collision is detected if the distance between the players is smaller
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment