Skip to content

Commit

Permalink
fixed get_this_player method on client
Browse files Browse the repository at this point in the history
broken impl meant it couldn't update when clients logged off. was using
ID as index in list rather than looking up by ID
  • Loading branch information
moltenwolfcub committed Dec 25, 2024
1 parent 46c00f9 commit 52fefdd
Showing 1 changed file with 18 additions and 4 deletions.
22 changes: 18 additions & 4 deletions client/main.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import _thread
import math
import sys
from typing import Optional

import pygame

Expand All @@ -21,7 +22,7 @@ def __init__(self, port: int) -> None:
self.settings: Settings = Settings()

self.this_player_id: int = -1

self.initialise_network(port)

# region SCREEN-SETUP
Expand Down Expand Up @@ -185,8 +186,18 @@ def screen_to_world(self, screen: Vec2D) -> Vec2D:

return world_vec

def get_this_player(self) -> ClientPlayer:
return self.players[self.this_player_id]
def get_this_player(self) -> Optional[ClientPlayer]:
if self.this_player_id == -1:
print("ID hasn't been set yet")
return None

for p in self.players:
if p.id == self.this_player_id:
return p
else:
print("Couldn't find this_player")
return None
# return self.players[self.this_player_id]

def exit_game(self) -> None:
self.network.close_connection()
Expand All @@ -199,7 +210,10 @@ def shoot(self) -> None:
raw_mouse_pos: Vec2D = Vec2D.from_tuple(pygame.mouse.get_pos())
mouse_pos: Vec2D = self.screen_to_world(raw_mouse_pos)

player_pos: Vec2D = self.get_this_player().pos
this_player: Optional[ClientPlayer] = self.get_this_player()
if this_player is None:
return # can't find self to shoot from
player_pos = this_player.pos

print(self.this_player_id)

Expand Down

0 comments on commit 52fefdd

Please sign in to comment.