extracting player code
This commit is contained in:
parent
1d9f49eb34
commit
e6726916a6
@ -31,10 +31,10 @@ impl fmt::Display for GameState {
|
|||||||
|
|
||||||
#[derive(Debug, Serialize, Deserialize, Default)]
|
#[derive(Debug, Serialize, Deserialize, Default)]
|
||||||
pub struct GameProgress {
|
pub struct GameProgress {
|
||||||
coins: u32,
|
pub coins: u32,
|
||||||
max_depth: f32,
|
pub max_depth: f32,
|
||||||
fastest_time: Option<f64>,
|
pub fastest_time: Option<f64>,
|
||||||
inventory: Vec<ShopItems>,
|
pub inventory: Vec<ShopItems>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl GameProgress {
|
impl GameProgress {
|
||||||
@ -44,6 +44,7 @@ impl GameProgress {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
pub fn from_file(file: String) -> Result<Self, Error> {
|
pub fn from_file(file: String) -> Result<Self, Error> {
|
||||||
// Load the file
|
// Load the file
|
||||||
let file = File::open(file)?;
|
let file = File::open(file)?;
|
||||||
|
@ -8,13 +8,7 @@ pub fn render_hud(
|
|||||||
window_center: Vector2,
|
window_center: Vector2,
|
||||||
) {
|
) {
|
||||||
// Get the relevant data
|
// Get the relevant data
|
||||||
let dist_from_player_to_end = game_core
|
let progress = game_core.player.calculate_depth_percent(&game_core.world);
|
||||||
.player
|
|
||||||
.position
|
|
||||||
.distance_to(game_core.world.end_position);
|
|
||||||
let dist_from_start_to_end = Vector2::zero().distance_to(game_core.world.end_position);
|
|
||||||
let progress = ((dist_from_start_to_end - dist_from_player_to_end) / dist_from_start_to_end)
|
|
||||||
.clamp(0.0, 1.0);
|
|
||||||
|
|
||||||
// Determine the progress slider position
|
// Determine the progress slider position
|
||||||
let slider_bound_height = 20.0;
|
let slider_bound_height = 20.0;
|
||||||
|
@ -126,7 +126,8 @@ impl Screen for InGameScreen {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Render Player
|
// Render Player
|
||||||
playerlogic::render_player(&mut context_2d, game_core);
|
// playerlogic::render_player(&mut context_2d, game_core);
|
||||||
|
game_core.player.render(&mut context_2d, &mut game_core.resources);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Render the hud
|
// Render the hud
|
||||||
|
@ -169,60 +169,3 @@ pub fn update_player_movement(
|
|||||||
// }
|
// }
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn render_player(context_2d: &mut RaylibMode2D<RaylibDrawHandle>, game_core: &mut GameCore) {
|
|
||||||
// Get the player
|
|
||||||
let player = &game_core.player;
|
|
||||||
|
|
||||||
// Convert the player direction to a rotation
|
|
||||||
let player_rotation = Vector2::zero().angle_to(player.direction);
|
|
||||||
|
|
||||||
// Render the player's boost ring
|
|
||||||
// This functions both as a breath meter, and as a boost meter
|
|
||||||
let boost_ring_max_radius = player.size.x + 5.0;
|
|
||||||
context_2d.draw_circle(
|
|
||||||
player.position.x as i32,
|
|
||||||
player.position.y as i32,
|
|
||||||
boost_ring_max_radius * player.boost_percent,
|
|
||||||
TRANSLUCENT_WHITE_64,
|
|
||||||
);
|
|
||||||
context_2d.draw_ring(
|
|
||||||
Vector2 {
|
|
||||||
x: player.position.x as i32 as f32,
|
|
||||||
y: player.position.y as i32 as f32,
|
|
||||||
},
|
|
||||||
boost_ring_max_radius,
|
|
||||||
boost_ring_max_radius + 1.0,
|
|
||||||
0,
|
|
||||||
(360.0 * player.breath_percent) as i32,
|
|
||||||
0,
|
|
||||||
TRANSLUCENT_WHITE_96,
|
|
||||||
);
|
|
||||||
|
|
||||||
// Render the player based on what is happening
|
|
||||||
if player.is_boost_charging {
|
|
||||||
game_core.resources.player_animation_boost_charge.draw(
|
|
||||||
context_2d,
|
|
||||||
player.position,
|
|
||||||
player_rotation.to_degrees() - 90.0,
|
|
||||||
);
|
|
||||||
} else if player.is_boosting {
|
|
||||||
game_core.resources.player_animation_boost.draw(
|
|
||||||
context_2d,
|
|
||||||
player.position,
|
|
||||||
player_rotation.to_degrees() - 90.0,
|
|
||||||
);
|
|
||||||
} else if player.is_moving {
|
|
||||||
game_core.resources.player_animation_regular.draw(
|
|
||||||
context_2d,
|
|
||||||
player.position,
|
|
||||||
player_rotation.to_degrees() - 90.0,
|
|
||||||
);
|
|
||||||
} else {
|
|
||||||
game_core.resources.player_animation_regular.draw_frame(
|
|
||||||
context_2d,
|
|
||||||
player.position,
|
|
||||||
player_rotation.to_degrees() - 90.0,
|
|
||||||
0,
|
|
||||||
);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
@ -1,6 +1,13 @@
|
|||||||
use raylib::math::{Rectangle, Vector2};
|
use raylib::prelude::*;
|
||||||
|
|
||||||
use crate::lib::utils::triangles::rotate_vector;
|
use crate::{
|
||||||
|
gamecore::{GameCore, GameProgress},
|
||||||
|
items::ShopItems,
|
||||||
|
lib::utils::triangles::rotate_vector,
|
||||||
|
pallette::{TRANSLUCENT_WHITE_64, TRANSLUCENT_WHITE_96},
|
||||||
|
resources::GlobalResources,
|
||||||
|
world::World,
|
||||||
|
};
|
||||||
|
|
||||||
#[derive(Debug, Default)]
|
#[derive(Debug, Default)]
|
||||||
pub struct Player {
|
pub struct Player {
|
||||||
@ -13,6 +20,7 @@ pub struct Player {
|
|||||||
pub is_moving: bool,
|
pub is_moving: bool,
|
||||||
pub is_boosting: bool,
|
pub is_boosting: bool,
|
||||||
pub is_boost_charging: bool,
|
pub is_boost_charging: bool,
|
||||||
|
pub inventory: Vec<ShopItems>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Player {
|
impl Player {
|
||||||
@ -56,4 +64,82 @@ impl Player {
|
|||||||
|
|
||||||
return rectangle.check_collision_circle_rec(self.position, (self.size.y * 0.5) / 2.0);
|
return rectangle.check_collision_circle_rec(self.position, (self.size.y * 0.5) / 2.0);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Calculate how far the player is
|
||||||
|
pub fn calculate_depth_percent(&self, world: &World) -> f32 {
|
||||||
|
let dist_from_player_to_end = self.position.distance_to(world.end_position);
|
||||||
|
let dist_from_start_to_end = Vector2::zero().distance_to(world.end_position);
|
||||||
|
return ((dist_from_start_to_end - dist_from_player_to_end) / dist_from_start_to_end)
|
||||||
|
.clamp(0.0, 1.0);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Create GameProgress from the current life
|
||||||
|
pub fn create_statistics(&self, game_core: &GameCore, current_time: f64) -> GameProgress {
|
||||||
|
GameProgress {
|
||||||
|
coins: self.coins,
|
||||||
|
inventory: self.inventory.clone(),
|
||||||
|
max_depth: self.calculate_depth_percent(&game_core.world),
|
||||||
|
fastest_time: Some(current_time - game_core.last_state_change_time),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Render the player
|
||||||
|
pub fn render(
|
||||||
|
&self,
|
||||||
|
context_2d: &mut RaylibMode2D<RaylibDrawHandle>,
|
||||||
|
resources: &mut GlobalResources,
|
||||||
|
) {
|
||||||
|
// Convert the player direction to a rotation
|
||||||
|
let player_rotation = Vector2::zero().angle_to(self.direction);
|
||||||
|
|
||||||
|
// Render the player's boost ring
|
||||||
|
// This functions both as a breath meter, and as a boost meter
|
||||||
|
let boost_ring_max_radius = self.size.x + 5.0;
|
||||||
|
context_2d.draw_circle(
|
||||||
|
self.position.x as i32,
|
||||||
|
self.position.y as i32,
|
||||||
|
boost_ring_max_radius * self.boost_percent,
|
||||||
|
TRANSLUCENT_WHITE_64,
|
||||||
|
);
|
||||||
|
context_2d.draw_ring(
|
||||||
|
Vector2 {
|
||||||
|
x: self.position.x as i32 as f32,
|
||||||
|
y: self.position.y as i32 as f32,
|
||||||
|
},
|
||||||
|
boost_ring_max_radius,
|
||||||
|
boost_ring_max_radius + 1.0,
|
||||||
|
0,
|
||||||
|
(360.0 * self.breath_percent) as i32,
|
||||||
|
0,
|
||||||
|
TRANSLUCENT_WHITE_96,
|
||||||
|
);
|
||||||
|
|
||||||
|
// Render the player based on what is happening
|
||||||
|
if self.is_boost_charging {
|
||||||
|
resources.player_animation_boost_charge.draw(
|
||||||
|
context_2d,
|
||||||
|
self.position,
|
||||||
|
player_rotation.to_degrees() - 90.0,
|
||||||
|
);
|
||||||
|
} else if self.is_boosting {
|
||||||
|
resources.player_animation_boost.draw(
|
||||||
|
context_2d,
|
||||||
|
self.position,
|
||||||
|
player_rotation.to_degrees() - 90.0,
|
||||||
|
);
|
||||||
|
} else if self.is_moving {
|
||||||
|
resources.player_animation_regular.draw(
|
||||||
|
context_2d,
|
||||||
|
self.position,
|
||||||
|
player_rotation.to_degrees() - 90.0,
|
||||||
|
);
|
||||||
|
} else {
|
||||||
|
resources.player_animation_regular.draw_frame(
|
||||||
|
context_2d,
|
||||||
|
self.position,
|
||||||
|
player_rotation.to_degrees() - 90.0,
|
||||||
|
0,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user