Skip to content

Commit

Permalink
akshwali working weapons (finally)
Browse files Browse the repository at this point in the history
  • Loading branch information
theunrealtarik committed Feb 25, 2024
1 parent 2d5746e commit 4f51f34
Show file tree
Hide file tree
Showing 8 changed files with 188 additions and 168 deletions.
91 changes: 45 additions & 46 deletions src/client/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,7 @@ impl NetUpdateHandle for Game {
.into_iter()
.map(|(id, data)| {
let client_id = ClientId::from_raw(id);
let mut enemy = Enemy::new(
let enemy = Enemy::new(
client_id,
data.position.0,
data.position.1,
Expand All @@ -178,9 +178,6 @@ impl NetUpdateHandle for Game {
Rc::clone(&self.assets),
);

enemy.inventory.select(data.weapon);
enemy.inventory.add(Weapon::new(data.weapon));

(client_id, enemy)
})
.collect::<Vec<(ClientId, Enemy)>>()
Expand All @@ -202,7 +199,7 @@ impl NetUpdateHandle for Game {
local_player.inventory.add(Weapon::new(data.weapon));
} else {
let id = ClientId::from_raw(data._id);
let mut enemy = Enemy::new(
let enemy = Enemy::new(
id,
pos_x,
pos_y,
Expand All @@ -211,18 +208,43 @@ impl NetUpdateHandle for Game {
Rc::clone(&self.assets),
);

enemy.inventory.select(data.weapon);
enemy.inventory.add(Weapon::new(data.weapon));
self.world.enemies.insert(id, enemy);
}
}
// these run at every frame
GameNetworkPacket::NET_PLAYER_WORLD_POSITION(id, (x, y)) => {
if let Some(enemy) = self.world.enemies.get_mut(&ClientId::from_raw(id)) {
enemy.rectangle.x = x;
enemy.rectangle.y = y;
_ => {}
}
};
}

while let Some(message) = network
.client
.receive_message(DefaultChannel::ReliableUnordered)
{
if let Ok(packet) = rmp_serde::from_slice::<GameNetworkPacket>(&message) {
match packet {
GameNetworkPacket::NET_PLAYER_LEFT(id) => {
log::info!("player {:?} left", id);
self.world
.enemies
.remove(&ClientId::from_raw(id))
.expect("failed to remove player data");
}
GameNetworkPacket::NET_PLAYER_RESPAWN(d_id, data) => {
if d_id == network.transport.client_id().raw() {
local_player.rectangle.x = data.position.0;
local_player.rectangle.y = data.position.1;
local_player.health = data.health;
local_player.ready = true;
local_player.inventory.cash = data.cash;
local_player.inventory.reset_weapons();
}
}
GameNetworkPacket::NET_PLAYER_KILL_REWARD(data) => {
local_player.inventory.cash = data.cash;
}
GameNetworkPacket::NET_PLAYER_WEAPON(variant) => {
local_player.inventory.add(variant.weapon_instance());
}
GameNetworkPacket::NET_PROJECTILE_CREATE(projectile) => {
self.world.projectiles.insert(
projectile.id,
Expand Down Expand Up @@ -254,40 +276,11 @@ impl NetUpdateHandle for Game {
}
}
}
_ => {}
}
};
}

while let Some(message) = network
.client
.receive_message(DefaultChannel::ReliableUnordered)
{
if let Ok(packet) = rmp_serde::from_slice::<GameNetworkPacket>(&message) {
match packet {
GameNetworkPacket::NET_PLAYER_LEFT(id) => {
log::info!("player {:?} left", id);
self.world
.enemies
.remove(&ClientId::from_raw(id))
.expect("failed to remove player data");
}
GameNetworkPacket::NET_PLAYER_RESPAWN(d_id, data) => {
if d_id == network.transport.client_id().raw() {
local_player.rectangle.x = data.position.0;
local_player.rectangle.y = data.position.1;
local_player.health = data.health;
local_player.ready = true;
local_player.inventory.cash = data.cash;
local_player.inventory.reset_weapons();
GameNetworkPacket::NET_PLAYER_WEAPON_SELECT(id, variant) => {
if let Some(enemy) = self.world.enemies.get_mut(&ClientId::from_raw(id)) {
enemy.weapon = Some(variant)
}
}
GameNetworkPacket::NET_PLAYER_KILL_REWARD(data) => {
local_player.inventory.cash = data.cash;
}
GameNetworkPacket::NET_PLAYER_WEAPON(variant) => {
local_player.inventory.add(variant.weapon_instance());
}
_ => {}
}
}
Expand All @@ -301,6 +294,12 @@ impl NetUpdateHandle for Game {
puppet.orientation = orientation
}
}
GameNetworkPacket::NET_PLAYER_WORLD_POSITION(id, (x, y)) => {
if let Some(enemy) = self.world.enemies.get_mut(&ClientId::from_raw(id)) {
enemy.rectangle.x = x;
enemy.rectangle.y = y;
}
}
_ => {}
}
}
Expand All @@ -326,7 +325,7 @@ impl NetUpdateHandle for Game {
);

network.client.send_message(
DefaultChannel::ReliableOrdered,
DefaultChannel::ReliableUnordered,
GameNetworkPacket::NET_PROJECTILE_CREATE(ProjectileData {
id: p.id,
position: (p.position.x, p.position.y),
Expand Down Expand Up @@ -363,7 +362,7 @@ impl NetUpdateHandle for Game {

let position = local_player.move_to(position);
network.client.send_message(
DefaultChannel::ReliableUnordered,
DefaultChannel::Unreliable,
GameNetworkPacket::NET_PLAYER_WORLD_POSITION(
network.uuid,
(position.x, position.y),
Expand Down
2 changes: 1 addition & 1 deletion src/lib/configs/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use crate::types::Health;

pub static INITIAL_PAYLOAD_SIZE: usize = 255;

pub static WORLD_TILE_SIZE: f32 = 50.0;
pub static WORLD_TILE_SIZE: f32 = 70.0;
pub static ENTITY_PLAYER_SIZE: f32 = WORLD_TILE_SIZE * 0.8;
pub static ENTITY_WEAPON_SIZE: f32 = ENTITY_PLAYER_SIZE * 0.0018;
pub static ENTITY_PLAYER_MAX_HEALTH: Health = 100;
Expand Down
19 changes: 10 additions & 9 deletions src/lib/entities/enemy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@ use raylib::prelude::*;

use nalgebra::Vector2;
use renet::ClientId;
use std::path::is_separator;
use std::rc::Rc;

use super::Invenotry;
use super::WeaponVariant;

use crate::configs::*;
use crate::core::*;
Expand All @@ -17,7 +18,7 @@ pub struct Enemy {
pub rectangle: Rectangle,
pub origin: Vector2<f32>,
pub health: Health,
pub inventory: Invenotry,
pub weapon: Option<WeaponVariant>,
assets: SharedAssets<GameAssets>,
}

Expand All @@ -36,7 +37,7 @@ impl Enemy {
rectangle: Rectangle::new(x, y, ENTITY_PLAYER_SIZE as f32, ENTITY_PLAYER_SIZE as f32),
origin: Default::default(),
health: hp,
inventory: Invenotry::new(Rc::clone(&assets)),
weapon: None,
assets,
}
}
Expand All @@ -47,12 +48,12 @@ impl RenderHandle for Enemy {
where
Self: AssetsHandle,
{
d.draw_rectangle_pro(self.rectangle, RVector2::zero(), 0.0, Color::RED);

let radius = self.rectangle.width / 2.0;
let origin = Vector2::new(self.rectangle.x, self.rectangle.y).add_scalar(radius);
self.inventory
.render_weapon(d, &self.rectangle, self.orientation);
let assets = Rc::clone(&self.assets);
d.draw_rectangle_pro(self.rectangle, RVector2::zero(), 0.0, Color::WHITE);
if let Some(wpn) = self.weapon {
wpn.weapon_instance()
.render_weapon(d, &self.rectangle, self.orientation, assets);
}
}
}

Expand Down
29 changes: 20 additions & 9 deletions src/lib/entities/player.rs
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,7 @@ impl NetUpdateHandle for Player {
if let Some(wpn) = self.inventory.selected_weapon_mut() {
if handle.is_key_pressed(KeyboardKey::KEY_R) {
self.reloading = true;
self.timers.add(Timers::PlayerReloading, Instant::now());
self.timers.add(Timers::PlayerReloading);
}

if self.reloading
Expand All @@ -215,16 +215,27 @@ impl NetUpdateHandle for Player {
for wpn_variant in WeaponVariant::VARIANTS {
let wpn = Weapon::new(*wpn_variant);

if !self.inventory.has(wpn_variant) {
network.client.send_message(
DefaultChannel::ReliableUnordered,
GameNetworkPacket::NET_PLAYER_WEAPON(*wpn_variant)
if handle.is_key_pressed(wpn.equip_key()) && !self.reloading {
if !self.inventory.has(wpn_variant) {
network.client.send_message(
DefaultChannel::ReliableUnordered,
GameNetworkPacket::NET_PLAYER_WEAPON(*wpn_variant)
.serialized()
.unwrap(),
)
} else {
self.inventory.select(*wpn_variant);
network.client.send_message(
DefaultChannel::ReliableUnordered,
GameNetworkPacket::NET_PLAYER_WEAPON_SELECT(
network.transport.client_id().raw(),
*wpn_variant,
)
.serialized()
.unwrap(),
)
} else if handle.is_key_pressed(wpn.equip_key()) && !self.reloading {
self.inventory.select(*wpn_variant);
};
)
}
}
}
}
}
Expand Down
Loading

0 comments on commit 4f51f34

Please sign in to comment.