Skip to content

Commit

Permalink
implemented shooting angles
Browse files Browse the repository at this point in the history
clients send over the shoot angle and the bullets move that angle
  • Loading branch information
moltenwolfcub committed Dec 25, 2024
1 parent 168054c commit 7418e23
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 10 deletions.
8 changes: 5 additions & 3 deletions common/bullet.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,12 @@
class CommonBullet:
ENCODED_SIZE: int = 4

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

self.pos = pos

self.shoot_angle = shoot_angle

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

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

def __str__(self) -> str:
return f"Bullet[pos= {self.pos}]"
return f"Bullet[pos= {self.pos}, shoot_angle= {self.shoot_angle}]"

def __eq__(self, other: object) -> bool:
if not isinstance(other, CommonBullet):
Expand Down
5 changes: 5 additions & 0 deletions common/data_types.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
import math
from dataclasses import dataclass

import pygame


@dataclass
class Vec2D:
Expand All @@ -13,6 +15,9 @@ def clone(self) -> 'Vec2D':

def is_none(self) -> bool:
return self.x == 0 and self.y == 0

def in_rect(self, rect: pygame.Rect) -> bool:
return rect.collidepoint(self.x, self.y)

def to_tuple(self) -> tuple[int, int]:
return (self.x, self.y)
Expand Down
15 changes: 13 additions & 2 deletions server/game_data.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
import math
import random
from typing import TYPE_CHECKING, Optional

import pygame

from common.bullet import CommonBullet
from common.data_types import Color, Vec2D
from common.player import CommonPlayer
Expand Down Expand Up @@ -42,10 +45,18 @@ def update(self) -> None:

bullet_dirty = False
for bullet in self.bullets:
bullet.pos.y += self.settings.bullet_speed

# pol to cart
shifted_angle: float = (bullet.shoot_angle / 100) - 90

rawx = int(self.settings.bullet_speed * math.cos(math.radians(shifted_angle)))
rawy = int(self.settings.bullet_speed * math.sin(math.radians(shifted_angle)))

bullet.pos += Vec2D(rawx, rawy)

bullet_dirty = True

if bullet.pos.y > self.settings.world_height:
if not bullet.pos.in_rect(pygame.Rect(0, 0, self.settings.world_width, self.settings.world_height)):
self.bullets.remove(bullet)
continue

Expand Down
6 changes: 4 additions & 2 deletions server/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

from common import packet_ids
from common.bullet import CommonBullet
from common.c2s_packets import C2SHandshake, C2SMovementUpdate
from common.c2s_packets import C2SCreateBullet, C2SHandshake, C2SMovementUpdate
from common.packet_base import Packet
from common.packet_header import PacketHeader
from common.s2c_packets import S2CBullets, S2CFailedHandshake, S2CHandshake, S2CPlayers, S2CSendID
Expand Down Expand Up @@ -257,13 +257,15 @@ def handle_packet(self, raw_packet: RawPacket) -> Optional[Exception]:
player.mov_dir = movement_packet.mov_dir

case packet_ids.C2S_CREATE_BULLET:
bullet_packet: C2SCreateBullet = C2SCreateBullet.decode_data(raw_packet.data)

shooting_player = self.game.get_player(self.open_connections[raw_packet.sender])

if shooting_player is None:
print(f"Error! No player assosiated with connection: {raw_packet.sender}")
return LookupError()

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

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

Expand Down
6 changes: 3 additions & 3 deletions test/s2c_packet_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,9 @@ def testPlayersPacket(self):

def testBulletsPacket(self):
packet = S2CBullets([
CommonBullet(Vec2D(128, 16)),
CommonBullet(Vec2D( 0, 250)),
CommonBullet(Vec2D(830, 678)),
CommonBullet(Vec2D(128, 16), -1),
CommonBullet(Vec2D( 0, 250), -1),
CommonBullet(Vec2D(830, 678), -1),
])

encoded = packet.encode()
Expand Down

0 comments on commit 7418e23

Please sign in to comment.