Skip to content

Instantly share code, notes, and snippets.

@vinci6k
Last active October 10, 2020 22:46
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save vinci6k/3609e8dd32d7bd3e9d8eadd5d6490f42 to your computer and use it in GitHub Desktop.
Save vinci6k/3609e8dd32d7bd3e9d8eadd5d6490f42 to your computer and use it in GitHub Desktop.
Smooth field of view (FOV) change in Source Engine games.
# ../smooth_fov/smooth_fov.py
# Source.Python
from players.entity import Player
from engines.server import global_vars
from commands.say import SayCommand
def change_fov(index, target_fov, rate=0.3):
"""Changes a player's field of view (FOV) smoothly.
Args:
index (int): A valid player index.
target_fov (int): The new FOV value.
rate (float): Duration of the FOV transition (in seconds).
Raises:
OverflowError: If 'target_fov' is less than 0 or greater than 255.
"""
player = Player(index)
# Get the player's current FOV.
old_fov = player.fov
if old_fov == 0:
# When changing a player's FOV for the first time, the value of 'fov'
# will be zero. So we get the 'default_fov' instead.
old_fov = player.default_fov
# Time when the FOV starts changing.
player.fov_time = global_vars.current_time
# Duration of the transition (in seconds).
player.fov_rate = rate
# The FOV will transition from 'fov_start' to 'fov'.
player.fov_start = old_fov
player.fov = target_fov
@SayCommand('fov')
def change_fov_command(command, index, team_only=False):
"""Chat command for changing your own FOV.
Example:
>>> fov 120 2
This will change your FOV to 120 over the next 2 seconds.
>>> fov 0 0
This will instantly change your FOV to the default value (90).
"""
# Was the new FOV value not specified? (required)
if len(command) <= 1:
return
try:
target_fov = clamp(abs(int(command[1])), 0, 255)
except ValueError:
return
rate = 0.3
# Was the rate specified? (optional)
if len(command) >= 3:
try:
rate = abs(float(command[2]))
except ValueError:
pass
change_fov(index, target_fov, rate)
def clamp(value, min_value, max_value):
"""Restricts 'value' to the specified range."""
return max(min_value, min(value, max_value))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment