Skip to content

Commit

Permalink
implemented basic client bullet
Browse files Browse the repository at this point in the history
  • Loading branch information
moltenwolfcub committed Dec 24, 2024
1 parent 116f677 commit 023e4b3
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 1 deletion.
30 changes: 30 additions & 0 deletions client/bullet.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
from typing import TYPE_CHECKING, Callable

import pygame

from common.bullet import CommonBullet
from common.data_types import Vec2D

if TYPE_CHECKING:
from client.main import Game


class ClientBullet:

def __init__(self, game: 'Game', pos: Vec2D) -> None:
self.game: Game = game

self.pos: Vec2D = pos

self.radius: int = 10

self.rect = pygame.Rect(self.pos.x-self.radius, self.pos.y-self.radius, self.radius*2, self.radius*2)

def draw(self, scaler: Callable[[pygame.Rect], pygame.Rect]) -> None:
draw_rect = scaler(self.rect)

pygame.draw.circle(self.game.screen, (0, 0, 0), draw_rect.center, self.radius)

@staticmethod
def from_common(common: CommonBullet, game: 'Game') -> 'ClientBullet':
return ClientBullet(game, common.pos)
6 changes: 6 additions & 0 deletions client/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import pygame

from client import keybinds
from client.bullet import ClientBullet
from client.network import Network
from client.player import ClientPlayer
from client.settings import Settings
Expand Down Expand Up @@ -43,6 +44,8 @@ def __init__(self, port: int) -> None:
self.players: list[ClientPlayer] = []
self.network.send(C2SRequestPlayerList())

self.bullets: list[ClientBullet] = []

self.movement_codes: list[int] = [0, 0, 0, 0]
self.movement_codes_dirty: bool = False

Expand Down Expand Up @@ -82,6 +85,9 @@ def scaler(r: pygame.Rect) -> pygame.Rect:

self.screen.fill(self.settings.color_bg.to_tuple())

for bullet in self.bullets:
bullet.draw(scaler)

for player in self.players:
player.draw(scaler)

Expand Down
13 changes: 12 additions & 1 deletion client/network.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,14 @@
import sys
from typing import TYPE_CHECKING, Optional

from client.bullet import ClientBullet
from client.player import ClientPlayer
from common import packet_ids
from common.c2s_packets import C2SCreateBullet, C2SHandshake, C2SMovementUpdate
from common.data_types import Vec2D
from common.packet_base import Packet
from common.packet_header import PacketHeader
from common.s2c_packets import S2CHandshake, S2CPlayers
from common.s2c_packets import S2CBullets, S2CHandshake, S2CPlayers

if TYPE_CHECKING:
from client.main import Game
Expand Down Expand Up @@ -126,6 +127,16 @@ def handle_packet(self, raw_packet: bytes) -> Optional[Exception]:
client_players.append(ClientPlayer.from_common(common_player, self.game))

self.game.players = client_players

case packet_ids.S2C_BULLETS:
bullets_packet: S2CBullets = S2CBullets.decode_data(raw_packet)

client_bullets: list[ClientBullet] = []

for common_bullet in bullets_packet.bullets:
client_bullets.append(ClientBullet.from_common(common_bullet, self.game))

self.game.bullets = client_bullets

case _:
print(f"Unknown packet (ID: {packet_type})")
Expand Down

0 comments on commit 023e4b3

Please sign in to comment.