Skip to content

Commit

Permalink
feat!: Add Buff, Mayhem and Reset commands
Browse files Browse the repository at this point in the history
  • Loading branch information
PraxTube committed Nov 3, 2023
1 parent dc26296 commit eba7c86
Show file tree
Hide file tree
Showing 10 changed files with 163 additions and 28 deletions.
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "ace-of-the-heavens"
version = "0.2.2"
version = "0.2.3"
edition = "2021"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
Expand Down
Binary file modified assets/sounds/damage.ogg
Binary file not shown.
87 changes: 86 additions & 1 deletion src/console/command/commands.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,92 @@ pub struct AceCommandPlugin;

impl Plugin for AceCommandPlugin {
fn build(&self, app: &mut App) {
app.add_console_command::<Nerf, _>(nerf_command);
app.add_console_command::<Mayhem, _>(mayhem_command)
.add_console_command::<Buff, _>(buff_command)
.add_console_command::<Reset, _>(reset_command)
.add_console_command::<Nerf, _>(nerf_command);
}
}

/// Let there be mayhem
#[derive(Parser, ConsoleCommand)]
#[command(name = "mayhem")]
pub struct Mayhem;

fn mayhem_command(
mut cmd: ConsoleCommand<Mayhem>,
mut socket: ResMut<AceSocket>,
mut command_queque: ResMut<CommandQueue>,
) {
if let Some(Ok(Mayhem)) = cmd.take() {
for p in socket.players() {
if let PlayerType::Remote(peer_id) = p {
let timestamp = Utc::now().timestamp_millis();
for i in 0..2 {
let push_cmd = AceCommands::Buff(i, 9, timestamp);
socket.send_tcp_message(peer_id, &push_cmd.to_string());
command_queque.queue.push(push_cmd.clone());
}
};
}
cmd.ok();
}
}

/// Set buff level for one player
#[derive(Parser, ConsoleCommand)]
#[command(name = "buff")]
pub struct Buff {
/// The handle of the player from 0 to PLAYER_COUNT - 1
pub handle: usize,
/// Buff Level from 0 (default, no nerf) to 9 (max buff)
pub level: usize,
}

fn buff_command(
mut cmd: ConsoleCommand<Buff>,
mut socket: ResMut<AceSocket>,
mut command_queque: ResMut<CommandQueue>,
) {
if let Some(Ok(Buff { handle, level })) = cmd.take() {
if handle >= PLAYER_COUNT || level >= 10 {
cmd.failed();
return;
}
for p in socket.players() {
if let PlayerType::Remote(peer_id) = p {
let timestamp = Utc::now().timestamp_millis();
let push_cmd = AceCommands::Buff(handle, level, timestamp);
socket.send_tcp_message(peer_id, &push_cmd.to_string());
command_queque.queue.push(push_cmd.clone());
};
}
cmd.ok();
}
}

/// Reset back to default
#[derive(Parser, ConsoleCommand)]
#[command(name = "reset")]
pub struct Reset;

fn reset_command(
mut cmd: ConsoleCommand<Reset>,
mut socket: ResMut<AceSocket>,
mut command_queque: ResMut<CommandQueue>,
) {
if let Some(Ok(Reset)) = cmd.take() {
for p in socket.players() {
if let PlayerType::Remote(peer_id) = p {
let timestamp = Utc::now().timestamp_millis();
for i in 0..2 {
let push_cmd = AceCommands::Nerf(i, 0, timestamp);
socket.send_tcp_message(peer_id, &push_cmd.to_string());
command_queque.queue.push(push_cmd.clone());
}
};
}
cmd.ok();
}
}

Expand Down
26 changes: 24 additions & 2 deletions src/console/command/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use chrono::Utc;
pub use commands::AceCommandPlugin;

mod commands;
mod player_nerf_stats;
mod player_stats;

use std::fmt::Display;

Expand All @@ -17,12 +17,16 @@ const MIN_TIME_THRESHOLD: i64 = 1500;

#[derive(Clone)]
pub enum AceCommands {
Buff(usize, usize, i64),
Nerf(usize, usize, i64),
}

impl Display for AceCommands {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
Self::Buff(handle, level, timestamp) => {
write!(f, "buff {} {} {}", handle, level, timestamp)
}
Self::Nerf(handle, level, timestamp) => {
write!(f, "nerf {} {} {}", handle, level, timestamp)
}
Expand All @@ -39,6 +43,15 @@ impl AceCommands {
}

match parts[0] {
"buff" => {
if let Ok(handle) = parts[1].parse() {
if let Ok(level) = parts[2].parse() {
if let Ok(timestamp) = parts[3].parse() {
return Some(AceCommands::Buff(handle, level, timestamp));
}
}
}
}
"nerf" => {
if let Ok(handle) = parts[1].parse() {
if let Ok(level) = parts[2].parse() {
Expand All @@ -62,14 +75,23 @@ pub fn apply_commands(
let mut skipped_queue = Vec::<AceCommands>::default();
for command in command_queque.queue.drain(..) {
match command {
AceCommands::Buff(handle, level, timestamp) => {
if Utc::now().timestamp_millis() - timestamp < MIN_TIME_THRESHOLD {
warn!("skipping this command as it was added too late");
skipped_queue.push(AceCommands::Buff(handle, level, timestamp));
continue;
}
let buffed_stats = player_stats::buff_stats(level);
stats.stats[handle] = buffed_stats;
}
AceCommands::Nerf(handle, level, timestamp) => {
if Utc::now().timestamp_millis() - timestamp < MIN_TIME_THRESHOLD {
warn!("skipping this command as it was added too late");
skipped_queue.push(AceCommands::Nerf(handle, level, timestamp));
continue;
}
info!("applying nerf command");
let nerfed_stats = player_nerf_stats::nerf_stats(level);
let nerfed_stats = player_stats::nerf_stats(level);
stats.stats[handle] = nerfed_stats;
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,33 @@
use bevy::utils::default;

use crate::player::{PlayerStats, MIN_SPEED};

const MAX_BUFF_STATS: PlayerStats = PlayerStats {
max_speed: 10000.0,
max_health: 10000000,
bullet_damage: 1000,
bullet_reload_time: 0.0,
bullet_heat: 0,
rocket_reload_time: 0.0,
dodge_time: 0.5,
dodge_cooldown: 0.1,
};

const MAX_NERF_STATS: PlayerStats = PlayerStats {
max_speed: MIN_SPEED,
max_health: 1,
bullet_damage: 1,
bullet_reload_time: 1.0,
bullet_heat: 100,
rocket_reload_time: 10.0,
dodge_time: 0.1,
dodge_cooldown: 2.5,
};

pub fn buff_stats(_level: usize) -> PlayerStats {
MAX_BUFF_STATS
}

pub fn nerf_stats(level: usize) -> PlayerStats {
let min_nerf = PlayerStats::default();
let max_nerf = MAX_NERF_STATS;
Expand All @@ -19,7 +40,11 @@ pub fn nerf_stats(level: usize) -> PlayerStats {
- ((min_nerf.max_health - max_nerf.max_health) as f32 * percent) as u32,
bullet_damage: min_nerf.bullet_damage
- ((min_nerf.bullet_damage - max_nerf.bullet_damage) as f32 * percent) as u32,
bullet_reload_time: min_nerf.bullet_reload_time
+ (max_nerf.bullet_reload_time - min_nerf.bullet_reload_time) * percent,
rocket_reload_time: min_nerf.rocket_reload_time
+ (max_nerf.rocket_reload_time - min_nerf.rocket_reload_time) * percent,
dodge_time: min_nerf.dodge_time + (max_nerf.dodge_time - min_nerf.dodge_time) * percent,
..default()
}
}
23 changes: 10 additions & 13 deletions src/player/dodge.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,9 @@ use crate::{input::dodge, misc::utils::quat_from_vec3, network::GgrsConfig};

use super::Player;

const DODGE_COOLDOWN: f32 = 2.5;
const DODGE_TIME: f32 = 0.5;

const DODGE_REFRESH_TIME: f32 = 0.50;

#[derive(Component, Reflect)]
#[derive(Component, Reflect, Default)]
#[reflect(Hash)]
pub struct DodgeTimer(Timer);

Expand All @@ -27,10 +24,10 @@ pub struct DodgeRefreshTimer {
handle: usize,
}

impl Default for DodgeTimer {
fn default() -> Self {
let mut timer = Timer::from_seconds(DODGE_COOLDOWN, TimerMode::Once);
timer.set_elapsed(Duration::from_secs_f32(DODGE_COOLDOWN));
impl DodgeTimer {
pub fn new(cooldown: f32) -> Self {
let mut timer = Timer::from_seconds(cooldown, TimerMode::Once);
timer.set_elapsed(Duration::from_secs_f32(cooldown));
Self(timer)
}
}
Expand All @@ -48,7 +45,7 @@ impl Hash for DodgeTimer {
}
}

pub fn initiate_dodge(
pub fn start_dodging(
mut commands: Commands,
assets: Res<GameAssets>,
frame: Res<FrameCount>,
Expand All @@ -75,19 +72,19 @@ pub fn initiate_dodge(
}
}

pub fn animate_dodge(mut players: Query<(&mut Transform, &mut Player, &DodgeTimer)>) {
pub fn animate_dodges(mut players: Query<(&mut Transform, &mut Player, &DodgeTimer)>) {
for (mut transform, mut player, timer) in &mut players {
if timer.0.elapsed_secs() > DODGE_TIME {
if timer.0.elapsed_secs() > player.stats.dodge_time {
transform.rotation = quat_from_vec3(transform.local_x());
player.dodging = false;
continue;
}

transform.rotate_local_x(2.0 * PI / DODGE_TIME / 60.0);
transform.rotate_local_x(2.0 * PI / player.stats.dodge_time / 60.0);
}
}

pub fn tick_dodge_timer(
pub fn tick_dodge_timers(
mut commands: Commands,
assets: Res<GameAssets>,
frame: Res<FrameCount>,
Expand Down
14 changes: 11 additions & 3 deletions src/player/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,11 @@ pub struct PlayerStats {
pub max_speed: f32,
pub max_health: u32,
pub bullet_damage: u32,
pub bullet_reload_time: f32,
pub bullet_heat: u32,
pub rocket_reload_time: f32,
pub dodge_time: f32,
pub dodge_cooldown: f32,
}

impl Default for PlayerStats {
Expand All @@ -48,7 +52,11 @@ impl Default for PlayerStats {
max_speed: 400.0 / 60.0,
max_health: 2000,
bullet_damage: 60,
bullet_reload_time: 0.25,
bullet_heat: 80,
rocket_reload_time: 2.5,
dodge_time: 0.5,
dodge_cooldown: 2.5,
}
}
}
Expand Down Expand Up @@ -177,9 +185,9 @@ impl Plugin for PlayerPlugin {
.add_systems(
GgrsSchedule,
(
dodge::tick_dodge_timer,
dodge::initiate_dodge,
dodge::animate_dodge,
dodge::tick_dodge_timers,
dodge::start_dodging,
dodge::animate_dodges,
dodge::animate_dodge_refresh,
)
.chain()
Expand Down
8 changes: 3 additions & 5 deletions src/player/shooting/bullet.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,6 @@ use super::reloading::OVERHEAT;

pub const BULLET_RADIUS: f32 = 3.0;
const BULLET_MOVE_SPEED: f32 = 450.0 / 60.0;
const BULLET_RELOAD_TIME: f32 = 0.25;
const FIRE_HEAT: u32 = 80;

const LEFT_WING_BULLET_SPAWN: Vec3 = Vec3::new(20.0, 10.0, 0.0);
const RIGHT_WING_BULLET_SPAWN: Vec3 = Vec3::new(20.0, -10.0, 0.0);
Expand Down Expand Up @@ -56,8 +54,8 @@ pub struct BulletTimer {
}

impl BulletTimer {
pub fn default() -> BulletTimer {
let mut timer = Timer::from_seconds(BULLET_RELOAD_TIME, TimerMode::Repeating);
pub fn new(reload_time: f32) -> BulletTimer {
let mut timer = Timer::from_seconds(reload_time, TimerMode::Repeating);
timer.tick(timer.duration());
BulletTimer { timer }
}
Expand Down Expand Up @@ -194,7 +192,7 @@ pub fn fire_bullets(
&mut ev_bullet_fired,
);

player.heat += FIRE_HEAT;
player.heat += player.stats.bullet_heat;
bullet_timer.timer.reset();
}
}
Expand Down
4 changes: 2 additions & 2 deletions src/player/spawning.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,9 +50,9 @@ fn spawn_player(
commands
.spawn((
Player::new(handle, stats.clone()),
BulletTimer::default(),
BulletTimer::new(stats.bullet_reload_time),
RocketTimer::new(stats.rocket_reload_time),
DodgeTimer::default(),
DodgeTimer::new(stats.dodge_cooldown),
CollisionEntity::default(),
DebugTransform::new(&transform),
SpriteBundle {
Expand Down

0 comments on commit eba7c86

Please sign in to comment.