Skip to content

Instantly share code, notes, and snippets.

@ajeetraina
Created October 1, 2021 08:43
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 ajeetraina/34c58039d9516918fc65c55c648a74d4 to your computer and use it in GitHub Desktop.
Save ajeetraina/34c58039d9516918fc65c55c648a74d4 to your computer and use it in GitHub Desktop.
Player Hit Function
def hit(self, game_id, user_id, enemy_user_id):
"""
Determines if the projectile has hit a user [user_id]
Extrapolates projectile position based on when projectile has spawned, and the time now.
Publishes a hit even if target is hit.
"""
projectiles = self.games_states[game_id]["projectiles"]
player = self.games_states[game_id]["players"][enemy_user_id]
for projectile in projectiles:
time_diff = self.ts - projectile['timestamp']
orientation = float(projectile["orientation"])
x = projectile['x'] + ( math.cos(orientation) * (projectile['speed'] * time_diff) )
y = projectile['y'] + ( math.sin(orientation) * (projectile['speed'] * time_diff) )
if abs(player['x'] - x < 50) and abs(player['y'] - y < 50):
self.games_states[game_id]['players'][projectile['user_id']]['score'] += 1
execute('PUBLISH', game_id, f"hit;{enemy_user_id}")
return False
return False
def respawn(self, game_id, user_id, x, y):
player = self.games_states[game_id]["players"][user_id]
player["respawns"] = player["respawns"] + 1
player["x"] = x
player["y"] = y
return True
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment