Skip to content

Commit

Permalink
Wind attacks (#25)
Browse files Browse the repository at this point in the history
* implement attacks

* add wgpu-glyph for text

* refactor respawn cooldown, add attack cooldown

* add text for displaying cooldowns

* add ammo for attacks
  • Loading branch information
alanlwang1 committed May 3, 2023
1 parent 66c5e4e commit e5d8f6a
Show file tree
Hide file tree
Showing 12 changed files with 459 additions and 66 deletions.
105 changes: 105 additions & 0 deletions Cargo.lock

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

Binary file added assets/Inconsolata-Regular.ttf
Binary file not shown.
1 change: 1 addition & 0 deletions client/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ cfg-if = "1"
serde = { version = "1.0", features = ["derive"] }
serde_json = "1.0"
hashbrown = "0.13.2"
wgpu_glyph = "0.19.0"
bus = "2.4.0"

[build-dependencies]
Expand Down
8 changes: 4 additions & 4 deletions client/src/inputs/mod.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
use crate::inputs::handlers::{handle_camera_update, handle_game_key_input, GameKeyKind};
use common::communication::commons::Protocol;
use common::core::command::Command;
use common::core::command::Command::{Action, Jump, Spawn};
use common::core::command::GameAction::Attack;
use common::core::command::Command::{Attack, Jump, Spawn};
use common::core::command::{Command};
use glm::{vec3, Vec3};
use log::debug;

Expand Down Expand Up @@ -65,7 +64,8 @@ impl InputEventProcessor {
VirtualKeyCode::Space => Some((GameKeyKind::Pressable, Jump)),
VirtualKeyCode::LShift => Some((GameKeyKind::Pressable, Spawn)),
// match PressRelease keys
VirtualKeyCode::F => Some((GameKeyKind::PressRelease, Action(Attack))),
VirtualKeyCode::LShift => Some((GameKeyKind::PressRelease, Spawn)),
VirtualKeyCode::F => Some((GameKeyKind::PressRelease, Attack)),
_ => None,
}
}
Expand Down
88 changes: 88 additions & 0 deletions client/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,9 @@ pub mod inputs;

use common::configs::scene_config::ConfigSceneGraph;
use common::core::states::GameState;
use common::core::command::Command;
use winit::window::Window;
use wgpu_glyph::{ab_glyph, GlyphBrushBuilder, Section, Text, Layout, GlyphBrush, HorizontalAlign};

const MODELS_CONFIG_PATH: &str = "models.json";
const SCENE_CONFIG_PATH: &str = "scene.json";
Expand All @@ -49,6 +51,8 @@ struct State {
screens: Vec<screen_objects::Screen>,
screen_ind: usize,
client_id: u8,
staging_belt: wgpu::util::StagingBelt,
glyph_brush: GlyphBrush<()>
}

impl State {
Expand Down Expand Up @@ -347,6 +351,15 @@ impl State {
multiview: None,
});

// text
let staging_belt = wgpu::util::StagingBelt::new(1024);
let inconsolata = ab_glyph::FontArc::try_from_slice(include_bytes!(
"../../assets/Inconsolata-Regular.ttf"
)).unwrap();

let glyph_brush = GlyphBrushBuilder::using_font(inconsolata)
.build(&device, surface_format);

let screens =
screen_objects::get_screens(&texture_bind_group_layout_2d, &device, &queue).await;

Expand All @@ -371,6 +384,8 @@ impl State {
#[cfg(feature = "debug-lobby")]
screen_ind: 1,
client_id,
staging_belt,
glyph_brush,
}
}

Expand Down Expand Up @@ -533,10 +548,83 @@ impl State {
}
}

let size = &self.window.inner_size();

// if player is alive
if !self.player.is_dead {
// render ammo remaining
self.glyph_brush.queue(Section {
screen_position: (30.0, 20.0),
bounds: (size.width as f32, size.height as f32),
text: vec![
Text::new(&format!("Ammo remaining: {:.1}\n", self.player.ammo_count).as_str())
.with_color([0.0, 0.0, 0.0, 1.0])
.with_scale(40.0)
],
..Section::default()
});
// render ability cooldowns
if self.player.on_cooldown.contains_key(&Command::Attack) {
let attack_cooldown = self.player.on_cooldown.get(&Command::Attack).unwrap();
self.glyph_brush.queue(Section {
screen_position: (30.0, 60.0),
bounds: (size.width as f32, size.height as f32),
text: vec![
Text::new(&format!("Attack cooldown: {:.1}\n", attack_cooldown).as_str())
.with_color([0.0, 0.0, 0.0, 1.0])
.with_scale(40.0)
],
..Section::default()
});
}
} else {
// render respawn cooldown
if self.player.on_cooldown.contains_key(&Command::Spawn) {
let spawn_cooldown = self.player.on_cooldown.get(&Command::Spawn).unwrap();
self.glyph_brush.queue(Section {
screen_position: (size.width as f32 * 0.5, size.height as f32 * 0.4),
bounds: (size.width as f32, size.height as f32),
text: vec![
Text::new("You died!\n")
.with_color([1.0, 1.0, 0.0, 1.0])
.with_scale(100.0),
Text::new("Respawning in ")
.with_color([1.0, 1.0, 0.0, 1.0])
.with_scale(60.0),
Text::new(&format!("{:.1}", spawn_cooldown).as_str())
.with_color([1.0, 1.0, 1.0, 1.0])
.with_scale(60.0),
Text::new(" seconds")
.with_color([1.0, 1.0, 0.0, 1.0])
.with_scale(60.0),
],
layout: Layout::default().h_align(HorizontalAlign::Center),
..Section::default()
});
}
}

// Draw the text!
self.glyph_brush
.draw_queued(
&self.device,
&mut self.staging_belt,
&mut encoder,
&view,
size.width,
size.height,
)
.expect("Draw queued");

// Submit the work!
self.staging_belt.finish();

// submit will accept anything that implements IntoIter
self.queue.submit(std::iter::once(encoder.finish()));
output.present();

// Recall unused staging buffers
self.staging_belt.recall();
Ok(())
}
}
29 changes: 23 additions & 6 deletions client/src/player.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
use common::core::command::Command;
use common::core::states::PlayerState;
use instant::Duration;

use std::time::SystemTime;
use std::f32::consts::FRAC_PI_2;
use std::f32::consts::PI;
use std::collections::HashMap;
use winit::dpi::PhysicalPosition;
use winit::event::*;



extern crate nalgebra_glm as glm;

use crate::camera::CameraState;
Expand Down Expand Up @@ -35,20 +39,22 @@ fn spherical_to_cartesian(spherical: &glm::Vec3) -> glm::Vec3 {
glm::vec3(x, y, z)
}

#[derive(Debug)]
#[derive(Debug, Default)]
pub struct Player {
pub position: glm::TVec3<f32>,
pub rotation: glm::Quat,
up: glm::TVec3<f32>,
pub is_dead: bool,
pub ammo_count: u32,
pub on_cooldown: HashMap<Command, f32>,
}

impl Player {
pub fn new(position: glm::TVec3<f32>) -> Self {
let up = glm::vec3(0.0, 1.0, 0.0);
Self {
position,
rotation: glm::quat_identity(),
up,
is_dead: false,
..Default::default()
}
}

Expand Down Expand Up @@ -92,7 +98,8 @@ impl PlayerController {
};
}

/// update the player's position and camera's position and target based on incoming player state
/// update the player's position, cooldowns, camera's position and target based on incoming player state
///
pub fn update(
&mut self,
player: &mut Player,
Expand Down Expand Up @@ -139,5 +146,15 @@ impl PlayerController {
+ self.scroll * self.scroll_sensitivity * dt)
.clamp(PI / 6.0, PI / 3.0);
self.scroll = 0.0;

// update dead status
player.is_dead = incoming_player_state.is_dead;

// update cooldowns
player.on_cooldown = incoming_player_state.on_cooldown.clone();

// update ammo count
player.ammo_count = incoming_player_state.ammo_count;

}
}

0 comments on commit e5d8f6a

Please sign in to comment.