use std::ops::{Add, Div, Mul, Sub}; use chrono::Utc; use raylib::prelude::*; use tracing::log::trace; use crate::{utilities::non_ref_raylib::HackedRaylibHandle, GameConfig}; use super::MainCharacter; pub fn render_character_in_camera_space( raylib: &mut RaylibMode2D<'_, HackedRaylibHandle>, player: &MainCharacter, config: &GameConfig, ) { // Calculate the time since the start of the state let time_since_state_change = Utc::now() - player.state_set_timestamp; // Calculate the number of frames since state change let frames_since_state_change = ((time_since_state_change.num_milliseconds() as f64 / 1000.0) * config.animation_fps as f64) as f32; // Calculate the frame ID to render let frame_id = match player.current_state { crate::character::CharacterState::Jumping => 6, _ => (frames_since_state_change % player.sprite_sheet.sprite_count as f32).floor() as usize, }; trace!( "Rendering player frame: {} ({})", frame_id, frames_since_state_change ); player.sprite_sheet.render( raylib, player .position .sub(Vector2::new(player.size.y, player.size.y).div(4.0)), Some(Vector2::new(player.size.y, player.size.y)), Some(frame_id), ); // Possibly render a debug vector #[cfg(all(debug_assertions, feature = "collider_debug"))] { raylib.draw_line_v( player.position.sub(player.size.div(2.0)), player .position .sub(player.size.div(2.0)) .add(player.velocity.mul(10.0).add(Vector2::new(0.0, 100.0))), Color::RED, ); raylib.draw_rectangle_lines_ex( Rectangle::new( player.position.x - (player.size.x / 2.0), player.position.y - (player.size.x / 2.0), player.size.x, player.size.y, ), 2, Color::RED, ); } }