Skip to content
Snippets Groups Projects
Commit fe21742f authored by Fabian Heinrich's avatar Fabian Heinrich
Browse files

Player view can have a restricted range, like a flashlight cone

parent 4ac9c4fa
No related branches found
No related tags found
No related merge requests found
Pipeline #45985 passed
...@@ -88,7 +88,8 @@ player_config: ...@@ -88,7 +88,8 @@ player_config:
player_speed_units_per_seconds: 6 player_speed_units_per_seconds: 6
interaction_range: 1.6 interaction_range: 1.6
restricted_view: False restricted_view: False
view_angle: 95 view_angle: 60
view_range: 3 # in grid units, can be "null"
effect_manager: effect_manager:
FireManager: FireManager:
......
...@@ -153,20 +153,23 @@ class Visualizer: ...@@ -153,20 +153,23 @@ class Visualizer:
direction = pygame.math.Vector2(state["view_restriction"]["direction"]) direction = pygame.math.Vector2(state["view_restriction"]["direction"])
pos = pygame.math.Vector2(state["view_restriction"]["position"]) pos = pygame.math.Vector2(state["view_restriction"]["position"])
angle = state["view_restriction"]["angle"] / 2 angle = state["view_restriction"]["angle"] / 2
range = state["view_restriction"]["range"]
pos = pos * grid_size + pygame.math.Vector2([grid_size / 2, grid_size / 2]) pos = pos * grid_size + pygame.math.Vector2([grid_size / 2, grid_size / 2])
rect_scale = max(width, height) rect_scale = max(width, height)
# rect_scale = 2 * grid_size
left_beam = pos + (direction.rotate(angle) * rect_scale * 2) left_beam = pos + (direction.rotate(angle) * rect_scale * 2)
right_beam = pos + (direction.rotate(-angle) * rect_scale * 2) right_beam = pos + (direction.rotate(-angle) * rect_scale * 2)
offset_front = direction * grid_size * 0.2
pygame.draw.polygon( pygame.draw.polygon(
screen, screen,
colors["black"], colors["black"],
( (
pos - (direction * grid_size * 0.6), pos - offset_front,
left_beam - (direction * grid_size * 0.6), left_beam - offset_front,
left_beam + (direction.rotate(90) * rect_scale), left_beam + (direction.rotate(90) * rect_scale),
pos pos
- (direction * rect_scale * 2) - (direction * rect_scale * 2)
...@@ -175,9 +178,28 @@ class Visualizer: ...@@ -175,9 +178,28 @@ class Visualizer:
- (direction * rect_scale * 2) - (direction * rect_scale * 2)
+ (direction.rotate(-90) * rect_scale), + (direction.rotate(-90) * rect_scale),
right_beam + (direction.rotate(-90) * rect_scale), right_beam + (direction.rotate(-90) * rect_scale),
right_beam - (direction * grid_size * 0.6), right_beam - offset_front,
), ),
) )
if range:
pygame.draw.polygon(
screen,
colors["black"],
(
pos
+ (direction.rotate(90) * rect_scale)
+ (direction * range * grid_size),
pos
+ (direction.rotate(-90) * rect_scale)
+ (direction * range * grid_size),
pos
+ (direction.rotate(-90) * rect_scale)
+ (direction * rect_scale),
pos
+ (direction.rotate(90) * rect_scale)
+ (direction * rect_scale),
),
)
def draw_background( def draw_background(
self, surface: pygame.Surface, width: int, height: int, grid_size: int self, surface: pygame.Surface, width: int, height: int, grid_size: int
......
...@@ -167,6 +167,9 @@ class Environment: ...@@ -167,6 +167,9 @@ class Environment:
self.player_view_angle = self.environment_config["player_config"][ self.player_view_angle = self.environment_config["player_config"][
"view_angle" "view_angle"
] ]
self.player_view_range = self.environment_config["player_config"][
"view_range"
]
self.extra_setup_functions() self.extra_setup_functions()
...@@ -663,8 +666,8 @@ class Environment: ...@@ -663,8 +666,8 @@ class Environment:
for idx, p in enumerate(self.players.values()): for idx, p in enumerate(self.players.values()):
if not (new_positions[idx] == player_positions[idx]).all(): if not (new_positions[idx] == player_positions[idx]).all():
p.turn(player_movement_vectors[idx])
p.move_abs(new_positions[idx]) p.move_abs(new_positions[idx])
p.turn(player_movement_vectors[idx])
def add_player(self, player_name: str, pos: npt.NDArray = None): def add_player(self, player_name: str, pos: npt.NDArray = None):
"""Add a player to the environment. """Add a player to the environment.
...@@ -780,6 +783,7 @@ class Environment: ...@@ -780,6 +783,7 @@ class Environment:
"position": self.players[player_id].pos.tolist(), "position": self.players[player_id].pos.tolist(),
"angle": self.player_view_angle, "angle": self.player_view_angle,
"counter_mask": None, "counter_mask": None,
"range": self.player_view_range,
} }
if self.player_view_restricted if self.player_view_restricted
else None, else None,
......
...@@ -37,6 +37,9 @@ class PlayerConfig: ...@@ -37,6 +37,9 @@ class PlayerConfig:
"""Whether or not the player can see the entire map at once or just a view frustrum.""" """Whether or not the player can see the entire map at once or just a view frustrum."""
view_angle: int | None = None view_angle: int | None = None
"""Angle of the players view if restricted.""" """Angle of the players view if restricted."""
view_range: float | None = None
"""Range of the players view if restricted. In grid units."""
class Player: class Player:
"""Class representing a player in the game environment. A player consists of a name, their position and what """Class representing a player in the game environment. A player consists of a name, their position and what
...@@ -165,7 +168,7 @@ class Player: ...@@ -165,7 +168,7 @@ class Player:
self.holding.combine(returned_by_counter) self.holding.combine(returned_by_counter)
log.debug( log.debug(
f"Self: {self.holding}, {counter.__class__.__name__}: {counter.occupied_by}" f"Self: {self.holding.__class__.__name__}: {self.holding}, {counter.__class__.__name__}: {counter.occupied_by}"
) )
# if isinstance(self.holding, Plate): # if isinstance(self.holding, Plate):
# log.debug(self.holding.clean) # log.debug(self.holding.clean)
......
...@@ -71,6 +71,7 @@ class ViewRestriction(BaseModel): ...@@ -71,6 +71,7 @@ class ViewRestriction(BaseModel):
position: list[float] position: list[float]
angle: int # degrees angle: int # degrees
counter_mask: None | list[bool] counter_mask: None | list[bool]
range: float | None
class StateRepresentation(BaseModel): class StateRepresentation(BaseModel):
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment