Created
October 1, 2021 08:43
-
-
Save ajeetraina/34c58039d9516918fc65c55c648a74d4 to your computer and use it in GitHub Desktop.
Player Hit Function
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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