Skip to content

Commit

Permalink
created basic server-side implementation of bullet
Browse files Browse the repository at this point in the history
  • Loading branch information
moltenwolfcub committed Dec 24, 2024
1 parent f4d6f3b commit 2866aff
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 0 deletions.
9 changes: 9 additions & 0 deletions common/bullet.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@

from common.data_types import Vec2D


class CommonBullet:

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

self.pos = pos
9 changes: 9 additions & 0 deletions server/game_data.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import random
from typing import TYPE_CHECKING, Optional

from common.bullet import CommonBullet
from common.data_types import Color, Vec2D
from common.player import CommonPlayer
from common.s2c_packets import S2CPlayers
Expand All @@ -16,6 +17,7 @@ def __init__(self, server: 'Server') -> None:
self.settings: Settings = Settings()

self.players: list[CommonPlayer] = []
self.bullets: list[CommonBullet] = []

def update(self) -> None:

Expand All @@ -37,6 +39,13 @@ def update(self) -> None:

if players_dirty:
self.server.broadcast(S2CPlayers(self.players))

for bullet in self.bullets:
bullet.pos.y += 1
if bullet.pos.y > self.settings.world_height:
self.bullets.remove(bullet)
continue
print(f"Bullet: {bullet.pos}")

def add_player(self, player: CommonPlayer) -> None:
self.players.append(player)
Expand Down
3 changes: 3 additions & 0 deletions server/main.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
import _thread
import dataclasses
import queue
import socket
import sys
import time
from typing import Optional

from common import packet_ids
from common.bullet import CommonBullet
from common.c2s_packets import C2SHandshake, C2SMovementUpdate
from common.packet_base import Packet
from common.packet_header import PacketHeader
Expand Down Expand Up @@ -254,6 +256,7 @@ def handle_packet(self, raw_packet: RawPacket) -> Optional[Exception]:
return LookupError()

print(f"Pow from {shooting_player.pos}")
self.game.bullets.append(CommonBullet(dataclasses.replace(shooting_player.pos)))

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

0 comments on commit 2866aff

Please sign in to comment.