Skip to content

Commit

Permalink
fixed players killing themselves
Browse files Browse the repository at this point in the history
the bullets had no player assosiated with them so upon shooting, they
were immediately in the player's hitbox and killed themselves. Now fixed
  • Loading branch information
moltenwolfcub committed Jan 11, 2025
1 parent bdfff13 commit 7730bd7
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 13 deletions.
13 changes: 7 additions & 6 deletions common/bullet.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@

from typing import Optional
from common.data_types import Vec2D
from common.player import CommonPlayer


class CommonBullet:
ENCODED_SIZE: int = 4

def __init__(self, pos: Vec2D, shoot_angle: int) -> None:

self.pos = pos

self.shoot_angle = shoot_angle
def __init__(self, pos: Vec2D, shoot_angle: int, shooter: Optional[CommonPlayer] = None) -> None:
self.owner: Optional[CommonPlayer] = shooter
self.pos: Vec2D = pos
self.shoot_angle: int = shoot_angle

def encode(self) -> bytes:
b = self.pos.x.to_bytes(2)
Expand All @@ -22,7 +23,7 @@ def decode(bytes: bytes) -> 'CommonBullet':
x = int.from_bytes(bytes[:2])
y = int.from_bytes(bytes[2:4])

return CommonBullet(pos=Vec2D(x, y), shoot_angle=-1)
return CommonBullet(pos=Vec2D(x, y), shoot_angle=-1, shooter=None)

def __str__(self) -> str:
return f"Bullet[pos= {self.pos}, shoot_angle= {self.shoot_angle}]"
Expand Down
31 changes: 25 additions & 6 deletions server/game_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,25 +54,36 @@ def update(self) -> None:
self.bullets.remove(bullet)
continue

dead_conns: list[Connection] = []
for player in self.players:
player_rect: Rect = Rect(
(player.pos-Vec2D(Settings.player_radius, Settings.player_radius)),
(player.pos+Vec2D(Settings.player_radius, Settings.player_radius)),
)
conn: Connection = self.server.open_connections[player.id]
conn: Optional[Connection] = self.get_connection(player.id)
if conn is None:
print(f"Couldn't find connection assosiated with player id {player.id}")
raise LookupError()

gone_bullets: list[CommonBullet] = []

for bullet in self.bullets:
if player_rect.contains(bullet.pos):
self.server.send(conn, S2CDisconnectPlayer(S2CDisconnectPlayer.KILLED))
self.server.close_connection(conn)
if bullet.owner is player:
continue

if player_rect.contains(bullet.pos):
dead_conns.append(conn)
gone_bullets.append(bullet)

bullet_dirty = True
players_dirty = True
break

self.bullets = [b for b in self.bullets if b not in gone_bullets]

for c in dead_conns:
self.server.send(c, S2CDisconnectPlayer(S2CDisconnectPlayer.KILLED))
self.server.close_connection(c)

self.bullets = [b for b in self.bullets if b not in gone_bullets]

if players_dirty:
self.server.broadcast(S2CPlayers(self.players))
Expand Down Expand Up @@ -107,3 +118,11 @@ def get_player(self, player_id: int) -> Optional[CommonPlayer]:

print(f"Couldn't find player with ID {player_id}")
return None

def get_connection(self, player_id: int) -> Optional[Connection]:
for conn in self.server.open_connections:
if conn.player_id == player_id:
return conn # if more than 1 connection with same ID something very wrong

print(f"Couldn't find connection with player ID {player_id}")
return None
2 changes: 1 addition & 1 deletion server/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -274,7 +274,7 @@ def handle_packet(self, raw_packet: RawPacket) -> Optional[Exception]:
print(f"Error! No player assosiated with connection: {raw_packet.sender}")
return LookupError()

self.game.bullets.append(CommonBullet(shooting_player.pos.clone(), bullet_packet.angle))
self.game.bullets.append(CommonBullet(pos=shooting_player.pos.clone(), shoot_angle=bullet_packet.angle, shooter=shooting_player))

self.broadcast(S2CBullets(self.game.bullets))

Expand Down

0 comments on commit 7730bd7

Please sign in to comment.